diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index 054175cc97..f51c6f56e6 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -47,6 +47,19 @@ impl Env { }) } + /// Calculates the maximum [EIP-4844] `data_fee` of the transaction. + /// + /// This is used for ensuring that the user has at least enough funds to pay the + /// `max_fee_per_blob_gas * total_blob_gas`, on top of regular gas costs. + /// + /// See EIP-4844: + /// + pub fn calc_max_data_fee(&self) -> Option { + self.tx.max_fee_per_blob_gas.map(|max_fee_per_blob_gas| { + max_fee_per_blob_gas.saturating_mul(U256::from(self.tx.get_total_blob_gas())) + }) + } + /// Validate the block environment. #[inline] pub fn validate_block_env(&self) -> Result<(), InvalidHeader> { @@ -218,7 +231,8 @@ impl Env { .ok_or(InvalidTransaction::OverflowPaymentInTransaction)?; if SPEC::enabled(SpecId::CANCUN) { - let data_fee = self.calc_data_fee().expect("already checked"); + // if the tx is not a blob tx, this will be None, so we add zero + let data_fee = self.calc_max_data_fee().unwrap_or_default(); balance_check = balance_check .checked_add(U256::from(data_fee)) .ok_or(InvalidTransaction::OverflowPaymentInTransaction)?; @@ -560,7 +574,7 @@ pub struct TxEnv { } impl TxEnv { - /// See [EIP-4844] and [`Env::calc_data_fee`]. + /// See [EIP-4844], [`Env::calc_data_fee`], and [`Env::calc_max_data_fee`]. /// /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 #[inline]