Skip to content

Commit 14a26d9

Browse files
authored
eth/gasestimator: fix block overrides in estimate gas (ethereum#34081)
Block overrides were to a great extent ignored by the gasestimator. This PR fixes that.
1 parent fc43170 commit 14a26d9

3 files changed

Lines changed: 46 additions & 28 deletions

File tree

eth/gasestimator/gasestimator.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/ethereum/go-ethereum/core/state"
2828
"github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/core/vm"
30-
"github.com/ethereum/go-ethereum/internal/ethapi/override"
3130
"github.com/ethereum/go-ethereum/log"
3231
"github.com/ethereum/go-ethereum/params"
3332
)
@@ -38,11 +37,12 @@ import (
3837
// these together, it would be excessively hard to test. Splitting the parts out
3938
// allows testing without needing a proper live chain.
4039
type Options struct {
41-
Config *params.ChainConfig // Chain configuration for hard fork selection
42-
Chain core.ChainContext // Chain context to access past block hashes
43-
Header *types.Header // Header defining the block context to execute in
44-
State *state.StateDB // Pre-state on top of which to estimate the gas
45-
BlockOverrides *override.BlockOverrides // Block overrides to apply during the estimation
40+
Config *params.ChainConfig // Chain configuration for hard fork selection
41+
Chain core.ChainContext // Chain context to access past block hashes
42+
Header *types.Header // Header defining the block context to execute in
43+
State *state.StateDB // Pre-state on top of which to estimate the gas
44+
45+
BlobBaseFee *big.Int // BlobBaseFee optionally overrides the blob base fee in the execution context.
4646

4747
ErrorRatio float64 // Allowed overestimation ratio for faster estimation termination
4848
}
@@ -64,16 +64,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
6464

6565
// Cap the maximum gas allowance according to EIP-7825 if the estimation targets Osaka
6666
if hi > params.MaxTxGas {
67-
blockNumber, blockTime := opts.Header.Number, opts.Header.Time
68-
if opts.BlockOverrides != nil {
69-
if opts.BlockOverrides.Number != nil {
70-
blockNumber = opts.BlockOverrides.Number.ToInt()
71-
}
72-
if opts.BlockOverrides.Time != nil {
73-
blockTime = uint64(*opts.BlockOverrides.Time)
74-
}
75-
}
76-
if opts.Config.IsOsaka(blockNumber, blockTime) {
67+
if opts.Config.IsOsaka(opts.Header.Number, opts.Header.Time) {
7768
hi = params.MaxTxGas
7869
}
7970
}
@@ -241,10 +232,8 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio
241232
evmContext = core.NewEVMBlockContext(opts.Header, opts.Chain, nil)
242233
dirtyState = opts.State.Copy()
243234
)
244-
if opts.BlockOverrides != nil {
245-
if err := opts.BlockOverrides.Apply(&evmContext); err != nil {
246-
return nil, err
247-
}
235+
if opts.BlobBaseFee != nil {
236+
evmContext.BlobBaseFee = new(big.Int).Set(opts.BlobBaseFee)
248237
}
249238
// Lower the basefee to 0 to avoid breaking EVM
250239
// invariants (basefee < feecap).

internal/ethapi/api.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,20 +897,25 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
897897
if err := blockOverrides.Apply(&blockCtx); err != nil {
898898
return 0, err
899899
}
900+
header = blockOverrides.MakeHeader(header)
900901
}
901902
rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time)
902903
precompiles := vm.ActivePrecompiledContracts(rules)
903904
if err := overrides.Apply(state, precompiles); err != nil {
904905
return 0, err
905906
}
906907
// Construct the gas estimator option from the user input
908+
var blobBaseFee *big.Int
909+
if blockOverrides != nil && blockOverrides.BlobBaseFee != nil {
910+
blobBaseFee = blockOverrides.BlobBaseFee.ToInt()
911+
}
907912
opts := &gasestimator.Options{
908-
Config: b.ChainConfig(),
909-
Chain: NewChainContext(ctx, b),
910-
Header: header,
911-
BlockOverrides: blockOverrides,
912-
State: state,
913-
ErrorRatio: estimateGasErrorRatio,
913+
Config: b.ChainConfig(),
914+
Chain: NewChainContext(ctx, b),
915+
Header: header,
916+
State: state,
917+
BlobBaseFee: blobBaseFee,
918+
ErrorRatio: estimateGasErrorRatio,
914919
}
915920
// Set any required transaction default, but make sure the gas cap itself is not messed with
916921
// if it was not specified in the original argument list.

internal/ethapi/api_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,17 @@ func TestEstimateGas(t *testing.T) {
780780
expectErr: core.ErrInsufficientFunds,
781781
want: 21000,
782782
},
783+
// block override gas limit should bound estimation search space.
784+
{
785+
blockNumber: rpc.LatestBlockNumber,
786+
call: TransactionArgs{
787+
From: &accounts[0].addr,
788+
Input: hex2Bytes("6080604052348015600f57600080fd5b50483a1015601c57600080fd5b60003a111560315760004811603057600080fd5b5b603f80603e6000396000f3fe6080604052600080fdfea264697066735822122060729c2cee02b10748fae5200f1c9da4661963354973d9154c13a8e9ce9dee1564736f6c63430008130033"),
789+
Gas: func() *hexutil.Uint64 { v := hexutil.Uint64(0); return &v }(),
790+
},
791+
blockOverrides: override.BlockOverrides{GasLimit: func() *hexutil.Uint64 { v := hexutil.Uint64(50000); return &v }()},
792+
expectErr: errors.New("gas required exceeds allowance (50000)"),
793+
},
783794
// empty create
784795
{
785796
blockNumber: rpc.LatestBlockNumber,
@@ -861,6 +872,19 @@ func TestEstimateGas(t *testing.T) {
861872
},
862873
want: 21000,
863874
},
875+
// blob base fee block override should be applied during estimation.
876+
{
877+
blockNumber: rpc.LatestBlockNumber,
878+
call: TransactionArgs{
879+
From: &accounts[0].addr,
880+
To: &accounts[1].addr,
881+
Value: (*hexutil.Big)(big.NewInt(1)),
882+
BlobHashes: []common.Hash{{0x01, 0x22}},
883+
BlobFeeCap: (*hexutil.Big)(big.NewInt(1)),
884+
},
885+
blockOverrides: override.BlockOverrides{BlobBaseFee: (*hexutil.Big)(big.NewInt(2))},
886+
expectErr: core.ErrBlobFeeCapTooLow,
887+
},
864888
// // SPDX-License-Identifier: GPL-3.0
865889
//pragma solidity >=0.8.2 <0.9.0;
866890
//
@@ -1014,7 +1038,7 @@ func TestCall(t *testing.T) {
10141038
Balance: big.NewInt(params.Ether),
10151039
Nonce: 1,
10161040
Storage: map[common.Hash]common.Hash{
1017-
common.Hash{}: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
1041+
{}: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
10181042
},
10191043
},
10201044
},
@@ -3795,7 +3819,7 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
37953819
Balance: (*hexutil.Big)(big.NewInt(1000000000000000000)),
37963820
Nonce: &nonce,
37973821
State: map[common.Hash]common.Hash{
3798-
common.Hash{}: common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000002a"),
3822+
{}: common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000002a"),
37993823
},
38003824
},
38013825
}

0 commit comments

Comments
 (0)