Skip to content
Merged
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
35 changes: 25 additions & 10 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime/debug"
"sort"
"sync"
"sync/atomic"
"time"

"github.com/cometbft/cometbft/privval"
Expand Down Expand Up @@ -145,9 +146,10 @@ type State struct {
nSteps int

// some functions can be overwritten for testing
decideProposal func(height int64, round int32)
doPrevote func(height int64, round int32)
setProposal func(proposal *types.Proposal) error
decideProposal func(height int64, round int32)
doPrevote func(height int64, round int32)
setProposal func(proposal *types.Proposal) error
StartedPrecommitSleep atomic.Bool
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this to the state instead of the round state because we copy the latter in multiple places and it's not worth handling that now.


// closed when we finish shutting down
done chan struct{}
Expand Down Expand Up @@ -1627,9 +1629,22 @@ func (cs *State) enterPrecommit(height int64, round int32) {
return
}

if ready, waitTime := cs.isReadyToPrecommit(); !ready {
logger.Trace("rescheduling precommit", "delay(ms)", waitTime.Milliseconds())
cs.scheduleTimeout(waitTime, height, round, cstypes.RoundStepPrevoteWait)
if cs.StartedPrecommitSleep.CompareAndSwap(false, true) {
waitTime := cs.precommitDelay()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid the timer and unlocking relocking, if the waitTime is 0

We could even add the check prior like:

waitTime := ps.precommitDelay()
if !cs.rs.StartedPrecommitSleep.Load() && waitTime > 0 {
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the check above would complicate the if statement given the else branch that will return

logger.Debug("delaying precommit", "delay", waitTime)
cs.unlockAll()
t := time.NewTimer(waitTime)
select {
case <-cs.Quit():
cs.lockAll()
return
case <-t.C:
}
cs.lockAll()
cs.StartedPrecommitSleep.Store(false)
} else {
logger.Debug("already entered precommit delay")
// if any other routine tries to enter precommit, we just return
return
}

Expand Down Expand Up @@ -2110,20 +2125,20 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
// KMSSigningDelay is a constant representing a delay used primarily to adjust for KMS signing latencies.
const KMSSigningDelay = 200 * time.Millisecond

// isReadyToPrecommit calculates if the process has waited at least a certain number of seconds
// precommitDelay calculates if the process has waited at least a certain number of seconds
// from their start time before they can vote
// If the application's DelayedPrecommitTimeout is set to 0, no precommit wait is done.
func (cs *State) isReadyToPrecommit() (bool, time.Duration) {
func (cs *State) precommitDelay() time.Duration {
if cs.state.Timeouts.DelayedPrecommitTimeout == 0 {
// setting 0 as a special case not to reschedule the pre-commit
return true, 0
return 0
}
precommitVoteTime := cs.rs.StartTime.Add(cs.state.Timeouts.DelayedPrecommitTimeout)
waitTime := time.Until(precommitVoteTime)
if _, ok := cs.privValidator.(*privval.SignerClient); ok {
waitTime = waitTime - KMSSigningDelay
}
return waitTime <= 0, waitTime
return waitTime
}

//-----------------------------------------------------------------------------
Expand Down
Loading