Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions consensus/byzantine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ import (
"github.com/cometbft/cometbft/types"
)

const (
minTestRoundTimeout = 500 * time.Millisecond
minTestRoundDelta = 100 * time.Millisecond
minTestCommitTimeout = 2 * time.Second
minTestDelayedTimeout = 500 * time.Millisecond
)

func applyTestConsensusTimeouts(timeoutInfo *cmtstate.TimeoutInfo) {
timeoutInfo.TimeoutPropose = maxDuration(timeoutInfo.TimeoutPropose, minTestRoundTimeout)
timeoutInfo.TimeoutPrevote = maxDuration(timeoutInfo.TimeoutPrevote, minTestRoundTimeout)
timeoutInfo.TimeoutPrecommit = maxDuration(timeoutInfo.TimeoutPrecommit, minTestRoundTimeout)

timeoutInfo.TimeoutProposeDelta = maxDuration(timeoutInfo.TimeoutProposeDelta, minTestRoundDelta)
timeoutInfo.TimeoutPrevoteDelta = maxDuration(timeoutInfo.TimeoutPrevoteDelta, minTestRoundDelta)
timeoutInfo.TimeoutPrecommitDelta = maxDuration(timeoutInfo.TimeoutPrecommitDelta, minTestRoundDelta)

timeoutInfo.TimeoutCommit = maxDuration(timeoutInfo.TimeoutCommit, minTestCommitTimeout)
timeoutInfo.DelayedPrecommitTimeout = maxDuration(timeoutInfo.DelayedPrecommitTimeout, minTestDelayedTimeout)
}

func maxDuration(current, min time.Duration) time.Duration {
if current < min {
return min
}

return current
}

//----------------------------------------------
// byzantine failures

Expand Down Expand Up @@ -80,6 +108,8 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
DelayedPrecommitTimeout: resp.TimeoutInfo.DelayedPrecommitTimeout,
}

applyTestConsensusTimeouts(&state.Timeouts)

// Save the updated state back to the store
err = stateStore.Save(state)
require.NoError(t, err)
Expand Down
16 changes: 0 additions & 16 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,22 +303,6 @@ func validatePrevote(t *testing.T, cs *State, round int32, privVal *validatorStu
}
}

func validateLastPrecommit(t *testing.T, cs *State, privVal *validatorStub, blockHash []byte) {
cs.rsMtx.RLock()
votes := cs.rs.LastCommit
cs.rsMtx.RUnlock()
pv, err := privVal.GetPubKey()
require.NoError(t, err)
address := pv.Address()
var vote *types.Vote
if vote = votes.GetByAddress(address); vote == nil {
panic("Failed to find precommit from validator")
}
if !bytes.Equal(vote.BlockID.Hash, blockHash) {
panic(fmt.Sprintf("Expected precommit to be for %X, got %X", blockHash, vote.BlockID.Hash))
}
}

func validatePrecommit(
t *testing.T,
cs *State,
Expand Down
10 changes: 7 additions & 3 deletions consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestStateOversizedBlock(t *testing.T) {

// propose, prevote, and precommit a block
func TestStateFullRound1(t *testing.T) {
cs, vss := randState(1)
cs, _ := randState(1)
height, round := cs.rs.Height, cs.rs.Round

// NOTE: buffer capacity of 0 ensures we can validate prevote and last commit
Expand All @@ -383,11 +383,15 @@ func TestStateFullRound1(t *testing.T) {
propBlockHash := waitForProposal(t, propCh, height, round)
ensurePrevoteMatch(t, voteCh, height, round, propBlockHash)

ensurePrecommit(voteCh, height, round) // wait for precommit
ensurePrecommitMatch(t, voteCh, height, round, propBlockHash)

// we're going to roll right into new height
ensureNewRound(newRoundCh, height+1, 0)
validateLastPrecommit(t, cs, vss[0], propBlockHash)
require.NoError(t, cs.timeoutTicker.Stop())
commit := cs.blockStore.LoadBlockCommit(height)
require.NotNil(t, commit, "expected commit for height %d", height)
assert.True(t, bytes.Equal(commit.BlockID.Hash, propBlockHash),
"expected commit hash %X, got %X", propBlockHash, commit.BlockID.Hash)
}

// nil is proposed, so prevote and precommit nil
Expand Down
Loading