Skip to content

Commit 97a59a2

Browse files
committed
internal/ethapi: support unlimited rpc gas cap in eth_createAccessList (ethereum#28846)
1 parent ebfb678 commit 97a59a2

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
@@ -2026,14 +2026,8 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
20262026
}
20272027
owner := common.Address{}
20282028

2029-
// If the gas amount is not set, default to RPC gas cap.
2030-
if args.Gas == nil {
2031-
tmp := hexutil.Uint64(b.RPCGasCap())
2032-
args.Gas = &tmp
2033-
}
2034-
20352029
// Ensure any missing fields are filled, extract the recipient and input data
2036-
if err := args.setDefaults(ctx, b); err != nil {
2030+
if err := args.setDefaults(ctx, b, true); err != nil {
20372031
return nil, 0, nil, err
20382032
}
20392033
var to common.Address
@@ -2365,7 +2359,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
23652359
}
23662360

23672361
// Set some sanity defaults and terminate on failure
2368-
if err := args.setDefaults(ctx, s.b); err != nil {
2362+
if err := args.setDefaults(ctx, s.b, false); err != nil {
23692363
return common.Hash{}, err
23702364
}
23712365
// Assemble the transaction and sign with the wallet
@@ -2387,7 +2381,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
23872381
// processing (signing + broadcast).
23882382
func (s *PublicTransactionPoolAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
23892383
// Set some sanity defaults and terminate on failure
2390-
if err := args.setDefaults(ctx, s.b); err != nil {
2384+
if err := args.setDefaults(ctx, s.b, false); err != nil {
23912385
return nil, err
23922386
}
23932387
// Assemble the transaction and obtain rlp
@@ -3292,7 +3286,7 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Tra
32923286
if args.Nonce == nil {
32933287
return nil, errors.New("not specify Nonce")
32943288
}
3295-
if err := args.setDefaults(ctx, s.b); err != nil {
3289+
if err := args.setDefaults(ctx, s.b, false); err != nil {
32963290
return nil, err
32973291
}
32983292
// Before actually sign the transaction, ensure the transaction fee is reasonable.
@@ -3341,7 +3335,7 @@ func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, sendArgs Transact
33413335
if sendArgs.Nonce == nil {
33423336
return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
33433337
}
3344-
if err := sendArgs.setDefaults(ctx, s.b); err != nil {
3338+
if err := sendArgs.setDefaults(ctx, s.b, false); err != nil {
33453339
return common.Hash{}, err
33463340
}
33473341
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)