From daef5a5a740a66af8cb3f4b4c0afac06e6105386 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:26:40 +0000 Subject: [PATCH 1/3] fix(consensus): deliver and process a polka block for a validator left in its round A validator that saw a polka for a block it never received precommits nil and, once its peers move to a later round, can no longer obtain the block: block parts are served only from the proposal part set, which entering a new round clears, while the valid and locked part sets that still hold the block are never gossiped. Even when the block does arrive, one completed in the prevote or precommit step is not processed, so an application that verifies vote extensions against its last processed proposal, like Drive, rejects every precommit for the block. Rejected precommits are not stored and do not count towards the +2/3 of any kind that start the precommit wait, so the validator stays in its round for good and a network without spare voting power stalls with it. Serve a validator peer at this height, in an earlier round, the complete valid or locked part set whose header it is collecting, from the gossip snapshot. Process the round's polka block when it completes too late for the round to vote on it, so that the precommits for it verify; the node casts no vote again, and a failure is not retried. Co-Authored-By: Claude Fable 5.1 --- internal/consensus/gossip_handlers.go | 29 +++ internal/consensus/gossip_handlers_test.go | 94 ++++++++ .../consensus/polka_block_delivery_test.go | 202 ++++++++++++++++++ internal/consensus/state_add_prop_block.go | 34 ++- internal/consensus/state_controller.go | 2 +- 5 files changed, 359 insertions(+), 2 deletions(-) create mode 100644 internal/consensus/polka_block_delivery_test.go diff --git a/internal/consensus/gossip_handlers.go b/internal/consensus/gossip_handlers.go index 122de500f..6fa8b6b6b 100644 --- a/internal/consensus/gossip_handlers.go +++ b/internal/consensus/gossip_handlers.go @@ -215,6 +215,14 @@ func dataGossipHandler(ps *PeerState, logger log.Logger, blockStore sm.BlockStor return } } + if parts := heldBlockPartsFor(rs, prs, isValidator); parts != nil { + // rs is this handler's copy of the snapshot. The gossiper serves its + // part set and marks each part sent as the peer's under its round + // (see PeerState.SetHasProposalBlockPart), the peer's own here. + rs.ProposalBlockParts, rs.Round = parts, prs.Round + gossiper.GossipProposalBlockParts(ctx, rs, prs) + return + } // if the peer is on a previous height that we have, help catch up blockStoreBase := blockStore.Base() @@ -267,6 +275,27 @@ func shouldProposalBeGossiped(rs cstypes.RoundState, prs *cstypes.PeerRoundState return rs.Proposal != nil && !prs.Proposal && isValidator } +// heldBlockPartsFor returns the complete part set of this node's valid or +// locked block when a validator peer at this height, in an earlier round, is +// collecting that block and rs.ProposalBlockParts is not it. Entering a later +// round drops the proposal part set, and a peer whose polka retargeted it at +// the block (see addVoteUpdateValidBlockMw) has nowhere else to get the parts +// from: without them it rejects every precommit for the block, counts none of +// them towards leaving its round, and stays there for good. The part set +// header is the block's identity; a round or a hash alone names no bytes. +func heldBlockPartsFor(rs cstypes.RoundState, prs *cstypes.PeerRoundState, isValidator bool) *types.PartSet { + if !isValidator || rs.Height != prs.Height || prs.Round >= rs.Round || + rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { + return nil + } + for _, parts := range []*types.PartSet{rs.ValidBlockParts, rs.LockedBlockParts} { + if parts.HasHeader(prs.ProposalBlockPartSetHeader) && parts.IsComplete() { + return parts + } + } + return nil +} + func shouldBlockPartsBeGossiped(rs cstypes.RoundState, prs *cstypes.PeerRoundState, isValidator bool) bool { if rs.Height != prs.Height || rs.Round < prs.Round { return false diff --git a/internal/consensus/gossip_handlers_test.go b/internal/consensus/gossip_handlers_test.go index 589636c2b..623dc96a7 100644 --- a/internal/consensus/gossip_handlers_test.go +++ b/internal/consensus/gossip_handlers_test.go @@ -743,3 +743,97 @@ func TestRoundVoteResendGossipHandler(t *testing.T) { }) } } + +// A validator peer at this height, in an earlier round, collecting the block +// this node holds as valid or locked is served its parts from there, since the +// round that left it dropped the proposal part set. The part set header names +// the block; nothing is served on round or hash alone, to another height, or to +// a non-validator. +func (suite *GossipHandlerTestSuite) TestDataGossipHandlerServesHeldBlockToLaggingPeer() { + ctx := context.Background() + logger := log.NewTestingLogger(suite.T()) + valSet, _ := factory.MockValidatorSet() + validator, nonValidator := valSet.GetProTxHashes()[0], crypto.RandProTxHash() + held := types.NewPartSetFromData(tmrand.Bytes(100), 100) + other := types.NewPartSetFromData(tmrand.Bytes(100), 100) + collecting := func(parts *types.PartSet, height int64, round int32) cstypes.PeerRoundState { + return cstypes.PeerRoundState{ + Height: height, + Round: round, + ProposalBlockPartSetHeader: parts.Header(), + ProposalBlockParts: types.NewPartSetFromHeader(parts.Header()).BitArray(), + } + } + testCases := []struct { + name string + proTxHash crypto.ProTxHash + rs cstypes.RoundState + prs cstypes.PeerRoundState + served bool + }{ + { + name: "valid block to a peer in an earlier round", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held}, + prs: collecting(held, 10, 1), + served: true, + }, + { + name: "locked block to a peer in an earlier round", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, LockedRound: 1, LockedBlockParts: held}, + prs: collecting(held, 10, 1), + served: true, + }, + { + name: "valid block while this round proposes another", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held, ProposalBlockParts: other}, + prs: collecting(held, 10, 1), + served: true, + }, + { + name: "not a block the peer collects", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held}, + prs: collecting(other, 10, 1), + }, + { + name: "not to another height", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held}, + prs: collecting(held, 11, 1), + }, + { + name: "not to a peer in this round or a later one", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held}, + prs: collecting(held, 10, 2), + }, + { + name: "not to a non-validator", + proTxHash: nonValidator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held}, + prs: collecting(held, 10, 1), + }, + } + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() + ps := &PeerState{ProTxHash: tc.proTxHash, PRS: tc.prs} + stateData := StateData{RoundState: tc.rs, state: sm.State{Validators: valSet}} + if tc.served { + // The part set served and the round it is marked as the peer's + // under are the peer's, on the handler's copy of the snapshot. + rsMatchedBy := mock.MatchedBy(func(rs cstypes.RoundState) bool { + return rs.ProposalBlockParts == held && rs.Round == tc.prs.Round + }) + suite.fakeGossiper.On("GossipProposalBlockParts", ctx, rsMatchedBy, mock.Anything).Once() + } else { + suite.fakeBlockStore.On("Base").Return(int64(1)).Maybe() + } + hd := dataGossipHandler(ps, logger, suite.fakeBlockStore, suite.fakeGossiper) + hd(ctx, stateData) + }) + } +} diff --git a/internal/consensus/polka_block_delivery_test.go b/internal/consensus/polka_block_delivery_test.go new file mode 100644 index 000000000..b8ee6c4be --- /dev/null +++ b/internal/consensus/polka_block_delivery_test.go @@ -0,0 +1,202 @@ +package consensus + +import ( + "bytes" + "context" + "sync" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/dashpay/tenderdash/abci/example/kvstore" + abci "github.com/dashpay/tenderdash/abci/types" + "github.com/dashpay/tenderdash/dash" + cstypes "github.com/dashpay/tenderdash/internal/consensus/types" + sf "github.com/dashpay/tenderdash/internal/state/test/factory" + "github.com/dashpay/tenderdash/libs/log" + tmtime "github.com/dashpay/tenderdash/libs/time" + tmproto "github.com/dashpay/tenderdash/proto/tendermint/types" + "github.com/dashpay/tenderdash/types" +) + +// verifyExtensionContractApp accepts a vote extension only for a (height, +// round, block hash) it ran ProcessProposal for, as Dash Drive does: it signs +// withdrawal transactions while processing a proposal and can verify the +// extension of a precommit for that block and round alone. +type verifyExtensionContractApp struct { + abci.BaseApplication + + mtx sync.Mutex + processed []processedProposal + accepted int + rejected int +} + +func (app *verifyExtensionContractApp) ProcessProposal( + ctx context.Context, + req *abci.RequestProcessProposal, +) (*abci.ResponseProcessProposal, error) { + app.mtx.Lock() + app.processed = append(app.processed, processedProposal{height: req.Height, round: req.Round, hash: bytes.Clone(req.Hash)}) + app.mtx.Unlock() + return app.BaseApplication.ProcessProposal(ctx, req) +} + +func (app *verifyExtensionContractApp) VerifyVoteExtension( + _ context.Context, + req *abci.RequestVerifyVoteExtension, +) (*abci.ResponseVerifyVoteExtension, error) { + app.mtx.Lock() + defer app.mtx.Unlock() + for _, p := range app.processed { + if p.height == req.Height && p.round == req.Round && bytes.Equal(p.hash, req.Hash) { + app.accepted++ + return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_ACCEPT}, nil + } + } + app.rejected++ + return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil +} + +func (app *verifyExtensionContractApp) counts() (processed, accepted, rejected int) { + app.mtx.Lock() + defer app.mtx.Unlock() + return len(app.processed), app.accepted, app.rejected +} + +// laggingPolkaNode is a validator that, in round 1 of height 1, saw a polka for +// a block it never received, precommitted nil on the prevote-wait timeout and +// rejected the non-nil precommits of the peers that had the block. +type laggingPolkaNode struct { + ctx context.Context + cs *State + sd StateData + app *verifyExtensionContractApp + parts *types.PartSet + blockID types.BlockID + precommit []*types.Vote // the peers' precommits for the block, rejected once +} + +func (n *laggingPolkaNode) deliver(t *testing.T, msg Message) { + t.Helper() + peer := types.NodeID("aa00000000000000000000000000000000000000") + require.NoError(t, n.cs.msgDispatcher.dispatch(n.ctx, &n.sd, msgInfo{Msg: msg, PeerID: peer, ReceiveTime: tmtime.Now()})) +} + +func (n *laggingPolkaNode) deliverParts(t *testing.T) { + t.Helper() + for i := uint32(0); i < n.parts.Total(); i++ { + n.deliver(t, &BlockPartMessage{Height: 1, Round: 1, Part: n.parts.GetPart(int(i))}) + } +} + +func newLaggingPolkaNode(ctx context.Context, t *testing.T) *laggingPolkaNode { + t.Helper() + app := &verifyExtensionContractApp{} + cs, vss := makeState(ctx, t, makeStateArgs{application: app, logger: log.NewNopLogger()}) + ctx = dash.ContextWithProTxHash(ctx, cs.privValidator.ProTxHash) + n := &laggingPolkaNode{ctx: ctx, cs: cs, app: app, sd: cs.GetStateData()} + sd := &n.sd + + block, err := sf.MakeBlock(sd.state, 1, &types.Commit{}, kvstore.ProtocolVersion) + require.NoError(t, err) + block.CoreChainLockedHeight = sd.state.LastCoreChainLockedBlockHeight + n.parts, err = block.MakePartSet(types.BlockPartSizeBytes) + require.NoError(t, err) + n.blockID = block.BlockID(n.parts) + + // Round 1, prevote step: this node prevoted nil on the propose timeout. + sd.Votes.SetRound(2) + sd.updateRoundStep(1, cstypes.RoundStepPrevote) + var self *validatorStub + var peers []*validatorStub + for _, vs := range vss { + vs.Round = 1 + proTxHash, err := vs.GetProTxHash(ctx) + require.NoError(t, err) + if proTxHash.Equal(cs.privValidator.ProTxHash) { + self = vs + } else { + peers = append(peers, vs) + } + } + require.NotNil(t, self) + require.Len(t, peers, 3) + withBlock, nilVoter := peers[:2], peers[2] + + qt, qh, chainID := sd.Validators.QuorumType, sd.Validators.QuorumHash, sd.state.ChainID + vote := func(vs *validatorStub, voteType tmproto.SignedMsgType, blockID types.BlockID) *types.Vote { + return signVote(ctx, t, vs, voteType, chainID, blockID, qt, qh) + } + + n.deliver(t, &VoteMessage{Vote: vote(self, tmproto.PrevoteType, types.BlockID{})}) + for _, vs := range peers { + n.deliver(t, &VoteMessage{Vote: vote(vs, tmproto.PrevoteType, n.blockID)}) + } + require.Equal(t, cstypes.RoundStepPrevoteWait, sd.Step, "polka for a block not held waits for it") + require.True(t, sd.ProposalBlockParts.HasHeader(n.blockID.PartSetHeader), "polka retargets the part set") + // The prevote-wait timeout. + require.NoError(t, cs.ctrl.Dispatch(ctx, &EnterPrecommitEvent{Height: 1, Round: 1}, sd)) + require.Equal(t, cstypes.RoundStepPrecommit, sd.Step) + + // Own and one peer's nil precommit are stored; the two for the block are + // rejected, as the application has not processed it. + n.deliver(t, &VoteMessage{Vote: vote(self, tmproto.PrecommitType, types.BlockID{})}) + n.deliver(t, &VoteMessage{Vote: vote(nilVoter, tmproto.PrecommitType, types.BlockID{})}) + for _, vs := range withBlock { + v := vote(vs, tmproto.PrecommitType, n.blockID) + n.precommit = append(n.precommit, v) + n.deliver(t, &VoteMessage{Vote: v}) + } + processed, accepted, rejected := app.counts() + require.Equal(t, [3]int{0, 0, 2}, [3]int{processed, accepted, rejected}) + require.Len(t, sd.Votes.Precommits(1).List(), 2) + require.False(t, sd.Votes.Precommits(1).HasTwoThirdsAny()) + require.False(t, sd.TriggeredTimeoutPrecommit) + return n +} + +// A polka block that arrives in the precommit step is processed for the round +// so that the application can verify the extensions of the precommits for it, +// without this node voting again. +func TestStatePolkaBlockCompletedInPrecommitIsProcessed(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := newLaggingPolkaNode(ctx, t) + + n.deliverParts(t) + + require.True(t, n.sd.ProposalBlock.HashesTo(n.blockID.Hash)) + processed, _, _ := n.app.counts() + require.Equal(t, 1, processed, "the completed polka block is processed once") + require.True(t, processedFor(&n.sd.RoundState, 1)) + require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step, "no vote is cast again") + require.Len(t, n.sd.Votes.Prevotes(1).List(), 4) + + // A peer's precommit for the block, delivered again, is now accepted. + n.deliver(t, &VoteMessage{Vote: n.precommit[0]}) + processed, accepted, _ := n.app.counts() + require.Equal(t, 1, processed, "a precommit does not process the block again") + require.Equal(t, 1, accepted) + require.Len(t, n.sd.Votes.Precommits(1).List(), 3) +} + +// Once the polka block and the rejected precommits for it are delivered, the +// lagging node holds +2/3 precommits of any kind and starts the precommit wait +// that takes it to the next round. +func TestStatePolkaBlockDeliveryEndsThePrecommitStall(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := newLaggingPolkaNode(ctx, t) + + n.deliverParts(t) + for _, v := range n.precommit { + n.deliver(t, &VoteMessage{Vote: v}) + } + + _, accepted, _ := n.app.counts() + require.Equal(t, 2, accepted) + require.True(t, n.sd.Votes.Precommits(1).HasTwoThirdsAny()) + require.True(t, n.sd.TriggeredTimeoutPrecommit, "precommit wait starts once every precommit counts") + require.Equal(t, int32(1), n.sd.Round) +} diff --git a/internal/consensus/state_add_prop_block.go b/internal/consensus/state_add_prop_block.go index 9ab668c4a..844a87f05 100644 --- a/internal/consensus/state_add_prop_block.go +++ b/internal/consensus/state_add_prop_block.go @@ -314,7 +314,8 @@ func (e *ProposalCompletedEvent) GetType() EventType { } type ProposalCompletedAction struct { - logger log.Logger + logger log.Logger + blockExec *blockExecutor } func (c *ProposalCompletedAction) Execute(ctx context.Context, stateEvent StateEvent) error { @@ -384,5 +385,36 @@ func (c *ProposalCompletedAction) Execute(ctx context.Context, stateEvent StateE // Transit to EnterPrecommit return stateEvent.Ctrl.Dispatch(ctx, &TryFinalizeCommitEvent{Height: height}, stateData) } + if hasTwoThirds && !blockID.IsNil() && !processedFor(&stateData.RoundState, stateData.Round) && + stateData.ProposalBlock.BlockID(stateData.ProposalBlockParts).Equals(blockID) { + c.processPolkaBlock(ctx, stateData) + } return nil } + +// processPolkaBlock has the application process the round's polka block, which +// completed too late for the round to vote on it, so that it can verify the +// extensions of the precommits for the block. An application keeping one +// execution context per height, like Drive, verifies an extension only for the +// block and round it processed last, and a precommit whose extension is +// rejected is not stored: with the block unprocessed, the precommits of the +// validators that had it never count towards the +2/3 of any kind that start +// the precommit wait, and this node, with no timeout to fall back on, never +// leaves the round. +// +// The block is processed, not voted on: the round's precommit is already nil, +// or will be, since the proposal did not arrive in time. A failure is not +// retried; the round's own path processes the block again should it vote on +// it, and panics there on an ABCI error, as before. +func (c *ProposalCompletedAction) processPolkaBlock(ctx context.Context, stateData *StateData) { + err := c.blockExec.ensureProcess(ctx, &stateData.RoundState, stateData.Round) + if err != nil { + c.logger.Error("cannot process the round's polka block that completed after prevote; precommits for it will be rejected", + "height", stateData.Height, "round", stateData.Round, + "hash", stateData.ProposalBlock.Hash(), "error", err) + return + } + c.logger.Debug("processed the round's polka block that completed after prevote so that precommits for it verify", + "height", stateData.Height, "round", stateData.Round, "step", stateData.Step, + "hash", stateData.ProposalBlock.Hash()) +} diff --git a/internal/consensus/state_controller.go b/internal/consensus/state_controller.go index ca941ac6f..1a782b0c6 100644 --- a/internal/consensus/state_controller.go +++ b/internal/consensus/state_controller.go @@ -101,7 +101,7 @@ func NewController(cs *State, wal *wrapWAL, statsQueue *chanQueue[msgInfo], prop partProofBudget: newBlockPartProofBudget(), candidates: candidates, }, - ProposalCompletedType: &ProposalCompletedAction{logger: cs.logger}, + ProposalCompletedType: &ProposalCompletedAction{logger: cs.logger, blockExec: cs.blockExecutor}, AddVoteType: newAddVoteAction(cs, ctrl, statsQueue), EnterCommitType: &EnterCommitAction{ logger: cs.logger, From 4d241a08640edcb2795bf33ba66bb9100f373f7c Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:39:23 +0000 Subject: [PATCH 2/3] test(consensus): cover the edges of polka block delivery and processing A polka block the application rejects is processed once and not retried on the precommits for it or on its parts arriving again; a block completing in the prevote wait is processed there and not again in the precommit step; one completing in the propose step with its proposal takes the usual path and is processed once; a block completing without being the round's polka block is not processed; an incomplete held part set is not served. Co-Authored-By: Claude Fable 5.1 --- internal/consensus/gossip_handlers_test.go | 11 +- .../consensus/polka_block_delivery_test.go | 293 +++++++++++++----- 2 files changed, 228 insertions(+), 76 deletions(-) diff --git a/internal/consensus/gossip_handlers_test.go b/internal/consensus/gossip_handlers_test.go index 623dc96a7..bec5149db 100644 --- a/internal/consensus/gossip_handlers_test.go +++ b/internal/consensus/gossip_handlers_test.go @@ -754,8 +754,11 @@ func (suite *GossipHandlerTestSuite) TestDataGossipHandlerServesHeldBlockToLaggi logger := log.NewTestingLogger(suite.T()) valSet, _ := factory.MockValidatorSet() validator, nonValidator := valSet.GetProTxHashes()[0], crypto.RandProTxHash() - held := types.NewPartSetFromData(tmrand.Bytes(100), 100) + held := types.NewPartSetFromData(tmrand.Bytes(200), 100) other := types.NewPartSetFromData(tmrand.Bytes(100), 100) + incomplete := types.NewPartSetFromHeader(held.Header()) + _, err := incomplete.AddPart(held.GetPart(0)) + suite.Require().NoError(err) collecting := func(parts *types.PartSet, height int64, round int32) cstypes.PeerRoundState { return cstypes.PeerRoundState{ Height: height, @@ -810,6 +813,12 @@ func (suite *GossipHandlerTestSuite) TestDataGossipHandlerServesHeldBlockToLaggi rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: held}, prs: collecting(held, 10, 2), }, + { + name: "not an incomplete block", + proTxHash: validator, + rs: cstypes.RoundState{Height: 10, Round: 2, ValidRound: 1, ValidBlockParts: incomplete}, + prs: collecting(held, 10, 1), + }, { name: "not to a non-validator", proTxHash: nonValidator, diff --git a/internal/consensus/polka_block_delivery_test.go b/internal/consensus/polka_block_delivery_test.go index b8ee6c4be..e75b57599 100644 --- a/internal/consensus/polka_block_delivery_test.go +++ b/internal/consensus/polka_block_delivery_test.go @@ -26,7 +26,11 @@ import ( type verifyExtensionContractApp struct { abci.BaseApplication + // rejectProposals makes ProcessProposal reject every block. + rejectProposals bool + mtx sync.Mutex + calls int // ProcessProposal calls, accepted or not processed []processedProposal accepted int rejected int @@ -37,8 +41,12 @@ func (app *verifyExtensionContractApp) ProcessProposal( req *abci.RequestProcessProposal, ) (*abci.ResponseProcessProposal, error) { app.mtx.Lock() + defer app.mtx.Unlock() + app.calls++ + if app.rejectProposals { + return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil + } app.processed = append(app.processed, processedProposal{height: req.Height, round: req.Round, hash: bytes.Clone(req.Hash)}) - app.mtx.Unlock() return app.BaseApplication.ProcessProposal(ctx, req) } @@ -58,102 +66,154 @@ func (app *verifyExtensionContractApp) VerifyVoteExtension( return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil } -func (app *verifyExtensionContractApp) counts() (processed, accepted, rejected int) { +// counts returns the ProcessProposal calls and the extensions accepted and +// rejected so far. +func (app *verifyExtensionContractApp) counts() (calls, accepted, rejected int) { app.mtx.Lock() defer app.mtx.Unlock() - return len(app.processed), app.accepted, app.rejected + return app.calls, app.accepted, app.rejected } -// laggingPolkaNode is a validator that, in round 1 of height 1, saw a polka for -// a block it never received, precommitted nil on the prevote-wait timeout and -// rejected the non-nil precommits of the peers that had the block. -type laggingPolkaNode struct { - ctx context.Context - cs *State - sd StateData - app *verifyExtensionContractApp - parts *types.PartSet - blockID types.BlockID - precommit []*types.Vote // the peers' precommits for the block, rejected once +// round1Node is a validator in round 1 of height 1, driven through the real +// message path, with a block of that round's proposer that its three peers +// hold and it does not until deliverParts. +type round1Node struct { + ctx context.Context + cs *State + sd StateData + app *verifyExtensionContractApp + self *validatorStub + peers []*validatorStub + block *types.Block + parts *types.PartSet + blockID types.BlockID } -func (n *laggingPolkaNode) deliver(t *testing.T, msg Message) { +func newRound1Node(ctx context.Context, t *testing.T, app *verifyExtensionContractApp, step cstypes.RoundStepType) *round1Node { t.Helper() - peer := types.NodeID("aa00000000000000000000000000000000000000") - require.NoError(t, n.cs.msgDispatcher.dispatch(n.ctx, &n.sd, msgInfo{Msg: msg, PeerID: peer, ReceiveTime: tmtime.Now()})) -} - -func (n *laggingPolkaNode) deliverParts(t *testing.T) { - t.Helper() - for i := uint32(0); i < n.parts.Total(); i++ { - n.deliver(t, &BlockPartMessage{Height: 1, Round: 1, Part: n.parts.GetPart(int(i))}) - } -} - -func newLaggingPolkaNode(ctx context.Context, t *testing.T) *laggingPolkaNode { - t.Helper() - app := &verifyExtensionContractApp{} cs, vss := makeState(ctx, t, makeStateArgs{application: app, logger: log.NewNopLogger()}) ctx = dash.ContextWithProTxHash(ctx, cs.privValidator.ProTxHash) - n := &laggingPolkaNode{ctx: ctx, cs: cs, app: app, sd: cs.GetStateData()} + n := &round1Node{ctx: ctx, cs: cs, app: app, sd: cs.GetStateData()} sd := &n.sd - block, err := sf.MakeBlock(sd.state, 1, &types.Commit{}, kvstore.ProtocolVersion) + var err error + n.block, err = sf.MakeBlock(sd.state, 1, &types.Commit{}, kvstore.ProtocolVersion) require.NoError(t, err) - block.CoreChainLockedHeight = sd.state.LastCoreChainLockedBlockHeight - n.parts, err = block.MakePartSet(types.BlockPartSizeBytes) + n.block.CoreChainLockedHeight = sd.state.LastCoreChainLockedBlockHeight + proposer, err := sd.ProposerSelector.GetProposer(1, 1) require.NoError(t, err) - n.blockID = block.BlockID(n.parts) + n.block.ProposerProTxHash = proposer.ProTxHash + n.parts, err = n.block.MakePartSet(types.BlockPartSizeBytes) + require.NoError(t, err) + n.blockID = n.block.BlockID(n.parts) - // Round 1, prevote step: this node prevoted nil on the propose timeout. sd.Votes.SetRound(2) - sd.updateRoundStep(1, cstypes.RoundStepPrevote) - var self *validatorStub - var peers []*validatorStub + sd.updateRoundStep(1, step) for _, vs := range vss { vs.Round = 1 proTxHash, err := vs.GetProTxHash(ctx) require.NoError(t, err) if proTxHash.Equal(cs.privValidator.ProTxHash) { - self = vs + n.self = vs } else { - peers = append(peers, vs) + n.peers = append(n.peers, vs) } } - require.NotNil(t, self) - require.Len(t, peers, 3) - withBlock, nilVoter := peers[:2], peers[2] + require.NotNil(t, n.self) + require.Len(t, n.peers, 3) + return n +} + +func (n *round1Node) deliver(t *testing.T, msg Message) { + t.Helper() + peer := types.NodeID("aa00000000000000000000000000000000000000") + require.NoError(t, n.cs.msgDispatcher.dispatch(n.ctx, &n.sd, msgInfo{Msg: msg, PeerID: peer, ReceiveTime: tmtime.Now()})) +} + +func (n *round1Node) vote(t *testing.T, vs *validatorStub, voteType tmproto.SignedMsgType, blockID types.BlockID) *types.Vote { + t.Helper() + return signVote(n.ctx, t, vs, voteType, n.sd.state.ChainID, blockID, n.sd.Validators.QuorumType, n.sd.Validators.QuorumHash) +} + +// deliverVotes delivers one vote of voteType for blockID from each of vss. +func (n *round1Node) deliverVotes(t *testing.T, voteType tmproto.SignedMsgType, blockID types.BlockID, vss ...*validatorStub) []*types.Vote { + t.Helper() + votes := make([]*types.Vote, 0, len(vss)) + for _, vs := range vss { + v := n.vote(t, vs, voteType, blockID) + votes = append(votes, v) + n.deliver(t, &VoteMessage{Vote: v}) + } + return votes +} - qt, qh, chainID := sd.Validators.QuorumType, sd.Validators.QuorumHash, sd.state.ChainID - vote := func(vs *validatorStub, voteType tmproto.SignedMsgType, blockID types.BlockID) *types.Vote { - return signVote(ctx, t, vs, voteType, chainID, blockID, qt, qh) +// deliverProposal delivers the round's proposal for the block, signed by the +// round's proposer. +func (n *round1Node) deliverProposal(t *testing.T) { + t.Helper() + proposer, err := n.sd.ProposerSelector.GetProposer(1, 1) + require.NoError(t, err) + var pv types.PrivValidator + for _, vs := range append([]*validatorStub{n.self}, n.peers...) { + if proTxHash, _ := vs.GetProTxHash(n.ctx); proTxHash.Equal(proposer.ProTxHash) { + pv = vs.PrivValidator + } } + require.NotNil(t, pv) + proposal := types.NewProposal(1, n.block.CoreChainLockedHeight, 1, -1, n.blockID, n.block.Time) + pp := proposal.ToProto() + _, err = pv.SignProposal(n.ctx, n.sd.state.ChainID, n.sd.Validators.QuorumType, n.sd.Validators.QuorumHash, pp) + require.NoError(t, err) + proposal.Signature = pp.Signature + n.deliver(t, &ProposalMessage{Proposal: proposal}) + require.NotNil(t, n.sd.Proposal) +} - n.deliver(t, &VoteMessage{Vote: vote(self, tmproto.PrevoteType, types.BlockID{})}) - for _, vs := range peers { - n.deliver(t, &VoteMessage{Vote: vote(vs, tmproto.PrevoteType, n.blockID)}) +func (n *round1Node) deliverParts(t *testing.T) { + t.Helper() + for i := uint32(0); i < n.parts.Total(); i++ { + n.deliver(t, &BlockPartMessage{Height: 1, Round: 1, Part: n.parts.GetPart(int(i))}) } - require.Equal(t, cstypes.RoundStepPrevoteWait, sd.Step, "polka for a block not held waits for it") - require.True(t, sd.ProposalBlockParts.HasHeader(n.blockID.PartSetHeader), "polka retargets the part set") - // The prevote-wait timeout. - require.NoError(t, cs.ctrl.Dispatch(ctx, &EnterPrecommitEvent{Height: 1, Round: 1}, sd)) - require.Equal(t, cstypes.RoundStepPrecommit, sd.Step) +} + +// polkaWithoutBlock takes the node, in the prevote step having prevoted nil, +// through its peers' polka for the block it does not hold: it waits for the +// block, as its part set is retargeted at it. +func (n *round1Node) polkaWithoutBlock(t *testing.T) { + t.Helper() + n.deliverVotes(t, tmproto.PrevoteType, types.BlockID{}, n.self) + n.deliverVotes(t, tmproto.PrevoteType, n.blockID, n.peers...) + require.Equal(t, cstypes.RoundStepPrevoteWait, n.sd.Step, "polka for a block not held waits for it") + require.True(t, n.sd.ProposalBlockParts.HasHeader(n.blockID.PartSetHeader), "polka retargets the part set") +} + +// timeoutPrevote is the prevote-wait timeout: the node precommits nil. +func (n *round1Node) timeoutPrevote(t *testing.T) { + t.Helper() + require.NoError(t, n.cs.ctrl.Dispatch(n.ctx, &EnterPrecommitEvent{Height: 1, Round: 1}, &n.sd)) + require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step) +} + +// newLaggingPolkaNode is a validator that, in round 1 of height 1, saw a polka +// for a block it never received, precommitted nil on the prevote-wait timeout +// and rejected the non-nil precommits of the two peers that had the block, +// which it returns. +func newLaggingPolkaNode(ctx context.Context, t *testing.T, app *verifyExtensionContractApp) (*round1Node, []*types.Vote) { + t.Helper() + n := newRound1Node(ctx, t, app, cstypes.RoundStepPrevote) + n.polkaWithoutBlock(t) + n.timeoutPrevote(t) // Own and one peer's nil precommit are stored; the two for the block are // rejected, as the application has not processed it. - n.deliver(t, &VoteMessage{Vote: vote(self, tmproto.PrecommitType, types.BlockID{})}) - n.deliver(t, &VoteMessage{Vote: vote(nilVoter, tmproto.PrecommitType, types.BlockID{})}) - for _, vs := range withBlock { - v := vote(vs, tmproto.PrecommitType, n.blockID) - n.precommit = append(n.precommit, v) - n.deliver(t, &VoteMessage{Vote: v}) - } - processed, accepted, rejected := app.counts() - require.Equal(t, [3]int{0, 0, 2}, [3]int{processed, accepted, rejected}) - require.Len(t, sd.Votes.Precommits(1).List(), 2) - require.False(t, sd.Votes.Precommits(1).HasTwoThirdsAny()) - require.False(t, sd.TriggeredTimeoutPrecommit) - return n + n.deliverVotes(t, tmproto.PrecommitType, types.BlockID{}, n.self, n.peers[2]) + precommits := n.deliverVotes(t, tmproto.PrecommitType, n.blockID, n.peers[:2]...) + calls, accepted, rejected := app.counts() + require.Equal(t, [3]int{0, 0, 2}, [3]int{calls, accepted, rejected}) + require.Len(t, n.sd.Votes.Precommits(1).List(), 2) + require.False(t, n.sd.Votes.Precommits(1).HasTwoThirdsAny()) + require.False(t, n.sd.TriggeredTimeoutPrecommit) + return n, precommits } // A polka block that arrives in the precommit step is processed for the round @@ -162,21 +222,21 @@ func newLaggingPolkaNode(ctx context.Context, t *testing.T) *laggingPolkaNode { func TestStatePolkaBlockCompletedInPrecommitIsProcessed(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - n := newLaggingPolkaNode(ctx, t) + n, precommits := newLaggingPolkaNode(ctx, t, &verifyExtensionContractApp{}) n.deliverParts(t) require.True(t, n.sd.ProposalBlock.HashesTo(n.blockID.Hash)) - processed, _, _ := n.app.counts() - require.Equal(t, 1, processed, "the completed polka block is processed once") + calls, _, _ := n.app.counts() + require.Equal(t, 1, calls, "the completed polka block is processed once") require.True(t, processedFor(&n.sd.RoundState, 1)) require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step, "no vote is cast again") require.Len(t, n.sd.Votes.Prevotes(1).List(), 4) // A peer's precommit for the block, delivered again, is now accepted. - n.deliver(t, &VoteMessage{Vote: n.precommit[0]}) - processed, accepted, _ := n.app.counts() - require.Equal(t, 1, processed, "a precommit does not process the block again") + n.deliver(t, &VoteMessage{Vote: precommits[0]}) + calls, accepted, _ := n.app.counts() + require.Equal(t, 1, calls, "a precommit does not process the block again") require.Equal(t, 1, accepted) require.Len(t, n.sd.Votes.Precommits(1).List(), 3) } @@ -187,10 +247,10 @@ func TestStatePolkaBlockCompletedInPrecommitIsProcessed(t *testing.T) { func TestStatePolkaBlockDeliveryEndsThePrecommitStall(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - n := newLaggingPolkaNode(ctx, t) + n, precommits := newLaggingPolkaNode(ctx, t, &verifyExtensionContractApp{}) n.deliverParts(t) - for _, v := range n.precommit { + for _, v := range precommits { n.deliver(t, &VoteMessage{Vote: v}) } @@ -200,3 +260,86 @@ func TestStatePolkaBlockDeliveryEndsThePrecommitStall(t *testing.T) { require.True(t, n.sd.TriggeredTimeoutPrecommit, "precommit wait starts once every precommit counts") require.Equal(t, int32(1), n.sd.Round) } + +// A polka block the application rejects is processed once: neither the +// precommits for it, which stay rejected, nor its parts arriving again process +// it anew. +func TestStatePolkaBlockRejectedByTheApplicationIsNotRetried(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n, precommits := newLaggingPolkaNode(ctx, t, &verifyExtensionContractApp{rejectProposals: true}) + + n.deliverParts(t) + calls, _, _ := n.app.counts() + require.Equal(t, 1, calls) + require.False(t, processedFor(&n.sd.RoundState, 1), "a rejected block does not count as processed") + + for _, v := range precommits { + n.deliver(t, &VoteMessage{Vote: v}) + } + n.deliverParts(t) + calls, accepted, rejected := n.app.counts() + require.Equal(t, [3]int{1, 0, 4}, [3]int{calls, accepted, rejected}) + require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step) + require.False(t, n.sd.TriggeredTimeoutPrecommit) +} + +// A polka block completing in the prevote wait is processed there, and the +// precommit step that follows, precommitting nil for want of the proposal, does +// not process it again. +func TestStatePolkaBlockCompletedInPrevoteWaitIsProcessedOnce(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := newRound1Node(ctx, t, &verifyExtensionContractApp{}, cstypes.RoundStepPrevote) + n.polkaWithoutBlock(t) + + n.deliverParts(t) + calls, _, _ := n.app.counts() + require.Equal(t, 1, calls) + require.True(t, processedFor(&n.sd.RoundState, 1)) + require.Equal(t, cstypes.RoundStepPrevoteWait, n.sd.Step, "processing casts no vote") + + n.timeoutPrevote(t) + calls, _, _ = n.app.counts() + require.Equal(t, 1, calls) + require.Equal(t, int32(-1), n.sd.LockedRound, "nil precommitted: no proposal was received") + require.Equal(t, int32(1), n.sd.ValidRound) +} + +// A proposal completing in the propose step after its polka takes the usual +// path: the node prevotes and precommits the block, processing it once. +func TestStatePolkaBlockCompletedInProposeVotesAndProcessesOnce(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := newRound1Node(ctx, t, &verifyExtensionContractApp{}, cstypes.RoundStepPropose) + n.deliverVotes(t, tmproto.PrevoteType, n.blockID, n.peers...) + require.Equal(t, cstypes.RoundStepPropose, n.sd.Step) + n.deliverProposal(t) + + n.deliverParts(t) + calls, _, _ := n.app.counts() + require.Equal(t, 1, calls) + require.True(t, processedFor(&n.sd.RoundState, 1)) + require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step) + require.Equal(t, int32(1), n.sd.LockedRound, "the block is precommitted") + require.True(t, n.sd.LockedBlock.HashesTo(n.blockID.Hash)) +} + +// A block that completes in the precommit step without being this round's +// polka block is not processed: here the round's polka is for nil. +func TestStateCompletedBlockWithoutItsPolkaIsNotProcessed(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := newRound1Node(ctx, t, &verifyExtensionContractApp{}, cstypes.RoundStepPrevote) + n.deliverProposal(t) + n.deliverVotes(t, tmproto.PrevoteType, types.BlockID{}, n.self) + n.deliverVotes(t, tmproto.PrevoteType, types.BlockID{}, n.peers...) + require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step, "+2/3 prevoted nil") + + n.deliverParts(t) + require.True(t, n.sd.ProposalBlock.HashesTo(n.blockID.Hash)) + calls, _, _ := n.app.counts() + require.Equal(t, 0, calls) + require.False(t, processedFor(&n.sd.RoundState, 1)) + require.Equal(t, int32(-1), n.sd.ValidRound) +} From 2cc8d96bed755e88bde1f6673d040e6624e9eabe Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:42:05 +0000 Subject: [PATCH 3/3] test(consensus): guard the polka block identity check on completion A block completing while the round's polka is for another block is not processed; a nil polka alone let the identity check go unexercised. The rejected-block test no longer redelivers parts the part set already holds, which completes nothing. Co-Authored-By: Claude Fable 5.1 --- .../consensus/polka_block_delivery_test.go | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/internal/consensus/polka_block_delivery_test.go b/internal/consensus/polka_block_delivery_test.go index e75b57599..628f6c459 100644 --- a/internal/consensus/polka_block_delivery_test.go +++ b/internal/consensus/polka_block_delivery_test.go @@ -13,6 +13,7 @@ import ( "github.com/dashpay/tenderdash/dash" cstypes "github.com/dashpay/tenderdash/internal/consensus/types" sf "github.com/dashpay/tenderdash/internal/state/test/factory" + "github.com/dashpay/tenderdash/internal/test/factory" "github.com/dashpay/tenderdash/libs/log" tmtime "github.com/dashpay/tenderdash/libs/time" tmproto "github.com/dashpay/tenderdash/proto/tendermint/types" @@ -261,9 +262,8 @@ func TestStatePolkaBlockDeliveryEndsThePrecommitStall(t *testing.T) { require.Equal(t, int32(1), n.sd.Round) } -// A polka block the application rejects is processed once: neither the -// precommits for it, which stay rejected, nor its parts arriving again process -// it anew. +// A polka block the application rejects is processed once: the precommits for +// it stay rejected and do not have it processed anew. func TestStatePolkaBlockRejectedByTheApplicationIsNotRetried(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -277,7 +277,6 @@ func TestStatePolkaBlockRejectedByTheApplicationIsNotRetried(t *testing.T) { for _, v := range precommits { n.deliver(t, &VoteMessage{Vote: v}) } - n.deliverParts(t) calls, accepted, rejected := n.app.counts() require.Equal(t, [3]int{1, 0, 4}, [3]int{calls, accepted, rejected}) require.Equal(t, cstypes.RoundStepPrecommit, n.sd.Step) @@ -343,3 +342,34 @@ func TestStateCompletedBlockWithoutItsPolkaIsNotProcessed(t *testing.T) { require.False(t, processedFor(&n.sd.RoundState, 1)) require.Equal(t, int32(-1), n.sd.ValidRound) } + +// A block that completes while this round's polka is for another block is not +// processed. A polka retargets the part set at its block, so the other block's +// parts stop at the part set; only a polka that leaves it be, after a block of +// this round was already valid, lets both exist, so the completion is driven at +// the state machine with the votes placed in the vote set directly. +func TestStateCompletedBlockOtherThanThePolkaBlockIsNotProcessed(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := newRound1Node(ctx, t, &verifyExtensionContractApp{}, cstypes.RoundStepPrevote) + n.deliverProposal(t) + n.deliverParts(t) + require.True(t, n.sd.ProposalBlock.HashesTo(n.blockID.Hash)) + n.sd.updateRoundStep(1, cstypes.RoundStepPrecommit) + + other := factory.MakeBlockID() + for _, vs := range n.peers { + added, err := n.sd.Votes.AddVote(n.vote(t, vs, tmproto.PrevoteType, other)) + require.NoError(t, err) + require.True(t, added) + } + polka, ok := n.sd.Votes.Prevotes(1).TwoThirdsMajority() + require.True(t, ok) + require.True(t, polka.Equals(other)) + + require.NoError(t, n.cs.ctrl.Dispatch(n.ctx, &ProposalCompletedEvent{Height: 1}, &n.sd)) + calls, _, _ := n.app.counts() + require.Equal(t, 0, calls) + require.False(t, processedFor(&n.sd.RoundState, 1)) + require.Equal(t, int32(-1), n.sd.ValidRound) +}