From 0496987220a6dcb624239e757ede1937349050ae Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Mon, 15 Jan 2024 20:10:22 -0500 Subject: [PATCH 1/3] fix: use maximum possible data fee for 4844 balance checks --- crates/primitives/src/env.rs | 15 ++++++++++++++- crates/revm/src/handler/mainnet/pre_execution.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index 054175cc97..b0adee0982 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: + /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#execution-layer-validation + 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> { @@ -560,7 +573,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] diff --git a/crates/revm/src/handler/mainnet/pre_execution.rs b/crates/revm/src/handler/mainnet/pre_execution.rs index 84bd2952f8..3a0ba6db2d 100644 --- a/crates/revm/src/handler/mainnet/pre_execution.rs +++ b/crates/revm/src/handler/mainnet/pre_execution.rs @@ -60,7 +60,7 @@ pub fn deduct_caller_inner(caller_account: &mut Account, env: &Env) // EIP-4844 if SPEC::enabled(CANCUN) { - let data_fee = env.calc_data_fee().expect("already checked"); + let data_fee = env.calc_max_data_fee().expect("already checked"); gas_cost = gas_cost.saturating_add(data_fee); } From d3988d7fa2cfe221e3297f5589c3b047f75160d2 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Mon, 15 Jan 2024 23:43:41 -0500 Subject: [PATCH 2/3] fix docs --- crates/primitives/src/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index b0adee0982..a16ae6269f 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -53,7 +53,7 @@ impl Env { /// `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 { 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())) From 0a6b689d87a2c523e15f3c70f21e62727c8c97ce Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Mon, 15 Jan 2024 23:57:03 -0500 Subject: [PATCH 3/3] use unwrap_or_default to cover non-blob txs --- crates/primitives/src/env.rs | 3 ++- crates/revm/src/handler/mainnet/pre_execution.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index a16ae6269f..f51c6f56e6 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -231,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)?; diff --git a/crates/revm/src/handler/mainnet/pre_execution.rs b/crates/revm/src/handler/mainnet/pre_execution.rs index 3a0ba6db2d..84bd2952f8 100644 --- a/crates/revm/src/handler/mainnet/pre_execution.rs +++ b/crates/revm/src/handler/mainnet/pre_execution.rs @@ -60,7 +60,7 @@ pub fn deduct_caller_inner(caller_account: &mut Account, env: &Env) // EIP-4844 if SPEC::enabled(CANCUN) { - let data_fee = env.calc_max_data_fee().expect("already checked"); + let data_fee = env.calc_data_fee().expect("already checked"); gas_cost = gas_cost.saturating_add(data_fee); }