Skip to content

Commit

Permalink
fix(baseapp): Reset GasMeter before deliverTX v0.47.x backport (#18826)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime authored Dec 20, 2023
1 parent 929e76b commit 9e60114
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (baseapp) [#18609](https://github.com/cosmos/cosmos-sdk/issues/18609) Fixed accounting in the block gas meter after BeginBlock and before DeliverTx, ensuring transaction processing always starts with the expected zeroed out block gas meter.
* (server) [#18537](https://github.com/cosmos/cosmos-sdk/pull/18537) Fix panic when defining minimum gas config as `100stake;100uatom`. Use a `,` delimiter instead of `;`. Fixes the server config getter to use the correct delimiter.
* (client/tx) [#18472](https://github.com/cosmos/cosmos-sdk/pull/18472) Utilizes the correct Pubkey when simulating a transaction.

Expand Down
4 changes: 4 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
}
}

// Reset the gas meter so that the AnteHandlers aren't required to
gasMeter = app.getBlockGasMeter(app.deliverState.ctx)
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(gasMeter)

return res
}

Expand Down
35 changes: 35 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,41 @@ func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...fun
return suite
}

func TestAnteHandlerGasMeter(t *testing.T) {
// run BeginBlock and assert that the gas meter passed into the first Txn's AnteHandlers is zeroed out
anteOpt := func(bapp *baseapp.BaseApp) {
bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) {
gasMeter := ctx.BlockGasMeter()
require.NotNil(t, gasMeter)
require.Equal(t, sdk.Gas(0), gasMeter.GasConsumed())
return ctx, nil
})
}
// set the beginBlocker to use some gas
beginBlockerOpt := func(bapp *baseapp.BaseApp) {
bapp.SetBeginBlocker(func(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
ctx.BlockGasMeter().ConsumeGas(1, "beginBlocker gas consumption")
return abci.ResponseBeginBlock{}
})
}

suite := NewBaseAppSuite(t, anteOpt, beginBlockerOpt)

_ = suite.baseApp.InitChain(abci.RequestInitChain{
ConsensusParams: &tmproto.ConsensusParams{},
})

// Run BeginBlock to consume some gas
header := tmproto.Header{Height: 1}
suite.baseApp.BeginBlock(abci.RequestBeginBlock{Header: header})

// Run our first Tx (make sure the AnteHandler doesn't see gas consumed in BeginBlock
tx := newTxCounter(t, suite.txConfig, 0, 0)
txBytes, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
}

func TestLoadVersion(t *testing.T) {
logger := defaultLogger()
pruningOpt := baseapp.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
Expand Down

0 comments on commit 9e60114

Please sign in to comment.