Skip to content

Commit

Permalink
refactor(gas): simplify EstimateGasForEvmCallType
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Nov 2, 2024
1 parent e53d6ad commit dfaf522
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Check warning on line 329 in x/evm/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/grpc_query.go#L328-L329

Added lines #L328 - L329 were not covered by tests

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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit dfaf522

Please sign in to comment.