Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonrpc: Gas estimate to optimize for max contract gas used #13913

Merged
merged 6 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*evmtype
SenderInitBalance: senderInitBalance,
CoinbaseInitBalance: coinbaseInitBalance,
FeeTipped: amount,
EvmRefund: st.state.GetRefund(),
EvmGasUsed: st.gasUsed(),
}

if st.evm.Context.PostApplyMessage != nil {
Expand Down
2 changes: 2 additions & 0 deletions core/vm/evmtypes/evmtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type ExecutionResult struct {
SenderInitBalance *uint256.Int
CoinbaseInitBalance *uint256.Int
FeeTipped *uint256.Int
EvmRefund uint64 // Gas refunded by EVM without considering refundQuotient
EvmGasUsed uint64 // Gas used by the execution of all instructions only
}

// Unwrap returns the internal evm error which allows us for further
Expand Down
71 changes: 29 additions & 42 deletions turbo/jsonrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/core/vm"
"github.com/erigontech/erigon/core/vm/evmtypes"
"github.com/erigontech/erigon/eth/stagedsync"
"github.com/erigontech/erigon/eth/tracers/logger"
"github.com/erigontech/erigon/params"
Expand Down Expand Up @@ -188,9 +187,8 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs

// Binary search the gas requirement, as it may be higher than the amount used
var (
lo = params.TxGas - 1
hi uint64
gasCap uint64
lo uint64
hi uint64
)
// Use zero address if sender unspecified.
if args.From == nil {
Expand All @@ -204,6 +202,11 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
// Retrieve the block to act as the gas ceiling
hi = header.GasLimit
}
// Recap the highest gas allowance with specified gascap.
if hi > api.GasCap {
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", api.GasCap)
hi = api.GasCap
}

var feeCap *big.Int
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
Expand Down Expand Up @@ -247,65 +250,49 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
}
}

// Recap the highest gas allowance with specified gascap.
if hi > api.GasCap {
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", api.GasCap)
hi = api.GasCap
}
gasCap = hi

caller, err := transactions.NewReusableCaller(engine, stateReader, overrides, header, args, api.GasCap, *blockNrOrHash, dbtx, api._blockReader, chainConfig, api.evmCallTimeout)
if err != nil {
return 0, err
}
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) (bool, *evmtypes.ExecutionResult, error) {
result, err := caller.DoCallWithNewGas(ctx, gas, engine, overrides)
if err != nil {
if errors.Is(err, core.ErrIntrinsicGas) {
// Special case, raise gas limit
return true, nil, nil
}

// Bail out
return true, nil, err
// First try with highest gas possible
result, err := caller.DoCallWithNewGas(ctx, hi, engine, overrides)
if err != nil || result == nil {
return 0, err
}
if result.Failed() {
if !errors.Is(result.Err, vm.ErrOutOfGas) {
if len(result.Revert()) > 0 {
return 0, ethapi2.NewRevertError(result)
}
return 0, result.Err
}
return result.Failed(), result, nil
// Otherwise, the specified gas cap is too low
return 0, fmt.Errorf("gas required exceeds allowance (%d)", hi)
}
// Assuming a contract can freely run all the instructions, we have
// the true amount of gas it wants to consume to execute fully.
// We want to ensure that the gas used doesn't fall below this
trueGas := result.EvmGasUsed // Must not fall below this
lo = min(trueGas+result.EvmRefund-1, params.TxGas-1)

i := 0
// Execute the binary search and hone in on an executable gas limit
for lo+1 < hi {
mid := (hi + lo) / 2
failed, _, err := executable(mid)
result, err := caller.DoCallWithNewGas(ctx, mid, engine, overrides)
// If the error is not nil(consensus error), it means the provided message
// call or transaction will never be accepted no matter how much gas it is
// assigened. Return the error directly, don't struggle any more.
if err != nil {
return 0, err
}
if failed {
if result.Failed() || result.EvmGasUsed < trueGas {
lo = mid
} else {
hi = mid
}
}

// 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 0, err
}
if failed {
if result != nil && !errors.Is(result.Err, vm.ErrOutOfGas) {
if len(result.Revert()) > 0 {
return 0, ethapi2.NewRevertError(result)
}
return 0, result.Err
}
// Otherwise, the specified gas cap is too low
return 0, fmt.Errorf("gas required exceeds allowance (%d)", gasCap)
}
i++
}
return hexutil.Uint64(hi), nil
}
Expand Down
Loading