diff --git a/.gitignore b/.gitignore index c5373f6f5d..6bda0f322e 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ loadtest_results.txt # nitro-replayer nitro-replayer/target nitro-replayer/Cargo.lock +# nitro mac artifact +x/nitro/replay/libnitro_replayer.dylib diff --git a/x/dex/keeper/contract.go b/x/dex/keeper/contract.go index 727dcf5653..e0d7e02c59 100644 --- a/x/dex/keeper/contract.go +++ b/x/dex/keeper/contract.go @@ -50,6 +50,21 @@ func (k Keeper) GetContract(ctx sdk.Context, contractAddr string) (types.Contrac return res, nil } +func (k Keeper) GetContractGasLimit(ctx sdk.Context, contractAddr sdk.AccAddress) (uint64, error) { + bech32ContractAddr := contractAddr.String() + contract, err := k.GetContract(ctx, bech32ContractAddr) + if err != nil { + return 0, err + } + rentBalance := contract.RentBalance + gasPrice := k.GetParams(ctx).SudoCallGasPrice + if gasPrice.LTE(sdk.ZeroDec()) { + return 0, errors.New("invalid gas price: must be positive") + } + gasDec := sdk.NewDec(int64(rentBalance)).Quo(gasPrice) + return gasDec.TruncateInt().Uint64(), nil // round down +} + func (k Keeper) GetAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 { store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(ContractPrefixKey)) iterator := sdk.KVStorePrefixIterator(store, []byte{}) diff --git a/x/dex/keeper/contract_test.go b/x/dex/keeper/contract_test.go index 0412da04c4..6dd527ac40 100644 --- a/x/dex/keeper/contract_test.go +++ b/x/dex/keeper/contract_test.go @@ -77,5 +77,19 @@ func TestGetAllContractInfo(t *testing.T) { require.Equal(t, keepertest.TestContract, contracts[0].ContractAddr) require.Equal(t, "ta2", contracts[1].Creator) require.Equal(t, "tc2", contracts[1].ContractAddr) +} +func TestGetContractGasLimit(t *testing.T) { + keeper, ctx := keepertest.DexKeeper(t) + contractAddr := sdk.MustAccAddressFromBech32("sei1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsgshtdj") + keeper.SetParams(ctx, types.Params{SudoCallGasPrice: sdk.NewDecWithPrec(1, 1), PriceSnapshotRetention: 1}) + keeper.SetContract(ctx, &types.ContractInfoV2{ + Creator: keepertest.TestAccount, + ContractAddr: "sei1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsgshtdj", + CodeId: 1, + RentBalance: 1000000, + }) + gasLimit, err := keeper.GetContractGasLimit(ctx, contractAddr) + require.Nil(t, err) + require.Equal(t, uint64(10000000), gasLimit) } diff --git a/x/dex/keeper/utils/wasm.go b/x/dex/keeper/utils/wasm.go index 583c608e84..f63c4b4aaa 100644 --- a/x/dex/keeper/utils/wasm.go +++ b/x/dex/keeper/utils/wasm.go @@ -35,14 +35,18 @@ func getMsgType(msg interface{}) string { } } -func sudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddress []byte, wasmMsg []byte, msgType string) ([]byte, uint64, error) { +func sudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddress sdk.AccAddress, wasmMsg []byte, msgType string) ([]byte, uint64, error) { // 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 // 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())) + gasLimit, err := k.GetContractGasLimit(sdkCtx, contractAddress) + if err != nil { + return nil, 0, err + } + tmpCtx := sdkCtx.WithGasMeter(sdk.NewGasMeter(gasLimit)) data, err := sudoWithoutOutOfGasPanic(tmpCtx, k, contractAddress, wasmMsg) gasConsumed := tmpCtx.GasMeter().GasConsumed() if gasConsumed > 0 { diff --git a/x/nitro/replay/libnitro_replayer.dylib b/x/nitro/replay/libnitro_replayer.dylib deleted file mode 100755 index 4d47ab40bf..0000000000 Binary files a/x/nitro/replay/libnitro_replayer.dylib and /dev/null differ