From 956d351f68705e073e6ba1bd412eb5e6ff8d2f7d Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 13 Nov 2018 11:30:06 -0500 Subject: [PATCH] basic structure in place --- baseapp/baseapp.go | 16 ++++++++++------ types/gas.go | 9 +++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 419f88eb61..9f9d98f7e9 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -427,20 +427,17 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg // Initialize the DeliverTx state. If this is the first block, it should // already be initialized in InitChain. Otherwise app.deliverState will be // nil, since it is reset on Commit. - blockGasMeter := sdk.NewGasMeter(app.maximumBlockGas) if app.deliverState == nil { app.setDeliverState(req.Header) - app.deliverState.ctx = app.deliverState.ctx. - WithBlockGasMeter(blockGasMeter) - } else { // In the first block, app.deliverState.ctx will already be initialized // by InitChain. Context is now updated with Header information. app.deliverState.ctx = app.deliverState.ctx. WithBlockHeader(req.Header). - WithBlockHeight(req.Header.Height). - WithBlockGasMeter(blockGasMeter) + WithBlockHeight(req.Header.Height) } + app.deliverState.ctx = app.deliverState.ctx. + WithBlockGasMeter(sdk.NewGasMeter(app.maximumBlockGas)) if app.beginBlocker != nil { res = app.beginBlocker(app.deliverState.ctx, req) @@ -607,6 +604,13 @@ func (app *BaseApp) initializeContext(ctx sdk.Context, mode runTxMode) sdk.Conte // anteHandler. txBytes may be nil in some cases, eg. in tests. Also, in the // future we may support "internal" transactions. func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk.Result) { + + // only run the tx if there is block gas remaining + if ctx.BlockGasMeter.PastLimit() { + result = sdk.ErrOutOfGas("no block gas left to run tx").Result() + return + } + // NOTE: GasWanted should be returned by the AnteHandler. GasUsed is // determined by the GasMeter. We need access to the context to get the gas // meter so we initialize upfront. diff --git a/types/gas.go b/types/gas.go index 7b03474670..6ec29dd139 100644 --- a/types/gas.go +++ b/types/gas.go @@ -29,6 +29,7 @@ type ErrorOutOfGas struct { type GasMeter interface { GasConsumed() Gas ConsumeGas(amount Gas, descriptor string) + PastLimit() bool } type basicGasMeter struct { @@ -55,6 +56,10 @@ func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) { } } +func (g *basicGasMeter) PastLimit() bool { + return g.consumed > g.limit +} + type infiniteGasMeter struct { consumed Gas } @@ -74,6 +79,10 @@ func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) { g.consumed += amount } +func (g *infiniteGasMeter) PastLimit() bool { + return false +} + // GasConfig defines gas cost for each operation on KVStores type GasConfig struct { HasCost Gas