Skip to content

Commit

Permalink
fix: Make WithBlockTime() consistent with CometBFT canonical time (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 committed Feb 22, 2023
1 parent 832517b commit e55b1a9
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 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)
}

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"
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"

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

0 comments on commit e55b1a9

Please sign in to comment.