GasMeter & context updates

This commit is contained in:
Christopher Goes 2018-05-07 19:48:12 +02:00
parent 46f9445f06
commit 26991803ee
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
15 changed files with 69 additions and 39 deletions

View File

@ -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
@ -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)
@ -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 {
@ -228,7 +230,7 @@ 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),
}
}
@ -236,7 +238,7 @@ 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),
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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"))
@ -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)

View File

@ -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(),
@ -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
}
@ -129,8 +128,7 @@ const (
contextKeyIsCheckTx
contextKeyTxBytes
contextKeyLogger
contextKeyGasLimit
contextKeyGasConsumed
contextKeyGasMeter
)
// NOTE: Do not expose MultiStore.
@ -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)
@ -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

View File

@ -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,
}
@ -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
}

36
types/gas.go Normal file
View File

@ -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()
}

View File

@ -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
}

View File

@ -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()
@ -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()
@ -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()
@ -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()
@ -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()

View File

@ -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()

View File

@ -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"))

View File

@ -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)
@ -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)
@ -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)

View File

@ -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
}

View File

@ -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