Skip to content

Commit 466e430

Browse files
authored
Use contract rent to decide sudo call gas limit (#467)
* Use contract rent to decide sudo call gas limit * address comments
1 parent 6518b77 commit 466e430

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ loadtest_results.txt
3737
# nitro-replayer
3838
nitro-replayer/target
3939
nitro-replayer/Cargo.lock
40+
# nitro mac artifact
41+
x/nitro/replay/libnitro_replayer.dylib

x/dex/keeper/contract.go

+15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ func (k Keeper) GetContract(ctx sdk.Context, contractAddr string) (types.Contrac
5050
return res, nil
5151
}
5252

53+
func (k Keeper) GetContractGasLimit(ctx sdk.Context, contractAddr sdk.AccAddress) (uint64, error) {
54+
bech32ContractAddr := contractAddr.String()
55+
contract, err := k.GetContract(ctx, bech32ContractAddr)
56+
if err != nil {
57+
return 0, err
58+
}
59+
rentBalance := contract.RentBalance
60+
gasPrice := k.GetParams(ctx).SudoCallGasPrice
61+
if gasPrice.LTE(sdk.ZeroDec()) {
62+
return 0, errors.New("invalid gas price: must be positive")
63+
}
64+
gasDec := sdk.NewDec(int64(rentBalance)).Quo(gasPrice)
65+
return gasDec.TruncateInt().Uint64(), nil // round down
66+
}
67+
5368
func (k Keeper) GetAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 {
5469
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(ContractPrefixKey))
5570
iterator := sdk.KVStorePrefixIterator(store, []byte{})

x/dex/keeper/contract_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,19 @@ func TestGetAllContractInfo(t *testing.T) {
7777
require.Equal(t, keepertest.TestContract, contracts[0].ContractAddr)
7878
require.Equal(t, "ta2", contracts[1].Creator)
7979
require.Equal(t, "tc2", contracts[1].ContractAddr)
80+
}
8081

82+
func TestGetContractGasLimit(t *testing.T) {
83+
keeper, ctx := keepertest.DexKeeper(t)
84+
contractAddr := sdk.MustAccAddressFromBech32("sei1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsgshtdj")
85+
keeper.SetParams(ctx, types.Params{SudoCallGasPrice: sdk.NewDecWithPrec(1, 1), PriceSnapshotRetention: 1})
86+
keeper.SetContract(ctx, &types.ContractInfoV2{
87+
Creator: keepertest.TestAccount,
88+
ContractAddr: "sei1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsgshtdj",
89+
CodeId: 1,
90+
RentBalance: 1000000,
91+
})
92+
gasLimit, err := keeper.GetContractGasLimit(ctx, contractAddr)
93+
require.Nil(t, err)
94+
require.Equal(t, uint64(10000000), gasLimit)
8195
}

x/dex/keeper/utils/wasm.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ func getMsgType(msg interface{}) string {
3535
}
3636
}
3737

38-
func sudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddress []byte, wasmMsg []byte, msgType string) ([]byte, uint64, error) {
38+
func sudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddress sdk.AccAddress, wasmMsg []byte, msgType string) ([]byte, uint64, error) {
3939
// Measure the time it takes to execute the contract in WASM
4040
defer metrics.MeasureSudoExecutionDuration(time.Now(), msgType)
4141
// set up a tmp context to prevent race condition in reading gas consumed
4242
// Note that the limit will effectively serve as a soft limit since it's
4343
// possible for the actual computation to go above the specified limit, but
4444
// the associated contract would be charged corresponding rent.
45-
tmpCtx := sdkCtx.WithGasMeter(sdk.NewGasMeter(sdkCtx.GasMeter().Limit()))
45+
gasLimit, err := k.GetContractGasLimit(sdkCtx, contractAddress)
46+
if err != nil {
47+
return nil, 0, err
48+
}
49+
tmpCtx := sdkCtx.WithGasMeter(sdk.NewGasMeter(gasLimit))
4650
data, err := sudoWithoutOutOfGasPanic(tmpCtx, k, contractAddress, wasmMsg)
4751
gasConsumed := tmpCtx.GasMeter().GasConsumed()
4852
if gasConsumed > 0 {
-12.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)