diff --git a/baseapp/abci.go b/baseapp/abci.go index 32a6d8683227..d82b27acafcd 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -73,7 +73,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC } // add block gas meter for any genesis transactions (allow infinite gas) - app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) + app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter(app.logger)) res = app.initChainer(app.deliverState.ctx, req) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index c3d606db018a..ba99410fc79f 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -10,6 +10,7 @@ import ( dbm "github.com/cometbft/cometbft-db" abci "github.com/cometbft/cometbft/abci/types" + "github.com/cometbft/cometbft/libs/log" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cosmos/gogoproto/jsonpb" "github.com/stretchr/testify/require" @@ -758,7 +759,7 @@ func TestABCI_Query_SimulateTx(t *testing.T) { gasConsumed := uint64(5) anteOpt := func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { - newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasConsumed)) + newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasConsumed, log.NewNopLogger())) return }) } @@ -923,7 +924,7 @@ func TestABCI_TxGasLimits(t *testing.T) { gasGranted := uint64(10) anteOpt := func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { - newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasGranted)) + newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasGranted, log.NewNopLogger())) // AnteHandlers must have their own defer/recover in order for the BaseApp // to know how much gas was used! This is because the GasMeter is created in @@ -1006,7 +1007,7 @@ func TestABCI_MaxBlockGasLimits(t *testing.T) { gasGranted := uint64(10) anteOpt := func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { - newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasGranted)) + newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasGranted, log.NewNopLogger())) defer func() { if r := recover(); r != nil { @@ -1101,7 +1102,7 @@ func TestABCI_GasConsumptionBadTx(t *testing.T) { gasWanted := uint64(5) anteOpt := func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { - newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasWanted)) + newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasWanted, log.NewNopLogger())) defer func() { if r := recover(); r != nil { diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 9b4faa5376ed..84c16fab5318 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -566,10 +566,10 @@ func (app *BaseApp) getState(mode runTxMode) *state { func (app *BaseApp) getBlockGasMeter(ctx sdk.Context) storetypes.GasMeter { if maxGas := app.GetMaximumBlockGas(ctx); maxGas > 0 { - return storetypes.NewGasMeter(maxGas) + return storetypes.NewGasMeter(maxGas, ctx.Logger()) } - return storetypes.NewInfiniteGasMeter() + return storetypes.NewInfiniteGasMeter(ctx.Logger()) } // retrieve the context for the tx w/ txBytes and other memoized values. diff --git a/store/gaskv/store_test.go b/store/gaskv/store_test.go index 2aabc9de7c2f..eaf834a547d5 100644 --- a/store/gaskv/store_test.go +++ b/store/gaskv/store_test.go @@ -7,6 +7,7 @@ import ( dbm "github.com/cometbft/cometbft-db" "github.com/stretchr/testify/require" + "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/gaskv" "github.com/cosmos/cosmos-sdk/store/types" @@ -19,7 +20,7 @@ func valFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) } func TestGasKVStoreBasic(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} - meter := types.NewGasMeter(10000) + meter := types.NewGasMeter(10000, log.NewNopLogger()) st := gaskv.NewStore(mem, meter, types.KVGasConfig()) require.Equal(t, types.StoreTypeDB, st.GetStoreType()) @@ -39,7 +40,7 @@ func TestGasKVStoreBasic(t *testing.T) { func TestGasKVStoreIterator(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} - meter := types.NewGasMeter(100000) + meter := types.NewGasMeter(100000, log.NewNopLogger()) st := gaskv.NewStore(mem, meter, types.KVGasConfig()) require.False(t, st.Has(keyFmt(1))) require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") @@ -104,14 +105,14 @@ func TestGasKVStoreIterator(t *testing.T) { func TestGasKVStoreOutOfGasSet(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} - meter := types.NewGasMeter(0) + meter := types.NewGasMeter(0, log.NewNopLogger()) st := gaskv.NewStore(mem, meter, types.KVGasConfig()) require.Panics(t, func() { st.Set(keyFmt(1), valFmt(1)) }, "Expected out-of-gas") } func TestGasKVStoreOutOfGasIterator(t *testing.T) { mem := dbadapter.Store{DB: dbm.NewMemDB()} - meter := types.NewGasMeter(20000) + meter := types.NewGasMeter(20000, log.NewNopLogger()) st := gaskv.NewStore(mem, meter, types.KVGasConfig()) st.Set(keyFmt(1), valFmt(1)) iterator := st.Iterator(nil, nil) diff --git a/store/types/gas.go b/store/types/gas.go index e0c950476b2e..96049f974e05 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -3,6 +3,8 @@ package types import ( "fmt" "math" + + "github.com/cometbft/cometbft/libs/log" ) // Gas consumption descriptors. @@ -53,13 +55,15 @@ type GasMeter interface { type basicGasMeter struct { limit Gas consumed Gas + logger log.Logger } // NewGasMeter returns a reference to a new basicGasMeter. -func NewGasMeter(limit Gas) GasMeter { +func NewGasMeter(limit Gas, logger log.Logger) GasMeter { return &basicGasMeter{ limit: limit, consumed: 0, + logger: logger, } } @@ -105,6 +109,8 @@ func addUint64Overflow(a, b uint64) (uint64, bool) { // ConsumeGas adds the given amount of gas to the gas consumed and panics if it overflows the limit or out of gas. func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) { + g.logger.Info("BasicGasMeter.ConsumeGas", "amount", amount, "descriptor", descriptor) + var overflow bool g.consumed, overflow = addUint64Overflow(g.consumed, amount) if overflow { @@ -148,12 +154,14 @@ func (g *basicGasMeter) String() string { type infiniteGasMeter struct { consumed Gas + logger log.Logger } // NewInfiniteGasMeter returns a new gas meter without a limit. -func NewInfiniteGasMeter() GasMeter { +func NewInfiniteGasMeter(logger log.Logger) GasMeter { return &infiniteGasMeter{ consumed: 0, + logger: logger, } } @@ -180,6 +188,8 @@ func (g *infiniteGasMeter) Limit() Gas { // ConsumeGas adds the given amount of gas to the gas consumed and panics if it overflows the limit. func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) { + g.logger.Info("InfiniteGasMeter.ConsumeGas", "amount", amount, "descriptor", descriptor) + var overflow bool // TODO: Should we set the consumed field after overflow checking? g.consumed, overflow = addUint64Overflow(g.consumed, amount) diff --git a/types/context.go b/types/context.go index 0942c05fc7a5..7802a73c46ca 100644 --- a/types/context.go +++ b/types/context.go @@ -105,7 +105,7 @@ func NewContext(ms MultiStore, header tmproto.Header, isCheckTx bool, logger log chainID: header.ChainID, checkTx: isCheckTx, logger: logger, - gasMeter: storetypes.NewInfiniteGasMeter(), + gasMeter: storetypes.NewInfiniteGasMeter(logger), minGasPrice: DecCoins{}, eventManager: NewEventManager(), kvGasConfig: storetypes.KVGasConfig(), diff --git a/types/context_test.go b/types/context_test.go index 45fd1ebcc8a2..5a99d10f0547 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -6,6 +6,7 @@ import ( "time" abci "github.com/cometbft/cometbft/abci/types" + "github.com/cometbft/cometbft/libs/log" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" tmtime "github.com/cometbft/cometbft/types/time" "github.com/golang/mock/gomock" @@ -90,8 +91,8 @@ func (s *contextTestSuite) TestContextWithCustom() { txbytes := []byte("txbytes") logger := mock.NewMockLogger(ctrl) voteinfos := []abci.VoteInfo{{}} - meter := types.NewGasMeter(10000) - blockGasMeter := types.NewGasMeter(20000) + meter := types.NewGasMeter(10000, log.NewNopLogger()) + blockGasMeter := types.NewGasMeter(20000, log.NewNopLogger()) minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)} headerHash := []byte("headerHash") zeroGasCfg := storetypes.GasConfig{} diff --git a/types/module/module.go b/types/module/module.go index 95e35f3546e4..aef13089bf3d 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -398,7 +398,7 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec, if module, ok := m.Modules[moduleName].(HasGenesis); ok { channels[moduleName] = make(chan json.RawMessage) go func(module HasGenesis, ch chan json.RawMessage) { - ctx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) // avoid race conditions + ctx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter(ctx.Logger())) // avoid race conditions ch <- module.ExportGenesis(ctx, cdc) }(module, channels[moduleName]) } diff --git a/types/store.go b/types/store.go index 274d4f9c2c2b..720a669cefb8 100644 --- a/types/store.go +++ b/types/store.go @@ -7,6 +7,8 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/types/kv" + + "github.com/cometbft/cometbft/libs/log" ) type ( @@ -156,10 +158,10 @@ type ( ErrorGasOverflow = types.ErrorGasOverflow ) -func NewGasMeter(limit Gas) GasMeter { - return types.NewGasMeter(limit) +func NewGasMeter(limit Gas, logger log.Logger) GasMeter { + return types.NewGasMeter(limit, logger) } -func NewInfiniteGasMeter() GasMeter { - return types.NewInfiniteGasMeter() +func NewInfiniteGasMeter(log log.Logger) GasMeter { + return types.NewInfiniteGasMeter(log) } diff --git a/types/store_test.go b/types/store_test.go index 2d2bc636d4f2..d5c2402a7b78 100644 --- a/types/store_test.go +++ b/types/store_test.go @@ -61,7 +61,7 @@ func (s *storeTestSuite) TestNewTransientStoreKeys() { } func (s *storeTestSuite) TestNewInfiniteGasMeter() { - gm := sdk.NewInfiniteGasMeter() + gm := sdk.NewInfiniteGasMeter(log.NewNopLogger()) s.Require().NotNil(gm) _, ok := gm.(types.GasMeter) s.Require().True(ok) diff --git a/x/auth/ante/setup.go b/x/auth/ante/setup.go index 5a101a24950a..759623906e55 100644 --- a/x/auth/ante/setup.go +++ b/x/auth/ante/setup.go @@ -75,8 +75,8 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64) sdk.Context { // In various cases such as simulation and during the genesis block, we do not // meter any gas utilization. if simulate || ctx.BlockHeight() == 0 { - return ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) + return ctx.WithGasMeter(sdk.NewInfiniteGasMeter(ctx.Logger())) } - return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit)) + return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit, ctx.Logger())) } diff --git a/x/auth/ante/setup_test.go b/x/auth/ante/setup_test.go index e3488ad2e9ea..f3c3486af505 100644 --- a/x/auth/ante/setup_test.go +++ b/x/auth/ante/setup_test.go @@ -3,6 +3,7 @@ package ante_test import ( "testing" + "github.com/cometbft/cometbft/libs/log" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -37,7 +38,7 @@ func TestSetupDecorator_BlockMaxGas(t *testing.T) { suite.ctx = suite.ctx. WithBlockHeight(1). - WithGasMeter(storetypes.NewGasMeter(0)). + WithGasMeter(storetypes.NewGasMeter(0, log.NewNopLogger())). WithConsensusParams(&tmproto.ConsensusParams{ Block: &tmproto.BlockParams{ MaxGas: 100, @@ -71,7 +72,7 @@ func TestSetup(t *testing.T) { antehandler := sdk.ChainAnteDecorators(sud) // Set height to non-zero value for GasMeter to be set - suite.ctx = suite.ctx.WithBlockHeight(1).WithGasMeter(sdk.NewGasMeter(0)) + suite.ctx = suite.ctx.WithBlockHeight(1).WithGasMeter(sdk.NewGasMeter(0, log.NewNopLogger())) // Context GasMeter Limit not set require.Equal(t, uint64(0), suite.ctx.GasMeter().Limit(), "GasMeter set with limit before setup") diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 9f7a14f5e95f..69e2ade4b314 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" @@ -95,11 +96,11 @@ func TestConsumeSignatureVerificationGas(t *testing.T) { gasConsumed uint64 shouldErr bool }{ - {"PubKeyEd25519", args{sdk.NewInfiniteGasMeter(), nil, ed25519.GenPrivKey().PubKey(), params}, p.SigVerifyCostED25519, true}, - {"PubKeySecp256k1", args{sdk.NewInfiniteGasMeter(), nil, secp256k1.GenPrivKey().PubKey(), params}, p.SigVerifyCostSecp256k1, false}, - {"PubKeySecp256r1", args{sdk.NewInfiniteGasMeter(), nil, skR1.PubKey(), params}, p.SigVerifyCostSecp256r1(), false}, - {"Multisig", args{sdk.NewInfiniteGasMeter(), multisignature1, multisigKey1, params}, expectedCost1, false}, - {"unknown key", args{sdk.NewInfiniteGasMeter(), nil, nil, params}, 0, true}, + {"PubKeyEd25519", args{sdk.NewInfiniteGasMeter(log.NewNopLogger()), nil, ed25519.GenPrivKey().PubKey(), params}, p.SigVerifyCostED25519, true}, + {"PubKeySecp256k1", args{sdk.NewInfiniteGasMeter(log.NewNopLogger()), nil, secp256k1.GenPrivKey().PubKey(), params}, p.SigVerifyCostSecp256k1, false}, + {"PubKeySecp256r1", args{sdk.NewInfiniteGasMeter(log.NewNopLogger()), nil, skR1.PubKey(), params}, p.SigVerifyCostSecp256r1(), false}, + {"Multisig", args{sdk.NewInfiniteGasMeter(log.NewNopLogger()), multisignature1, multisigKey1, params}, expectedCost1, false}, + {"unknown key", args{sdk.NewInfiniteGasMeter(log.NewNopLogger()), nil, nil, params}, 0, true}, } for _, tt := range tests { sigV2 := signing.SignatureV2{ diff --git a/x/capability/capability_test.go b/x/capability/capability_test.go index a090eec6013e..c05f16318716 100644 --- a/x/capability/capability_test.go +++ b/x/capability/capability_test.go @@ -68,7 +68,7 @@ func (suite *CapabilityTestSuite) TestInitializeMemStore() { suite.Require().False(newKeeper.IsInitialized(ctx), "memstore initialized flag set before BeginBlock") // Mock app beginblock and ensure that no gas has been consumed and memstore is initialized - ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockGasMeter(sdk.NewGasMeter(50)) + ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockGasMeter(sdk.NewGasMeter(50, ctx.Logger())) prevGas := ctx.BlockGasMeter().GasConsumed() restartedModule := capability.NewAppModule(suite.cdc, *newKeeper, true) restartedModule.BeginBlock(ctx, abci.RequestBeginBlock{}) diff --git a/x/capability/genesis_test.go b/x/capability/genesis_test.go index c6575ea0e226..e1c45687ff18 100644 --- a/x/capability/genesis_test.go +++ b/x/capability/genesis_test.go @@ -3,6 +3,7 @@ package capability_test import ( tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + "github.com/cometbft/cometbft/libs/log" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -37,7 +38,7 @@ func (suite *CapabilityTestSuite) TestGenesis() { newSk1 := newKeeper.ScopeToModule(banktypes.ModuleName) newSk2 := newKeeper.ScopeToModule(stakingtypes.ModuleName) - deliverCtx, _ := newApp.BaseApp.NewUncachedContext(false, tmproto.Header{}).WithBlockGasMeter(sdk.NewInfiniteGasMeter()).CacheContext() + deliverCtx, _ := newApp.BaseApp.NewUncachedContext(false, tmproto.Header{}).WithBlockGasMeter(sdk.NewInfiniteGasMeter(log.NewNopLogger())).CacheContext() capability.InitGenesis(deliverCtx, *newKeeper, *genState) diff --git a/x/capability/keeper/keeper.go b/x/capability/keeper/keeper.go index 1fa6d0305118..25b4a7d2761b 100644 --- a/x/capability/keeper/keeper.go +++ b/x/capability/keeper/keeper.go @@ -119,7 +119,7 @@ func (k *Keeper) InitMemStore(ctx sdk.Context) { } // create context with no block gas meter to ensure we do not consume gas during local initialization logic. - noGasCtx := ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) + noGasCtx := ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter(ctx.Logger())) // check if memory store has not been initialized yet by checking if initialized flag is nil. if !k.IsInitialized(noGasCtx) { diff --git a/x/group/internal/orm/testsupport.go b/x/group/internal/orm/testsupport.go index f4878d265bd2..f8e8d9679fab 100644 --- a/x/group/internal/orm/testsupport.go +++ b/x/group/internal/orm/testsupport.go @@ -4,6 +4,7 @@ import ( "fmt" dbm "github.com/cometbft/cometbft-db" + "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/store" "github.com/cosmos/cosmos-sdk/store/gaskv" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -81,7 +82,7 @@ type GasCountingMockContext struct { func NewGasCountingMockContext() *GasCountingMockContext { return &GasCountingMockContext{ - GasMeter: &debuggingGasMeter{sdk.NewInfiniteGasMeter()}, + GasMeter: &debuggingGasMeter{sdk.NewInfiniteGasMeter(log.NewNopLogger())}, } } @@ -98,5 +99,5 @@ func (g GasCountingMockContext) GasRemaining() storetypes.Gas { } func (g *GasCountingMockContext) ResetGasMeter() { - g.GasMeter = sdk.NewInfiniteGasMeter() + g.GasMeter = sdk.NewInfiniteGasMeter(log.NewNopLogger()) } diff --git a/x/upgrade/abci.go b/x/upgrade/abci.go index 188e0cd02b5b..dbe6e5a94f55 100644 --- a/x/upgrade/abci.go +++ b/x/upgrade/abci.go @@ -80,7 +80,7 @@ func BeginBlocker(k *keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) { // We have an upgrade handler for this upgrade name, so apply the upgrade ctx.Logger().Info(fmt.Sprintf("applying upgrade \"%s\" at %s", plan.Name, plan.DueAt())) - ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) + ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter(ctx.Logger())) k.ApplyUpgrade(ctx, plan) return }