From 472a6ff9244e2dd7d1dcb9c2e73edbfe202ff97a Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:25:35 +0000 Subject: [PATCH 01/15] fix(consensus): validate a proposed block against state before ProcessProposal ProcessProposal sent a proposed block to the application before checking it against the committed state; ValidateBlock ran only after the application answered. A block that does not continue the chain (for example a header with a past height, another chain ID or a different last block ID) therefore reached the application, and an application that returns an error instead of REJECT for such input stopped the consensus routine, also when the proposal was read back from the WAL on the next start. With verify, ProcessProposal now runs ValidateBlock first and returns ErrInvalidBlock without calling the application, so consensus prevotes nil. Checks that depend on the application's answer (AppHash, ResultsHash, next validators and params, chain lock) stay after the call. The pre-application validation is timed as its own process_proposal_prevalidate stage. ValidateBlock's cache is keyed on the block hash alone; a hit now counts only when the block still continues the given state (version, chain ID, height, last block ID). Also report expected and actual heights in the right order in the initial-height error. Co-Authored-By: Claude Opus 5.5 --- .../proposal_state_validation_test.go | 137 ++++++++++++++++++ internal/state/execution.go | 18 ++- internal/state/execution_test.go | 54 +++++++ internal/state/validation.go | 71 +++++---- spec/abci/methods.md | 7 +- spec/consensus/consensus-happy-path.md | 6 +- spec/consensus/consensus-transitions.md | 11 +- 7 files changed, 262 insertions(+), 42 deletions(-) create mode 100644 internal/consensus/proposal_state_validation_test.go diff --git a/internal/consensus/proposal_state_validation_test.go b/internal/consensus/proposal_state_validation_test.go new file mode 100644 index 000000000..4ae9f516d --- /dev/null +++ b/internal/consensus/proposal_state_validation_test.go @@ -0,0 +1,137 @@ +package consensus + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + + abcimocks "github.com/dashpay/tenderdash/abci/types/mocks" + "github.com/dashpay/tenderdash/dash" + cstypes "github.com/dashpay/tenderdash/internal/consensus/types" + sm "github.com/dashpay/tenderdash/internal/state" + "github.com/dashpay/tenderdash/internal/test/factory" + "github.com/dashpay/tenderdash/types" +) + +// blocksNotFollowingState corrupts, one per case, a header field that only the +// committed state can tell is wrong. +var blocksNotFollowingState = []struct { + name string + mutate func(*types.Block) +}{ + {"height", func(b *types.Block) { b.Height++ }}, + {"chain ID", func(b *types.Block) { b.ChainID = "other-chain" }}, + {"last block ID", func(b *types.Block) { b.LastBlockID = factory.MakeBlockID() }}, +} + +// newNodeNotProposing returns a validator node at the initial height that is +// not round 0's proposer, the proposer's key, and the node's application, which +// fails ProcessProposal as an application may for a block it cannot process. +func newNodeNotProposing(ctx context.Context, t *testing.T) (*State, types.PrivValidator, *abcimocks.Application) { + t.Helper() + app := abcimocks.NewApplication(t) + app.On("ProcessProposal", mock.Anything, mock.Anything). + Return(nil, errors.New("application cannot process this block")).Maybe() + + genDoc, privVals := factory.RandGenesisDoc(2, factory.ConsensusParams()) + state, err := sm.MakeGenesisState(genDoc) + require.NoError(t, err) + cs := newStateWithConfig(ctx, t, consensusLogger(t), configSetup(t), state, privVals[0], app) + + sd := cs.GetStateData() + proposer, err := sd.ProposerSelector.GetProposer(sd.Height, 0) + require.NoError(t, err) + if proposer.ProTxHash.Equal(cs.privValidator.ProTxHash) { + cs.SetPrivValidator(ctx, privVals[1]) + return cs, privVals[0], app + } + return cs, privVals[1], app +} + +// proposalNotFollowingState returns round 0's proposal, signed by proposer, of a +// block that mutate makes not follow the node's committed state. +func proposalNotFollowingState( + ctx context.Context, + t *testing.T, + cs *State, + proposer types.PrivValidator, + mutate func(*types.Block), +) (*types.Proposal, *types.PartSet) { + t.Helper() + sd := cs.GetStateData() + proTxHash, err := proposer.GetProTxHash(ctx) + require.NoError(t, err) + + block := sd.state.MakeBlock(sd.Height, nil, types.NewCommit(0, 0, types.BlockID{}, nil, nil), nil, proTxHash, 1) + block.CoreChainLockedHeight = sd.state.LastCoreChainLockedBlockHeight + mutate(block) + parts, err := block.MakePartSet(types.BlockPartSizeBytes) + require.NoError(t, err) + + proposal := types.NewProposal(sd.Height, block.CoreChainLockedHeight, 0, -1, block.BlockID(parts), block.Time) + p := proposal.ToProto() + _, err = proposer.SignProposal(ctx, sd.state.ChainID, sd.Validators.QuorumType, sd.Validators.QuorumHash, p) + require.NoError(t, err) + proposal.Signature = p.Signature + return proposal, parts +} + +// TestStatePrevotesNilOnBlockNotFollowingState checks that a validator prevotes +// nil, without asking the application, on a proposed block whose header does +// not follow its committed state. +func TestStatePrevotesNilOnBlockNotFollowingState(t *testing.T) { + for _, tc := range blocksNotFollowingState { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + cs, proposer, app := newNodeNotProposing(ctx, t) + proposal, parts := proposalNotFollowingState(ctx, t, cs, proposer, tc.mutate) + voteCh := subscribeToVoter(ctx, t, cs, cs.privValidator.ProTxHash) + + require.NoError(t, cs.SetProposalAndBlock(ctx, proposal, parts, "peer")) + startTestRound(ctx, cs, proposal.Height, 0) + + ensurePrevoteMatch(t, voteCh, proposal.Height, 0, nil) + app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) + }) + } +} + +// TestReplayOfBlockNotFollowingStatePrevotesNil checks that replaying the WAL +// of a node that received a proposed block not following its committed state +// prevotes nil again without asking the application, and the node keeps running. +func TestReplayOfBlockNotFollowingStatePrevotesNil(t *testing.T) { + for _, tc := range blocksNotFollowingState { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + cs, proposer, app := newNodeNotProposing(ctx, t) + proposal, parts := proposalNotFollowingState(ctx, t, cs, proposer, tc.mutate) + wal := []msgInfo{{Msg: &ProposalMessage{Proposal: proposal}, PeerID: "peer"}} + for i := 0; i < int(parts.Total()); i++ { + wal = append(wal, msgInfo{ + Msg: &BlockPartMessage{Height: proposal.Height, Round: 0, Part: parts.GetPart(i)}, + PeerID: "peer", + }) + } + + cs.emitter.Emit(setReplayModeEventName, true) + ctx = dash.ContextWithProTxHash(ctx, cs.privValidator.ProTxHash) + require.NotPanics(t, func() { + for _, msg := range wal { + assert.NoError(t, cs.readReplayMessage(ctx, &TimedWALMessage{Msg: msg}, nil)) + } + }) + + sd := cs.GetStateData() + assert.Equal(t, cstypes.RoundStepPrevote, sd.Step) + app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) + }) + } +} diff --git a/internal/state/execution.go b/internal/state/execution.go index 6f90f77b9..48757c223 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -359,7 +359,9 @@ func deterministicPrepareProposalHashes(request *abci.RequestPrepareProposal, re return crypto.Checksum(reqBytes), crypto.Checksum(respBytes) } -// ProcessProposal sends the proposal to ABCI App and verifies the response +// ProcessProposal sends the proposal to ABCI App and verifies the response. +// With verify, the block is validated against state before the app sees it, +// and against the app's response afterwards; a failure returns ErrInvalidBlock. func (blockExec *BlockExecutor) ProcessProposal( ctx context.Context, block *types.Block, @@ -368,8 +370,16 @@ func (blockExec *BlockExecutor) ProcessProposal( verify bool, lastCommit types.VerifiedCommit, ) (CurrentRoundState, error) { - version := block.Version.ToProto() stages := blockExec.metrics.startStages() + // The app may fail, rather than reject, on a block that does not continue + // state, and consensus treats such a failure as fatal. + if verify { + if err := blockExec.ValidateBlock(ctx, state, block, lastCommit); err != nil { + return CurrentRoundState{}, ErrInvalidBlock{err} + } + stages.done("process_proposal_prevalidate") + } + version := block.Version.ToProto() resp, err := blockExec.appClient.ProcessProposal(ctx, &abci.RequestProcessProposal{ Hash: block.Header.Hash(), Height: block.Header.Height, @@ -448,7 +458,9 @@ func (blockExec *BlockExecutor) ValidateBlock( lastCommit types.VerifiedCommit, ) error { hash := block.Hash() - if _, ok := blockExec.cache[hash.String()]; ok { + // The cache is keyed on the block hash alone, so a hit counts only for a + // block that still continues state. + if _, ok := blockExec.cache[hash.String()]; ok && validateBlockPosition(state, block) == nil { return nil } diff --git a/internal/state/execution_test.go b/internal/state/execution_test.go index c277409bf..c48f3cb72 100644 --- a/internal/state/execution_test.go +++ b/internal/state/execution_test.go @@ -285,6 +285,60 @@ func TestProcessProposal(t *testing.T) { app.AssertCalled(t, "ProcessProposal", ctx, expectedRpp) } +// TestProcessProposalValidatesBlockBeforeApp checks that a block whose header +// does not follow the committed state is refused as invalid without reaching the +// application, which may fail on it rather than reject it. +func TestProcessProposalValidatesBlockBeforeApp(t *testing.T) { + state, stateDB, _ := makeState(t, 1, 1) + // the state after the genesis block, for a proposal that repeats that block + advanced := state.Copy() + advanced.LastBlockHeight = state.InitialHeight + advanced.LastBlockID = factory.MakeBlockID() + + testCases := []struct { + name string + mutate func(*types.Block) + state sm.State + // validate the block against state first, caching its hash + validateFirst bool + }{ + {name: "height", mutate: func(b *types.Block) { b.Height++ }, state: state}, + {name: "chain ID", mutate: func(b *types.Block) { b.ChainID = "other-chain" }, state: state}, + {name: "last block ID", mutate: func(b *types.Block) { b.LastBlockID = factory.MakeBlockID() }, state: state}, + {name: "genesis block at the next height", state: advanced, validateFirst: true}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + app := abcimocks.NewApplication(t) + app.On("ProcessProposal", mock.Anything, mock.Anything). + Return(nil, errors.New("application cannot process this block")).Maybe() + logger := log.NewNopLogger() + proxyApp := proxy.New(abciclient.NewLocalClient(logger, app), logger, proxy.NopMetrics()) + require.NoError(t, proxyApp.Start(ctx)) + eventBus := eventbus.NewDefault(logger) + require.NoError(t, eventBus.Start(ctx)) + blockExec := sm.NewBlockExecutor(sm.NewStore(stateDB), proxyApp, new(mpmocks.Mempool), + sm.EmptyEvidencePool{}, store.NewBlockStore(dbm.NewMemDB()), eventBus) + + block, err := sf.MakeBlock(state, state.InitialHeight, new(types.Commit), 1) + require.NoError(t, err) + if tc.validateFirst { + require.NoError(t, blockExec.ValidateBlock(ctx, state, block, types.VerifiedCommit{})) + } + if tc.mutate != nil { + tc.mutate(block) + } + + _, err = blockExec.ProcessProposal(ctx, block, 0, tc.state, true, types.VerifiedCommit{}) + require.ErrorAs(t, err, &sm.ErrInvalidBlock{}) + app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) + }) + } +} + func TestUpdateConsensusParams(t *testing.T) { const ( height = 1 diff --git a/internal/state/validation.go b/internal/state/validation.go index a9bb3bc70..b16f81787 100644 --- a/internal/state/validation.go +++ b/internal/state/validation.go @@ -29,37 +29,8 @@ func validateBlock(state State, block *types.Block, lastCommit types.VerifiedCom } } - // Validate basic info. - // We don't validate app version because proposer can override it on a block-by-block basis. - if block.Version.Block != state.Version.Consensus.Block { - return fmt.Errorf("wrong Block.Header.Version.Block. Expected %v, got %v", - state.Version.Consensus, - block.Version, - ) - } - if block.ChainID != state.ChainID { - return fmt.Errorf("wrong Block.Header.ChainID. Expected %v, got %v", - state.ChainID, - block.ChainID, - ) - } - if state.LastBlockHeight == 0 && block.Height != state.InitialHeight { - return fmt.Errorf("wrong Block.Header.Height. Expected %v for initial block, got %v", - block.Height, state.InitialHeight) - } - if state.LastBlockHeight > 0 && block.Height != state.LastBlockHeight+1 { - return fmt.Errorf("wrong Block.Header.Height. Expected %v, got %v", - state.LastBlockHeight+1, - block.Height, - ) - } - - // Validate prev block info. - if !block.LastBlockID.Equals(state.LastBlockID) { - return fmt.Errorf("wrong Block.Header.LastBlockID. Expected %v, got %v", - state.LastBlockID, - block.LastBlockID, - ) + if err := validateBlockPosition(state, block); err != nil { + return err } // Validate app info @@ -133,6 +104,44 @@ func validateBlock(state State, block *types.Block, lastCommit types.VerifiedCom return nil } +// validateBlockPosition checks that block's header continues the chain where +// state ends: same chain and block version, next height, and last block ID. +func validateBlockPosition(state State, block *types.Block) error { + // Validate basic info. + // We don't validate app version because proposer can override it on a block-by-block basis. + if block.Version.Block != state.Version.Consensus.Block { + return fmt.Errorf("wrong Block.Header.Version.Block. Expected %v, got %v", + state.Version.Consensus, + block.Version, + ) + } + if block.ChainID != state.ChainID { + return fmt.Errorf("wrong Block.Header.ChainID. Expected %v, got %v", + state.ChainID, + block.ChainID, + ) + } + if state.LastBlockHeight == 0 && block.Height != state.InitialHeight { + return fmt.Errorf("wrong Block.Header.Height. Expected %v for initial block, got %v", + state.InitialHeight, block.Height) + } + if state.LastBlockHeight > 0 && block.Height != state.LastBlockHeight+1 { + return fmt.Errorf("wrong Block.Header.Height. Expected %v, got %v", + state.LastBlockHeight+1, + block.Height, + ) + } + + // Validate prev block info. + if !block.LastBlockID.Equals(state.LastBlockID) { + return fmt.Errorf("wrong Block.Header.LastBlockID. Expected %v, got %v", + state.LastBlockID, + block.LastBlockID, + ) + } + return nil +} + // verifyLastCommit verifies block.LastCommit against state.LastValidators as // the commit for state.LastBlockID. lastCommit's proof spares that BLS // threshold verification only when it covers exactly this verification — same diff --git a/spec/abci/methods.md b/spec/abci/methods.md index 880c016ac..b65c51c30 100644 --- a/spec/abci/methods.md +++ b/spec/abci/methods.md @@ -406,8 +406,11 @@ validation and reuse rules are in `ensureProcess` processes the held block for the round being checked unless the same block/round result is already current. It runs before the prevote lock rules, -so a locked validator can process another block and still prevote nil. A -well-formed application rejection or block/header mismatch produces a nil prevote; +so a locked validator can process another block and still prevote nil. A block +that does not validate against Tenderdash's committed state (for example a wrong +height, chain ID or last block ID) is never sent to the application and produces +a nil prevote, as do a well-formed application rejection and a block/header +mismatch with the application's answer; unknown status, malformed results and transport errors are fatal on this path. See the exact [prevote decision](../consensus/consensus-transitions.md#prevote-decision-prevoterdo). diff --git a/spec/consensus/consensus-happy-path.md b/spec/consensus/consensus-happy-path.md index 6290ef32d..34700afda 100644 --- a/spec/consensus/consensus-happy-path.md +++ b/spec/consensus/consensus-happy-path.md @@ -206,8 +206,10 @@ POL round's prevotes already have a majority) → `EnterPrevote(h, Round)`. already `ValidBlock`); the core chain lock is consistent (`sm.ValidateBlockChainLock`); for `POLRound == -1` the block's `ProposerProTxHash` is this round's proposer. -2. `blockExecutor.ensureProcess(rs, Round)` → ABCI `ProcessProposal` - (`verify = true`): the response must be `ACCEPT`, well-formed, and +2. `blockExecutor.ensureProcess(rs, Round)` → `ValidateBlock` against the + committed state, then ABCI `ProcessProposal` (`verify = true`); a block + failing `ValidateBlock` never reaches the application. The response must be + `ACCEPT`, well-formed, and `ValidateBlockWithRoundState` must pass — header fields `AppHash`, `ResultsHash`, `NextValidatorsHash`, `NextConsensusHash` must equal the application's answer, `LastCommit` must verify against `LastValidators`, diff --git a/spec/consensus/consensus-transitions.md b/spec/consensus/consensus-transitions.md index ba7971c43..474bf6a37 100644 --- a/spec/consensus/consensus-transitions.md +++ b/spec/consensus/consensus-transitions.md @@ -395,10 +395,13 @@ Rules, in evaluation order: (`POLRound ≥ 0`) keep the original header's proposer and are exempt. 6. **P-Process.** `blockExecutor.ensureProcess(rs, r)`: ABCI `ProcessProposal` for round `r` unless `CurrentRoundState` already is the - result for this block and round. `ErrBlockRejected` (application - `REJECT`) or `ErrInvalidBlock` (a well-formed `ACCEPT` whose contents - disagree with the block: header fields differ from the application's - answer, `LastCommit` invalid, chain lock invalid — + result for this block and round. The block is first validated against the + committed state (`BlockExecutor.ValidateBlock`: version, chain ID, height, + `LastBlockID`, `LastCommit`, proposer, evidence, …); a block that fails is + never sent to the application and yields `ErrInvalidBlock`. + `ErrBlockRejected` (application `REJECT`) or `ErrInvalidBlock` (also a + well-formed `ACCEPT` whose contents disagree with the block: header fields + differ from the application's answer, `LastCommit` invalid, chain lock invalid — `ValidateBlockWithRoundState`) → nil prevote; any other error → **panic** (`prevoter.handleError`). A malformed application response is in the panic class, not the nil-prevote class: `UNKNOWN` status, an From ab1895a6949e404dcb2b855f8e56667357c079b5 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:21:10 +0000 Subject: [PATCH 02/15] fix(consensus): judge proposal timeliness on WAL replay Replay skipped the timeliness check, so a proposal that a node prevoted nil on when it received it reached ProcessProposal when the node replayed its WAL. Replay now dates a message by its WAL entry, the closest record of when it was received and never earlier, and runs the check: a proposal received too early, or a first-height block without the genesis time, gets a nil prevote again without an application call. Too late is not judged in replay, since the WAL entry can lag the receipt. Co-Authored-By: Claude Opus 5.5 --- .../proposal_state_validation_test.go | 77 ++++++++++++++----- internal/consensus/replay.go | 2 + internal/consensus/state_data.go | 6 +- internal/consensus/state_data_test.go | 37 ++++++++- 4 files changed, 98 insertions(+), 24 deletions(-) diff --git a/internal/consensus/proposal_state_validation_test.go b/internal/consensus/proposal_state_validation_test.go index 4ae9f516d..3addcaeab 100644 --- a/internal/consensus/proposal_state_validation_test.go +++ b/internal/consensus/proposal_state_validation_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -14,6 +15,7 @@ import ( cstypes "github.com/dashpay/tenderdash/internal/consensus/types" sm "github.com/dashpay/tenderdash/internal/state" "github.com/dashpay/tenderdash/internal/test/factory" + tmtime "github.com/dashpay/tenderdash/libs/time" "github.com/dashpay/tenderdash/types" ) @@ -52,9 +54,9 @@ func newNodeNotProposing(ctx context.Context, t *testing.T) (*State, types.PrivV return cs, privVals[1], app } -// proposalNotFollowingState returns round 0's proposal, signed by proposer, of a -// block that mutate makes not follow the node's committed state. -func proposalNotFollowingState( +// proposalOfMutatedBlock returns round 0's proposal, signed by proposer, of the +// block the node expects, altered by mutate. +func proposalOfMutatedBlock( ctx context.Context, t *testing.T, cs *State, @@ -90,7 +92,7 @@ func TestStatePrevotesNilOnBlockNotFollowingState(t *testing.T) { defer cancel() cs, proposer, app := newNodeNotProposing(ctx, t) - proposal, parts := proposalNotFollowingState(ctx, t, cs, proposer, tc.mutate) + proposal, parts := proposalOfMutatedBlock(ctx, t, cs, proposer, tc.mutate) voteCh := subscribeToVoter(ctx, t, cs, cs.privValidator.ProTxHash) require.NoError(t, cs.SetProposalAndBlock(ctx, proposal, parts, "peer")) @@ -112,22 +114,8 @@ func TestReplayOfBlockNotFollowingStatePrevotesNil(t *testing.T) { defer cancel() cs, proposer, app := newNodeNotProposing(ctx, t) - proposal, parts := proposalNotFollowingState(ctx, t, cs, proposer, tc.mutate) - wal := []msgInfo{{Msg: &ProposalMessage{Proposal: proposal}, PeerID: "peer"}} - for i := 0; i < int(parts.Total()); i++ { - wal = append(wal, msgInfo{ - Msg: &BlockPartMessage{Height: proposal.Height, Round: 0, Part: parts.GetPart(i)}, - PeerID: "peer", - }) - } - - cs.emitter.Emit(setReplayModeEventName, true) - ctx = dash.ContextWithProTxHash(ctx, cs.privValidator.ProTxHash) - require.NotPanics(t, func() { - for _, msg := range wal { - assert.NoError(t, cs.readReplayMessage(ctx, &TimedWALMessage{Msg: msg}, nil)) - } - }) + proposal, parts := proposalOfMutatedBlock(ctx, t, cs, proposer, tc.mutate) + replayProposal(ctx, t, cs, proposal, parts, tmtime.Now()) sd := cs.GetStateData() assert.Equal(t, cstypes.RoundStepPrevote, sd.Step) @@ -135,3 +123,52 @@ func TestReplayOfBlockNotFollowingStatePrevotesNil(t *testing.T) { }) } } + +// TestReplayOfUntimelyProposalPrevotesNil checks that replaying the WAL of a +// node that received an untimely proposal prevotes nil without asking the +// application, as the node did when it received it, and dates the proposal by +// its WAL entry. +func TestReplayOfUntimelyProposalPrevotesNil(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + cs, proposer, app := newNodeNotProposing(ctx, t) + proposal, parts := proposalOfMutatedBlock(ctx, t, cs, proposer, func(b *types.Block) { + b.Time = b.Time.AddDate(100, 0, 0) + }) + walTime := tmtime.Now() + replayProposal(ctx, t, cs, proposal, parts, walTime) + + sd := cs.GetStateData() + assert.Equal(t, cstypes.RoundStepPrevote, sd.Step) + assert.True(t, walTime.Equal(sd.ProposalReceiveTime), "the WAL time dates the proposal") + app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) +} + +// replayProposal replays, in replay mode, the WAL entries of proposal and its +// parts as written at walTime by a node that received them from a peer. +func replayProposal( + ctx context.Context, + t *testing.T, + cs *State, + proposal *types.Proposal, + parts *types.PartSet, + walTime time.Time, +) { + t.Helper() + wal := []msgInfo{{Msg: &ProposalMessage{Proposal: proposal}, PeerID: "peer"}} + for i := 0; i < int(parts.Total()); i++ { + wal = append(wal, msgInfo{ + Msg: &BlockPartMessage{Height: proposal.Height, Round: 0, Part: parts.GetPart(i)}, + PeerID: "peer", + }) + } + + cs.emitter.Emit(setReplayModeEventName, true) + ctx = dash.ContextWithProTxHash(ctx, cs.privValidator.ProTxHash) + require.NotPanics(t, func() { + for _, msg := range wal { + assert.NoError(t, cs.readReplayMessage(ctx, &TimedWALMessage{Time: walTime, Msg: msg}, nil)) + } + }) +} diff --git a/internal/consensus/replay.go b/internal/consensus/replay.go index 166c0af48..fccde2e2c 100644 --- a/internal/consensus/replay.go +++ b/internal/consensus/replay.go @@ -82,6 +82,8 @@ func (cs *State) readReplayMessage(ctx context.Context, msg *TimedWALMessage, ne } } case msgInfo: + // The WAL keeps no receive time; the entry's time is the closest, never earlier. + m.ReceiveTime = msg.Time peerID := m.PeerID if peerID == "" { peerID = "local" diff --git a/internal/consensus/state_data.go b/internal/consensus/state_data.go index 9efd97e23..7e6f7775a 100644 --- a/internal/consensus/state_data.go +++ b/internal/consensus/state_data.go @@ -370,6 +370,10 @@ func (s *StateData) proposalIsTimely() error { s.ProposalReceiveTime.Sub(s.Proposal.Timestamp).String(), ) case 1: // too late + if s.replayMode { + // In replay, the receive time is the WAL time, which can lag the receipt. + return nil + } return fmt.Errorf( "%w: received too late: height %d, round %d, delay %s", errPrevoteProposalNotTimely, s.Height, s.Round, @@ -726,7 +730,7 @@ func (s *StateData) isValidForPrevote() error { } // if this block was not validated yet, we check if it's timely - if !s.replayMode && !s.ProposalBlock.HashesTo(s.ValidBlock.Hash()) { + if !s.ProposalBlock.HashesTo(s.ValidBlock.Hash()) { if err := s.proposalIsTimely(); err != nil { return err } diff --git a/internal/consensus/state_data_test.go b/internal/consensus/state_data_test.go index 5a7a5fcf1..d1c3a1e75 100644 --- a/internal/consensus/state_data_test.go +++ b/internal/consensus/state_data_test.go @@ -27,9 +27,10 @@ func TestIsValidForPrevote(t *testing.T) { Validators: valSet, } testCases := []struct { - state sm.State - rs cstypes.RoundState - wantErr string + state sm.State + rs cstypes.RoundState + replayMode bool + wantErr string }{ { // invalid proposal-block @@ -100,12 +101,42 @@ func TestIsValidForPrevote(t *testing.T) { }, wantErr: "", }, + { + // in replay, the WAL time shows the proposal was received too early + state: sm.State{InitialHeight: 1}, + rs: cstypes.RoundState{ + Height: 1000, + ProposalBlock: &types.Block{Header: types.Header{Time: now.Add(time.Hour)}}, + Proposal: &types.Proposal{Timestamp: now.Add(time.Hour), POLRound: -1}, + ProposalReceiveTime: now, + LockedRound: -1, + Validators: valSet, + }, + replayMode: true, + wantErr: "proposal is not timely", + }, + { + // in replay, a WAL time later than the window is no proof of a late + // receipt: the WAL dates a message by when it was handled + state: sm.State{InitialHeight: 1}, + rs: cstypes.RoundState{ + Height: 1000, + ProposalBlock: &types.Block{Header: types.Header{Time: now}}, + Proposal: &types.Proposal{Timestamp: now, POLRound: -1}, + ProposalReceiveTime: now.Add(time.Hour), + LockedRound: -1, + Validators: valSet, + }, + replayMode: true, + wantErr: "", + }, } for i, tc := range testCases { t.Run(fmt.Sprintf("test-case #%d", i), func(t *testing.T) { stateData := StateData{ state: tc.state, RoundState: tc.rs, + replayMode: tc.replayMode, } selector, err := selectproposer.NewHeightRoundProposerSelector(valSet.Copy(), tc.rs.Height, tc.rs.Round, nil, nil) require.NoError(t, err) From f33748e062f46896966e211313306d2027905258 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:21:10 +0000 Subject: [PATCH 03/15] docs(consensus): describe validation before ProcessProposal and replay timeliness Align the prevote flowchart, the happy-path sequence diagram and failure paragraph, and the P-Timely rule with the current order: ValidateBlock runs against the committed state before the application call, and replay judges timeliness by the WAL entry time. Update the ValidateBlockWithRoundState comment on what a ValidateBlock cache hit re-checks. Co-Authored-By: Claude Opus 5.5 --- internal/state/execution.go | 10 +++++----- spec/consensus/consensus-alternative-flows.md | 7 ++++--- spec/consensus/consensus-happy-path.md | 14 +++++++------ spec/consensus/consensus-transitions.md | 20 +++++++++++-------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/internal/state/execution.go b/internal/state/execution.go index 48757c223..e034ec206 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -520,11 +520,11 @@ func (blockExec *BlockExecutor) ValidateBlockWithRoundState( ) } - // ValidateBlock above normally verifies block.LastCommit, but it short-circuits - // on its per-height cache, which is keyed on the block hash alone and so says - // nothing about the state the earlier validation ran against. That is why this - // second verification exists. lastCommit's proof is safe to skip on instead: - // it names the verified data itself, not a block hash. + // ValidateBlock above normally verifies block.LastCommit, but a hit on its + // cache, keyed on the block hash, re-checks only the block's position against + // state (validateBlockPosition), not its LastCommit. That is why this second + // verification exists. lastCommit's proof is safe to skip on instead: it names + // the verified data itself, not a block hash. if block.Height > state.InitialHeight { if err := verifyLastCommit(state, block, lastCommit, blockExec.metrics); err != nil { return err diff --git a/spec/consensus/consensus-alternative-flows.md b/spec/consensus/consensus-alternative-flows.md index 53408e7a7..d88799f14 100644 --- a/spec/consensus/consensus-alternative-flows.md +++ b/spec/consensus/consensus-alternative-flows.md @@ -289,9 +289,10 @@ and `addVoteErrorMw` (`internal/consensus/state_add_vote.go`). - Timeliness is checked **only at prevote time**, against the proposal's receive time (`ProposalReceiveTime` = enqueue time of the `ProposalMessage`, `chanMsgSender.send`), with the window of - `checkTimely` (rule P-Timely). It is skipped in replay mode and for a - block that is the node's `ValidBlock`, so re-proposals of an already - polka'd block are not re-judged. + `checkTimely` (rule P-Timely). It is skipped for a block that is the + node's `ValidBlock`, so re-proposals of an already polka'd block are not + re-judged. In replay mode the receive time is the time of the proposal's + WAL entry, and only a proposal received too early fails. - Timeliness governs re-proposal (`Proposaler.checkValidBlock`). - Metric `ProposalTimestampDifference{is_timely}` is recorded for proposals with `POLRound = -1` (`Proposaler.proposalTimestampDifferenceMetric`). diff --git a/spec/consensus/consensus-happy-path.md b/spec/consensus/consensus-happy-path.md index 34700afda..990932a4c 100644 --- a/spec/consensus/consensus-happy-path.md +++ b/spec/consensus/consensus-happy-path.md @@ -32,6 +32,7 @@ sequenceDiagram P-->>V: ProposalMessage, BlockPartMessages (gossip) V->>V: Proposaler.Set (verify proposer signature), AddProposalBlockPart V->>V: ProposalCompleted → EnterPrevote(h,0) + V->>V: ValidateBlock against committed state V->>A: ProcessProposal(block, round=0) A-->>V: ACCEPT + AppHash/TxResults/... V->>V: ValidateBlockWithRoundState, sign PREVOTE(B) @@ -202,8 +203,9 @@ POL round's prevotes already have a majority) → `EnterPrevote(h, Round)`. → `prevoter.Do` (`internal/consensus/state_prevoter.go`): 1. `StateData.isValidForPrevote`: block and proposal held; proposal timestamp - equals block time; the proposal is timely (unless replaying or the block is - already `ValidBlock`); the core chain lock is consistent + equals block time; the proposal is timely (unless the block is already + `ValidBlock`; in replay, only a proposal received too early fails); the + core chain lock is consistent (`sm.ValidateBlockChainLock`); for `POLRound == -1` the block's `ProposerProTxHash` is this round's proposer. 2. `blockExecutor.ensureProcess(rs, Round)` → `ValidateBlock` against the @@ -220,10 +222,10 @@ POL round's prevotes already have a majority) → `EnterPrevote(h, Round)`. `checkProposalBlock` is true → `voteSigner.signAddVote(PrevoteType, BlockID)`. Step `Prevote` is set on return. A failure in step 1, or `ErrBlockRejected` -(application `REJECT`) or `ErrInvalidBlock` (`ValidateBlockWithRoundState`) in -step 2, yields a **nil prevote** instead. Any other step-2 error — transport -failure, `UNKNOWN` status or a malformed response — **panics** the node -(`prevoter.handleError`). **⇢** [Prevote rule](./consensus-transitions.md#prevote-decision-prevoterdo). +(application `REJECT`) or `ErrInvalidBlock` (`ValidateBlock` before the call, +`ValidateBlockWithRoundState` after it) in step 2, yields a **nil prevote** +instead. Any other step-2 error — transport failure, `UNKNOWN` status or a +malformed response — **panics** the node (`prevoter.handleError`). **⇢** [Prevote rule](./consensus-transitions.md#prevote-decision-prevoterdo). ### 6. Collecting prevotes diff --git a/spec/consensus/consensus-transitions.md b/spec/consensus/consensus-transitions.md index 474bf6a37..be464b21f 100644 --- a/spec/consensus/consensus-transitions.md +++ b/spec/consensus/consensus-transitions.md @@ -349,16 +349,18 @@ flowchart TD B -- no --> NIL B -- yes --> C{Proposal.Timestamp == block.Time?} C -- no --> NIL - C -- yes --> D{replay mode or block == ValidBlock?} + C -- yes --> D{block == ValidBlock?} D -- yes --> F - D -- no --> E{timely? InitialHeight: timestamp == genesis time; else receive time within window for round r} + D -- no --> E{timely? InitialHeight: timestamp == genesis time; else receive time within window for round r, in replay only too early fails} E -- no --> NIL E -- yes --> F{ValidateBlockChainLock ok?} F -- no --> NIL F -- yes --> G{POLRound == -1 implies block.ProposerProTxHash == proposer h,r?} G -- no --> NIL - G -- yes --> H[ensureProcess: ABCI ProcessProposal for round r unless already processed] - H --> I{ACCEPT and header matches app result?} + G -- yes --> H[ensureProcess for round r unless already processed] + H --> V{ValidateBlock against committed state ok?} + V -- no --> NIL + V -- yes --> I{ABCI ProcessProposal: ACCEPT and header matches app result?} I -- REJECT or mismatch --> NIL I -- other error --> PANIC[panic] I -- yes --> J[mustValidate] @@ -379,12 +381,14 @@ Rules, in evaluation order: (`errPrevoteProposalBlockNil`, `errPrevoteProposalNil`). This is the path taken on `timeoutPropose`. No ABCI call is made. 2. **P-Timestamp.** `Proposal.Timestamp ≠ ProposalBlock.Time` → nil. -3. **P-Timely.** Skipped in replay mode and when the block is already - `ValidBlock`. At `InitialHeight` the timestamp must equal the genesis time; +3. **P-Timely.** Skipped when the block is already `ValidBlock`. At + `InitialHeight` the timestamp must equal the genesis time; otherwise `Proposal.CheckTimely(ProposalReceiveTime, SynchronyParams, r)` must be 0 (`checkTimely` in `types/validation.go`): `ts − Precision ≤ recv ≤ ts + MessageDelay·2^⌊r/10⌋ + Precision` - (the shift is capped to avoid overflow). + (the shift is capped to avoid overflow). In replay mode `recv` is the time + of the proposal's WAL entry, which can lag its receipt, so only + `recv < ts − Precision` (too early) fails. 4. **P-ChainLock.** `sm.ValidateBlockChainLock(state, block)` (`internal/state/validation.go`): with a `CoreChainLock` in the block, `Header.CoreChainLockedHeight == CoreChainLock.CoreBlockHeight` and @@ -494,7 +498,7 @@ that checks each is given. | Proposal `BlockID` hash and part-set header equal the assembled block's | `AddProposalBlockPartAction.addProposalBlockPart` | | Parts' Merkle proofs valid; total size `≤ Block.MaxBytes` | `PartSet.AddPart`, `addProposalBlockPart` | | `Proposal.Timestamp == Header.Time` | `isValidForPrevote`, `EnterPrecommitAction.Execute` | -| Timely (PBTS window), unless `ValidBlock` or replay | `StateData.proposalIsTimely` | +| Timely (PBTS window; in replay, not too early), unless `ValidBlock` | `StateData.proposalIsTimely` | | Core chain lock consistent with state | `sm.ValidateBlockChainLock` | | `Header.ProposerProTxHash == proposer(h, r)` when `POLRound == -1` | `isValidForPrevote` | | `Block.ValidateBasic`; version, chain id, height, `LastBlockID`, `ConsensusHash`, `ValidatorsHash`, `LastCommit` threshold signature, proposer is a validator, `Time > LastBlockTime` (`≥ genesis` at initial height), evidence size | `validateBlock` in `internal/state/validation.go` | From b7e4b99497be6ea13f530b526af69a7cd2e4ff77 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:25:34 +0000 Subject: [PATCH 04/15] fix(consensus): tell a pre-application validation failure from an ABCI error A block that failed ValidateBlock before ProcessProposal was logged as "ProcessProposal abci method: ..." although the application was never called. BlockExecutor.ProcessProposal now labels the pre-application failure "validate block before ProcessProposal" and the application call error "ProcessProposal abci method"; ensureProcess wraps both as "process proposal". Co-Authored-By: Claude Opus 5.5 --- internal/consensus/block_executor.go | 2 +- internal/consensus/state_test.go | 2 +- internal/state/execution.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/consensus/block_executor.go b/internal/consensus/block_executor.go index d8f55d815..d5fe16dd1 100644 --- a/internal/consensus/block_executor.go +++ b/internal/consensus/block_executor.go @@ -96,7 +96,7 @@ func (c *blockExecutor) ensureProcess(ctx context.Context, rs *cstypes.RoundStat if err != nil { rs.CurrentRoundState = sm.CurrentRoundState{} rs.ProcessedBlockID = types.BlockID{} - return fmt.Errorf("ProcessProposal abci method: %w", err) + return fmt.Errorf("process proposal: %w", err) } rs.CurrentRoundState = uncommittedState rs.ProcessedBlockID = block.BlockID(rs.ProposalBlockParts).Copy() diff --git a/internal/consensus/state_test.go b/internal/consensus/state_test.go index cea535df6..985c1aec4 100644 --- a/internal/consensus/state_test.go +++ b/internal/consensus/state_test.go @@ -3453,7 +3453,7 @@ func TestStateTryAddCommitPanicsOnClientError(t *testing.T) { ReceiveTime: time.Time{}, }) assert.PanicsWithError(t, - "ABCI client stopped, Tenderdash needs to be restarted: ProcessProposal abci method: client has stopped", + "ABCI client stopped, Tenderdash needs to be restarted: process proposal: ProcessProposal abci method: client has stopped", func() { _ = consensusState.ctrl.Dispatch(ctx, &TryAddCommitEvent{Commit: commit, PeerID: peerID}, &stateData) }) diff --git a/internal/state/execution.go b/internal/state/execution.go index e034ec206..104319311 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -375,7 +375,7 @@ func (blockExec *BlockExecutor) ProcessProposal( // state, and consensus treats such a failure as fatal. if verify { if err := blockExec.ValidateBlock(ctx, state, block, lastCommit); err != nil { - return CurrentRoundState{}, ErrInvalidBlock{err} + return CurrentRoundState{}, ErrInvalidBlock{fmt.Errorf("validate block before ProcessProposal: %w", err)} } stages.done("process_proposal_prevalidate") } @@ -399,7 +399,7 @@ func (blockExec *BlockExecutor) ProcessProposal( QuorumHash: state.Validators.QuorumHash, }) if err != nil { - return CurrentRoundState{}, err + return CurrentRoundState{}, fmt.Errorf("ProcessProposal abci method: %w", err) } if resp.IsStatusUnknown() { return CurrentRoundState{}, fmt.Errorf("ProcessProposal responded with status %s", resp.Status.String()) From e1382b12286609badbe4e31106da3f81d72a3016 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:25:34 +0000 Subject: [PATCH 05/15] test(consensus): check the prevote replay casts is nil The replay tests checked only the round step. They now read the node's own prevote from its queue and require it to be for nil. Co-Authored-By: Claude Opus 5.5 --- .../proposal_state_validation_test.go | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/consensus/proposal_state_validation_test.go b/internal/consensus/proposal_state_validation_test.go index 3addcaeab..303a5df3f 100644 --- a/internal/consensus/proposal_state_validation_test.go +++ b/internal/consensus/proposal_state_validation_test.go @@ -16,6 +16,7 @@ import ( sm "github.com/dashpay/tenderdash/internal/state" "github.com/dashpay/tenderdash/internal/test/factory" tmtime "github.com/dashpay/tenderdash/libs/time" + tmproto "github.com/dashpay/tenderdash/proto/tendermint/types" "github.com/dashpay/tenderdash/types" ) @@ -117,8 +118,7 @@ func TestReplayOfBlockNotFollowingStatePrevotesNil(t *testing.T) { proposal, parts := proposalOfMutatedBlock(ctx, t, cs, proposer, tc.mutate) replayProposal(ctx, t, cs, proposal, parts, tmtime.Now()) - sd := cs.GetStateData() - assert.Equal(t, cstypes.RoundStepPrevote, sd.Step) + requireOwnNilPrevote(ctx, t, cs) app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) }) } @@ -139,12 +139,28 @@ func TestReplayOfUntimelyProposalPrevotesNil(t *testing.T) { walTime := tmtime.Now() replayProposal(ctx, t, cs, proposal, parts, walTime) - sd := cs.GetStateData() - assert.Equal(t, cstypes.RoundStepPrevote, sd.Step) - assert.True(t, walTime.Equal(sd.ProposalReceiveTime), "the WAL time dates the proposal") + requireOwnNilPrevote(ctx, t, cs) + assert.True(t, walTime.Equal(cs.GetStateData().ProposalReceiveTime), "the WAL time dates the proposal") app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) } +// requireOwnNilPrevote checks that cs, not running, is at the prevote step and +// has queued its own prevote, for nil. +func requireOwnNilPrevote(ctx context.Context, t *testing.T, cs *State) { + t.Helper() + require.Equal(t, cstypes.RoundStepPrevote, cs.GetStateData().Step) + ctx, cancel := context.WithTimeout(ctx, time.Second) + defer cancel() + for { + mi, ok := cs.msgInfoQueue.sender.internalQueue.recv(ctx) + require.True(t, ok, "the node cast no prevote") + if msg, isVote := mi.Msg.(*VoteMessage); isVote && msg.Vote.Type == tmproto.PrevoteType { + assert.True(t, msg.Vote.BlockID.IsNil(), "the node prevoted for block %v", msg.Vote.BlockID) + return + } + } +} + // replayProposal replays, in replay mode, the WAL entries of proposal and its // parts as written at walTime by a node that received them from a peer. func replayProposal( From 717e991eebb4e2b722b96eb01cc3bfa4ba55e904 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:25:34 +0000 Subject: [PATCH 06/15] docs(consensus): describe validation before ProcessProposal in alternative flows Co-Authored-By: Claude Opus 5.5 --- spec/consensus/consensus-alternative-flows.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spec/consensus/consensus-alternative-flows.md b/spec/consensus/consensus-alternative-flows.md index d88799f14..84d3bb9b0 100644 --- a/spec/consensus/consensus-alternative-flows.md +++ b/spec/consensus/consensus-alternative-flows.md @@ -102,12 +102,14 @@ undecodable block → error (`addProposalBlockPart`). Parts with invalid Merkle proofs are charged to the sender's `blockPartProofBudget` and dropped silently once it is exhausted (`internal/consensus/block_part_budget.go`). -### Application rejects the block, or the header does not match - -`ProcessProposal` returning `REJECT` (`sm.ErrBlockRejected`), or a -well-formed `ACCEPT` whose contents disagree with the block — header -mismatch / invalid `LastCommit` / invalid chain lock (`sm.ErrInvalidBlock`) -— → nil prevote (`prevoter.handleError`). A failed `ProcessProposal` clears +### Block is invalid, application rejects it, or the header does not match + +`BlockExecutor.ProcessProposal` first validates the block against the +committed state (`ValidateBlock`); a block that fails is never sent to the +application. That failure (`sm.ErrInvalidBlock`), the application returning +`REJECT` (`sm.ErrBlockRejected`), or a well-formed `ACCEPT` whose contents +disagree with the block — header mismatch / invalid `LastCommit` / invalid +chain lock (`sm.ErrInvalidBlock`) — → nil prevote (`prevoter.handleError`). A failed `ProcessProposal` clears `CurrentRoundState`, so no round counts as processed until a later call succeeds (`blockExecutor.ensureProcess`). Any **other** error **panics** the node: transport failure, `UNKNOWN` status, or a malformed response — an From 77b3bd9b81e0534c55dacf44a19561278adf36ad Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:40:57 +0000 Subject: [PATCH 07/15] test(consensus): judge replayed proposal timeliness above the first height At the first height a proposal is judged by the genesis time alone, so the replay test there never reached the receive-time window. Commit a block first and replay a proposal whose WAL entry is too early for its timestamp (nil prevote, no application call) and one whose entry is timely (reaches the application). Keep the first-height case under a name that says what it checks. Co-Authored-By: Claude Opus 5.5 --- .../proposal_state_validation_test.go | 74 ++++++++++++++++--- 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/internal/consensus/proposal_state_validation_test.go b/internal/consensus/proposal_state_validation_test.go index 303a5df3f..af21474e8 100644 --- a/internal/consensus/proposal_state_validation_test.go +++ b/internal/consensus/proposal_state_validation_test.go @@ -10,11 +10,13 @@ import ( "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + abci "github.com/dashpay/tenderdash/abci/types" abcimocks "github.com/dashpay/tenderdash/abci/types/mocks" "github.com/dashpay/tenderdash/dash" cstypes "github.com/dashpay/tenderdash/internal/consensus/types" sm "github.com/dashpay/tenderdash/internal/state" "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" "github.com/dashpay/tenderdash/types" @@ -69,7 +71,11 @@ func proposalOfMutatedBlock( proTxHash, err := proposer.GetProTxHash(ctx) require.NoError(t, err) - block := sd.state.MakeBlock(sd.Height, nil, types.NewCommit(0, 0, types.BlockID{}, nil, nil), nil, proTxHash, 1) + lastCommit := sd.LastCommit + if lastCommit == nil { + lastCommit = types.NewCommit(0, 0, types.BlockID{}, nil, nil) + } + block := sd.state.MakeBlock(sd.Height, nil, lastCommit, nil, proTxHash, 1) block.CoreChainLockedHeight = sd.state.LastCoreChainLockedBlockHeight mutate(block) parts, err := block.MakePartSet(types.BlockPartSizeBytes) @@ -124,11 +130,10 @@ func TestReplayOfBlockNotFollowingStatePrevotesNil(t *testing.T) { } } -// TestReplayOfUntimelyProposalPrevotesNil checks that replaying the WAL of a -// node that received an untimely proposal prevotes nil without asking the -// application, as the node did when it received it, and dates the proposal by -// its WAL entry. -func TestReplayOfUntimelyProposalPrevotesNil(t *testing.T) { +// TestReplayOfFirstBlockWithoutGenesisTimePrevotesNil checks that replaying the +// WAL of a node that received a first block not dated at genesis time prevotes +// nil without asking the application, as the node did when it received it. +func TestReplayOfFirstBlockWithoutGenesisTimePrevotesNil(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -136,14 +141,65 @@ func TestReplayOfUntimelyProposalPrevotesNil(t *testing.T) { proposal, parts := proposalOfMutatedBlock(ctx, t, cs, proposer, func(b *types.Block) { b.Time = b.Time.AddDate(100, 0, 0) }) - walTime := tmtime.Now() - replayProposal(ctx, t, cs, proposal, parts, walTime) + replayProposal(ctx, t, cs, proposal, parts, tmtime.Now()) requireOwnNilPrevote(ctx, t, cs) - assert.True(t, walTime.Equal(cs.GetStateData().ProposalReceiveTime), "the WAL time dates the proposal") app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) } +// TestReplayJudgesProposalTimelinessByWALTime checks, above the first height, +// that replay judges a proposal by the time of its WAL entry: one written too +// early for the proposal's timestamp gets a nil prevote without an application +// call, as the node gave it when it received it, and a timely one reaches the +// application. +func TestReplayJudgesProposalTimelinessByWALTime(t *testing.T) { + for _, tc := range []struct { + name string + walDelay time.Duration + reachesApp bool + }{ + {"received too early", -time.Hour, false}, + {"received in time", 0, true}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + live := newRoundSkipTest(ctx, t, "consensus_replay_proposal_timeliness_test", nil) + config, err := ResetConfig(t, "consensus_replay_proposal_timeliness_test") + require.NoError(t, err) + config.Consensus.CreateEmptyBlocks = false + app := abcimocks.NewApplication(t) + app.On("ProcessProposal", mock.Anything, mock.Anything). + Return(&abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil).Maybe() + cs := newStateWithConfigAndBlockStore(ctx, t, log.NewNopLogger(), config, live.cs.GetStateData().state, + live.cs.privValidator.PrivValidator, app, live.cs.blockStore) + + sd := cs.GetStateData() + require.Equal(t, sd.state.InitialHeight+1, sd.Height) + proposer, err := sd.ProposerSelector.GetProposer(sd.Height, 0) + require.NoError(t, err) + var proposerKey types.PrivValidator + for _, vs := range live.vss { + if proTxHash, _ := vs.GetProTxHash(ctx); proposer.ProTxHash.Equal(proTxHash) { + proposerKey = vs.PrivValidator + } + } + require.NotNil(t, proposerKey) + + proposal, parts := proposalOfMutatedBlock(ctx, t, cs, proposerKey, func(*types.Block) {}) + replayProposal(ctx, t, cs, proposal, parts, proposal.Timestamp.Add(tc.walDelay)) + + requireOwnNilPrevote(ctx, t, cs) + if tc.reachesApp { + app.AssertNumberOfCalls(t, "ProcessProposal", 1) + } else { + app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) + } + }) + } +} + // requireOwnNilPrevote checks that cs, not running, is at the prevote step and // has queued its own prevote, for nil. func requireOwnNilPrevote(ctx context.Context, t *testing.T, cs *State) { From 0523cf05dd7bd880728c96f571c0d8b2910b57fe Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:42:41 +0000 Subject: [PATCH 08/15] fix(consensus): drop the repeated prefix from ProcessProposal failures ensureProcess already prefixes its errors with "process proposal", so the prevoter panic and the commit path no longer add their own prefix on top. A failed ProcessProposal call now panics as "process proposal: ProcessProposal abci method: ..." instead of "ProcessProposal: process proposal: ProcessProposal abci method: ...". Co-Authored-By: Claude Opus 5.5 --- internal/consensus/state_prevoter.go | 3 +-- internal/consensus/state_prevoter_test.go | 16 ++++++++++++++++ internal/consensus/state_try_add_commit.go | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/consensus/state_prevoter.go b/internal/consensus/state_prevoter.go index e9f4ae2c7..68db46401 100644 --- a/internal/consensus/state_prevoter.go +++ b/internal/consensus/state_prevoter.go @@ -3,7 +3,6 @@ package consensus import ( "context" "errors" - "fmt" "time" cstypes "github.com/dashpay/tenderdash/internal/consensus/types" @@ -72,7 +71,7 @@ func (p *prevoter) handleError(err error, args ...any) { return } // Unknown error, so we panic - panic(fmt.Sprintf("ProcessProposal: %v", err)) + panic(err.Error()) } func (p *prevoter) signAndAddNilVote(ctx context.Context, stateData *StateData) { diff --git a/internal/consensus/state_prevoter_test.go b/internal/consensus/state_prevoter_test.go index 8723b615e..f01a97b51 100644 --- a/internal/consensus/state_prevoter_test.go +++ b/internal/consensus/state_prevoter_test.go @@ -2,6 +2,7 @@ package consensus import ( "context" + "errors" "fmt" "regexp" "testing" @@ -152,6 +153,21 @@ func (suite *PrevoterTestSuite) TestDo() { } } +// TestDoPanicsOnProcessProposalFailure pins the panic text for a ProcessProposal +// error that is neither a rejection nor an invalid block. +func (suite *PrevoterTestSuite) TestDoPanicsOnProcessProposalFailure() { + ctx := context.Background() + validStateData := suite.makeValidStateData() + stateData := StateData{state: validStateData.state, RoundState: validStateData.RoundState} + suite.mockExecutor. + On("ProcessProposal", ctx, mock.Anything, int32(0), mock.Anything, true, types.VerifiedCommit{}). + Once(). + Return(sm.CurrentRoundState{}, errors.New("ProcessProposal abci method: client has stopped")) + suite.PanicsWithValue("process proposal: ProcessProposal abci method: client has stopped", func() { + _ = suite.prevoter.Do(ctx, &stateData) + }) +} + func (suite *PrevoterTestSuite) TestCheckProposalBlock() { testCases := []struct { modifier func(stateData *StateData) diff --git a/internal/consensus/state_try_add_commit.go b/internal/consensus/state_try_add_commit.go index b98941c86..33c25b94b 100644 --- a/internal/consensus/state_try_add_commit.go +++ b/internal/consensus/state_try_add_commit.go @@ -335,7 +335,7 @@ func verifyProcessedCommit(ctx context.Context, blockExec *blockExecutor, stateD // this is a non-recoverable error in current architecture panic(fmt.Errorf("ABCI client stopped, Tenderdash needs to be restarted: %w", err)) } - return fmt.Errorf("unable to process proposal: %w", err) + return err } if err := blockExec.validate(ctx, stateData); err != nil { return fmt.Errorf("+2/3 committed an invalid block: %w", err) From 34de205c908f389327d8b1a41b1d56a68cc90071 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:28:24 +0000 Subject: [PATCH 09/15] test(state): expect the chain lock height check before ProcessProposal TestProcessProposalValidatesChainLockHeightBeforeApp states the intended behaviour and fails on 0523cf05d: with verify, a block whose header core chain locked height does not follow the committed state reaches the application, and its failure is returned instead of ErrInvalidBlock. The check (validateCoreChainLock) needs nothing from the application; only the prevote path runs it first, through ValidateBlockChainLock. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01WunYYgi2uMsQDcBcKAwLdt --- internal/state/execution_test.go | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/state/execution_test.go b/internal/state/execution_test.go index c48f3cb72..e5f43b031 100644 --- a/internal/state/execution_test.go +++ b/internal/state/execution_test.go @@ -1858,3 +1858,36 @@ func TestAppReadyProbesTheProposalBackends(t *testing.T) { assert.NoError(t, ready(backend(true), backend(false)), "a stalled default backend does not hold back a healthy proposal backend") } + +// TestProcessProposalValidatesChainLockHeightBeforeApp checks that a block whose +// header core chain locked height does not follow the committed state, a check +// that needs nothing from the application, is refused as invalid without +// reaching the application. Consensus checks this before prevoting +// (ValidateBlockChainLock); the other callers that verify rely on +// ProcessProposal. +func TestProcessProposalValidatesChainLockHeightBeforeApp(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + state, stateDB, _ := makeState(t, 1, 1) + app := abcimocks.NewApplication(t) + app.On("ProcessProposal", mock.Anything, mock.Anything). + Return(nil, errors.New("application cannot process this block")).Maybe() + logger := log.NewNopLogger() + proxyApp := proxy.New(abciclient.NewLocalClient(logger, app), logger, proxy.NopMetrics()) + require.NoError(t, proxyApp.Start(ctx)) + eventBus := eventbus.NewDefault(logger) + require.NoError(t, eventBus.Start(ctx)) + blockExec := sm.NewBlockExecutor(sm.NewStore(stateDB), proxyApp, new(mpmocks.Mempool), + sm.EmptyEvidencePool{}, store.NewBlockStore(dbm.NewMemDB()), eventBus) + + block, err := sf.MakeBlock(state, state.InitialHeight, new(types.Commit), 1) + require.NoError(t, err) + // no chain lock in the block, yet the header moves the locked height + require.Nil(t, block.CoreChainLock) + block.CoreChainLockedHeight = state.LastCoreChainLockedBlockHeight + 1000 + + _, err = blockExec.ProcessProposal(ctx, block, 0, state, true, types.VerifiedCommit{}) + require.ErrorAs(t, err, &sm.ErrInvalidBlock{}) + app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) +} From 9420e311b25e34a157ea2fd0ab56b09ab84db167 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:36:16 +0000 Subject: [PATCH 10/15] fix(state): check the core chain lock before ProcessProposal validateCoreChainLock needs only the block and the committed state, yet it ran after the application call, in ValidateBlockWithRoundState. Run it in validateBlock instead, so ProcessProposal with verify and the block sync applier refuse such a block before the application sees it. The header hash does not cover the block's chain lock, so a ValidateBlock cache hit re-checks it as well. Co-Authored-By: Claude Opus 5.5 --- internal/state/execution.go | 14 ++++++++------ internal/state/validation.go | 3 +++ spec/consensus/consensus-alternative-flows.md | 6 +++--- spec/consensus/consensus-happy-path.md | 7 ++++--- spec/consensus/consensus-transitions.md | 6 +++--- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/state/execution.go b/internal/state/execution.go index 104319311..3d25b20fb 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -458,9 +458,11 @@ func (blockExec *BlockExecutor) ValidateBlock( lastCommit types.VerifiedCommit, ) error { hash := block.Hash() - // The cache is keyed on the block hash alone, so a hit counts only for a - // block that still continues state. - if _, ok := blockExec.cache[hash.String()]; ok && validateBlockPosition(state, block) == nil { + // The cache is keyed on the header hash alone, which covers neither state nor + // the block's chain lock, so a hit counts only for a block that still + // continues state with a chain lock that fits it. + if _, ok := blockExec.cache[hash.String()]; ok && validateBlockPosition(state, block) == nil && + validateCoreChainLock(block, state) == nil { return nil } @@ -521,8 +523,8 @@ func (blockExec *BlockExecutor) ValidateBlockWithRoundState( } // ValidateBlock above normally verifies block.LastCommit, but a hit on its - // cache, keyed on the block hash, re-checks only the block's position against - // state (validateBlockPosition), not its LastCommit. That is why this second + // cache, keyed on the block hash, re-checks only the block's position and + // chain lock against state, not its LastCommit. That is why this second // verification exists. lastCommit's proof is safe to skip on instead: it names // the verified data itself, not a block hash. if block.Height > state.InitialHeight { @@ -542,7 +544,7 @@ func (blockExec *BlockExecutor) ValidateBlockWithRoundState( uncommittedState.NextConsensusParams.HashConsensusParams(), block.NextConsensusHash, ) } - return validateCoreChainLock(block, state) + return nil } // FinalizeBlock validates the block against the state, fires the relevant events, diff --git a/internal/state/validation.go b/internal/state/validation.go index b16f81787..4bfad3917 100644 --- a/internal/state/validation.go +++ b/internal/state/validation.go @@ -32,6 +32,9 @@ func validateBlock(state State, block *types.Block, lastCommit types.VerifiedCom if err := validateBlockPosition(state, block); err != nil { return err } + if err := validateCoreChainLock(block, state); err != nil { + return err + } // Validate app info hashCP := state.ConsensusParams.HashConsensusParams() diff --git a/spec/consensus/consensus-alternative-flows.md b/spec/consensus/consensus-alternative-flows.md index 84d3bb9b0..075c9267e 100644 --- a/spec/consensus/consensus-alternative-flows.md +++ b/spec/consensus/consensus-alternative-flows.md @@ -108,8 +108,8 @@ silently once it is exhausted (`internal/consensus/block_part_budget.go`). committed state (`ValidateBlock`); a block that fails is never sent to the application. That failure (`sm.ErrInvalidBlock`), the application returning `REJECT` (`sm.ErrBlockRejected`), or a well-formed `ACCEPT` whose contents -disagree with the block — header mismatch / invalid `LastCommit` / invalid -chain lock (`sm.ErrInvalidBlock`) — → nil prevote (`prevoter.handleError`). A failed `ProcessProposal` clears +disagree with the block — header mismatch / invalid `LastCommit` +(`sm.ErrInvalidBlock`) — → nil prevote (`prevoter.handleError`). A failed `ProcessProposal` clears `CurrentRoundState`, so no round counts as processed until a later call succeeds (`blockExecutor.ensureProcess`). Any **other** error **panics** the node: transport failure, `UNKNOWN` status, or a malformed response — an @@ -348,7 +348,7 @@ counts from `StartTime`. Other rounds `≥ 1` never wait. On - Header fields: `CoreChainLockedHeight`; optional `CoreChainLock` in the block (and `CoreChainLockUpdate` in the proposal, not signed). - `ValidateBlockChainLock` (prevote) and `validateCoreChainLock` - (`ValidateBlockWithRoundState`): with a lock, the header height equals the + (`validateBlock`, before the application call): with a lock, the header height equals the lock's and exceeds `state.LastCoreChainLockedBlockHeight`; without one, it equals it. - `Proposaler.Set` rejects `proposal.CoreChainLockedHeight` below the diff --git a/spec/consensus/consensus-happy-path.md b/spec/consensus/consensus-happy-path.md index 990932a4c..0a1c9ae15 100644 --- a/spec/consensus/consensus-happy-path.md +++ b/spec/consensus/consensus-happy-path.md @@ -209,13 +209,14 @@ POL round's prevotes already have a majority) → `EnterPrevote(h, Round)`. (`sm.ValidateBlockChainLock`); for `POLRound == -1` the block's `ProposerProTxHash` is this round's proposer. 2. `blockExecutor.ensureProcess(rs, Round)` → `ValidateBlock` against the - committed state, then ABCI `ProcessProposal` (`verify = true`); a block + committed state, core chain lock included, then ABCI `ProcessProposal` + (`verify = true`); a block failing `ValidateBlock` never reaches the application. The response must be `ACCEPT`, well-formed, and `ValidateBlockWithRoundState` must pass — header fields `AppHash`, `ResultsHash`, `NextValidatorsHash`, `NextConsensusHash` must equal the - application's answer, `LastCommit` must verify against `LastValidators`, - and the core chain lock must validate. The result becomes + application's answer, and `LastCommit` must verify against + `LastValidators`. The result becomes `CurrentRoundState` tagged with round 0. 3. `blockExecutor.mustValidate` (panics on failure, cannot fail after step 2). 4. `prevoter.signVote`: with no lock and `POLRound == -1`, diff --git a/spec/consensus/consensus-transitions.md b/spec/consensus/consensus-transitions.md index be464b21f..2b04ec008 100644 --- a/spec/consensus/consensus-transitions.md +++ b/spec/consensus/consensus-transitions.md @@ -401,11 +401,11 @@ Rules, in evaluation order: `ProcessProposal` for round `r` unless `CurrentRoundState` already is the result for this block and round. The block is first validated against the committed state (`BlockExecutor.ValidateBlock`: version, chain ID, height, - `LastBlockID`, `LastCommit`, proposer, evidence, …); a block that fails is + `LastBlockID`, core chain lock, `LastCommit`, proposer, evidence, …); a block that fails is never sent to the application and yields `ErrInvalidBlock`. `ErrBlockRejected` (application `REJECT`) or `ErrInvalidBlock` (also a well-formed `ACCEPT` whose contents disagree with the block: header fields - differ from the application's answer, `LastCommit` invalid, chain lock invalid — + differ from the application's answer, `LastCommit` invalid — `ValidateBlockWithRoundState`) → nil prevote; any other error → **panic** (`prevoter.handleError`). A malformed application response is in the panic class, not the nil-prevote class: `UNKNOWN` status, an @@ -499,7 +499,7 @@ that checks each is given. | Parts' Merkle proofs valid; total size `≤ Block.MaxBytes` | `PartSet.AddPart`, `addProposalBlockPart` | | `Proposal.Timestamp == Header.Time` | `isValidForPrevote`, `EnterPrecommitAction.Execute` | | Timely (PBTS window; in replay, not too early), unless `ValidBlock` | `StateData.proposalIsTimely` | -| Core chain lock consistent with state | `sm.ValidateBlockChainLock` | +| Core chain lock consistent with state | `sm.ValidateBlockChainLock`, `validateBlock` | | `Header.ProposerProTxHash == proposer(h, r)` when `POLRound == -1` | `isValidForPrevote` | | `Block.ValidateBasic`; version, chain id, height, `LastBlockID`, `ConsensusHash`, `ValidatorsHash`, `LastCommit` threshold signature, proposer is a validator, `Time > LastBlockTime` (`≥ genesis` at initial height), evidence size | `validateBlock` in `internal/state/validation.go` | | ABCI `ProcessProposal` = `ACCEPT`; `AppHash`, `ResultsHash`, `NextValidatorsHash`, `NextConsensusHash` equal the application's; app hash size | `BlockExecutor.ProcessProposal`, `ValidateBlockWithRoundState` | From f3040936f4444d797dc866e4ccded7c7e362dc9b Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:36:16 +0000 Subject: [PATCH 11/15] docs(consensus): note what dating replayed proposals by the WAL affects The WAL time bounds the receipt from above, so replay refuses only proposals the node refused when it received them. It also dates a block made valid during replay, which decides whether the node re-proposes it after a restart, and feeds the proposal timestamp metric. Co-Authored-By: Claude Opus 5.5 --- internal/consensus/proposal_state_validation_test.go | 4 ++-- spec/consensus/consensus-alternative-flows.md | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/consensus/proposal_state_validation_test.go b/internal/consensus/proposal_state_validation_test.go index af21474e8..5a09e83d6 100644 --- a/internal/consensus/proposal_state_validation_test.go +++ b/internal/consensus/proposal_state_validation_test.go @@ -149,8 +149,8 @@ func TestReplayOfFirstBlockWithoutGenesisTimePrevotesNil(t *testing.T) { // TestReplayJudgesProposalTimelinessByWALTime checks, above the first height, // that replay judges a proposal by the time of its WAL entry: one written too -// early for the proposal's timestamp gets a nil prevote without an application -// call, as the node gave it when it received it, and a timely one reaches the +// early for the proposal's timestamp, and so received too early, gets a nil +// prevote without an application call, and a timely one reaches the // application. func TestReplayJudgesProposalTimelinessByWALTime(t *testing.T) { for _, tc := range []struct { diff --git a/spec/consensus/consensus-alternative-flows.md b/spec/consensus/consensus-alternative-flows.md index 075c9267e..7c89e902d 100644 --- a/spec/consensus/consensus-alternative-flows.md +++ b/spec/consensus/consensus-alternative-flows.md @@ -294,7 +294,12 @@ and `addVoteErrorMw` (`internal/consensus/state_add_vote.go`). `checkTimely` (rule P-Timely). It is skipped for a block that is the node's `ValidBlock`, so re-proposals of an already polka'd block are not re-judged. In replay mode the receive time is the time of the proposal's - WAL entry, and only a proposal received too early fails. + WAL entry, and only a proposal received too early fails. The entry is never + earlier than the receipt, so replay refuses only what the node refused when + it received the proposal, not always all of it. That time also becomes + `ValidBlockRecvTime` for a block made valid during replay, which decides + whether the node re-proposes it after a restart + (`Proposaler.checkValidBlock`), and feeds `ProposalTimestampDifference`. - Timeliness governs re-proposal (`Proposaler.checkValidBlock`). - Metric `ProposalTimestampDifference{is_timely}` is recorded for proposals with `POLRound = -1` (`Proposaler.proposalTimestampDifferenceMetric`). From ed5eba691e3f50e1b3ec86a87aa420f7176cdfbd Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:03:22 +0000 Subject: [PATCH 12/15] test(state): refuse a cached block whose LastCommit or chain lock changed The header hash covers the LastCommit only by its threshold signature and does not cover the chain lock. A block validated once and then presented with a different LastCommit quorum hash, round or vote extensions, or a different chain lock, must be refused as invalid without an application call. Co-Authored-By: Claude Opus 5.5 --- internal/state/execution_test.go | 40 ++++++++++++++++++++++++++++++++ internal/state/helpers_test.go | 4 ++++ 2 files changed, 44 insertions(+) diff --git a/internal/state/execution_test.go b/internal/state/execution_test.go index e5f43b031..72f964c59 100644 --- a/internal/state/execution_test.go +++ b/internal/state/execution_test.go @@ -1501,6 +1501,7 @@ const badCommitSignature = "invalid commit signatures for quorum" // commit verification flow needs. type verifiedCommitFixture struct { ctx context.Context + app *testApp blockExec *sm.BlockExecutor // skipped counts the LastCommit verifications the executor skipped skipped *metricspy.Counter @@ -1554,6 +1555,7 @@ func newVerifiedCommitFixture(t *testing.T) verifiedCommitFixture { f := verifiedCommitFixture{ ctx: ctx, + app: app, blockExec: blockExec, skipped: skipped, state: state, @@ -1891,3 +1893,41 @@ func TestProcessProposalValidatesChainLockHeightBeforeApp(t *testing.T) { require.ErrorAs(t, err, &sm.ErrInvalidBlock{}) app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything) } + +// TestProcessProposalRevalidatesUncoveredPartsOfCachedBlock checks that a block +// whose header was validated before, but whose LastCommit or chain lock +// differs where the header hash does not cover it, is refused as invalid +// without reaching the application. +func TestProcessProposalRevalidatesUncoveredPartsOfCachedBlock(t *testing.T) { + for _, tc := range []struct { + name string + mutate func(*types.Block) + }{ + {"last commit quorum hash", func(b *types.Block) { b.LastCommit.QuorumHash = crypto.RandQuorumHash() }}, + {"last commit round", func(b *types.Block) { b.LastCommit.Round++ }}, + {"last commit vote extensions", func(b *types.Block) { + b.LastCommit.ThresholdVoteExtensions = append(b.LastCommit.ThresholdVoteExtensions, + &tmtypes.VoteExtension{Type: tmtypes.VoteExtensionType_THRESHOLD_RECOVER, Signature: []byte{1}}) + }}, + {"chain lock", func(b *types.Block) { + b.CoreChainLock = &types.CoreChainLock{CoreBlockHeight: b.CoreChainLockedHeight} + }}, + } { + t.Run(tc.name, func(t *testing.T) { + f := newVerifiedCommitFixture(t) + require.NoError(t, f.blockExec.ValidateBlock(f.ctx, f.state, f.block, types.VerifiedCommit{})) + + pb, err := f.block.ToProto() + require.NoError(t, err) + altered, err := types.BlockFromProto(pb) + require.NoError(t, err) + tc.mutate(altered) + require.Equal(t, f.block.Hash(), altered.Hash(), "the header must not change") + + calls := f.app.ProcessProposalCalls.Load() + _, err = f.blockExec.ProcessProposal(f.ctx, altered, 0, f.state, true, types.VerifiedCommit{}) + require.ErrorAs(t, err, &sm.ErrInvalidBlock{}) + assert.Equal(t, calls, f.app.ProcessProposalCalls.Load(), "the application was asked") + }) + } +} diff --git a/internal/state/helpers_test.go b/internal/state/helpers_test.go index e883011b3..fb85e70d9 100644 --- a/internal/state/helpers_test.go +++ b/internal/state/helpers_test.go @@ -3,6 +3,7 @@ package state_test import ( "context" "fmt" + "sync/atomic" "testing" "github.com/dashpay/dashd-go/btcjson" @@ -242,6 +243,8 @@ type testApp struct { ValidatorSetUpdate *abci.ValidatorSetUpdate // ProposeNextBlockImmediately is returned from FinalizeBlock. ProposeNextBlockImmediately bool + // ProcessProposalCalls counts ProcessProposal requests. + ProcessProposalCalls atomic.Int32 } var _ abci.Application = (*testApp)(nil) @@ -283,6 +286,7 @@ func (app *testApp) PrepareProposal(_ context.Context, req *abci.RequestPrepareP } func (app *testApp) ProcessProposal(_ context.Context, req *abci.RequestProcessProposal) (*abci.ResponseProcessProposal, error) { + app.ProcessProposalCalls.Add(1) resTxs := factory.ExecTxResults(types.NewTxs(req.Txs)) if resTxs == nil { return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil From 79d32ef62ea98d737cbba0bcdd56b9904960a365 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:03:22 +0000 Subject: [PATCH 13/15] fix(state): key the ValidateBlock cache on the content it checked The cache was keyed on the block hash, which covers the LastCommit only by its threshold signature and leaves out the chain lock, so a block that differed there from a validated one passed ValidateBlock unchecked and reached ProcessProposal. Key it on the block hash plus a checksum of the LastCommit and the chain lock. The chain lock no longer needs re-checking on a hit, and an unchanged block still hits, so the honest path performs no extra commit verification. Co-Authored-By: Claude Opus 5.5 --- internal/state/execution.go | 38 ++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/internal/state/execution.go b/internal/state/execution.go index 3d25b20fb..d3833e541 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -17,6 +17,7 @@ import ( "github.com/dashpay/tenderdash/internal/mempool" "github.com/dashpay/tenderdash/libs/log" tmstate "github.com/dashpay/tenderdash/proto/tendermint/state" + tmproto "github.com/dashpay/tenderdash/proto/tendermint/types" "github.com/dashpay/tenderdash/types" ) @@ -457,16 +458,17 @@ func (blockExec *BlockExecutor) ValidateBlock( block *types.Block, lastCommit types.VerifiedCommit, ) error { - hash := block.Hash() - // The cache is keyed on the header hash alone, which covers neither state nor - // the block's chain lock, so a hit counts only for a block that still - // continues state with a chain lock that fits it. - if _, ok := blockExec.cache[hash.String()]; ok && validateBlockPosition(state, block) == nil && - validateCoreChainLock(block, state) == nil { + key, err := validationCacheKey(block) + if err != nil { + return err + } + // The key does not cover state, so a hit counts only for a block that still + // continues state. + if _, ok := blockExec.cache[key]; ok && validateBlockPosition(state, block) == nil { return nil } - err := validateBlock(state, block, lastCommit, blockExec.metrics) + err = validateBlock(state, block, lastCommit, blockExec.metrics) if err != nil { return err } @@ -476,10 +478,24 @@ func (blockExec *BlockExecutor) ValidateBlock( return err } - blockExec.cache[hash.String()] = struct{}{} + blockExec.cache[key] = struct{}{} return nil } +// validationCacheKey identifies the block content ValidateBlock checks: the +// header, by the block hash, and in full the last commit and the chain lock, +// which the header covers only by the commit's threshold signature. +func validationCacheKey(block *types.Block) (string, error) { + uncovered, err := (&tmproto.Block{ + LastCommit: block.LastCommit.ToProto(), + CoreChainLock: block.CoreChainLock.ToProto(), + }).Marshal() + if err != nil { + return "", fmt.Errorf("encode block for validation cache: %w", err) + } + return block.Hash().String() + hex.EncodeToString(crypto.Checksum(uncovered)), nil +} + // VerifyCommit verifies commit against state.Validators as the commit for the // block blockID at height. On success it returns commit with the proof of that // verification, which the caller offers back as lastCommit when the same commit @@ -523,9 +539,9 @@ func (blockExec *BlockExecutor) ValidateBlockWithRoundState( } // ValidateBlock above normally verifies block.LastCommit, but a hit on its - // cache, keyed on the block hash, re-checks only the block's position and - // chain lock against state, not its LastCommit. That is why this second - // verification exists. lastCommit's proof is safe to skip on instead: it names + // cache re-checks only the block's position against state, not whether + // LastCommit verifies against it. That is why this second verification + // exists. lastCommit's proof is safe to skip on instead: it names // the verified data itself, not a block hash. if block.Height > state.InitialHeight { if err := verifyLastCommit(state, block, lastCommit, blockExec.metrics); err != nil { From e8e4208ef9ce6cec44999a6c50b6dadb71cd6261 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:11:31 +0000 Subject: [PATCH 14/15] test(state): repro cache hit on a block whose unhashed header chain locked height changed The header hash omits CoreChainLockedHeight, so the validation cache key does not cover it. Adds a RED case for that, and a chain lock case that is RED against the pre-fix cache so it pins the chain lock part of the key. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYgi2uMsQDcBcKAwLdt --- internal/state/execution_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/state/execution_test.go b/internal/state/execution_test.go index 72f964c59..e6e63885e 100644 --- a/internal/state/execution_test.go +++ b/internal/state/execution_test.go @@ -1912,6 +1912,11 @@ func TestProcessProposalRevalidatesUncoveredPartsOfCachedBlock(t *testing.T) { {"chain lock", func(b *types.Block) { b.CoreChainLock = &types.CoreChainLock{CoreBlockHeight: b.CoreChainLockedHeight} }}, + {"header chain locked height", func(b *types.Block) { b.CoreChainLockedHeight++ }}, + {"malformed chain lock agreeing with the header", func(b *types.Block) { + b.CoreChainLockedHeight++ + b.CoreChainLock = &types.CoreChainLock{CoreBlockHeight: b.CoreChainLockedHeight} + }}, } { t.Run(tc.name, func(t *testing.T) { f := newVerifiedCommitFixture(t) From 0302edb6d5c0f0f4c72fd94aa8dd339d853654ca Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:14:56 +0000 Subject: [PATCH 15/15] fix(state): cover the full header in the ValidateBlock cache key Header.Hash() leaves out CoreChainLockedHeight, so a block differing from a validated one only there kept the same key and passed ValidateBlock unchecked. Checksum the full header proto along with the LastCommit and the chain lock. Co-Authored-By: Claude Opus 5.5 --- internal/state/execution.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/state/execution.go b/internal/state/execution.go index d3833e541..a248b0b6d 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -483,17 +483,19 @@ func (blockExec *BlockExecutor) ValidateBlock( } // validationCacheKey identifies the block content ValidateBlock checks: the -// header, by the block hash, and in full the last commit and the chain lock, -// which the header covers only by the commit's threshold signature. +// block hash, which covers the data and evidence, and in full the header, the +// last commit and the chain lock, which the hash covers only in part. func validationCacheKey(block *types.Block) (string, error) { + hash := block.Hash() // fills the header's data, evidence and commit hashes uncovered, err := (&tmproto.Block{ + Header: *block.Header.ToProto(), LastCommit: block.LastCommit.ToProto(), CoreChainLock: block.CoreChainLock.ToProto(), }).Marshal() if err != nil { return "", fmt.Errorf("encode block for validation cache: %w", err) } - return block.Hash().String() + hex.EncodeToString(crypto.Checksum(uncovered)), nil + return hash.String() + hex.EncodeToString(crypto.Checksum(uncovered)), nil } // VerifyCommit verifies commit against state.Validators as the commit for the