Skip to content

Commit

Permalink
Fix Clone by forcing all Header.Time in Context to be UTC
Browse files Browse the repository at this point in the history
 gogo/protobuf#519
Seems like the old code never properly cloned this, which was not
easily detectable through so many layers of indirection
  • Loading branch information
ethanfrey committed Jul 11, 2019
1 parent 346e3a2 commit a0f6e4d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 6 additions & 1 deletion types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (c Context) ConsensusParams() *abci.ConsensusParams {

// create a new context
func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, logger log.Logger) Context {
// https://github.com/gogo/protobuf/issues/519
header.Time = header.Time.UTC()
return Context{
ctx: context.Background(),
ms: ms,
Expand All @@ -87,13 +89,16 @@ func (c Context) WithMultiStore(ms MultiStore) Context {
}

func (c Context) WithBlockHeader(header abci.Header) Context {
// https://github.com/gogo/protobuf/issues/519
header.Time = header.Time.UTC()
c.header = header
return c
}

func (c Context) WithBlockTime(newTime time.Time) Context {
newHeader := c.BlockHeader()
newHeader.Time = newTime
// https://github.com/gogo/protobuf/issues/519
newHeader.Time = newTime.UTC()
return c.WithBlockHeader(newHeader)
}

Expand Down
8 changes: 3 additions & 5 deletions types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestContextHeader(t *testing.T) {
WithProposer(proposer)
require.Equal(t, height, ctx.BlockHeight())
require.Equal(t, height, ctx.BlockHeader().Height)
require.Equal(t, time, ctx.BlockHeader().Time)
require.Equal(t, time.UTC(), ctx.BlockHeader().Time)
require.Equal(t, proposer.Bytes(), ctx.BlockHeader().ProposerAddress)
}

Expand All @@ -170,7 +170,6 @@ func TestContextHeaderClone(t *testing.T) {
Time: time.Unix(12345677, 12345),
},
},
// https://github.com/gogo/protobuf/issues/519
"zero time": {
h: abci.Header{
Time: time.Unix(0, 0),
Expand All @@ -197,15 +196,14 @@ func TestContextHeaderClone(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
ctx := types.NewContext(nil, tc.h, false, nil)
require.Equal(t, tc.h, ctx.BlockHeader())
require.Equal(t, tc.h.Height, ctx.BlockHeight())
require.Equal(t, tc.h.Time, ctx.BlockTime())
require.Equal(t, tc.h.Time.UTC(), ctx.BlockTime())

// update only changes one field
var newHeight int64 = 17
ctx = ctx.WithBlockHeight(newHeight)
require.Equal(t, newHeight, ctx.BlockHeight())
require.Equal(t, tc.h.Time, ctx.BlockTime())
require.Equal(t, tc.h.Time.UTC(), ctx.BlockTime())
})
}
}

0 comments on commit a0f6e4d

Please sign in to comment.