Skip to content

Commit

Permalink
Merge pull request #3 from initia-labs/fix/gas-estimate
Browse files Browse the repository at this point in the history
customize gas simulator to return base + simulated gas amount
  • Loading branch information
beer-1 authored Oct 31, 2023
2 parents d027a7a + 70b1f14 commit 5cc7b3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
9 changes: 8 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,9 +1160,16 @@ func (app *InitiaApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.API
}
}

// Simulate customize gas simulation to add fee deduction gas amount.
func (app *InitiaApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) {
gasInfo, result, err := app.BaseApp.Simulate(txBytes)
gasInfo.GasUsed += FeeDeductionGasAmount
return gasInfo, result, err
}

// RegisterTxService implements the Application.RegisterTxService method.
func (app *InitiaApp) RegisterTxService(clientCtx client.Context) {
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.BaseApp.Simulate, app.interfaceRegistry)
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.Simulate, app.interfaceRegistry)
}

// RegisterTendermintService implements the Application.RegisterTendermintService method.
Expand Down
3 changes: 3 additions & 0 deletions app/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

const (
// FeeDeductionGasAmount is a estimated gas amount of fee payment
FeeDeductionGasAmount = 180_000

// AccountAddressPrefix is the prefix of bech32 encoded address
AccountAddressPrefix = "init"

Expand Down
37 changes: 20 additions & 17 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (k Keeper) PublishModuleBundle(
return err
}

// ignore staking deltas and events
err = k.ExecuteEntryFunction(
ctx,
sender,
Expand Down Expand Up @@ -123,17 +122,19 @@ func (k Keeper) ExecuteEntryFunctionWithMultiSenders(
typeArgs []vmtypes.TypeTag,
args [][]byte,
) error {
var gasForRuntime uint64
vm := k.moveVM
gasMeter := ctx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

isSimulationOrCheckTx := isSimulationOrCheckTx(ctx)
if isSimulationOrCheckTx {
vm = k.buildSimulationVM()
defer vm.Destroy()

gasForRuntime = k.config.ContractSimulationGasLimit
} else {
if gasMeter.Limit() != 0 {
gasForRuntime = gasMeter.Limit() - gasMeter.GasConsumedToLimit()
} else {
gasForRuntime = math.MaxUint64
}
} else if gasMeter.Limit() == 0 {
// infinite gas meter
gasForRuntime = math.MaxUint64
}

// delegate gas metering to move vm
Expand Down Expand Up @@ -166,7 +167,7 @@ func (k Keeper) ExecuteEntryFunctionWithMultiSenders(
k.IncreaseExecutionCounter(ctx),
)

execRes, err := k.moveVM.ExecuteEntryFunction(
execRes, err := vm.ExecuteEntryFunction(
kvStore,
api,
env,
Expand Down Expand Up @@ -220,17 +221,19 @@ func (k Keeper) ExecuteScriptWithMultiSenders(
typeArgs []vmtypes.TypeTag,
args [][]byte,
) error {
var gasForRuntime uint64
vm := k.moveVM
gasMeter := ctx.GasMeter()
gasForRuntime := gasMeter.Limit() - gasMeter.GasConsumedToLimit()

isSimulationOrCheckTx := isSimulationOrCheckTx(ctx)
if isSimulationOrCheckTx {
vm = k.buildSimulationVM()
defer vm.Destroy()

gasForRuntime = k.config.ContractSimulationGasLimit
} else {
if gasMeter.Limit() != 0 {
gasForRuntime = gasMeter.Limit() - gasMeter.GasConsumedToLimit()
} else {
gasForRuntime = math.MaxUint64
}
} else if gasMeter.Limit() == 0 {
// infinite gas meter
gasForRuntime = math.MaxUint64
}

// delegate gas metering to move vm
Expand Down Expand Up @@ -261,7 +264,7 @@ func (k Keeper) ExecuteScriptWithMultiSenders(
k.IncreaseExecutionCounter(ctx),
)

execRes, err := k.moveVM.ExecuteScript(
execRes, err := vm.ExecuteScript(
kvStore,
api,
env,
Expand Down

0 comments on commit 5cc7b3a

Please sign in to comment.