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
…5124)

(cherry picked from commit e55b1a9)

# Conflicts:
#	types/context.go
#	types/context_test.go
  • Loading branch information
likhita-809 authored and mergify[bot] committed Feb 24, 2023
1 parent b123507 commit 95b4430
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

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

0 comments on commit 95b4430

Please sign in to comment.