Skip to content

Commit 88cbfab

Browse files
antonydenyers1na
andauthored
internal/ethapi: add block override to estimateGas (#30695)
Add block overrides to `eth_estimateGas` to align consistency with `eth_call`. #27800 (comment) Fixes #28175 --------- Co-authored-by: Sina Mahmoodi <[email protected]>
1 parent c1c2507 commit 88cbfab

File tree

13 files changed

+545
-375
lines changed

13 files changed

+545
-375
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ profile.cov
3737

3838
# IdeaIDE
3939
.idea
40+
*.iml
4041

4142
# VS Code
4243
.vscode

eth/gasestimator/gasestimator.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/core/state"
2929
"github.com/ethereum/go-ethereum/core/types"
3030
"github.com/ethereum/go-ethereum/core/vm"
31+
"github.com/ethereum/go-ethereum/internal/ethapi/override"
3132
"github.com/ethereum/go-ethereum/log"
3233
"github.com/ethereum/go-ethereum/params"
3334
)
@@ -38,10 +39,11 @@ import (
3839
// these together, it would be excessively hard to test. Splitting the parts out
3940
// allows testing without needing a proper live chain.
4041
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
42+
Config *params.ChainConfig // Chain configuration for hard fork selection
43+
Chain core.ChainContext // Chain context to access past block hashes
44+
Header *types.Header // Header defining the block context to execute in
45+
State *state.StateDB // Pre-state on top of which to estimate the gas
46+
BlockOverrides *override.BlockOverrides // Block overrides to apply during the estimation
4547

4648
ErrorRatio float64 // Allowed overestimation ratio for faster estimation termination
4749
}
@@ -220,6 +222,9 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio
220222
evmContext = core.NewEVMBlockContext(opts.Header, opts.Chain, nil)
221223
dirtyState = opts.State.Copy()
222224
)
225+
if opts.BlockOverrides != nil {
226+
opts.BlockOverrides.Apply(&evmContext)
227+
}
223228
// Lower the basefee to 0 to avoid breaking EVM
224229
// invariants (basefee < feecap).
225230
if call.GasPrice.Sign() == 0 {

eth/tracers/api.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/ethereum/go-ethereum/eth/tracers/logger"
4040
"github.com/ethereum/go-ethereum/ethdb"
4141
"github.com/ethereum/go-ethereum/internal/ethapi"
42+
"github.com/ethereum/go-ethereum/internal/ethapi/override"
4243
"github.com/ethereum/go-ethereum/log"
4344
"github.com/ethereum/go-ethereum/params"
4445
"github.com/ethereum/go-ethereum/rlp"
@@ -163,8 +164,8 @@ type TraceConfig struct {
163164
// field to override the state for tracing.
164165
type TraceCallConfig struct {
165166
TraceConfig
166-
StateOverrides *ethapi.StateOverride
167-
BlockOverrides *ethapi.BlockOverrides
167+
StateOverrides *override.StateOverride
168+
BlockOverrides *override.BlockOverrides
168169
TxIndex *hexutil.Uint
169170
}
170171

eth/tracers/api_test.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"github.com/ethereum/go-ethereum/eth/tracers/logger"
4545
"github.com/ethereum/go-ethereum/ethdb"
4646
"github.com/ethereum/go-ethereum/internal/ethapi"
47+
"github.com/ethereum/go-ethereum/internal/ethapi/override"
4748
"github.com/ethereum/go-ethereum/params"
4849
"github.com/ethereum/go-ethereum/rpc"
4950
)
@@ -454,7 +455,7 @@ func TestTraceCall(t *testing.T) {
454455
Input: &hexutil.Bytes{0x43}, // blocknumber
455456
},
456457
config: &TraceCallConfig{
457-
BlockOverrides: &ethapi.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
458+
BlockOverrides: &override.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
458459
},
459460
expectErr: nil,
460461
expect: ` {"gas":53018,"failed":false,"returnValue":"","structLogs":[
@@ -698,8 +699,8 @@ func TestTracingWithOverrides(t *testing.T) {
698699
Value: (*hexutil.Big)(big.NewInt(1000)),
699700
},
700701
config: &TraceCallConfig{
701-
StateOverrides: &ethapi.StateOverride{
702-
randomAccounts[0].addr: ethapi.OverrideAccount{Balance: newRPCBalance(new(big.Int).Mul(big.NewInt(1), big.NewInt(params.Ether)))},
702+
StateOverrides: &override.StateOverride{
703+
randomAccounts[0].addr: override.OverrideAccount{Balance: newRPCBalance(new(big.Int).Mul(big.NewInt(1), big.NewInt(params.Ether)))},
703704
},
704705
},
705706
want: `{"gas":21000,"failed":false,"returnValue":""}`,
@@ -740,8 +741,8 @@ func TestTracingWithOverrides(t *testing.T) {
740741
},
741742
config: &TraceCallConfig{
742743
//Tracer: &tracer,
743-
StateOverrides: &ethapi.StateOverride{
744-
randomAccounts[2].addr: ethapi.OverrideAccount{
744+
StateOverrides: &override.StateOverride{
745+
randomAccounts[2].addr: override.OverrideAccount{
745746
Code: newRPCBytes(common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060285760003560e01c80638381f58a14602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea2646970667358221220eab35ffa6ab2adfe380772a48b8ba78e82a1b820a18fcb6f59aa4efb20a5f60064736f6c63430007040033")),
746747
StateDiff: newStates([]common.Hash{{}}, []common.Hash{common.BigToHash(big.NewInt(123))}),
747748
},
@@ -757,7 +758,7 @@ func TestTracingWithOverrides(t *testing.T) {
757758
Input: newRPCBytes(common.Hex2Bytes("4360005260206000f3")),
758759
},
759760
config: &TraceCallConfig{
760-
BlockOverrides: &ethapi.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
761+
BlockOverrides: &override.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
761762
},
762763
want: `{"gas":59537,"failed":false,"returnValue":"0000000000000000000000000000000000000000000000000000000000001337"}`,
763764
},
@@ -777,7 +778,7 @@ func TestTracingWithOverrides(t *testing.T) {
777778
}, // blocknumber
778779
},
779780
config: &TraceCallConfig{
780-
BlockOverrides: &ethapi.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
781+
BlockOverrides: &override.BlockOverrides{Number: (*hexutil.Big)(big.NewInt(0x1337))},
781782
},
782783
want: `{"gas":72666,"failed":false,"returnValue":"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}`,
783784
},
@@ -807,8 +808,8 @@ func TestTracingWithOverrides(t *testing.T) {
807808
Data: newRPCBytes(common.Hex2Bytes("f8a8fd6d")), //
808809
},
809810
config: &TraceCallConfig{
810-
StateOverrides: &ethapi.StateOverride{
811-
randomAccounts[2].addr: ethapi.OverrideAccount{
811+
StateOverrides: &override.StateOverride{
812+
randomAccounts[2].addr: override.OverrideAccount{
812813
Code: newRPCBytes(common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060325760003560e01c806366e41cb7146037578063f8a8fd6d14603f575b600080fd5b603d6057565b005b60456062565b60405190815260200160405180910390f35b610539600090815580fd5b60006001600081905550306001600160a01b03166366e41cb76040518163ffffffff1660e01b8152600401600060405180830381600087803b15801560a657600080fd5b505af192505050801560b6575060015b60e9573d80801560e1576040519150601f19603f3d011682016040523d82523d6000602084013e60e6565b606091505b50505b506000549056fea26469706673582212205ce45de745a5308f713cb2f448589177ba5a442d1a2eff945afaa8915961b4d064736f6c634300080c0033")),
813814
},
814815
},
@@ -823,8 +824,8 @@ func TestTracingWithOverrides(t *testing.T) {
823824
Data: newRPCBytes(common.Hex2Bytes("f8a8fd6d")), //
824825
},
825826
config: &TraceCallConfig{
826-
StateOverrides: &ethapi.StateOverride{
827-
randomAccounts[2].addr: ethapi.OverrideAccount{
827+
StateOverrides: &override.StateOverride{
828+
randomAccounts[2].addr: override.OverrideAccount{
828829
Code: newRPCBytes(common.Hex2Bytes("6080604052348015600f57600080fd5b506004361060325760003560e01c806366e41cb7146037578063f8a8fd6d14603f575b600080fd5b603d6057565b005b60456062565b60405190815260200160405180910390f35b610539600090815580fd5b60006001600081905550306001600160a01b03166366e41cb76040518163ffffffff1660e01b8152600401600060405180830381600087803b15801560a657600080fd5b505af192505050801560b6575060015b60e9573d80801560e1576040519150601f19603f3d011682016040523d82523d6000602084013e60e6565b606091505b50505b506000549056fea26469706673582212205ce45de745a5308f713cb2f448589177ba5a442d1a2eff945afaa8915961b4d064736f6c634300080c0033")),
829830
State: newStates([]common.Hash{{}}, []common.Hash{{}}),
830831
},
@@ -841,8 +842,8 @@ func TestTracingWithOverrides(t *testing.T) {
841842
Data: newRPCBytes(common.Hex2Bytes("f8a8fd6d")), //
842843
},
843844
config: &TraceCallConfig{
844-
StateOverrides: &ethapi.StateOverride{
845-
storageAccount: ethapi.OverrideAccount{
845+
StateOverrides: &override.StateOverride{
846+
storageAccount: override.OverrideAccount{
846847
Code: newRPCBytes([]byte{
847848
// SLOAD(3) + SLOAD(4) (which is 0x77)
848849
byte(vm.PUSH1), 0x04,
@@ -876,8 +877,8 @@ func TestTracingWithOverrides(t *testing.T) {
876877
Data: newRPCBytes(common.Hex2Bytes("f8a8fd6d")), //
877878
},
878879
config: &TraceCallConfig{
879-
StateOverrides: &ethapi.StateOverride{
880-
storageAccount: ethapi.OverrideAccount{
880+
StateOverrides: &override.StateOverride{
881+
storageAccount: override.OverrideAccount{
881882
Code: newRPCBytes([]byte{
882883
// SLOAD(3) + SLOAD(4) (which is now 0x11 + 0x00)
883884
byte(vm.PUSH1), 0x04,
@@ -914,8 +915,8 @@ func TestTracingWithOverrides(t *testing.T) {
914915
Data: newRPCBytes(common.Hex2Bytes("f8a8fd6d")), //
915916
},
916917
config: &TraceCallConfig{
917-
StateOverrides: &ethapi.StateOverride{
918-
storageAccount: ethapi.OverrideAccount{
918+
StateOverrides: &override.StateOverride{
919+
storageAccount: override.OverrideAccount{
919920
Code: newRPCBytes([]byte{
920921
// SLOAD(3) + SLOAD(4) (which is now 0x11 + 0x44)
921922
byte(vm.PUSH1), 0x04,

graphql/graphql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ func (b *Block) Call(ctx context.Context, args struct {
12081208
func (b *Block) EstimateGas(ctx context.Context, args struct {
12091209
Data ethapi.TransactionArgs
12101210
}) (hexutil.Uint64, error) {
1211-
return ethapi.DoEstimateGas(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, b.r.backend.RPCGasCap())
1211+
return ethapi.DoEstimateGas(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, nil, b.r.backend.RPCGasCap())
12121212
}
12131213

12141214
type Pending struct {
@@ -1272,7 +1272,7 @@ func (p *Pending) EstimateGas(ctx context.Context, args struct {
12721272
Data ethapi.TransactionArgs
12731273
}) (hexutil.Uint64, error) {
12741274
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
1275-
return ethapi.DoEstimateGas(ctx, p.r.backend, args.Data, latestBlockNr, nil, p.r.backend.RPCGasCap())
1275+
return ethapi.DoEstimateGas(ctx, p.r.backend, args.Data, latestBlockNr, nil, nil, p.r.backend.RPCGasCap())
12761276
}
12771277

12781278
// Resolver is the top-level object in the GraphQL hierarchy.

0 commit comments

Comments
 (0)