@@ -406,6 +406,10 @@ func (s *PrivateAccountAPI) SignTransaction(ctx context.Context, args SendTxArgs
406
406
if args .Nonce == nil {
407
407
return nil , fmt .Errorf ("nonce not specified" )
408
408
}
409
+ // Before actually sign the transaction, ensure the transaction fee is reasonable.
410
+ if err := checkTxFee (args .GasPrice .ToInt (), uint64 (* args .Gas ), s .b .RPCTxFeeCap ()); err != nil {
411
+ return nil , err
412
+ }
409
413
signed , err := s .signTransaction (ctx , & args , passwd )
410
414
if err != nil {
411
415
log .Warn ("Failed transaction sign attempt" , "from" , args .From , "to" , args .To , "value" , args .Value .ToInt (), "err" , err )
@@ -1545,10 +1549,8 @@ func (args *SendTxArgs) toTransaction() *types.Transaction {
1545
1549
func SubmitTransaction (ctx context.Context , b Backend , tx * types.Transaction ) (common.Hash , error ) {
1546
1550
// If the transaction fee cap is already specified, ensure the
1547
1551
// fee of the given transaction is _reasonable_.
1548
- feeEth := new (big.Float ).Quo (new (big.Float ).SetInt (new (big.Int ).Mul (tx .GasPrice (), new (big.Int ).SetUint64 (tx .Gas ()))), new (big.Float ).SetInt (big .NewInt (params .Ether )))
1549
- feeFloat , _ := feeEth .Float64 ()
1550
- if b .RPCTxFeeCap () != 0 && feeFloat > b .RPCTxFeeCap () {
1551
- return common.Hash {}, fmt .Errorf ("tx fee (%.2f ether) exceeds the configured cap (%.2f ether)" , feeFloat , b .RPCTxFeeCap ())
1552
+ if err := checkTxFee (tx .GasPrice (), tx .Gas (), b .RPCTxFeeCap ()); err != nil {
1553
+ return common.Hash {}, err
1552
1554
}
1553
1555
if err := b .SendTx (ctx , tx ); err != nil {
1554
1556
return common.Hash {}, err
@@ -1672,6 +1674,10 @@ func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args Sen
1672
1674
if err := args .setDefaults (ctx , s .b ); err != nil {
1673
1675
return nil , err
1674
1676
}
1677
+ // Before actually sign the transaction, ensure the transaction fee is reasonable.
1678
+ if err := checkTxFee (args .GasPrice .ToInt (), uint64 (* args .Gas ), s .b .RPCTxFeeCap ()); err != nil {
1679
+ return nil , err
1680
+ }
1675
1681
tx , err := s .sign (args .From , args .toTransaction ())
1676
1682
if err != nil {
1677
1683
return nil , err
@@ -1720,11 +1726,24 @@ func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, sendArgs SendTxAr
1720
1726
return common.Hash {}, err
1721
1727
}
1722
1728
matchTx := sendArgs .toTransaction ()
1729
+
1730
+ // Before replacing the old transaction, ensure the _new_ transaction fee is reasonable.
1731
+ var price = matchTx .GasPrice ()
1732
+ if gasPrice != nil {
1733
+ price = gasPrice .ToInt ()
1734
+ }
1735
+ var gas = matchTx .Gas ()
1736
+ if gasLimit != nil {
1737
+ gas = uint64 (* gasLimit )
1738
+ }
1739
+ if err := checkTxFee (price , gas , s .b .RPCTxFeeCap ()); err != nil {
1740
+ return common.Hash {}, err
1741
+ }
1742
+ // Iterate the pending list for replacement
1723
1743
pending , err := s .b .GetPoolTransactions ()
1724
1744
if err != nil {
1725
1745
return common.Hash {}, err
1726
1746
}
1727
-
1728
1747
for _ , p := range pending {
1729
1748
var signer types.Signer = types.HomesteadSigner {}
1730
1749
if p .Protected () {
@@ -1901,3 +1920,18 @@ func (s *PublicNetAPI) PeerCount() hexutil.Uint {
1901
1920
func (s * PublicNetAPI ) Version () string {
1902
1921
return fmt .Sprintf ("%d" , s .networkVersion )
1903
1922
}
1923
+
1924
+ // checkTxFee is an internal function used to check whether the fee of
1925
+ // the given transaction is _reasonable_(under the cap).
1926
+ func checkTxFee (gasPrice * big.Int , gas uint64 , cap float64 ) error {
1927
+ // Short circuit if there is no cap for transaction fee at all.
1928
+ if cap == 0 {
1929
+ return nil
1930
+ }
1931
+ feeEth := new (big.Float ).Quo (new (big.Float ).SetInt (new (big.Int ).Mul (gasPrice , new (big.Int ).SetUint64 (gas ))), new (big.Float ).SetInt (big .NewInt (params .Ether )))
1932
+ feeFloat , _ := feeEth .Float64 ()
1933
+ if feeFloat > cap {
1934
+ return fmt .Errorf ("tx fee (%.2f ether) exceeds the configured cap (%.2f ether)" , feeFloat , cap )
1935
+ }
1936
+ return nil
1937
+ }
0 commit comments