From a6adb082146234b7d6bd1b0773153fdc55a92224 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 24 Feb 2023 20:40:36 +0000 Subject: [PATCH] fix: Make WithBlockTime() consistent with CometBFT canonical time (backport #15124) (#15156) Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Co-authored-by: Julien Robert --- types/context.go | 5 +++-- types/context_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/types/context.go b/types/context.go index 32ae7debbb26..0942c05fc7a5 100644 --- a/types/context.go +++ b/types/context.go @@ -142,11 +142,12 @@ func (c Context) WithHeaderHash(hash []byte) Context { return c } -// WithBlockTime returns a Context with an updated tendermint block header time in UTC time +// WithBlockTime returns a Context with an updated tendermint block header time in UTC with no monotonic component. +// Stripping the monotonic component is for time equality. func (c Context) WithBlockTime(newTime time.Time) Context { newHeader := c.BlockHeader() // https://github.com/gogo/protobuf/issues/519 - newHeader.Time = newTime.UTC() + newHeader.Time = newTime.Round(0).UTC() return c.WithBlockHeader(newHeader) } diff --git a/types/context_test.go b/types/context_test.go index 3e300e5faf42..45fd1ebcc8a2 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -7,6 +7,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmtime "github.com/cometbft/cometbft/types/time" "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" @@ -162,6 +163,14 @@ func (s *contextTestSuite) TestContextHeader() { s.Require().Equal(proposer.Bytes(), ctx.BlockHeader().ProposerAddress) } +func (s *contextTestSuite) TestWithBlockTime() { + now := time.Now() + ctx := types.NewContext(nil, tmproto.Header{}, false, nil) + ctx = ctx.WithBlockTime(now) + tmtime2 := tmtime.Canonical(now) + s.Require().Equal(ctx.BlockTime(), tmtime2) +} + func (s *contextTestSuite) TestContextHeaderClone() { cases := map[string]struct { h tmproto.Header