diff --git a/types/context.go b/types/context.go index 7c8ec950792a..5c3a072053b9 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 CometBFT 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. 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 746e2bc364b2..10cfba092219 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -7,6 +7,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + cmttime "github.com/cometbft/cometbft/types/time" "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" @@ -163,6 +164,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 cmtproto.Header