Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f6ea091

Browse files
mmsqesergio-mena
andauthoredJun 14, 2023
fix: avoid recursive call after rename to (*PeerState).MarshalJSON (celestiaorg#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 598a9ef4c86fc29cf038251676c33a222217826c. * Revert "fix golangci-lint to v1.52.2" This reverts commit a8aad121e27382813e95b1911b1b560c62e1c7c3. * 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]>
1 parent 21e94a2 commit f6ea091

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `[consensus]` Avoid recursive call after rename to (*PeerState).MarshalJSON
2+
([\#863](https://github.com/cometbft/cometbft/pull/863))

‎consensus/reactor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,8 @@ func (ps *PeerState) MarshalJSON() ([]byte, error) {
10751075
ps.mtx.Lock()
10761076
defer ps.mtx.Unlock()
10771077

1078-
return cmtjson.Marshal(ps)
1078+
type jsonPeerState PeerState
1079+
return cmtjson.Marshal((*jsonPeerState)(ps))
10791080
}
10801081

10811082
// GetHeight returns an atomic snapshot of the PeerRoundState's height

‎consensus/reactor_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/cometbft/cometbft/crypto/tmhash"
2525
"github.com/cometbft/cometbft/libs/bits"
2626
"github.com/cometbft/cometbft/libs/bytes"
27+
"github.com/cometbft/cometbft/libs/json"
2728
"github.com/cometbft/cometbft/libs/log"
2829
cmtsync "github.com/cometbft/cometbft/libs/sync"
2930
mempl "github.com/cometbft/cometbft/mempool"
@@ -1096,3 +1097,32 @@ func TestVoteSetBitsMessageValidateBasic(t *testing.T) {
10961097
})
10971098
}
10981099
}
1100+
1101+
func TestMarshalJSONPeerState(t *testing.T) {
1102+
ps := NewPeerState(nil)
1103+
data, err := json.Marshal(ps)
1104+
require.NoError(t, err)
1105+
require.JSONEq(t, `{
1106+
"round_state":{
1107+
"height": "0",
1108+
"round": -1,
1109+
"step": 0,
1110+
"start_time": "0001-01-01T00:00:00Z",
1111+
"proposal": false,
1112+
"proposal_block_part_set_header":
1113+
{"total":0, "hash":""},
1114+
"proposal_block_parts": null,
1115+
"proposal_pol_round": -1,
1116+
"proposal_pol": null,
1117+
"prevotes": null,
1118+
"precommits": null,
1119+
"last_commit_round": -1,
1120+
"last_commit": null,
1121+
"catchup_commit_round": -1,
1122+
"catchup_commit": null
1123+
},
1124+
"stats":{
1125+
"votes":"0",
1126+
"block_parts":"0"}
1127+
}`, string(data))
1128+
}

0 commit comments

Comments
 (0)
Please sign in to comment.