diff --git a/types/context.go b/types/context.go index 32ae7debbb26..2237e3644bda 100644 --- a/types/context.go +++ b/types/context.go @@ -142,11 +142,16 @@ func (c Context) WithHeaderHash(hash []byte) Context { return c } +<<<<<<< HEAD // WithBlockTime returns a Context with an updated tendermint block header time in UTC time +======= +// WithBlockTime returns a Context with an updated CometBFT block header time in UTC with no monotonic component. +// Stripping the monotonic component is for time equality. +>>>>>>> e55b1a9fb (fix: Make WithBlockTime() consistent with CometBFT canonical time (#15124)) 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..58a8dc2f4049 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -6,7 +6,12 @@ import ( "time" abci "github.com/cometbft/cometbft/abci/types" +<<<<<<< HEAD tmproto "github.com/cometbft/cometbft/proto/tendermint/types" +======= + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + cmttime "github.com/cometbft/cometbft/types/time" +>>>>>>> e55b1a9fb (fix: Make WithBlockTime() consistent with CometBFT canonical time (#15124)) "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" @@ -162,6 +167,14 @@ func (s *contextTestSuite) TestContextHeader() { s.Require().Equal(proposer.Bytes(), ctx.BlockHeader().ProposerAddress) } +func (s *contextTestSuite) TestWithBlockTime() { + now := time.Now() + ctx := types.NewContext(nil, cmtproto.Header{}, false, nil) + ctx = ctx.WithBlockTime(now) + cmttime2 := cmttime.Canonical(now) + s.Require().Equal(ctx.BlockTime(), cmttime2) +} + func (s *contextTestSuite) TestContextHeaderClone() { cases := map[string]struct { h tmproto.Header