forked from cerc-io/plugeth
eth/gasestimator, internal/ethapi: move gas estimator out of rpc (#28600)
This commit is contained in:
+24
-125
@@ -40,6 +40,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/eth/gasestimator"
|
||||
"github.com/ethereum/go-ethereum/eth/tracers/logger"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
@@ -1120,15 +1121,16 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
|
||||
return doCall(ctx, b, args, state, header, overrides, blockOverrides, timeout, globalGasCap)
|
||||
}
|
||||
|
||||
func newRevertError(result *core.ExecutionResult) *revertError {
|
||||
reason, errUnpack := abi.UnpackRevert(result.Revert())
|
||||
err := errors.New("execution reverted")
|
||||
func newRevertError(revert []byte) *revertError {
|
||||
err := vm.ErrExecutionReverted
|
||||
|
||||
reason, errUnpack := abi.UnpackRevert(revert)
|
||||
if errUnpack == nil {
|
||||
err = fmt.Errorf("execution reverted: %v", reason)
|
||||
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
|
||||
}
|
||||
return &revertError{
|
||||
error: err,
|
||||
reason: hexutil.Encode(result.Revert()),
|
||||
reason: hexutil.Encode(revert),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1167,147 +1169,44 @@ func (s *BlockChainAPI) Call(ctx context.Context, args TransactionArgs, blockNrO
|
||||
}
|
||||
// If the result contains a revert reason, try to unpack and return it.
|
||||
if len(result.Revert()) > 0 {
|
||||
return nil, newRevertError(result)
|
||||
return nil, newRevertError(result.Revert())
|
||||
}
|
||||
return result.Return(), result.Err
|
||||
}
|
||||
|
||||
// executeEstimate is a helper that executes the transaction under a given gas limit and returns
|
||||
// true if the transaction fails for a reason that might be related to not enough gas. A non-nil
|
||||
// error means execution failed due to reasons unrelated to the gas limit.
|
||||
func executeEstimate(ctx context.Context, b Backend, args TransactionArgs, state *state.StateDB, header *types.Header, gasCap uint64, gasLimit uint64) (bool, *core.ExecutionResult, error) {
|
||||
args.Gas = (*hexutil.Uint64)(&gasLimit)
|
||||
result, err := doCall(ctx, b, args, state, header, nil, nil, 0, gasCap)
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrIntrinsicGas) {
|
||||
return true, nil, nil // Special case, raise gas limit
|
||||
}
|
||||
return true, nil, err // Bail out
|
||||
}
|
||||
return result.Failed(), result, nil
|
||||
}
|
||||
|
||||
// DoEstimateGas returns the lowest possible gas limit that allows the transaction to run
|
||||
// successfully at block `blockNrOrHash`. It returns error if the transaction would revert, or if
|
||||
// there are unexpected failures. The gas limit is capped by both `args.Gas` (if non-nil &
|
||||
// non-zero) and `gasCap` (if non-zero).
|
||||
func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, gasCap uint64) (hexutil.Uint64, error) {
|
||||
// Binary search the gas limit, as it may need to be higher than the amount used
|
||||
var (
|
||||
lo uint64 // lowest-known gas limit where tx execution fails
|
||||
hi uint64 // lowest-known gas limit where tx execution succeeds
|
||||
)
|
||||
// Use zero address if sender unspecified.
|
||||
if args.From == nil {
|
||||
args.From = new(common.Address)
|
||||
}
|
||||
// Determine the highest gas limit can be used during the estimation.
|
||||
if args.Gas != nil && uint64(*args.Gas) >= params.TxGas {
|
||||
hi = uint64(*args.Gas)
|
||||
} else {
|
||||
// Retrieve the block to act as the gas ceiling
|
||||
block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if block == nil {
|
||||
return 0, errors.New("block not found")
|
||||
}
|
||||
hi = block.GasLimit()
|
||||
}
|
||||
// Normalize the max fee per gas the call is willing to spend.
|
||||
var feeCap *big.Int
|
||||
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
|
||||
return 0, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
||||
} else if args.GasPrice != nil {
|
||||
feeCap = args.GasPrice.ToInt()
|
||||
} else if args.MaxFeePerGas != nil {
|
||||
feeCap = args.MaxFeePerGas.ToInt()
|
||||
} else {
|
||||
feeCap = common.Big0
|
||||
}
|
||||
|
||||
// Retrieve the base state and mutate it with any overrides
|
||||
state, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
||||
if state == nil || err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := overrides.Apply(state); err != nil {
|
||||
if err = overrides.Apply(state); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Recap the highest gas limit with account's available balance.
|
||||
if feeCap.BitLen() != 0 {
|
||||
balance := state.GetBalance(*args.From) // from can't be nil
|
||||
available := new(big.Int).Set(balance)
|
||||
if args.Value != nil {
|
||||
if args.Value.ToInt().Cmp(available) >= 0 {
|
||||
return 0, core.ErrInsufficientFundsForTransfer
|
||||
}
|
||||
available.Sub(available, args.Value.ToInt())
|
||||
}
|
||||
allowance := new(big.Int).Div(available, feeCap)
|
||||
|
||||
// If the allowance is larger than maximum uint64, skip checking
|
||||
if allowance.IsUint64() && hi > allowance.Uint64() {
|
||||
transfer := args.Value
|
||||
if transfer == nil {
|
||||
transfer = new(hexutil.Big)
|
||||
}
|
||||
log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
|
||||
"sent", transfer.ToInt(), "maxFeePerGas", feeCap, "fundable", allowance)
|
||||
hi = allowance.Uint64()
|
||||
}
|
||||
// Construct the gas estimator option from the user input
|
||||
opts := &gasestimator.Options{
|
||||
Config: b.ChainConfig(),
|
||||
Chain: NewChainContext(ctx, b),
|
||||
Header: header,
|
||||
State: state,
|
||||
}
|
||||
// Recap the highest gas allowance with specified gascap.
|
||||
if gasCap != 0 && hi > gasCap {
|
||||
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", gasCap)
|
||||
hi = gasCap
|
||||
}
|
||||
|
||||
// We first execute the transaction at the highest allowable gas limit, since if this fails we
|
||||
// can return error immediately.
|
||||
failed, result, err := executeEstimate(ctx, b, args, state.Copy(), header, gasCap, hi)
|
||||
// Run the gas estimation andwrap any revertals into a custom return
|
||||
call, err := args.ToMessage(gasCap, header.BaseFee)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if failed {
|
||||
if result != nil && !errors.Is(result.Err, vm.ErrOutOfGas) {
|
||||
if len(result.Revert()) > 0 {
|
||||
return 0, newRevertError(result)
|
||||
}
|
||||
return 0, result.Err
|
||||
estimate, revert, err := gasestimator.Estimate(ctx, call, opts, gasCap)
|
||||
if err != nil {
|
||||
if len(revert) > 0 {
|
||||
return 0, newRevertError(revert)
|
||||
}
|
||||
return 0, fmt.Errorf("gas required exceeds allowance (%d)", hi)
|
||||
return 0, err
|
||||
}
|
||||
// For almost any transaction, the gas consumed by the unconstrained execution above
|
||||
// lower-bounds the gas limit required for it to succeed. One exception is those txs that
|
||||
// explicitly check gas remaining in order to successfully execute within a given limit, but we
|
||||
// probably don't want to return a lowest possible gas limit for these cases anyway.
|
||||
lo = result.UsedGas - 1
|
||||
|
||||
// Binary search for the smallest gas limit that allows the tx to execute successfully.
|
||||
for lo+1 < hi {
|
||||
mid := (hi + lo) / 2
|
||||
if mid > lo*2 {
|
||||
// Most txs don't need much higher gas limit than their gas used, and most txs don't
|
||||
// require near the full block limit of gas, so the selection of where to bisect the
|
||||
// range here is skewed to favor the low side.
|
||||
mid = lo * 2
|
||||
}
|
||||
failed, _, err = executeEstimate(ctx, b, args, state.Copy(), header, gasCap, mid)
|
||||
if err != nil {
|
||||
// This should not happen under normal conditions since if we make it this far the
|
||||
// transaction had run without error at least once before.
|
||||
log.Error("execution error in estimate gas", "err", err)
|
||||
return 0, err
|
||||
}
|
||||
if failed {
|
||||
lo = mid
|
||||
} else {
|
||||
hi = mid
|
||||
}
|
||||
}
|
||||
return hexutil.Uint64(hi), nil
|
||||
return hexutil.Uint64(estimate), nil
|
||||
}
|
||||
|
||||
// EstimateGas returns the lowest possible gas limit that allows the transaction to run
|
||||
|
||||
@@ -910,18 +910,18 @@ func TestCall(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
type account struct {
|
||||
key *ecdsa.PrivateKey
|
||||
addr common.Address
|
||||
}
|
||||
|
||||
func newAccounts(n int) (accounts []Account) {
|
||||
func newAccounts(n int) (accounts []account) {
|
||||
for i := 0; i < n; i++ {
|
||||
key, _ := crypto.GenerateKey()
|
||||
addr := crypto.PubkeyToAddress(key.PublicKey)
|
||||
accounts = append(accounts, Account{key: key, addr: addr})
|
||||
accounts = append(accounts, account{key: key, addr: addr})
|
||||
}
|
||||
slices.SortFunc(accounts, func(a, b Account) int { return a.addr.Cmp(b.addr) })
|
||||
slices.SortFunc(accounts, func(a, b account) int { return a.addr.Cmp(b.addr) })
|
||||
return accounts
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user