From 4fa5a9733f07f8acfca959b78c19ad9e6e5d350f Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Fri, 22 May 2026 15:18:56 +0300 Subject: [PATCH] core: fixed linear timer --- core/consensus/timer/roundtimer.go | 2 +- core/consensus/timer/roundtimer_test.go | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/consensus/timer/roundtimer.go b/core/consensus/timer/roundtimer.go index b864819973..ed9b4c7208 100644 --- a/core/consensus/timer/roundtimer.go +++ b/core/consensus/timer/roundtimer.go @@ -298,7 +298,7 @@ func (t *linearRoundTimer) Timer(round int64) (<-chan time.Time, func()) { timeout = time.Second } else { // Subsequent rounds have linearly more time starting at 400 milliseconds - timeout = time.Duration(200*(round-1) + 200) + timeout = time.Duration(200*(round-1)+200) * time.Millisecond } timer := t.clock.NewTimer(timeout) diff --git a/core/consensus/timer/roundtimer_test.go b/core/consensus/timer/roundtimer_test.go index 5e1d5ff5cd..f9f7429e3d 100644 --- a/core/consensus/timer/roundtimer_test.go +++ b/core/consensus/timer/roundtimer_test.go @@ -147,10 +147,18 @@ func TestLinearRoundTimer(t *testing.T) { // Start the timerType timerC, stop := timer.Timer(tt.round) - // Advance the fake clock - fakeClock.Advance(tt.want) + // Advance to just before the expected timeout; timer must not fire yet. + fakeClock.Advance(tt.want - time.Millisecond) + + select { + case <-timerC: + require.Fail(t, "Fail", "Timer(round %d) fired too early, want %v", tt.round, tt.want) + default: + } + + // Advance the remaining time; timer must now fire. + fakeClock.Advance(time.Millisecond) - // Check if the timerType fires select { case <-timerC: default: