Skip to content

Commit b34df20

Browse files
committed
clearn up
1 parent 88fc329 commit b34df20

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

signer/cosigner_nonce_cache.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,31 @@ func (cnc *CosignerNonceCache) getUuids(n int) []uuid.UUID {
199199
}
200200

201201
func (cnc *CosignerNonceCache) target(noncesPerMinute float64) int {
202-
// Calculate for nonce expiration window
203-
blocksPerExpirationWindow := int(cnc.nonceExpiration.Seconds() / 0.5) // 0.5 second blocks
204-
205202
// Nonces needed per block:
206203
// - 1 for proposal
207204
// - 1 for prevote
208205
// - 1 for precommit
209206
// - 1 for vote extension (if enabled)
210-
noncesPerBlock := 4 // proposal + prevote + precommit + extension
207+
const (
208+
noncesPerBlock = 4 // proposal + prevote + precommit + extension
209+
blockTime = 0.5 // block time in seconds
210+
)
211211

212-
// Total nonces needed for expiration window with overallocation
213-
activeNoncesPerWindow := int(float64(blocksPerExpirationWindow) *
214-
float64(noncesPerBlock) *
215-
nonceOverallocation)
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)
216216

217-
// Calculate based on current usage with buffer
218-
currentUsageTarget := int((noncesPerMinute / 60) *
219-
((cnc.getNoncesInterval.Seconds() * nonceOverallocation) + cnc.getNoncesTimeout.Seconds()))
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()))
220221

221-
return max(currentUsageTarget, activeNoncesPerWindow)
222+
// Always maintain at least one nonce
223+
if t <= 0 {
224+
return 1
225+
}
226+
return t
222227
}
223228

224229
func (cnc *CosignerNonceCache) reconcile(ctx context.Context) {

0 commit comments

Comments
 (0)