From e9dbe6f86ae571bcc05a8ae1386bc37a866df6a6 Mon Sep 17 00:00:00 2001 From: Bryce Neal Date: Wed, 15 Feb 2023 10:33:30 -0500 Subject: [PATCH] fix: `x/capability` `InitMemStore` should not consume gas (#15030) --- CHANGELOG.md | 1 + x/capability/capability_test.go | 9 ++++++--- x/capability/keeper/keeper.go | 5 +++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 716421873780..fe8b696d5fc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -270,6 +270,7 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf ### Bug Fixes +* (x/capability) [#15030](https://github.com/cosmos/cosmos-sdk/pull/15030) Fixed bug where `x/capability` consumed `GasMeter` gas during `InitMemStore`. * [#14995](https://github.com/cosmos/cosmos-sdk/pull/14995) Allow unknown fields in `ParseTypedEvent`. * [#14952](https://github.com/cosmos/cosmos-sdk/pull/14952) Pin version of github.com/syndtr/goleveldb `v1.0.1-0.20210819022825-2ae1ddf74ef7` to avoid issues in the store. * (store) [#14931](https://github.com/cosmos/cosmos-sdk/pull/14931) Exclude in-memory KVStores, i.e. `StoreTypeMemory`, from CommitInfo commitments. diff --git a/x/capability/capability_test.go b/x/capability/capability_test.go index 9e5c43a16ba8..a907b3989221 100644 --- a/x/capability/capability_test.go +++ b/x/capability/capability_test.go @@ -70,13 +70,16 @@ func (suite *CapabilityTestSuite) TestInitializeMemStore() { // Mock app beginblock and ensure that no gas has been consumed and memstore is initialized ctx = suite.app.BaseApp.NewContext(false, cmtproto.Header{}).WithBlockGasMeter(storetypes.NewGasMeter(50)) - prevGas := ctx.BlockGasMeter().GasConsumed() + prevBlockGas := ctx.BlockGasMeter().GasConsumed() + prevGas := ctx.GasMeter().GasConsumed() restartedModule := capability.NewAppModule(suite.cdc, *newKeeper, true) restartedModule.BeginBlock(ctx, abci.RequestBeginBlock{}) + gasUsed := ctx.GasMeter().GasConsumed() suite.Require().True(newKeeper.IsInitialized(ctx), "memstore initialized flag not set") - gasUsed := ctx.BlockGasMeter().GasConsumed() + blockGasUsed := ctx.BlockGasMeter().GasConsumed() - suite.Require().Equal(prevGas, gasUsed, "beginblocker consumed gas during execution") + suite.Require().Equal(prevBlockGas, blockGasUsed, "ensure beginblocker consumed no block gas during execution") + suite.Require().Equal(prevGas, gasUsed, "ensure beginblocker consumed no gas during execution") // Mock the first transaction getting capability and subsequently failing // by using a cached context and discarding all cached writes. diff --git a/x/capability/keeper/keeper.go b/x/capability/keeper/keeper.go index 8f5772ff96b8..41959ad0c89c 100644 --- a/x/capability/keeper/keeper.go +++ b/x/capability/keeper/keeper.go @@ -119,8 +119,9 @@ func (k *Keeper) InitMemStore(ctx sdk.Context) { panic(fmt.Sprintf("invalid memory store type; got %s, expected: %s", memStoreType, storetypes.StoreTypeMemory)) } - // create context with no block gas meter to ensure we do not consume gas during local initialization logic. - noGasCtx := ctx.WithBlockGasMeter(storetypes.NewInfiniteGasMeter()) + // Create a context with no `BlockGasMeter`, and no `GasMeter`. This ensures we do not consume gas during local + // initialization logic. + noGasCtx := ctx.WithBlockGasMeter(storetypes.NewInfiniteGasMeter()).WithGasMeter(storetypes.NewInfiniteGasMeter()) // check if memory store has not been initialized yet by checking if initialized flag is nil. if !k.IsInitialized(noGasCtx) {