From 2f73cf4193faab13fead96614405947d7e3e037c Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 12 Nov 2018 23:12:09 -0500 Subject: [PATCH] block gas meter working --- baseapp/baseapp.go | 25 +++++++++++++++++++++---- types/context.go | 7 +++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 827536d215..419f88eb61 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -68,8 +68,10 @@ type BaseApp struct { deliverState *state // for DeliverTx voteInfos []abci.VoteInfo // absent validators from begin block - // minimum fees for spam prevention - minimumFees sdk.Coins + // spam prevention + minimumFees sdk.Coins + maximumBlockGas int64 + deliverGas // flag for sealing sealed bool @@ -194,6 +196,9 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error { // SetMinimumFees sets the minimum fees. func (app *BaseApp) SetMinimumFees(fees sdk.Coins) { app.minimumFees = fees } +// SetMaximumBlockGas sets the maximum gas allowable per block. +func (app *BaseApp) SetMaximumBlockGas(gas int64) { app.maximumBlockGas = gas } + // 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 { @@ -422,12 +427,19 @@ 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) + app.deliverState.ctx = app.deliverState.ctx. + WithBlockHeader(req.Header). + WithBlockHeight(req.Header.Height). + WithBlockGasMeter(blockGasMeter) } if app.beginBlocker != nil { @@ -467,9 +479,10 @@ func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { // Implements ABCI func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { + // Decode the Tx. - var result sdk.Result var tx, err = app.txDecoder(txBytes) + var result sdk.Result if err != nil { result = err.Result() } else { @@ -655,6 +668,10 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk result = app.runMsgs(ctx, msgs, mode) result.GasWanted = gasWanted + // consume block gas + ctx.BlockGasMeter.ConsumeGas( + ctx.GasMeter().GasConsumed(), "block gas meter") + // only update state if all messages pass if result.IsOK() { msCache.Write() diff --git a/types/context.go b/types/context.go index bfb4c58fed..ed65e57d92 100644 --- a/types/context.go +++ b/types/context.go @@ -140,6 +140,7 @@ const ( contextKeyLogger contextKeyVoteInfos contextKeyGasMeter + contextKeyBlockGasMeter contextKeyMinimumFees ) @@ -170,6 +171,8 @@ func (c Context) VoteInfos() []abci.VoteInfo { func (c Context) GasMeter() GasMeter { return c.Value(contextKeyGasMeter).(GasMeter) } +func (c Context) BlockGasMeter() GasMeter { return c.Value(contextKeyBlockGasMeter).(GasMeter) } + func (c Context) IsCheckTx() bool { return c.Value(contextKeyIsCheckTx).(bool) } func (c Context) MinimumFees() Coins { return c.Value(contextKeyMinimumFees).(Coins) } @@ -219,6 +222,10 @@ func (c Context) WithVoteInfos(VoteInfos []abci.VoteInfo) Context { func (c Context) WithGasMeter(meter GasMeter) Context { return c.withValue(contextKeyGasMeter, meter) } +func (c Context) WithBlockGasMeter(meter GasMeter) Context { + return c.withValue(contextKeyBlockGasMeter, meter) +} + func (c Context) WithIsCheckTx(isCheckTx bool) Context { return c.withValue(contextKeyIsCheckTx, isCheckTx) }