Skip to content

Commit

Permalink
chore: simplify cost calc (#12796)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 22, 2024
1 parent 6a97a6d commit 36db1c2
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,34 +1142,20 @@ impl EthPooledTransaction {
pub fn new(transaction: TransactionSignedEcRecovered, encoded_length: usize) -> Self {
let mut blob_sidecar = EthBlobTransactionSidecar::None;

#[allow(unreachable_patterns)]
let gas_cost = match &transaction.transaction {
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).saturating_mul(U256::from(t.gas_limit))
}
Transaction::Eip7702(t) => {
U256::from(t.max_fee_per_gas).saturating_mul(U256::from(t.gas_limit))
}
_ => U256::ZERO,
};
let mut cost = transaction.value();
cost = cost.saturating_add(gas_cost);
let gas_cost = U256::from(transaction.transaction.max_fee_per_gas())
.saturating_mul(U256::from(transaction.transaction.gas_limit()));

let mut cost = gas_cost.saturating_add(transaction.value());

if let Some(blob_tx) = transaction.as_eip4844() {
// 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),
));

// because the blob sidecar is not included in this transaction variant, mark it as
// missing
blob_sidecar = EthBlobTransactionSidecar::Missing;
}

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

0 comments on commit 36db1c2

Please sign in to comment.