diff --git a/app/app.go b/app/app.go index 4074db44..2af40acf 100644 --- a/app/app.go +++ b/app/app.go @@ -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. diff --git a/app/const.go b/app/const.go index 19c96aa4..4613729c 100644 --- a/app/const.go +++ b/app/const.go @@ -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" diff --git a/x/move/keeper/handler.go b/x/move/keeper/handler.go index 467e739f..c03eef44 100644 --- a/x/move/keeper/handler.go +++ b/x/move/keeper/handler.go @@ -73,7 +73,6 @@ func (k Keeper) PublishModuleBundle( return err } - // ignore staking deltas and events err = k.ExecuteEntryFunction( ctx, sender, @@ -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 @@ -166,7 +167,7 @@ func (k Keeper) ExecuteEntryFunctionWithMultiSenders( k.IncreaseExecutionCounter(ctx), ) - execRes, err := k.moveVM.ExecuteEntryFunction( + execRes, err := vm.ExecuteEntryFunction( kvStore, api, env, @@ -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 @@ -261,7 +264,7 @@ func (k Keeper) ExecuteScriptWithMultiSenders( k.IncreaseExecutionCounter(ctx), ) - execRes, err := k.moveVM.ExecuteScript( + execRes, err := vm.ExecuteScript( kvStore, api, env,