Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
472a6ff
fix(consensus): validate a proposed block against state before Proces…
lklimek Nov 11, 2026
ab1895a
fix(consensus): judge proposal timeliness on WAL replay
lklimek Nov 11, 2026
f33748e
docs(consensus): describe validation before ProcessProposal and repla…
lklimek Nov 11, 2026
b7e4b99
fix(consensus): tell a pre-application validation failure from an ABC…
lklimek Nov 11, 2026
e1382b1
test(consensus): check the prevote replay casts is nil
lklimek Nov 11, 2026
717e991
docs(consensus): describe validation before ProcessProposal in altern…
lklimek Nov 11, 2026
0abceda
Merge remote-tracking branch 'origin/dev' into fix/consensus-validate…
lklimek Nov 11, 2026
77b3bd9
test(consensus): judge replayed proposal timeliness above the first h…
lklimek Nov 11, 2026
0523cf0
fix(consensus): drop the repeated prefix from ProcessProposal failures
lklimek Nov 11, 2026
79d73c8
Merge remote-tracking branch 'origin/dev' into fix/consensus-validate…
lklimek Nov 11, 2026
34de205
test(state): expect the chain lock height check before ProcessProposal
lklimek Nov 11, 2026
9420e31
fix(state): check the core chain lock before ProcessProposal
lklimek Nov 11, 2026
f304093
docs(consensus): note what dating replayed proposals by the WAL affects
lklimek Nov 11, 2026
ed5eba6
test(state): refuse a cached block whose LastCommit or chain lock cha…
lklimek Nov 11, 2026
79d32ef
fix(state): key the ValidateBlock cache on the content it checked
lklimek Nov 11, 2026
e8e4208
test(state): repro cache hit on a block whose unhashed header chain l…
lklimek Nov 11, 2026
0302edb
fix(state): cover the full header in the ValidateBlock cache key
lklimek Nov 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/consensus/block_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
246 changes: 246 additions & 0 deletions internal/consensus/proposal_state_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package consensus

import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"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"
)

// 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
}

// 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,
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)

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)
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 := proposalOfMutatedBlock(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 := proposalOfMutatedBlock(ctx, t, cs, proposer, tc.mutate)
replayProposal(ctx, t, cs, proposal, parts, tmtime.Now())

requireOwnNilPrevote(ctx, t, cs)
app.AssertNotCalled(t, "ProcessProposal", mock.Anything, mock.Anything)
})
}
}

// 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()

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)
})
replayProposal(ctx, t, cs, proposal, parts, tmtime.Now())

requireOwnNilPrevote(ctx, t, cs)
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, 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 {
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) {
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(
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))
}
})
}
2 changes: 2 additions & 0 deletions internal/consensus/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion internal/consensus/state_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
37 changes: 34 additions & 3 deletions internal/consensus/state_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions internal/consensus/state_prevoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consensus
import (
"context"
"errors"
"fmt"
"time"

cstypes "github.com/dashpay/tenderdash/internal/consensus/types"
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions internal/consensus/state_prevoter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consensus

import (
"context"
"errors"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Loading
Loading