Skip to content

Commit

Permalink
fix: Make WithBlockTime() consistent with CometBFT canonical time (ba…
Browse files Browse the repository at this point in the history
…ckport #15124) (#15156)

Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people committed Feb 24, 2023
1 parent b123507 commit a6adb08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
9 changes: 9 additions & 0 deletions types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

1 comment on commit a6adb08

@faddat
Copy link
Contributor

@faddat faddat commented on a6adb08 Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. I see y'all working at crazy hours <3

Please sign in to comment.