Skip to content

Commit b55a7da

Browse files
committed
internal/ethapi: optimize calculating effective price
Seems better to have this in a dedicated function, and we can also avoid creating multiple temp bigints.
1 parent caa4ffb commit b55a7da

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

internal/ethapi/api.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,11 +1470,11 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
14701470
// if the transaction has been mined, compute the effective gas price
14711471
if baseFee != nil && blockHash != (common.Hash{}) {
14721472
// price = min(gasTipCap + baseFee, gasFeeCap)
1473-
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
1474-
result.GasPrice = (*hexutil.Big)(price)
1473+
result.GasPrice = (*hexutil.Big)(effectiveGasPrice(tx, baseFee))
14751474
} else {
14761475
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
14771476
}
1477+
14781478
case types.BlobTxType:
14791479
al := tx.AccessList()
14801480
yparity := hexutil.Uint64(v.Sign())
@@ -1485,9 +1485,7 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
14851485
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
14861486
// if the transaction has been mined, compute the effective gas price
14871487
if baseFee != nil && blockHash != (common.Hash{}) {
1488-
// price = min(gasTipCap + baseFee, gasFeeCap)
1489-
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
1490-
result.GasPrice = (*hexutil.Big)(price)
1488+
result.GasPrice = (*hexutil.Big)(effectiveGasPrice(tx, baseFee))
14911489
} else {
14921490
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
14931491
}
@@ -1497,6 +1495,18 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
14971495
return result
14981496
}
14991497

1498+
// effectiveGasPrice computes the transaction gas fee, based on the given basefee value.
1499+
//
1500+
// price = min(gasTipCap + baseFee, gasFeeCap)
1501+
func effectiveGasPrice(tx *types.Transaction, baseFee *big.Int) *big.Int {
1502+
fee := tx.GasTipCap()
1503+
fee = fee.Add(fee, baseFee)
1504+
if tx.GasTipCapIntCmp(fee) < 0 {
1505+
return tx.GasTipCap()
1506+
}
1507+
return fee
1508+
}
1509+
15001510
// NewRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation
15011511
func NewRPCPendingTransaction(tx *types.Transaction, current *types.Header, config *params.ChainConfig) *RPCTransaction {
15021512
var (

0 commit comments

Comments
 (0)