Skip to content

Commit d75736e

Browse files
committed
internal/ethapi: support unlimited rpc gas cap in eth_createAccessList (ethereum#28846)
1 parent 3ca466f commit d75736e

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

internal/ethapi/api.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (s *PrivateAccountAPI) signTransaction(ctx context.Context, args *Transacti
440440
return nil, err
441441
}
442442
// Set some sanity defaults and terminate on failure
443-
if err := args.setDefaults(ctx, s.b); err != nil {
443+
if err := args.setDefaults(ctx, s.b, false); err != nil {
444444
return nil, err
445445
}
446446
// Assemble the transaction and sign with the wallet
@@ -2020,14 +2020,8 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
20202020
}
20212021
owner := common.Address{}
20222022

2023-
// If the gas amount is not set, default to RPC gas cap.
2024-
if args.Gas == nil {
2025-
tmp := hexutil.Uint64(b.RPCGasCap())
2026-
args.Gas = &tmp
2027-
}
2028-
20292023
// Ensure any missing fields are filled, extract the recipient and input data
2030-
if err := args.setDefaults(ctx, b); err != nil {
2024+
if err := args.setDefaults(ctx, b, true); err != nil {
20312025
return nil, 0, nil, err
20322026
}
20332027
var to common.Address
@@ -2359,7 +2353,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
23592353
}
23602354

23612355
// Set some sanity defaults and terminate on failure
2362-
if err := args.setDefaults(ctx, s.b); err != nil {
2356+
if err := args.setDefaults(ctx, s.b, false); err != nil {
23632357
return common.Hash{}, err
23642358
}
23652359
// Assemble the transaction and sign with the wallet
@@ -2381,7 +2375,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
23812375
// processing (signing + broadcast).
23822376
func (s *PublicTransactionPoolAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
23832377
// Set some sanity defaults and terminate on failure
2384-
if err := args.setDefaults(ctx, s.b); err != nil {
2378+
if err := args.setDefaults(ctx, s.b, false); err != nil {
23852379
return nil, err
23862380
}
23872381
// Assemble the transaction and obtain rlp
@@ -3286,7 +3280,7 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Tra
32863280
if args.Nonce == nil {
32873281
return nil, errors.New("not specify Nonce")
32883282
}
3289-
if err := args.setDefaults(ctx, s.b); err != nil {
3283+
if err := args.setDefaults(ctx, s.b, false); err != nil {
32903284
return nil, err
32913285
}
32923286
// Before actually sign the transaction, ensure the transaction fee is reasonable.
@@ -3335,7 +3329,7 @@ func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, sendArgs Transact
33353329
if sendArgs.Nonce == nil {
33363330
return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
33373331
}
3338-
if err := sendArgs.setDefaults(ctx, s.b); err != nil {
3332+
if err := sendArgs.setDefaults(ctx, s.b, false); err != nil {
33393333
return common.Hash{}, err
33403334
}
33413335
matchTx := sendArgs.toTransaction()

internal/ethapi/transaction_args.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (args *TransactionArgs) data() []byte {
7474
}
7575

7676
// setDefaults fills in default values for unspecified tx fields.
77-
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
77+
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGasEstimation bool) error {
7878
if err := args.setFeeDefaults(ctx, b); err != nil {
7979
return err
8080
}
@@ -94,29 +94,38 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
9494
if args.To == nil && len(args.data()) == 0 {
9595
return errors.New(`contract creation without any data provided`)
9696
}
97-
// Estimate the gas usage if necessary.
97+
9898
if args.Gas == nil {
99-
// These fields are immutable during the estimation, safe to
100-
// pass the pointer directly.
101-
data := args.data()
102-
callArgs := TransactionArgs{
103-
From: args.From,
104-
To: args.To,
105-
GasPrice: args.GasPrice,
106-
MaxFeePerGas: args.MaxFeePerGas,
107-
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
108-
Value: args.Value,
109-
Data: (*hexutil.Bytes)(&data),
110-
AccessList: args.AccessList,
111-
}
112-
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
113-
estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, nil, b.RPCGasCap())
114-
if err != nil {
115-
return err
99+
if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
100+
gas := hexutil.Uint64(b.RPCGasCap())
101+
if gas == 0 {
102+
gas = hexutil.Uint64(math.MaxUint64 / 2)
103+
}
104+
args.Gas = &gas
105+
} else { // Estimate the gas usage otherwise.
106+
// These fields are immutable during the estimation, safe to
107+
// pass the pointer directly.
108+
data := args.data()
109+
callArgs := TransactionArgs{
110+
From: args.From,
111+
To: args.To,
112+
GasPrice: args.GasPrice,
113+
MaxFeePerGas: args.MaxFeePerGas,
114+
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
115+
Value: args.Value,
116+
Data: (*hexutil.Bytes)(&data),
117+
AccessList: args.AccessList,
118+
}
119+
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
120+
estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, b.RPCGasCap())
121+
if err != nil {
122+
return err
123+
}
124+
args.Gas = &estimated
125+
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
116126
}
117-
args.Gas = &estimated
118-
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
119127
}
128+
120129
// If chain id is provided, ensure it matches the local chain id. Otherwise, set the local
121130
// chain id as the default.
122131
want := b.ChainConfig().ChainId

0 commit comments

Comments
 (0)