Skip to content

Commit

Permalink
remove BlockHeight in context (#4580)
Browse files Browse the repository at this point in the history
closes #4578
  • Loading branch information
colin-axner authored and Alessio Treglia committed Jun 18, 2019
1 parent 55928ad commit 84a2582
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4580-remove-block-he
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4580 Update `Context#BlockHeight` to properly set the block height via `WithBlockHeader`.
6 changes: 2 additions & 4 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, logger log.Lo
}
c = c.WithMultiStore(ms)
c = c.WithBlockHeader(header)
c = c.WithBlockHeight(header.Height)
c = c.WithChainID(header.ChainID)
c = c.WithIsCheckTx(isCheckTx)
c = c.WithTxBytes(nil)
Expand Down Expand Up @@ -136,7 +135,6 @@ type contextKey int // local to the context module
const (
contextKeyMultiStore contextKey = iota
contextKeyBlockHeader
contextKeyBlockHeight
contextKeyChainID
contextKeyIsCheckTx
contextKeyTxBytes
Expand All @@ -154,7 +152,7 @@ func (c Context) MultiStore() MultiStore {

func (c Context) BlockHeader() abci.Header { return c.Value(contextKeyBlockHeader).(abci.Header) }

func (c Context) BlockHeight() int64 { return c.Value(contextKeyBlockHeight).(int64) }
func (c Context) BlockHeight() int64 { return c.BlockHeader().Height }

func (c Context) ChainID() string { return c.Value(contextKeyChainID).(string) }

Expand Down Expand Up @@ -202,7 +200,7 @@ func (c Context) WithProposer(addr ConsAddress) Context {
func (c Context) WithBlockHeight(height int64) Context {
newHeader := c.BlockHeader()
newHeader.Height = height
return c.withValue(contextKeyBlockHeight, height).withValue(contextKeyBlockHeader, newHeader)
return c.WithBlockHeader(newHeader)
}

func (c Context) WithChainID(chainID string) Context { return c.withValue(contextKeyChainID, chainID) }
Expand Down
27 changes: 27 additions & 0 deletions types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

Expand All @@ -10,6 +11,8 @@ import (

abci "github.com/tendermint/tendermint/abci/types"

"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -184,3 +187,27 @@ func TestContextWithCustom(t *testing.T) {
require.Equal(t, meter, ctx.GasMeter())
require.Equal(t, minGasPrices, ctx.MinGasPrices())
}

// Testing saving/loading of header fields to/from the context
func TestContextHeader(t *testing.T) {
var ctx types.Context

require.Panics(t, func() { ctx.BlockHeader() })
require.Panics(t, func() { ctx.BlockHeight() })

height := int64(5)
time := time.Now()
addr := secp256k1.GenPrivKey().PubKey().Address()
proposer := types.ConsAddress(addr)

ctx = types.NewContext(nil, abci.Header{}, false, nil)

ctx = ctx.
WithBlockHeight(height).
WithBlockTime(time).
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, proposer.Bytes(), ctx.BlockHeader().ProposerAddress)
}

0 comments on commit 84a2582

Please sign in to comment.