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) (#970)

* 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 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 <sergio@informal.systems>
(cherry picked from commit f6ea09171a2bf9f695f59b65f5c51e4a8c168015)

# Conflicts:
#	consensus/reactor_test.go

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

* 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 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 <sergio@informal.systems>

---------

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 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.