Skip to content

Commit

Permalink
fix to consider movevm gas scale when we use infinity gas meter (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 23, 2024
1 parent d14d1bc commit dd671f7
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"strings"
"unsafe"

Expand Down Expand Up @@ -190,10 +191,7 @@ func (k Keeper) executeEntryFunction(

sdkCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()
if isSimulation(ctx) {
gasForRuntime = k.config.ContractSimulationGasLimit
}
gasForRuntime := k.computeGasForRuntime(ctx, gasMeter)

// delegate gas metering to move vm
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())
Expand Down Expand Up @@ -305,10 +303,7 @@ func (k Keeper) executeScript(

sdkCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()
if isSimulation(ctx) {
gasForRuntime = k.config.ContractSimulationGasLimit
}
gasForRuntime := k.computeGasForRuntime(ctx, gasMeter)

// delegate gas metering to move vm
sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter())
Expand Down Expand Up @@ -655,7 +650,7 @@ func (k Keeper) executeViewFunction(

sdkCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sdkCtx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()
gasForRuntime := k.computeGasForRuntime(ctx, gasMeter)

gasBalance := gasForRuntime
viewRes, err := execVM(ctx, k, func(vm types.VMEngine) (vmtypes.ViewOutput, error) {
Expand Down Expand Up @@ -688,3 +683,19 @@ func execVM[T any](ctx context.Context, k Keeper, f func(types.VMEngine) (T, err

return f(vm)
}

func (k Keeper) computeGasForRuntime(ctx context.Context, gasMeter storetypes.GasMeter) uint64 {
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()
if isSimulation(ctx) {
gasForRuntime = k.config.ContractSimulationGasLimit
}

// gasUnitScale is multiplied in moveVM to scale the gas limit, so we need to divide it here
// if gasForRuntime is too large, it will overflow when multiplied in moveVM.
const gasUintScale = 100
if gasForRuntime > math.MaxUint64/gasUintScale {
return math.MaxUint64 / gasUintScale
}

return gasForRuntime
}

0 comments on commit dd671f7

Please sign in to comment.