From c52acd6529b37ee8e007a9e5a2c91d5fb42d89a6 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Sun, 8 Oct 2023 22:39:20 +0200 Subject: [PATCH 1/5] use l2 base fee --- crates/revm/src/handler/optimism.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/revm/src/handler/optimism.rs b/crates/revm/src/handler/optimism.rs index b178157683..2eafb18d34 100644 --- a/crates/revm/src/handler/optimism.rs +++ b/crates/revm/src/handler/optimism.rs @@ -132,8 +132,11 @@ pub fn reward_beneficiary( panic!("[OPTIMISM] Failed to load Base Fee Vault account"); }; base_fee_vault_account.mark_touch(); - base_fee_vault_account.info.balance += - l1_block_info.l1_base_fee.mul(U256::from(gas.spend())); + base_fee_vault_account.info.balance += data + .env + .block + .basefee + .mul(U256::from(gas.spend() - gas_refund)); } Ok(()) } From d5dd7cd9dfe144ab6065a7268dd0207f0306a9b6 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Sun, 8 Oct 2023 22:40:32 +0200 Subject: [PATCH 2/5] use optimism handler when activated --- crates/revm/src/evm_impl.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index e8141d71f9..a1067ff548 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -404,6 +404,15 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, precompiles: Precompiles, ) -> Self { let journaled_state = JournaledState::new(precompiles.len(), GSPEC::SPEC_ID); + #[cfg(feature = "optimism")] + let handler = if env.cfg.optimism { + Handler::optimism::() + } else { + Handler::mainnet::() + }; + #[cfg(not(feature = "optimism"))] + let handler = Handler::mainnet::(); + Self { data: EVMData { env, @@ -415,7 +424,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, l1_block_info: None, }, inspector, - handler: Handler::mainnet::(), + handler, _phantomdata: PhantomData {}, } } From fd616f2e6a47fd44c82c7a3ca29ee0af6863a785 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Tue, 10 Oct 2023 16:36:51 +0200 Subject: [PATCH 3/5] fix panic for eth execution with optimism flag --- crates/revm/src/evm_impl.rs | 33 ++++++------------ crates/revm/src/handler/optimism.rs | 2 +- crates/revm/src/optimism.rs | 52 ++++++++++------------------- 3 files changed, 29 insertions(+), 58 deletions(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index a1067ff548..929dbdfc2b 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -196,34 +196,23 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact let tx_data = env.tx.data.clone(); let tx_gas_limit = env.tx.gas_limit; + // the L1-cost fee is only computed for Optimism non-deposit transactions. #[cfg(feature = "optimism")] - let tx_l1_cost = { - let is_deposit = env.tx.optimism.source_hash.is_some(); - + let tx_l1_cost = if env.cfg.optimism && !env.tx.optimism.source_hash.is_some() { let l1_block_info = - optimism::L1BlockInfo::try_fetch(self.data.db, self.data.env.cfg.optimism) - .map_err(EVMError::Database)?; - - // Perform this calculation optimistically to avoid cloning the enveloped tx. - let tx_l1_cost = l1_block_info.as_ref().map(|l1_block_info| { - env.tx - .optimism - .enveloped_tx - .as_ref() - .map(|enveloped_tx| { - l1_block_info.calculate_tx_l1_cost::(enveloped_tx, is_deposit) - }) - .unwrap_or(U256::ZERO) - }); - // storage l1 block info for later use. - self.data.l1_block_info = l1_block_info; + optimism::L1BlockInfo::try_fetch(self.data.db).map_err(EVMError::Database)?; - // - let Some(tx_l1_cost) = tx_l1_cost else { - panic!("[OPTIMISM] L1 Block Info could not be loaded from the DB.") + let Some(enveloped_tx) = &env.tx.optimism.enveloped_tx else { + panic!("[OPTIMISM] Failed to load enveloped transaction."); }; + let tx_l1_cost = l1_block_info.calculate_tx_l1_cost::(enveloped_tx); + + // storage l1 block info for later use. + self.data.l1_block_info = Some(l1_block_info); tx_l1_cost + } else { + U256::ZERO }; let initial_gas_spend = initial_tx_gas::( diff --git a/crates/revm/src/handler/optimism.rs b/crates/revm/src/handler/optimism.rs index 2eafb18d34..b5f6b305cc 100644 --- a/crates/revm/src/handler/optimism.rs +++ b/crates/revm/src/handler/optimism.rs @@ -112,7 +112,7 @@ pub fn reward_beneficiary( panic!("[OPTIMISM] Failed to load enveloped transaction."); }; - let l1_cost = l1_block_info.calculate_tx_l1_cost::(enveloped_tx, is_deposit); + let l1_cost = l1_block_info.calculate_tx_l1_cost::(enveloped_tx); // Send the L1 cost of the transaction to the L1 Fee Vault. let Ok((l1_fee_vault_account, _)) = data diff --git a/crates/revm/src/optimism.rs b/crates/revm/src/optimism.rs index 09323d8a25..472a4a8ea1 100644 --- a/crates/revm/src/optimism.rs +++ b/crates/revm/src/optimism.rs @@ -41,23 +41,17 @@ pub struct L1BlockInfo { } impl L1BlockInfo { - pub fn try_fetch( - db: &mut DB, - is_optimism: bool, - ) -> Result, DB::Error> { - is_optimism - .then(|| { - let l1_base_fee = db.storage(L1_BLOCK_CONTRACT, L1_BASE_FEE_SLOT)?; - let l1_fee_overhead = db.storage(L1_BLOCK_CONTRACT, L1_OVERHEAD_SLOT)?; - let l1_fee_scalar = db.storage(L1_BLOCK_CONTRACT, L1_SCALAR_SLOT)?; - - Ok(L1BlockInfo { - l1_base_fee, - l1_fee_overhead, - l1_fee_scalar, - }) - }) - .map_or(Ok(None), |v| v.map(Some)) + /// Try to fetch the L1 block info from the database. + pub fn try_fetch(db: &mut DB) -> Result { + let l1_base_fee = db.storage(L1_BLOCK_CONTRACT, L1_BASE_FEE_SLOT)?; + let l1_fee_overhead = db.storage(L1_BLOCK_CONTRACT, L1_OVERHEAD_SLOT)?; + let l1_fee_scalar = db.storage(L1_BLOCK_CONTRACT, L1_SCALAR_SLOT)?; + + Ok(L1BlockInfo { + l1_base_fee, + l1_fee_overhead, + l1_fee_scalar, + }) } /// Calculate the data gas for posting the transaction on L1. Calldata costs 16 gas per non-zero @@ -83,19 +77,16 @@ impl L1BlockInfo { } /// Calculate the gas cost of a transaction based on L1 block data posted on L2 - pub fn calculate_tx_l1_cost(&self, input: &Bytes, is_deposit: bool) -> U256 { - let rollup_data_gas_cost = self.data_gas::(input); - - if is_deposit || rollup_data_gas_cost == U256::ZERO { - return U256::ZERO; - } + pub fn calculate_tx_l1_cost(&self, input: &Bytes) -> U256 { + // input must not be an deposit transaction + debug_assert!(!input.is_empty() && input[0] != 0x7E); + let rollup_data_gas_cost = self.data_gas::(input); rollup_data_gas_cost .saturating_add(self.l1_fee_overhead) .saturating_mul(self.l1_base_fee) .saturating_mul(self.l1_fee_scalar) - .checked_div(U256::from(1_000_000)) - .unwrap_or_default() + / U256::from(1_000_000) } } @@ -160,17 +151,8 @@ mod tests { l1_fee_scalar: U256::from(1_000), }; - // The gas cost here should be zero since the tx is a deposit let input = bytes!("FACADE"); - let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input, true); - assert_eq!(gas_cost, U256::ZERO); - - let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input, false); + let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input); assert_eq!(gas_cost, U256::from(1048)); - - // Zero rollup data gas cost should result in zero for non-deposits - let input = bytes!(""); - let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input, false); - assert_eq!(gas_cost, U256::ZERO); } } From e7b6b1d88bdcac1127a749acc4d9e177095f4c81 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Tue, 10 Oct 2023 17:57:33 +0200 Subject: [PATCH 4/5] use is_none --- crates/revm/src/evm_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 929dbdfc2b..0e36e68833 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -198,7 +198,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact // the L1-cost fee is only computed for Optimism non-deposit transactions. #[cfg(feature = "optimism")] - let tx_l1_cost = if env.cfg.optimism && !env.tx.optimism.source_hash.is_some() { + let tx_l1_cost = if env.cfg.optimism && env.tx.optimism.source_hash.is_none() { let l1_block_info = optimism::L1BlockInfo::try_fetch(self.data.db).map_err(EVMError::Database)?; From 56804fab1bd2c51ac5138bf083825a3db8c29319 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Wed, 11 Oct 2023 11:57:46 +0200 Subject: [PATCH 5/5] address review comments --- crates/revm/src/optimism.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/revm/src/optimism.rs b/crates/revm/src/optimism.rs index 472a4a8ea1..e6d1f093c8 100644 --- a/crates/revm/src/optimism.rs +++ b/crates/revm/src/optimism.rs @@ -78,8 +78,10 @@ impl L1BlockInfo { /// Calculate the gas cost of a transaction based on L1 block data posted on L2 pub fn calculate_tx_l1_cost(&self, input: &Bytes) -> U256 { - // input must not be an deposit transaction - debug_assert!(!input.is_empty() && input[0] != 0x7E); + // If the input is not a deposit transaction, the default value is zero. + if input.is_empty() || input.first() == Some(&0x7F) { + return U256::ZERO; + } let rollup_data_gas_cost = self.data_gas::(input); rollup_data_gas_cost @@ -154,5 +156,15 @@ mod tests { let input = bytes!("FACADE"); let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input); assert_eq!(gas_cost, U256::from(1048)); + + // Zero rollup data gas cost should result in zero + let input = bytes!(""); + let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input); + assert_eq!(gas_cost, U256::ZERO); + + // Deposit transactions with the EIP-2718 type of 0x7F should result in zero + let input = bytes!("7FFACADE"); + let gas_cost = l1_block_info.calculate_tx_l1_cost::(&input); + assert_eq!(gas_cost, U256::ZERO); } }