diff --git a/internal/consensus/state.go b/internal/consensus/state.go index 4646426a6..4adc00fa7 100644 --- a/internal/consensus/state.go +++ b/internal/consensus/state.go @@ -2413,14 +2413,14 @@ func repairWalFile(src, dst string) error { return nil } -// proposerWaitUntil determines when the proposer should propose its next block +// proposerWaitTime determines how long the proposer should wait to propose its next block. // Block times must be monotonically increasing, so if the block time of the previous // block is larger than the proposer's current time, then the proposer will sleep // until its local clock exceeds the previous block time. -func proposerWaitUntil(lt tmtime.Source, h types.Header) time.Time { +func proposerWaitTime(lt tmtime.Source, h types.Header) time.Duration { t := lt.Now() if t.After(h.Time) { - return t + return 0 } - return h.Time + return h.Time.Sub(t) } diff --git a/internal/consensus/state_test.go b/internal/consensus/state_test.go index 767f5defc..f0b193422 100644 --- a/internal/consensus/state_test.go +++ b/internal/consensus/state_test.go @@ -2448,25 +2448,25 @@ func TestProposerWaitUntil(t *testing.T) { name string blockTime time.Time localTime time.Time - expectedTime time.Time + expectedWait time.Duration }{ { name: "block time greater than local time", blockTime: genesisTime.Add(5 * time.Nanosecond), localTime: genesisTime.Add(1 * time.Nanosecond), - expectedTime: genesisTime.Add(5 * time.Nanosecond), + expectedWait: 4 * time.Nanosecond, }, { name: "local time greater than block time", blockTime: genesisTime.Add(1 * time.Nanosecond), localTime: genesisTime.Add(5 * time.Nanosecond), - expectedTime: genesisTime.Add(5 * time.Nanosecond), + expectedWait: 0, }, { name: "both times equal", blockTime: genesisTime.Add(5 * time.Nanosecond), localTime: genesisTime.Add(5 * time.Nanosecond), - expectedTime: genesisTime.Add(5 * time.Nanosecond), + expectedWait: 0, }, } for _, testCase := range testCases { @@ -2480,8 +2480,8 @@ func TestProposerWaitUntil(t *testing.T) { mockSource := new(tmtimemocks.Source) mockSource.On("Now").Return(testCase.localTime) - ti := proposerWaitUntil(mockSource, b.Header) - assert.Equal(t, testCase.expectedTime, ti) + ti := proposerWaitTime(mockSource, b.Header) + assert.Equal(t, testCase.expectedWait, ti) }) } }