Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions x/dex/keeper/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible for gasPrice to be zero? If so we should have zero div protection here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, updated

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{})
Expand Down
14 changes: 14 additions & 0 deletions x/dex/keeper/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 6 additions & 2 deletions x/dex/keeper/utils/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we passing contractAddress here (and above in sudo args as []byte instead of AccAddress which is a defined type for []byte IIRC?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

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 {
Expand Down
Binary file removed x/nitro/replay/libnitro_replayer.dylib
Binary file not shown.