internal/ethapi: support unlimited rpc gas cap in eth_createAccessList (#28846)

This commit is contained in:
Daniel Liu 2024-06-14 15:27:55 +08:00
parent 6b8dd86b52
commit bc14c672f2
2 changed files with 36 additions and 33 deletions

View file

@ -440,7 +440,7 @@ func (s *PrivateAccountAPI) signTransaction(ctx context.Context, args *Transacti
return nil, err return nil, err
} }
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err return nil, err
} }
// Assemble the transaction and sign with the wallet // Assemble the transaction and sign with the wallet
@ -2026,14 +2026,8 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
} }
owner := common.Address{} owner := common.Address{}
// If the gas amount is not set, default to RPC gas cap.
if args.Gas == nil {
tmp := hexutil.Uint64(b.RPCGasCap())
args.Gas = &tmp
}
// Ensure any missing fields are filled, extract the recipient and input data // Ensure any missing fields are filled, extract the recipient and input data
if err := args.setDefaults(ctx, b); err != nil { if err := args.setDefaults(ctx, b, true); err != nil {
return nil, 0, nil, err return nil, 0, nil, err
} }
var to common.Address var to common.Address
@ -2365,7 +2359,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
} }
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
// Assemble the transaction and sign with the wallet // Assemble the transaction and sign with the wallet
@ -2387,7 +2381,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
// processing (signing + broadcast). // processing (signing + broadcast).
func (s *PublicTransactionPoolAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) { func (s *PublicTransactionPoolAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err return nil, err
} }
// Assemble the transaction and obtain rlp // Assemble the transaction and obtain rlp
@ -3292,7 +3286,7 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Tra
if args.Nonce == nil { if args.Nonce == nil {
return nil, errors.New("not specify Nonce") return nil, errors.New("not specify Nonce")
} }
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err return nil, err
} }
// Before actually sign the transaction, ensure the transaction fee is reasonable. // Before actually sign the transaction, ensure the transaction fee is reasonable.
@ -3341,7 +3335,7 @@ func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, sendArgs Transact
if sendArgs.Nonce == nil { if sendArgs.Nonce == nil {
return common.Hash{}, errors.New("missing transaction nonce in transaction spec") return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
} }
if err := sendArgs.setDefaults(ctx, s.b); err != nil { if err := sendArgs.setDefaults(ctx, s.b, false); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
matchTx := sendArgs.toTransaction() matchTx := sendArgs.toTransaction()

View file

@ -74,7 +74,7 @@ func (args *TransactionArgs) data() []byte {
} }
// setDefaults fills in default values for unspecified tx fields. // setDefaults fills in default values for unspecified tx fields.
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error { func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGasEstimation bool) error {
if err := args.setFeeDefaults(ctx, b); err != nil { if err := args.setFeeDefaults(ctx, b); err != nil {
return err return err
} }
@ -94,29 +94,38 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
if args.To == nil && len(args.data()) == 0 { if args.To == nil && len(args.data()) == 0 {
return errors.New(`contract creation without any data provided`) return errors.New(`contract creation without any data provided`)
} }
// Estimate the gas usage if necessary.
if args.Gas == nil { if args.Gas == nil {
// These fields are immutable during the estimation, safe to if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
// pass the pointer directly. gas := hexutil.Uint64(b.RPCGasCap())
data := args.data() if gas == 0 {
callArgs := TransactionArgs{ gas = hexutil.Uint64(math.MaxUint64 / 2)
From: args.From, }
To: args.To, args.Gas = &gas
GasPrice: args.GasPrice, } else { // Estimate the gas usage otherwise.
MaxFeePerGas: args.MaxFeePerGas, // These fields are immutable during the estimation, safe to
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, // pass the pointer directly.
Value: args.Value, data := args.data()
Data: (*hexutil.Bytes)(&data), callArgs := TransactionArgs{
AccessList: args.AccessList, From: args.From,
To: args.To,
GasPrice: args.GasPrice,
MaxFeePerGas: args.MaxFeePerGas,
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
Value: args.Value,
Data: (*hexutil.Bytes)(&data),
AccessList: args.AccessList,
}
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, b.RPCGasCap())
if err != nil {
return err
}
args.Gas = &estimated
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
} }
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, nil, b.RPCGasCap())
if err != nil {
return err
}
args.Gas = &estimated
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
} }
// If chain id is provided, ensure it matches the local chain id. Otherwise, set the local // If chain id is provided, ensure it matches the local chain id. Otherwise, set the local
// chain id as the default. // chain id as the default.
want := b.ChainConfig().ChainId want := b.ChainConfig().ChainId