Skip to content

Commit e13f604

Browse files
Rjectedrakita
authored andcommitted
fix: use maximum possible data fee for 4844 balance checks (bluealloy#981)
* fix: use maximum possible data fee for 4844 balance checks * fix docs * use unwrap_or_default to cover non-blob txs
1 parent 5ea9d79 commit e13f604

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

crates/primitives/src/env.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ impl Env {
4040
})
4141
}
4242

43+
/// Calculates the maximum [EIP-4844] `data_fee` of the transaction.
44+
///
45+
/// This is used for ensuring that the user has at least enough funds to pay the
46+
/// `max_fee_per_blob_gas * total_blob_gas`, on top of regular gas costs.
47+
///
48+
/// See EIP-4844:
49+
/// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#execution-layer-validation>
50+
pub fn calc_max_data_fee(&self) -> Option<U256> {
51+
self.tx.max_fee_per_blob_gas.map(|max_fee_per_blob_gas| {
52+
max_fee_per_blob_gas.saturating_mul(U256::from(self.tx.get_total_blob_gas()))
53+
})
54+
}
55+
4356
/// Validate the block environment.
4457
#[inline]
4558
pub fn validate_block_env<SPEC: Spec>(&self) -> Result<(), InvalidHeader> {
@@ -213,7 +226,8 @@ impl Env {
213226
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;
214227

215228
if SpecId::enabled(self.cfg.spec_id, SpecId::CANCUN) {
216-
let data_fee = self.calc_data_fee().expect("already checked");
229+
// if the tx is not a blob tx, this will be None, so we add zero
230+
let data_fee = self.calc_max_data_fee().unwrap_or_default();
217231
balance_check = balance_check
218232
.checked_add(U256::from(data_fee))
219233
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;
@@ -549,7 +563,7 @@ pub struct TxEnv {
549563
}
550564

551565
impl TxEnv {
552-
/// See [EIP-4844] and [`Env::calc_data_fee`].
566+
/// See [EIP-4844], [`Env::calc_data_fee`], and [`Env::calc_max_data_fee`].
553567
///
554568
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
555569
#[inline]

0 commit comments

Comments
 (0)