Skip to content

Commit 846badc

Browse files
authored
internal/ethapi: fix transaction APIs (#23179)
* internal/ethapi: fix transaction APIs * internal/ethapi: fix typo * internal/ethapi: address comments * internal/ethapi: address comment from Peter
1 parent 8fe47b0 commit 846badc

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

internal/ethapi/api.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,15 @@ func (s *PrivateAccountAPI) SignTransaction(ctx context.Context, args Transactio
448448
if args.Gas == nil {
449449
return nil, fmt.Errorf("gas not specified")
450450
}
451-
if args.GasPrice == nil {
452-
return nil, fmt.Errorf("gasPrice not specified")
451+
if args.GasPrice == nil && (args.MaxFeePerGas == nil || args.MaxPriorityFeePerGas == nil) {
452+
return nil, fmt.Errorf("missing gasPrice or maxFeePerGas/maxPriorityFeePerGas")
453453
}
454454
if args.Nonce == nil {
455455
return nil, fmt.Errorf("nonce not specified")
456456
}
457457
// Before actually sign the transaction, ensure the transaction fee is reasonable.
458-
if err := checkTxFee(args.GasPrice.ToInt(), uint64(*args.Gas), s.b.RPCTxFeeCap()); err != nil {
458+
tx := args.toTransaction()
459+
if err := checkTxFee(tx.GasPrice(), tx.Gas(), s.b.RPCTxFeeCap()); err != nil {
459460
return nil, err
460461
}
461462
signed, err := s.signTransaction(ctx, &args, passwd)
@@ -1697,8 +1698,9 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Tra
16971698
return SubmitTransaction(ctx, s.b, signed)
16981699
}
16991700

1700-
// FillTransaction fills the defaults (nonce, gas, gasPrice) on a given unsigned transaction,
1701-
// and returns it to the caller for further processing (signing + broadcast)
1701+
// FillTransaction fills the defaults (nonce, gas, gasPrice or 1559 fields)
1702+
// on a given unsigned transaction, and returns it to the caller for further
1703+
// processing (signing + broadcast).
17021704
func (s *PublicTransactionPoolAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
17031705
// Set some sanity defaults and terminate on failure
17041706
if err := args.setDefaults(ctx, s.b); err != nil {
@@ -1761,8 +1763,8 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Tra
17611763
if args.Gas == nil {
17621764
return nil, fmt.Errorf("gas not specified")
17631765
}
1764-
if args.GasPrice == nil {
1765-
return nil, fmt.Errorf("gasPrice not specified")
1766+
if args.GasPrice == nil && (args.MaxPriorityFeePerGas == nil || args.MaxFeePerGas == nil) {
1767+
return nil, fmt.Errorf("missing gasPrice or maxFeePerGas/maxPriorityFeePerGas")
17661768
}
17671769
if args.Nonce == nil {
17681770
return nil, fmt.Errorf("nonce not specified")
@@ -1771,18 +1773,19 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Tra
17711773
return nil, err
17721774
}
17731775
// Before actually sign the transaction, ensure the transaction fee is reasonable.
1774-
if err := checkTxFee(args.GasPrice.ToInt(), uint64(*args.Gas), s.b.RPCTxFeeCap()); err != nil {
1776+
tx := args.toTransaction()
1777+
if err := checkTxFee(tx.GasPrice(), tx.Gas(), s.b.RPCTxFeeCap()); err != nil {
17751778
return nil, err
17761779
}
1777-
tx, err := s.sign(args.from(), args.toTransaction())
1780+
signed, err := s.sign(args.from(), tx)
17781781
if err != nil {
17791782
return nil, err
17801783
}
1781-
data, err := tx.MarshalBinary()
1784+
data, err := signed.MarshalBinary()
17821785
if err != nil {
17831786
return nil, err
17841787
}
1785-
return &SignTransactionResult{data, tx}, nil
1788+
return &SignTransactionResult{data, signed}, nil
17861789
}
17871790

17881791
// PendingTransactions returns the transactions that are in the transaction pool

internal/ethapi/transaction_args.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type TransactionArgs struct {
4949
Data *hexutil.Bytes `json:"data"`
5050
Input *hexutil.Bytes `json:"input"`
5151

52-
// For non-legacy transactions
52+
// Introduced by AccessListTxType transaction.
5353
AccessList *types.AccessList `json:"accessList,omitempty"`
5454
ChainID *hexutil.Big `json:"chainId,omitempty"`
5555
}
@@ -108,6 +108,9 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
108108
return err
109109
}
110110
if b.ChainConfig().IsLondon(head.Number) {
111+
// The legacy tx gas price suggestion should not add 2x base fee
112+
// because all fees are consumed, so it would result in a spiral
113+
// upwards.
111114
price.Add(price, head.BaseFee)
112115
}
113116
args.GasPrice = (*hexutil.Big)(price)

0 commit comments

Comments
 (0)