Skip to content

Commit

Permalink
GasMeter & context updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed May 15, 2018
1 parent 46f9445 commit 2699180
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 39 deletions.
10 changes: 6 additions & 4 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type BaseApp struct {
// must be set
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
anteHandler sdk.AnteHandler // ante handler for fee and auth
txGasLimit sdk.Gas // per-transaction gas limit

// may be nil
initChainer sdk.InitChainer // initialize state with validators and state blob
Expand Down Expand Up @@ -66,6 +67,7 @@ func NewBaseApp(name string, cdc *wire.Codec, logger log.Logger, db dbm.DB) *Bas
router: NewRouter(),
codespacer: sdk.NewCodespacer(),
txDecoder: defaultTxDecoder(cdc),
txGasLimit: sdk.Gas(10000),
}
// Register the undefined & root codespaces, which should not be used by any modules
app.codespacer.RegisterOrPanic(sdk.CodespaceUndefined)
Expand Down Expand Up @@ -210,9 +212,9 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, nil, app.Logger)
return sdk.NewContext(app.checkState.ms, header, true, nil, app.Logger, app.txGasLimit)
}
return sdk.NewContext(app.deliverState.ms, header, false, nil, app.Logger)
return sdk.NewContext(app.deliverState.ms, header, false, nil, app.Logger, app.txGasLimit)
}

type state struct {
Expand All @@ -228,15 +230,15 @@ func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, nil, app.Logger),
ctx: sdk.NewContext(ms, header, true, nil, app.Logger, app.txGasLimit),
}
}

func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, nil, app.Logger),
ctx: sdk.NewContext(ms, header, false, nil, app.Logger, app.txGasLimit),
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/cool/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCoolKeeper(t *testing.T) {
auth.RegisterBaseAccount(cdc)

am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, nil)
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, nil, 0)
ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, ck, DefaultCodespace)

Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestPowHandler(t *testing.T) {
auth.RegisterBaseAccount(cdc)

am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace)
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestPowKeeperGetSet(t *testing.T) {
auth.RegisterBaseAccount(cdc)

am := auth.NewAccountMapper(cdc, capKey, &auth.BaseAccount{})
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
config := NewConfig("pow", int64(1))
ck := bank.NewKeeper(am)
keeper := NewKeeper(capKey, config, ck, DefaultCodespace)
Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/simplestake/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) {
func TestKeeperGetSet(t *testing.T) {
ms, _, capKey := setupMultiStore()

ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
stakeKeeper := NewKeeper(capKey, bank.NewKeeper(nil), DefaultCodespace)
addr := sdk.Address([]byte("some-address"))

Expand All @@ -60,7 +60,7 @@ func TestBonding(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)

ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)

accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := bank.NewKeeper(accountMapper)
Expand Down
22 changes: 7 additions & 15 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Context struct {
}

// create a new context
func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byte, logger log.Logger) Context {
func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byte, logger log.Logger, gasLimit Gas) Context {
c := Context{
Context: context.Background(),
pst: newThePast(),
Expand All @@ -43,8 +43,7 @@ func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, txBytes []byt
c = c.WithIsCheckTx(isCheckTx)
c = c.WithTxBytes(txBytes)
c = c.WithLogger(logger)
c = c.WithGasLimit(0)
c = c.WithGasConsumed(0)
c = c.WithGasMeter(NewGasMeter(gasLimit))
return c
}

Expand Down Expand Up @@ -129,8 +128,7 @@ const (
contextKeyIsCheckTx
contextKeyTxBytes
contextKeyLogger
contextKeyGasLimit
contextKeyGasConsumed
contextKeyGasMeter
)

// NOTE: Do not expose MultiStore.
Expand Down Expand Up @@ -159,11 +157,8 @@ func (c Context) TxBytes() []byte {
func (c Context) Logger() log.Logger {
return c.Value(contextKeyLogger).(log.Logger)
}
func (c Context) GasLimit() uint64 {
return c.Value(contextKeyGasLimit).(uint64)
}
func (c Context) GasConsumed() uint64 {
return c.Value(contextKeyGasConsumed).(uint64)
func (c Context) GasMeter() GasMeter {
return c.Value(contextKeyGasMeter).(GasMeter)
}
func (c Context) WithMultiStore(ms MultiStore) Context {
return c.withValue(contextKeyMultiStore, ms)
Expand All @@ -187,11 +182,8 @@ func (c Context) WithTxBytes(txBytes []byte) Context {
func (c Context) WithLogger(logger log.Logger) Context {
return c.withValue(contextKeyLogger, logger)
}
func (c Context) WithGasLimit(limit uint64) Context {
return c.withValue(contextKeyGasLimit, limit)
}
func (c Context) WithGasConsumed(consumed uint64) Context {
return c.withValue(contextKeyGasConsumed, consumed)
func (c Context) WithGasMeter(meter GasMeter) Context {
return c.withValue(contextKeyGasMeter, meter)
}

// Cache the multistore and return a new cached context. The cached context is
Expand Down
4 changes: 2 additions & 2 deletions types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (l MockLogger) With(kvs ...interface{}) log.Logger {

func TestContextGetOpShouldNeverPanic(t *testing.T) {
var ms types.MultiStore
ctx := types.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := types.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
indices := []int64{
-10, 1, 0, 10, 20,
}
Expand All @@ -58,7 +58,7 @@ func defaultContext(key types.StoreKey) types.Context {
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, types.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := types.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := types.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
return ctx
}

Expand Down
36 changes: 36 additions & 0 deletions types/gas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package types

import ()

type Gas uint64

type GasMeter interface {
GasExceeded() bool
ConsumeGas(amount Gas)
ConsumeGasOrFail(amount Gas) bool
}

type basicGasMeter struct {
limit Gas
consumed Gas
}

func NewGasMeter(limit Gas) GasMeter {
return &basicGasMeter{
limit: limit,
consumed: 0,
}
}

func (g *basicGasMeter) GasExceeded() bool {
return g.consumed > g.limit
}

func (g *basicGasMeter) ConsumeGas(amount Gas) {
g.consumed += amount
}

func (g *basicGasMeter) ConsumeGasOrFail(amount Gas) bool {
g.ConsumeGas(amount)
return g.GasExceeded()
}
2 changes: 1 addition & 1 deletion types/lib/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func defaultComponents(key sdk.StoreKey) (sdk.Context, *wire.Codec) {
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
cdc := wire.NewCodec()
return ctx, cdc
}
Expand Down
10 changes: 5 additions & 5 deletions x/auth/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestAnteHandlerSigErrors(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), 0)

// keys and addresses
priv1, addr1 := privAndAddr()
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestAnteHandlerSequences(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), 0)

// keys and addresses
priv1, addr1 := privAndAddr()
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestAnteHandlerFees(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), 0)

// keys and addresses
priv1, addr1 := privAndAddr()
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), 0)

// keys and addresses
priv1, addr1 := privAndAddr()
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
RegisterBaseAccount(cdc)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})
anteHandler := NewAnteHandler(mapper, BurnFeeHandler)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), 0)

