From dfaf52279ffbf2149cebc6e00761e48a9c3144fb Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Sat, 2 Nov 2024 12:01:00 -0700 Subject: [PATCH] refactor(gas): simplify EstimateGasForEvmCallType --- x/evm/keeper/grpc_query.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/x/evm/keeper/grpc_query.go b/x/evm/keeper/grpc_query.go index 54fd906bc..37a067ed9 100644 --- a/x/evm/keeper/grpc_query.go +++ b/x/evm/keeper/grpc_query.go @@ -323,17 +323,25 @@ func (k Keeper) EstimateGasForEvmCallType( ctx := sdk.UnwrapSDKContext(goCtx) chainID := k.EthChainID(ctx) + cfg, err := k.GetEVMConfig(ctx, ParseProposerAddr(ctx, req.ProposerAddress), chainID) + if err != nil { + return nil, grpcstatus.Error(grpccodes.Internal, "failed to load evm config") + } if req.GasCap < gethparams.TxGas { return nil, grpcstatus.Errorf(grpccodes.InvalidArgument, "gas cap cannot be lower than %d", gethparams.TxGas) } var args evm.JsonTxArgs - err := json.Unmarshal(req.Args, &args) + err = json.Unmarshal(req.Args, &args) if err != nil { return nil, grpcstatus.Error(grpccodes.InvalidArgument, err.Error()) } + // ApplyMessageWithConfig expect correct nonce set in msg + nonce := k.GetAccNonce(ctx, args.GetFrom()) + args.Nonce = (*hexutil.Uint64)(&nonce) + // Binary search the gas requirement, as it may be higher than the amount used var ( lo = gethparams.TxGas - 1 @@ -361,16 +369,6 @@ func (k Keeper) EstimateGasForEvmCallType( } gasCap = hi - cfg, err := k.GetEVMConfig(ctx, ParseProposerAddr(ctx, req.ProposerAddress), chainID) - if err != nil { - return nil, grpcstatus.Error(grpccodes.Internal, "failed to load evm config") - } - - // ApplyMessageWithConfig expect correct nonce set in msg - nonce := k.GetAccNonce(ctx, args.GetFrom()) - args.Nonce = (*hexutil.Uint64)(&nonce) - - txConfig := statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())) // convert the tx args to an ethereum message msg, err := args.ToMessage(req.GasCap, cfg.BaseFeeWei) @@ -422,6 +420,7 @@ func (k Keeper) EstimateGasForEvmCallType( WithTransientKVGasConfig(storetypes.GasConfig{}) } // pass false to not commit StateDB + txConfig := statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())) rsp, _, err = k.ApplyEvmMsg(tmpCtx, msg, nil, false, cfg, txConfig, false) if err != nil { if errors.Is(err, core.ErrIntrinsicGas) {