Skip to content

Commit

Permalink
Merge branch 'fix/local-gas' of github.com:NibiruChain/nibiru into fi…
Browse files Browse the repository at this point in the history
…x/local-gas
  • Loading branch information
onikonychev committed Nov 4, 2024
2 parents f688bae + dfaf522 commit fe15859
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 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 All @@ -438,24 +437,27 @@ func (k Keeper) EstimateGasForEvmCallType(
return nil, err
}

// The gas limit is now the highest gas limit that results in an executable transaction
// Reject the transaction as invalid if it still fails at the highest allowance
if hi == gasCap {
failed, result, err := executable(hi)
if err != nil {
return nil, fmt.Errorf("eth call exec error: %w", err)
}

if failed {
if result != nil && result.VmError != vm.ErrOutOfGas.Error() {
if result.VmError == vm.ErrExecutionReverted.Error() {
return nil, fmt.Errorf("VMError: %w", evm.NewExecErrorWithReason(result.Ret))
}
return nil, fmt.Errorf("VMError: %s", result.VmError)
if failed && result != nil {
if result.VmError == vm.ErrExecutionReverted.Error() {
return nil, fmt.Errorf("Estimate gas VMError: %w", evm.NewExecErrorWithReason(result.Ret))
}

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

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/grpc_query.go#L450-L451

Added lines #L450 - L451 were not covered by tests

if result.VmError == vm.ErrOutOfGas.Error() {
return nil, fmt.Errorf("gas required exceeds allowance (%d)", gasCap)

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

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/grpc_query.go#L454

Added line #L454 was not covered by tests
}
// Otherwise, the specified gas cap is too low
return nil, fmt.Errorf("gas required exceeds allowance (%d)", gasCap)

return nil, fmt.Errorf("Estimgate gas VMError: %s", result.VmError)
}
}

return &evm.EstimateGasResponse{Gas: hi}, nil
}

Expand Down

0 comments on commit fe15859

Please sign in to comment.