mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
pass block gas limit to the gas estimator
This commit is contained in:
parent
86a1f0c394
commit
c8077a1748
2 changed files with 16 additions and 2 deletions
|
|
@ -49,17 +49,21 @@ 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) (uint64, []byte, error) {
|
func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uint64, blockGasLimit 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
|
||||||
hi uint64 // lowest-known gas limit where tx execution succeeds
|
hi uint64 // lowest-known gas limit where tx execution succeeds
|
||||||
)
|
)
|
||||||
|
|
||||||
// Determine the highest gas limit can be used during the estimation.
|
// Determine the highest gas limit can be used during the estimation.
|
||||||
hi = opts.Header.GasLimit
|
hi = opts.Header.GasLimit
|
||||||
if call.GasLimit >= params.TxGas {
|
if call.GasLimit >= params.TxGas {
|
||||||
hi = call.GasLimit
|
hi = call.GasLimit
|
||||||
}
|
}
|
||||||
|
if hi > blockGasLimit {
|
||||||
|
hi = blockGasLimit
|
||||||
|
}
|
||||||
// Normalize the max fee per gas the call is willing to spend.
|
// Normalize the max fee per gas the call is willing to spend.
|
||||||
var feeCap *big.Int
|
var feeCap *big.Int
|
||||||
if call.GasFeeCap != nil {
|
if call.GasFeeCap != nil {
|
||||||
|
|
|
||||||
|
|
@ -1202,9 +1202,19 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
|
||||||
if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil {
|
if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
call := args.ToMessage(header.BaseFee)
|
call := args.ToMessage(header.BaseFee)
|
||||||
|
|
||||||
|
block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if block == nil {
|
||||||
|
return 0, errors.New("block not found")
|
||||||
|
}
|
||||||
// Run the gas estimation andwrap any revertals into a custom return
|
// Run the gas estimation andwrap any revertals into a custom return
|
||||||
estimate, revert, err := gasestimator.Estimate(ctx, call, opts, gasCap)
|
estimate, revert, err := gasestimator.Estimate(ctx, call, opts, gasCap, block.GasLimit())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(revert) > 0 {
|
if len(revert) > 0 {
|
||||||
return 0, newRevertError(revert)
|
return 0, newRevertError(revert)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue