From 7a43e490669f8073d0f93d06afaeb3758325d2b7 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 18 Dec 2023 19:25:10 +0100 Subject: [PATCH] fix(baseapp): Reset GasMeter before deliverTX (backport #18714) (#18774) --- CHANGELOG.md | 4 ++++ baseapp/abci.go | 4 ++++ baseapp/baseapp_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e183a9544f6..86d78ed61fd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation. * (x/auth/tx) [#18772](https://github.com/cosmos/cosmos-sdk/pull/18772) Remove misleading gas wanted from tx simulation failure log. +### 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. + ## [v0.50.2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.2) - 2023-12-11 ### Features diff --git a/baseapp/abci.go b/baseapp/abci.go index a78fc700bc14..d1ab2b53ce57 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -761,6 +761,10 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request events = append(events, beginBlock.Events...) + // Reset the gas meter so that the AnteHandlers aren't required to + gasMeter = app.getBlockGasMeter(app.finalizeBlockState.ctx) + app.finalizeBlockState.ctx = app.finalizeBlockState.ctx.WithBlockGasMeter(gasMeter) + // Iterate over all raw transactions in the proposal and attempt to execute // them, gathering the execution results. // diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 3fe1b3463f0f..082aeacf402b 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -189,6 +189,40 @@ 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 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, storetypes.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) (sdk.BeginBlock, error) { + ctx.BlockGasMeter().ConsumeGas(1, "beginBlocker gas consumption") + return sdk.BeginBlock{}, nil + }) + } + + suite := NewBaseAppSuite(t, anteOpt, beginBlockerOpt) + _, err := suite.baseApp.InitChain(&abci.RequestInitChain{ + ConsensusParams: &cmtproto.ConsensusParams{}, + }) + require.NoError(t, err) + + deliverKey := []byte("deliver-key") + baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey}) + + tx := newTxCounter(t, suite.txConfig, 0, 0) + txBytes, err := suite.txConfig.TxEncoder()(tx) + require.NoError(t, err) + _, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1, Txs: [][]byte{txBytes}}) + require.NoError(t, err) +} + func TestLoadVersion(t *testing.T) { logger := log.NewTestLogger(t) pruningOpt := baseapp.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))