// keys and addresses
priv1, addr1 := privAndAddr()
Expand Down
2 changes: 1 addition & 1 deletion x/auth/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestContextWithSigners(t *testing.T) {
ms, _ := setupMultiStore()
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, false, nil, log.NewNopLogger(), 0)

_, _, addr1 := keyPubAddr()
_, _, addr2 := keyPubAddr()
Expand Down
2 changes: 1 addition & 1 deletion x/auth/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccountMapperGetSet(t *testing.T) {
RegisterBaseAccount(cdc)

// make context and mapper
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
mapper := NewAccountMapper(cdc, capKey, &BaseAccount{})

addr := sdk.Address([]byte("some-address"))
Expand Down
6 changes: 3 additions & 3 deletions x/bank/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestKeeper(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)

ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper)

Expand Down Expand Up @@ -117,7 +117,7 @@ func TestSendKeeper(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)

ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper)
sendKeeper := NewSendKeeper(accountMapper)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestViewKeeper(t *testing.T) {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)

ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
coinKeeper := NewKeeper(accountMapper)
viewKeeper := NewViewKeeper(accountMapper)
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func defaultContext(key sdk.StoreKey) sdk.Context {
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger())
ctx := sdk.NewContext(cms, abci.Header{}, false, nil, log.NewNopLogger(), 0)
return ctx
}

Expand Down
2 changes: 1 addition & 1 deletion x/stake/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context
err := ms.LoadLatestVersion()
require.Nil(t, err)

ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil, log.NewNopLogger())
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil, log.NewNopLogger(), 0)
cdc := makeTestCodec()
accountMapper := auth.NewAccountMapper(
cdc, // amino codec
Expand Down

0 comments on commit 2699180

Please sign in to comment.