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 #865) (#968)

* fix: avoid recursive call after rename to (*PeerState).MarshalJSON (#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 <sergio@informal.systems>
(cherry picked from commit f6ea091)

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

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

This reverts commit 2b96118.

---------

Co-authored-by: mmsqe <mavis@crypto.com>
Co-authored-by: Sergio Mena <sergio@informal.systems>
  • Loading branch information
3 people committed Jun 14, 2023
1 parent f75e13b commit c377cb8
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 @@ -1075,7 +1075,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 @@ -24,6 +24,7 @@ import (
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cometbft/cometbft/libs/bits"
"github.com/cometbft/cometbft/libs/bytes"
"github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/libs/log"
cmtsync "github.com/cometbft/cometbft/libs/sync"
mempl "github.com/cometbft/cometbft/mempool"
Expand Down Expand Up @@ -1096,3 +1097,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 c377cb8

Please sign in to comment.