From e20b0b6e23389bee4e90ccaf0184d2f7b700eb5f Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:02:39 +0000 Subject: [PATCH 1/2] fix(consensus): stop a previous-height precommit marking the peer's current-height bit getVoteBitArray resolved (PRS.Height-1, LastCommitRound, Precommit) to PRS.Precommits, the bit array of the peer's current height, after #751 removed the dedicated LastPrecommits array. A late precommit of the height the peer had just left (reactor Vote handler -> SetHasVote) therefore marked the same validator index as already held for the current height, and that validator's current-height precommit was never offered to the peer. A VoteSetBits answer about the previous height's commit (ApplyVoteSetBitsMessage with ourVotes == nil) overwrote the whole current-height precommit bit array the same way. Previous-height votes are not gossiped individually (the commit is sent whole), so the lookup now returns nil for them: no tracking, no side effects. LastCommitRound bookkeeping in ApplyNewRoundStepMessage is unchanged. Co-Authored-By: Claude Fable 5.1 --- internal/consensus/peer_state.go | 17 ++--- .../consensus/peer_state_lastcommit_test.go | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 internal/consensus/peer_state_lastcommit_test.go diff --git a/internal/consensus/peer_state.go b/internal/consensus/peer_state.go index 25befbe19..1b7080009 100644 --- a/internal/consensus/peer_state.go +++ b/internal/consensus/peer_state.go @@ -359,20 +359,11 @@ func (ps *PeerState) getVoteBitArray(height int64, round int32, votesType tmprot return nil } - if ps.PRS.Height == height+1 { - if ps.PRS.LastCommitRound == round { - switch votesType { - case tmproto.PrevoteType: - return nil - - case tmproto.PrecommitType: - return ps.PRS.Precommits - } - } - - return nil - } + // Votes of the height the peer has left are not tracked: its commit is + // gossiped whole (see GossipCommit), and ps.PRS.Precommits belongs to the + // peer's current height, so a late previous-height precommit must not + // touch it. return nil } diff --git a/internal/consensus/peer_state_lastcommit_test.go b/internal/consensus/peer_state_lastcommit_test.go new file mode 100644 index 000000000..66d79556a --- /dev/null +++ b/internal/consensus/peer_state_lastcommit_test.go @@ -0,0 +1,72 @@ +package consensus + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/dashpay/tenderdash/libs/bits" + "github.com/dashpay/tenderdash/libs/log" + tmproto "github.com/dashpay/tenderdash/proto/tendermint/types" + "github.com/dashpay/tenderdash/types" +) + +// lastCommitPeerState is a peer at height 2, round 0, whose commit for +// height 1 was at round 0, with validator bit arrays allocated for height 2. +func lastCommitPeerState(t *testing.T) *PeerState { + t.Helper() + ps := NewPeerState(log.NewNopLogger(), "testPeerState") + ps.PRS.Height, ps.PRS.Round, ps.PRS.LastCommitRound = 2, 0, 0 + ps.ensureVoteBitArrays(2, 3) + return ps +} + +// A precommit of the previous height, received from a peer that has already +// moved on, must not mark the same validator index as held in the peer's +// current-height precommits. Otherwise this node's current-height precommit +// of that validator is never offered to the peer. +func TestSetHasVoteLastHeightPrecommitDoesNotMarkCurrentHeight(t *testing.T) { + ps := lastCommitPeerState(t) + + lateLastCommit := &types.Vote{Height: 1, Round: 0, Type: tmproto.PrecommitType, ValidatorIndex: 1} + require.NoError(t, ps.SetHasVote(lateLastCommit)) + + require.False(t, ps.PRS.Precommits.GetIndex(1), + "a height-1 precommit must not mark the peer as holding the height-2 precommit of that validator") + + ours := bits.NewBitArray(3) + ours.SetIndex(1, true) + vote, ok := ps.PickVoteToSend(&fixedVoteSet{height: 2, round: 0, msgType: tmproto.PrecommitType, have: ours}) + require.True(t, ok, "the height-2 precommit of validator 1 must still be offered to the peer") + require.EqualValues(t, 1, vote.ValidatorIndex) +} + +// A VoteSetBits answer about the previous height's commit (sent while the peer +// was still catching up, applied after it moved on) must not overwrite the +// peer's current-height precommit bit array. +func TestApplyVoteSetBitsLastHeightPrecommitsDoNotMarkCurrentHeight(t *testing.T) { + ps := lastCommitPeerState(t) + + peerHas := bits.NewBitArray(3) + for i := 0; i < 3; i++ { + peerHas.SetIndex(i, true) + } + ps.ApplyVoteSetBitsMessage(&VoteSetBitsMessage{ + Height: 1, Round: 0, Type: tmproto.PrecommitType, Votes: peerHas, + }, nil) + + require.Equal(t, bits.NewBitArray(3), ps.PRS.Precommits, + "height-1 commit bits must not be applied to the peer's height-2 precommits") +} + +// NewRoundStep moving the peer to the next height still records the round of +// its commit for the height it left. +func TestApplyNewRoundStepMessageKeepsLastCommitRound(t *testing.T) { + ps := lastCommitPeerState(t) + ps.PRS.Round, ps.PRS.LastCommitRound = 3, -1 + + ps.ApplyNewRoundStepMessage(&NewRoundStepMessage{Height: 3, Round: 0, Step: 1, LastCommitRound: 3}) + + require.EqualValues(t, 3, ps.PRS.Height) + require.EqualValues(t, 3, ps.PRS.LastCommitRound) +} From 47b943923fa4d81a7a7089eb19dcaca9e13ea063 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:07:39 +0000 Subject: [PATCH 2/2] test(consensus): cover VoteSetBits with own votes; drop dead LastPrecommits branch ApplyVoteSetBitsMessage has two branches (ourVotes nil or not); the previous-height regression test now exercises both. ApplyNewRoundStepMessage kept an if/else with identical branches under a "Shift Precommits to LastPrecommits" comment from before #751 removed that array; collapsed to the single assignment it performs. Co-Authored-By: Claude Fable 5.1 --- internal/consensus/peer_state.go | 7 +---- .../consensus/peer_state_lastcommit_test.go | 28 ++++++++++++------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/internal/consensus/peer_state.go b/internal/consensus/peer_state.go index 1b7080009..6be98e857 100644 --- a/internal/consensus/peer_state.go +++ b/internal/consensus/peer_state.go @@ -635,12 +635,7 @@ func (ps *PeerState) ApplyNewRoundStepMessage(msg *NewRoundStepMessage) { } if psHeight != msg.Height { - // Shift Precommits to LastPrecommits. - if psHeight+1 == msg.Height && psRound == msg.LastCommitRound { - ps.PRS.LastCommitRound = msg.LastCommitRound - } else { - ps.PRS.LastCommitRound = msg.LastCommitRound - } + ps.PRS.LastCommitRound = msg.LastCommitRound // we'll update the BitArray capacity later ps.PRS.CatchupCommitRound = -1 diff --git a/internal/consensus/peer_state_lastcommit_test.go b/internal/consensus/peer_state_lastcommit_test.go index 66d79556a..1ba2e6984 100644 --- a/internal/consensus/peer_state_lastcommit_test.go +++ b/internal/consensus/peer_state_lastcommit_test.go @@ -41,22 +41,30 @@ func TestSetHasVoteLastHeightPrecommitDoesNotMarkCurrentHeight(t *testing.T) { require.EqualValues(t, 1, vote.ValidatorIndex) } -// A VoteSetBits answer about the previous height's commit (sent while the peer -// was still catching up, applied after it moved on) must not overwrite the -// peer's current-height precommit bit array. +// A VoteSetBits answer about the previous height's commit, applied after the +// peer moved on, must not overwrite the peer's current-height precommit bit +// array — whether this node has already left that height too (ourVotes nil) +// or still holds its votes for it. func TestApplyVoteSetBitsLastHeightPrecommitsDoNotMarkCurrentHeight(t *testing.T) { - ps := lastCommitPeerState(t) - peerHas := bits.NewBitArray(3) for i := 0; i < 3; i++ { peerHas.SetIndex(i, true) } - ps.ApplyVoteSetBitsMessage(&VoteSetBitsMessage{ - Height: 1, Round: 0, Type: tmproto.PrecommitType, Votes: peerHas, - }, nil) + for name, ourVotes := range map[string]*bits.BitArray{ + "we moved on too": nil, + "we still hold height-1 votes": peerHas.Copy(), + } { + t.Run(name, func(t *testing.T) { + ps := lastCommitPeerState(t) - require.Equal(t, bits.NewBitArray(3), ps.PRS.Precommits, - "height-1 commit bits must not be applied to the peer's height-2 precommits") + ps.ApplyVoteSetBitsMessage(&VoteSetBitsMessage{ + Height: 1, Round: 0, Type: tmproto.PrecommitType, Votes: peerHas, + }, ourVotes) + + require.Equal(t, bits.NewBitArray(3), ps.PRS.Precommits, + "height-1 commit bits must not be applied to the peer's height-2 precommits") + }) + } } // NewRoundStep moving the peer to the next height still records the round of