Skip to content

Commit 0f07378

Browse files
authored
Merge pull request #1 from initia-labs/feat/nonce-buffer
add some nonce buffer
2 parents f254d5d + b34df20 commit 0f07378

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

signer/cosigner_nonce_cache.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,29 @@ func (cnc *CosignerNonceCache) getUuids(n int) []uuid.UUID {
199199
}
200200

201201
func (cnc *CosignerNonceCache) target(noncesPerMinute float64) int {
202-
t := int((noncesPerMinute / 60) *
203-
((cnc.getNoncesInterval.Seconds() * nonceOverallocation) + cnc.getNoncesTimeout.Seconds()))
202+
// Nonces needed per block:
203+
// - 1 for proposal
204+
// - 1 for prevote
205+
// - 1 for precommit
206+
// - 1 for vote extension (if enabled)
207+
const (
208+
noncesPerBlock = 4 // proposal + prevote + precommit + extension
209+
blockTime = 0.5 // block time in seconds
210+
)
211+
212+
// Calculate nonces needed per second based on either:
213+
// - Actual usage (noncesPerMinute converted to per second)
214+
// - Theoretical usage (noncesPerBlock / blockTime)
215+
noncesPerSecond := max(noncesPerMinute/60, float64(noncesPerBlock)/blockTime)
216+
217+
// Calculate target number of nonces to maintain in cache:
218+
// noncesPerSecond * (interval * overallocation + timeout)
219+
t := int(noncesPerSecond * ((cnc.getNoncesInterval.Seconds() * nonceOverallocation) +
220+
cnc.getNoncesTimeout.Seconds()))
221+
222+
// Always maintain at least one nonce
204223
if t <= 0 {
205-
return 1 // always target at least one nonce ready
224+
return 1
206225
}
207226
return t
208227
}

0 commit comments

Comments
 (0)