Skip to content

Commit

Permalink
fix: use maximum possible data fee for 4844 balance checks (#981)
Browse files Browse the repository at this point in the history
* fix: use maximum possible data fee for 4844 balance checks

* fix docs

* use unwrap_or_default to cover non-blob txs
  • Loading branch information
Rjected authored Jan 16, 2024
1 parent fecb507 commit 9d42562
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#execution-layer-validation>
pub fn calc_max_data_fee(&self) -> Option<U256> {
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<SPEC: Spec>(&self) -> Result<(), InvalidHeader> {
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 9d42562

Please sign in to comment.