-
Notifications
You must be signed in to change notification settings - Fork 870
Use contract rent to decide sudo call gas limit #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible for
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we passing contractAddress here (and above in
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.