Skip to content

Commit

Permalink
fix: avoid recursive call after rename to (*PeerState).MarshalJSON (b…
Browse files Browse the repository at this point in the history
…ackport cometbft#865) (cometbft#970)

* fix: avoid recursive call after rename to (*PeerState).MarshalJSON (cometbft#865)

* avoid recursive call after rename to (*PeerState).MarshalJSON

* add test

* add change doc

* explain for nolint

* fix lint

* fix golangci-lint to v1.52.2

* fix golangci-lint to v1.52.2

* Revert "fix golangci-lint to v1.52.2"

This reverts commit 598a9ef.

* Revert "fix golangci-lint to v1.52.2"

This reverts commit a8aad12.

* Reintroduced `cmtjson`

* Avoid copying Mutex

* Avoid copying Mutex -- 2nd try, more succint

* Update .changelog/unreleased/bug-fixes/865-fix-peerstate-marshaljson.md

* Update consensus/reactor_test.go

---------

Co-authored-by: Sergio Mena <[email protected]>
(cherry picked from commit f6ea091)

# Conflicts:
#	consensus/reactor_test.go

* Revert "fix: avoid recursive call after rename to (*PeerState).MarshalJSON (cometbft#865)"

* fix: avoid recursive call after rename to (*PeerState).MarshalJSON (cometbft#865)

* avoid recursive call after rename to (*PeerState).MarshalJSON

* add test

* add change doc

* explain for nolint

* fix lint

* fix golangci-lint to v1.52.2

* fix golangci-lint to v1.52.2

* Revert "fix golangci-lint to v1.52.2"

This reverts commit 598a9ef.

* Revert "fix golangci-lint to v1.52.2"

This reverts commit a8aad12.

* Reintroduced `cmtjson`

* Avoid copying Mutex

* Avoid copying Mutex -- 2nd try, more succint

* Update .changelog/unreleased/bug-fixes/865-fix-peerstate-marshaljson.md

* Update consensus/reactor_test.go

---------

Co-authored-by: Sergio Mena <[email protected]>

---------

Co-authored-by: mmsqe <[email protected]>
Co-authored-by: Sergio Mena <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2023
1 parent ea902b6 commit 2a526ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[consensus]` Avoid recursive call after rename to (*PeerState).MarshalJSON
([\#863](https://github.com/cometbft/cometbft/pull/863))
3 changes: 2 additions & 1 deletion consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ func (ps *PeerState) MarshalJSON() ([]byte, error) {
ps.mtx.Lock()
defer ps.mtx.Unlock()

return cmtjson.Marshal(ps)
type jsonPeerState PeerState
return cmtjson.Marshal((*jsonPeerState)(ps))
}

// GetHeight returns an atomic snapshot of the PeerRoundState's height
Expand Down
30 changes: 30 additions & 0 deletions consensus/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consensus

import (
"context"
"encoding/json"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -1032,3 +1033,32 @@ func TestVoteSetBitsMessageValidateBasic(t *testing.T) {
})
}
}

func TestMarshalJSONPeerState(t *testing.T) {
ps := NewPeerState(nil)
data, err := json.Marshal(ps)
require.NoError(t, err)
require.JSONEq(t, `{
"round_state":{
"height": "0",
"round": -1,
"step": 0,
"start_time": "0001-01-01T00:00:00Z",
"proposal": false,
"proposal_block_part_set_header":
{"total":0, "hash":""},
"proposal_block_parts": null,
"proposal_pol_round": -1,
"proposal_pol": null,
"prevotes": null,
"precommits": null,
"last_commit_round": -1,
"last_commit": null,
"catchup_commit_round": -1,
"catchup_commit": null
},
"stats":{
"votes":"0",
"block_parts":"0"}
}`, string(data))
}

0 comments on commit 2a526ca

Please sign in to comment.