Revert "Move gasestimator into the ethapi package"

This reverts commit c5af175792.
This commit is contained in:
Sina Mahmoodi 2024-11-27 16:54:32 +01:00
parent c5af175792
commit 6475f5a24f
2 changed files with 13 additions and 15 deletions

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package ethapi package gasestimator
import ( import (
"context" "context"
@ -49,7 +49,7 @@ type Options struct {
// Estimate returns the lowest possible gas limit that allows the transaction to // Estimate returns the lowest possible gas limit that allows the transaction to
// run successfully with the provided context options. It returns an error if the // run successfully with the provided context options. It returns an error if the
// transaction would always revert, or if there are unexpected failures. // transaction would always revert, or if there are unexpected failures.
func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uint64, overrides *BlockOverrides) (uint64, []byte, error) { func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uint64) (uint64, []byte, error) {
// Binary search the gas limit, as it may need to be higher than the amount used // Binary search the gas limit, as it may need to be higher than the amount used
var ( var (
lo uint64 // lowest-known gas limit where tx execution fails lo uint64 // lowest-known gas limit where tx execution fails
@ -114,7 +114,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
// unused access list items). Ever so slightly wasteful, but safer overall. // unused access list items). Ever so slightly wasteful, but safer overall.
if len(call.Data) == 0 { if len(call.Data) == 0 {
if call.To != nil && opts.State.GetCodeSize(*call.To) == 0 { if call.To != nil && opts.State.GetCodeSize(*call.To) == 0 {
failed, _, err := execute(ctx, call, opts, params.TxGas, nil) failed, _, err := execute(ctx, call, opts, params.TxGas)
if !failed && err == nil { if !failed && err == nil {
return params.TxGas, nil, nil return params.TxGas, nil, nil
} }
@ -122,7 +122,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
} }
// We first execute the transaction at the highest allowable gas limit, since if this fails we // We first execute the transaction at the highest allowable gas limit, since if this fails we
// can return error immediately. // can return error immediately.
failed, result, err := execute(ctx, call, opts, hi, overrides) failed, result, err := execute(ctx, call, opts, hi)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
@ -144,7 +144,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
// check that gas amount and use as a limit for the binary search. // check that gas amount and use as a limit for the binary search.
optimisticGasLimit := (result.UsedGas + result.RefundedGas + params.CallStipend) * 64 / 63 optimisticGasLimit := (result.UsedGas + result.RefundedGas + params.CallStipend) * 64 / 63
if optimisticGasLimit < hi { if optimisticGasLimit < hi {
failed, _, err = execute(ctx, call, opts, optimisticGasLimit, nil) failed, _, err = execute(ctx, call, opts, optimisticGasLimit)
if err != nil { if err != nil {
// This should not happen under normal conditions since if we make it this far the // This should not happen under normal conditions since if we make it this far the
// transaction had run without error at least once before. // transaction had run without error at least once before.
@ -175,7 +175,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
// range here is skewed to favor the low side. // range here is skewed to favor the low side.
mid = lo * 2 mid = lo * 2
} }
failed, _, err = execute(ctx, call, opts, mid, nil) failed, _, err = execute(ctx, call, opts, mid)
if err != nil { if err != nil {
// This should not happen under normal conditions since if we make it this far the // This should not happen under normal conditions since if we make it this far the
// transaction had run without error at least once before. // transaction had run without error at least once before.
@ -195,14 +195,14 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
// returns true if the transaction fails for a reason that might be related to // 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 // not enough gas. A non-nil error means execution failed due to reasons unrelated
// to the gas limit. // to the gas limit.
func execute(ctx context.Context, call *core.Message, opts *Options, gasLimit uint64, overrides *BlockOverrides) (bool, *core.ExecutionResult, error) { func execute(ctx context.Context, call *core.Message, opts *Options, gasLimit uint64) (bool, *core.ExecutionResult, error) {
// Configure the call for this specific execution (and revert the change after) // Configure the call for this specific execution (and revert the change after)
defer func(gas uint64) { call.GasLimit = gas }(call.GasLimit) defer func(gas uint64) { call.GasLimit = gas }(call.GasLimit)
call.GasLimit = gasLimit call.GasLimit = gasLimit
// Execute the call and separate execution faults caused by a lack of gas or // Execute the call and separate execution faults caused by a lack of gas or
// other non-fixable conditions // other non-fixable conditions
result, err := run(ctx, call, opts, overrides) result, err := run(ctx, call, opts)
if err != nil { if err != nil {
if errors.Is(err, core.ErrIntrinsicGas) { if errors.Is(err, core.ErrIntrinsicGas) {
return true, nil, nil // Special case, raise gas limit return true, nil, nil // Special case, raise gas limit
@ -214,7 +214,7 @@ func execute(ctx context.Context, call *core.Message, opts *Options, gasLimit ui
// run assembles the EVM as defined by the consensus rules and runs the requested // run assembles the EVM as defined by the consensus rules and runs the requested
// call invocation. // call invocation.
func run(ctx context.Context, call *core.Message, opts *Options, overrides *BlockOverrides) (*core.ExecutionResult, error) { func run(ctx context.Context, call *core.Message, opts *Options) (*core.ExecutionResult, error) {
// Assemble the call and the call context // Assemble the call and the call context
var ( var (
msgContext = core.NewEVMTxContext(call) msgContext = core.NewEVMTxContext(call)
@ -222,9 +222,6 @@ func run(ctx context.Context, call *core.Message, opts *Options, overrides *Bloc
dirtyState = opts.State.Copy() dirtyState = opts.State.Copy()
) )
overrides.Apply(&evmContext)
// Lower the basefee to 0 to avoid breaking EVM // Lower the basefee to 0 to avoid breaking EVM
// invariants (basefee < feecap). // invariants (basefee < feecap).
if msgContext.GasPrice.Sign() == 0 { if msgContext.GasPrice.Sign() == 0 {

View file

@ -40,6 +40,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "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/eth/tracers/logger"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
@ -990,10 +991,10 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
return 0, err return 0, err
} }
// Construct the gas estimator option from the user input // Construct the gas estimator option from the user input
opts := &Options{ opts := &gasestimator.Options{
Config: b.ChainConfig(), Config: b.ChainConfig(),
Chain: chainContext, Chain: chainContext,
Header: header, Header: blockOverrides.MakeHeader(header),
State: state, State: state,
ErrorRatio: estimateGasErrorRatio, ErrorRatio: estimateGasErrorRatio,
} }
@ -1008,7 +1009,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
call := args.ToMessage(header.BaseFee, true, true) call := args.ToMessage(header.BaseFee, true, true)
// Run the gas estimation and wrap any revertals into a custom return // Run the gas estimation and wrap any revertals into a custom return
estimate, revert, err := Estimate(ctx, call, opts, gasCap, blockOverrides) estimate, revert, err := gasestimator.Estimate(ctx, call, opts, gasCap)
if err != nil { if err != nil {
if len(revert) > 0 { if len(revert) > 0 {
return 0, newRevertError(revert) return 0, newRevertError(revert)