Skip to content

Commit

Permalink
feat: sat math for tx value (#6900)
Browse files Browse the repository at this point in the history
  • Loading branch information
justcode740 committed Mar 1, 2024
1 parent 1d28fc6 commit cdca09e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,22 +918,30 @@ impl EthPooledTransaction {
pub fn new(transaction: TransactionSignedEcRecovered, encoded_length: usize) -> Self {
let mut blob_sidecar = EthBlobTransactionSidecar::None;
let gas_cost = match &transaction.transaction {
Transaction::Legacy(t) => U256::from(t.gas_price) * U256::from(t.gas_limit),
Transaction::Eip2930(t) => U256::from(t.gas_price) * U256::from(t.gas_limit),
Transaction::Eip1559(t) => U256::from(t.max_fee_per_gas) * U256::from(t.gas_limit),
Transaction::Legacy(t) => {
U256::from(t.gas_price).saturating_mul(U256::from(t.gas_limit))
}
Transaction::Eip2930(t) => {
U256::from(t.gas_price).saturating_mul(U256::from(t.gas_limit))
}
Transaction::Eip1559(t) => {
U256::from(t.max_fee_per_gas).saturating_mul(U256::from(t.gas_limit))
}
Transaction::Eip4844(t) => {
blob_sidecar = EthBlobTransactionSidecar::Missing;
U256::from(t.max_fee_per_gas) * U256::from(t.gas_limit)
U256::from(t.max_fee_per_gas).saturating_mul(U256::from(t.gas_limit))
}
#[cfg(feature = "optimism")]
Transaction::Deposit(_) => U256::ZERO,
};
let mut cost: U256 = transaction.value();
cost += gas_cost;
let mut cost = transaction.value();
cost = cost.saturating_add(gas_cost);

if let Some(blob_tx) = transaction.as_eip4844() {
// add max blob cost
cost += U256::from(blob_tx.max_fee_per_blob_gas * blob_tx.blob_gas() as u128);
// Add max blob cost using saturating math to avoid overflow
cost = cost.saturating_add(U256::from(
blob_tx.max_fee_per_blob_gas.saturating_mul(blob_tx.blob_gas() as u128),
));
}

Self { transaction, cost, encoded_length, blob_sidecar }
Expand Down

0 comments on commit cdca09e

Please sign in to comment.