diff --git a/x/dex/keeper/abci/begin_block_new_block_test.go b/x/dex/keeper/abci/begin_block_new_block_test.go index fe9ba72516..fa6d1a02fe 100644 --- a/x/dex/keeper/abci/begin_block_new_block_test.go +++ b/x/dex/keeper/abci/begin_block_new_block_test.go @@ -1,12 +1,17 @@ package abci_test import ( + "context" "testing" + "time" - wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/sei-protocol/sei-chain/x/dex/keeper" + keepertest "github.com/sei-protocol/sei-chain/testutil/keeper" + dexcache "github.com/sei-protocol/sei-chain/x/dex/cache" "github.com/sei-protocol/sei-chain/x/dex/keeper/abci" + "github.com/sei-protocol/sei-chain/x/dex/types" + dexutils "github.com/sei-protocol/sei-chain/x/dex/utils" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) const ( @@ -17,9 +22,15 @@ const ( func TestHandleBBNewBlock(t *testing.T) { // this test only ensures that HandleBBNewBlock doesn't crash. The actual logic // is tested in module_test.go where an actual wasm file is deployed and invoked. - wasmkeeper.TestingStakeParams.MinCommissionRate = sdk.NewDecWithPrec(5, 2) - ctx, wasmkeepers := wasmkeeper.CreateTestInput(t, false, SupportedFeatures) - dexKeeper := keeper.Keeper{WasmKeeper: *wasmkeepers.WasmKeeper} - wrapper := abci.KeeperWrapper{Keeper: &dexKeeper} + testApp := keepertest.TestApp() + ctx := testApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) + ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(testApp.GetKey(types.StoreKey)))) + keeper := testApp.DexKeeper + ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) + keeper.SetContract(ctx, &types.ContractInfoV2{ + ContractAddr: TestContract, + RentBalance: 100000000, + }) + wrapper := abci.KeeperWrapper{Keeper: &keeper} wrapper.HandleBBNewBlock(ctx, TestContract, 1) } diff --git a/x/dex/keeper/utils/wasm.go b/x/dex/keeper/utils/wasm.go index dd776fb32b..583c608e84 100644 --- a/x/dex/keeper/utils/wasm.go +++ b/x/dex/keeper/utils/wasm.go @@ -39,18 +39,33 @@ func sudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddress []byte, wasmMsg // Measure the time it takes to execute the contract in WASM defer metrics.MeasureSudoExecutionDuration(time.Now(), msgType) // set up a tmp context to prevent race condition in reading gas consumed - tmpCtx := sdkCtx.WithGasMeter(sdk.NewInfiniteGasMeter()) - data, err := k.WasmKeeper.Sudo( - tmpCtx, contractAddress, wasmMsg, - ) + // Note that the limit will effectively serve as a soft limit since it's + // possible for the actual computation to go above the specified limit, but + // the associated contract would be charged corresponding rent. + tmpCtx := sdkCtx.WithGasMeter(sdk.NewGasMeter(sdkCtx.GasMeter().Limit())) + data, err := sudoWithoutOutOfGasPanic(tmpCtx, k, contractAddress, wasmMsg) gasConsumed := tmpCtx.GasMeter().GasConsumed() - sdkCtx.GasMeter().ConsumeGas(gasConsumed, "sudo") + if gasConsumed > 0 { + sdkCtx.GasMeter().ConsumeGas(gasConsumed, "sudo") + } if hasErrInstantiatingWasmModuleDueToCPUFeature(err) { panic(utils.DecorateHardFailError(err)) } return data, gasConsumed, err } +func sudoWithoutOutOfGasPanic(ctx sdk.Context, k *keeper.Keeper, contractAddress []byte, wasmMsg []byte) ([]byte, error) { + defer func() { + if err := recover(); err != nil { + // only propagate panic if the error is out of gas + if _, ok := err.(sdk.ErrorOutOfGas); !ok { + panic(err) + } + } + }() + return k.WasmKeeper.Sudo(ctx, contractAddress, wasmMsg) +} + func hasErrInstantiatingWasmModuleDueToCPUFeature(err error) bool { if err == nil { return false diff --git a/x/oracle/simulation/operations.go b/x/oracle/simulation/operations.go index 3988c4b164..2028e73625 100644 --- a/x/oracle/simulation/operations.go +++ b/x/oracle/simulation/operations.go @@ -65,7 +65,7 @@ func WeightedOperations( } // SimulateMsgAggregateExchangeRateVote generates a MsgAggregateExchangeRateVote with random values. -//nolint: funlen +// nolint: funlen func SimulateMsgAggregateExchangeRateVote(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -122,7 +122,7 @@ func SimulateMsgAggregateExchangeRateVote(ak types.AccountKeeper, bk types.BankK } // SimulateMsgDelegateFeedConsent generates a MsgDelegateFeedConsent with random values. -//nolint: funlen +// nolint: funlen func SimulateMsgDelegateFeedConsent(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,