diff --git a/crates/consensus/src/block/header.rs b/crates/consensus/src/block/header.rs index d5a3ec493cc..329536d5fc3 100644 --- a/crates/consensus/src/block/header.rs +++ b/crates/consensus/src/block/header.rs @@ -4,7 +4,7 @@ use alloy_eips::{ calc_blob_gasprice, eip1559::{calc_next_block_base_fee, BaseFeeParams}, eip1898::BlockWithParent, - eip4844::{self}, + eip4844::{self, calc_blob_gasprice_with_update_fraction}, eip7742, merge::ALLOWED_FUTURE_BLOCK_TIME_SECONDS, BlockNumHash, @@ -214,10 +214,39 @@ impl Header { /// Returns `None` if `excess_blob_gas` is None. /// /// See also [Self::next_block_excess_blob_gas] + #[deprecated( + since = "0.8.1", + note = "Use `next_block_blob_fee_with_fraction` to account for update fractions" + )] pub fn next_block_blob_fee(&self) -> Option { Some(eip4844::calc_blob_gasprice(self.next_block_excess_blob_gas()?)) } + /// Returns the blob fee for the next block according to the EIP-4844 spec, + /// applying the provided update fraction. + /// + /// # Arguments + /// + /// - `update_fraction`: Fraction for the fee adjustment calculation (must be between 0.0 and + /// 1.0). + /// + /// # Returns + /// + /// - `Some(u128)` containing the calculated blob fee if `excess_blob_gas` is `Some`. + /// - `None` if `excess_blob_gas` is `None`. + /// + /// # Panics + /// + /// - Panics if `update_fraction` is not within the range [0.0, 1.0]. + pub fn next_block_blob_fee_with_fraction(&self, update_fraction: f64) -> Option { + if !(0.0..=1.0).contains(&update_fraction) { + panic!("update_fraction must be between 0.0 and 1.0"); + } + + let excess_blob_gas = self.next_block_excess_blob_gas()?; + Some(calc_blob_gasprice_with_update_fraction(excess_blob_gas, update_fraction)) + } + /// Calculate base fee for next block according to the EIP-1559 spec. /// /// Returns a `None` if no base fee is set, no EIP-1559 support @@ -731,6 +760,17 @@ pub trait BlockHeader { txs_and_ommers_empty && withdrawals_root == EMPTY_ROOT_HASH }) } + + /// Returns the blob fee for the next block according to the EIP-4844 spec, + /// applying the provided update fraction. + fn next_block_blob_fee_with_fraction(&self, update_fraction: f64) -> Option { + if !(0.0..=1.0).contains(&update_fraction) { + panic!("update_fraction must be between 0.0 and 1.0"); + } + + let excess_blob_gas = self.next_block_excess_blob_gas()?; + Some(calc_blob_gasprice_with_update_fraction(excess_blob_gas, update_fraction)) + } } impl BlockHeader for Header { diff --git a/crates/eips/src/eip4844/mod.rs b/crates/eips/src/eip4844/mod.rs index 698d30b856d..3b42209e89f 100644 --- a/crates/eips/src/eip4844/mod.rs +++ b/crates/eips/src/eip4844/mod.rs @@ -140,7 +140,14 @@ pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 { BLOB_GASPRICE_UPDATE_FRACTION, ) } - +/// Calculate the blob gas price using an update fraction. +pub fn calc_blob_gasprice_with_update_fraction(excess_blob_gas: u64, update_fraction: f64) -> u128 { + fake_exponential( + BLOB_TX_MIN_BLOB_GASPRICE, + (excess_blob_gas as f64 * update_fraction) as u128, + BLOB_GASPRICE_UPDATE_FRACTION, + ) +} /// Approximates `factor * e ** (numerator / denominator)` using Taylor expansion. /// /// This is used to calculate the blob price.