Skip to content

Commit eeebb07

Browse files
authored
internal/ethapi: add state override to estimateGas (#27845)
1 parent d14c07d commit eeebb07

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

graphql/graphql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ func (b *Block) Call(ctx context.Context, args struct {
11301130
func (b *Block) EstimateGas(ctx context.Context, args struct {
11311131
Data ethapi.TransactionArgs
11321132
}) (hexutil.Uint64, error) {
1133-
return ethapi.DoEstimateGas(ctx, b.r.backend, args.Data, *b.numberOrHash, b.r.backend.RPCGasCap())
1133+
return ethapi.DoEstimateGas(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, b.r.backend.RPCGasCap())
11341134
}
11351135

11361136
type Pending struct {
@@ -1194,7 +1194,7 @@ func (p *Pending) EstimateGas(ctx context.Context, args struct {
11941194
Data ethapi.TransactionArgs
11951195
}) (hexutil.Uint64, error) {
11961196
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
1197-
return ethapi.DoEstimateGas(ctx, p.r.backend, args.Data, latestBlockNr, p.r.backend.RPCGasCap())
1197+
return ethapi.DoEstimateGas(ctx, p.r.backend, args.Data, latestBlockNr, nil, p.r.backend.RPCGasCap())
11981198
}
11991199

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

internal/ethapi/api.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ func (s *BlockChainAPI) Call(ctx context.Context, args TransactionArgs, blockNrO
11341134
return result.Return(), result.Err
11351135
}
11361136

1137-
func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap uint64) (hexutil.Uint64, error) {
1137+
func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, gasCap uint64) (hexutil.Uint64, error) {
11381138
// Binary search the gas requirement, as it may be higher than the amount used
11391139
var (
11401140
lo uint64 = params.TxGas - 1
@@ -1176,6 +1176,10 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
11761176
if err != nil {
11771177
return 0, err
11781178
}
1179+
err = overrides.Apply(state)
1180+
if err != nil {
1181+
return 0, err
1182+
}
11791183
balance := state.GetBalance(*args.From) // from can't be nil
11801184
available := new(big.Int).Set(balance)
11811185
if args.Value != nil {
@@ -1221,6 +1225,10 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
12211225
if state == nil || err != nil {
12221226
return 0, err
12231227
}
1228+
err = overrides.Apply(state)
1229+
if err != nil {
1230+
return 0, err
1231+
}
12241232
// Execute the binary search and hone in on an executable gas limit
12251233
for lo+1 < hi {
12261234
s := state.Copy()
@@ -1261,12 +1269,12 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
12611269

12621270
// EstimateGas returns an estimate of the amount of gas needed to execute the
12631271
// given transaction against the current pending block.
1264-
func (s *BlockChainAPI) EstimateGas(ctx context.Context, args TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash) (hexutil.Uint64, error) {
1272+
func (s *BlockChainAPI) EstimateGas(ctx context.Context, args TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash, overrides *StateOverride) (hexutil.Uint64, error) {
12651273
bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
12661274
if blockNrOrHash != nil {
12671275
bNrOrHash = *blockNrOrHash
12681276
}
1269-
return DoEstimateGas(ctx, s.b, args, bNrOrHash, s.b.RPCGasCap())
1277+
return DoEstimateGas(ctx, s.b, args, bNrOrHash, overrides, s.b.RPCGasCap())
12701278
}
12711279

12721280
// RPCMarshalHeader converts the given header to the RPC output .

internal/ethapi/api_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ func TestEstimateGas(t *testing.T) {
560560
var testSuite = []struct {
561561
blockNumber rpc.BlockNumber
562562
call TransactionArgs
563+
overrides StateOverride
563564
expectErr error
564565
want uint64
565566
}{
@@ -592,9 +593,30 @@ func TestEstimateGas(t *testing.T) {
592593
expectErr: nil,
593594
want: 53000,
594595
},
596+
{
597+
blockNumber: rpc.LatestBlockNumber,
598+
call: TransactionArgs{},
599+
overrides: StateOverride{
600+
randomAccounts[0].addr: OverrideAccount{Balance: newRPCBalance(new(big.Int).Mul(big.NewInt(1), big.NewInt(params.Ether)))},
601+
},
602+
expectErr: nil,
603+
want: 53000,
604+
},
605+
{
606+
blockNumber: rpc.LatestBlockNumber,
607+
call: TransactionArgs{
608+
From: &randomAccounts[0].addr,
609+
To: &randomAccounts[1].addr,
610+
Value: (*hexutil.Big)(big.NewInt(1000)),
611+
},
612+
overrides: StateOverride{
613+
randomAccounts[0].addr: OverrideAccount{Balance: newRPCBalance(big.NewInt(0))},
614+
},
615+
expectErr: core.ErrInsufficientFunds,
616+
},
595617
}
596618
for i, tc := range testSuite {
597-
result, err := api.EstimateGas(context.Background(), tc.call, &rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber})
619+
result, err := api.EstimateGas(context.Background(), tc.call, &rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, &tc.overrides)
598620
if tc.expectErr != nil {
599621
if err == nil {
600622
t.Errorf("test %d: want error %v, have nothing", i, tc.expectErr)

internal/ethapi/transaction_args.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
111111
AccessList: args.AccessList,
112112
}
113113
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
114-
estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap())
114+
estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, nil, b.RPCGasCap())
115115
if err != nil {
116116
return err
117117
}

internal/web3ext/web3ext.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ web3._extend({
536536
new web3._extend.Method({
537537
name: 'estimateGas',
538538
call: 'eth_estimateGas',
539-
params: 2,
540-
inputFormatter: [web3._extend.formatters.inputCallFormatter, web3._extend.formatters.inputBlockNumberFormatter],
539+
params: 3,
540+
inputFormatter: [web3._extend.formatters.inputCallFormatter, web3._extend.formatters.inputBlockNumberFormatter, null],
541541
outputFormatter: web3._extend.utils.toDecimal
542542
}),
543543
new web3._extend.Method({

0 commit comments

Comments
 (0)