Skip to content

Commit

Permalink
feat: add blob costs to cost value (#4489)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Sep 5, 2023
1 parent 01d4933 commit ca3753d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 4 additions & 5 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
compression::{TRANSACTION_COMPRESSOR, TRANSACTION_DECOMPRESSOR},
constants::eip4844::DATA_GAS_PER_BLOB,
keccak256, Address, Bytes, TxHash, H256,
};
pub use access_list::{AccessList, AccessListItem, AccessListWithGasUsed};
Expand Down Expand Up @@ -236,10 +235,10 @@ impl Transaction {
/// Returns the blob gas used for all blobs of the EIP-4844 transaction if it is an EIP-4844
/// transaction.
///
/// This is the number of blobs times the [DATA_GAS_PER_BLOB] a single blob consumes.
pub fn blob_gas_used(&self) -> Option<u128> {
let tx = self.as_eip4844()?;
Some(tx.blob_versioned_hashes.len() as u128 * DATA_GAS_PER_BLOB as u128)
/// This is the number of blobs times the
/// [DATA_GAS_PER_BLOB](crate::constants::eip4844::DATA_GAS_PER_BLOB) a single blob consumes.
pub fn blob_gas_used(&self) -> Option<u64> {
self.as_eip4844().map(TxEip4844::blob_gas)
}

/// Return the max priority fee per gas if the transaction is an EIP-1559 transaction, and
Expand Down
13 changes: 12 additions & 1 deletion crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ pub trait PoolTransaction:
///
/// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
/// For legacy transactions: `gas_price * gas_limit + tx_value`.
/// For EIP-4844 blob transactions: `max_fee_per_gas * gas_limit + tx_value +
/// max_blob_fee_per_gas * blob_gas_used`.
fn cost(&self) -> U256;

/// Amount of gas that should be used in executing this transaction. This is paid up-front.
Expand Down Expand Up @@ -721,6 +723,8 @@ pub struct EthPooledTransaction {

/// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
/// For legacy transactions: `gas_price * gas_limit + tx_value`.
/// For EIP-4844 blob transactions: `max_fee_per_gas * gas_limit + tx_value +
/// max_blob_fee_per_gas * blob_gas_used`.
pub(crate) cost: U256,

/// The blob side car this transaction
Expand Down Expand Up @@ -754,7 +758,12 @@ impl EthPooledTransaction {
U256::from(t.max_fee_per_gas) * U256::from(t.gas_limit)
}
};
let cost = gas_cost + U256::from(transaction.value());
let mut cost = gas_cost + U256::from(transaction.value());

if let Some(blob_tx) = transaction.as_eip4844() {
// add max blob cost
cost += U256::from(blob_tx.max_fee_per_gas * blob_tx.blob_gas() as u128);
}

Self { transaction, cost, blob_sidecar }
}
Expand Down Expand Up @@ -806,6 +815,8 @@ impl PoolTransaction for EthPooledTransaction {
///
/// For EIP-1559 transactions: `max_fee_per_gas * gas_limit + tx_value`.
/// For legacy transactions: `gas_price * gas_limit + tx_value`.
/// For EIP-4844 blob transactions: `max_fee_per_gas * gas_limit + tx_value +
/// max_blob_fee_per_gas * blob_gas_used`.
fn cost(&self) -> U256 {
self.cost
}
Expand Down

0 comments on commit ca3753d

Please sign in to comment.