Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: remove BlockHeight in context #4580

Merged
merged 4 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}