From a84ea91744437321a2e20da89044290935a94f3d Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 28 Aug 2023 16:25:46 +0300 Subject: [PATCH] perf(executor): pre-parse address constants --- crates/payload/basic/src/optimism.rs | 4 +-- crates/revm/Cargo.toml | 3 ++ crates/revm/src/optimism/executor.rs | 4 +-- crates/revm/src/optimism/mod.rs | 41 ++++++++++++++-------------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/crates/payload/basic/src/optimism.rs b/crates/payload/basic/src/optimism.rs index c208e527557e..edf2ff2a6b30 100644 --- a/crates/payload/basic/src/optimism.rs +++ b/crates/payload/basic/src/optimism.rs @@ -232,7 +232,7 @@ where &mut db, &mut post_state, parent_block.number + 1, - executor::optimism::l1_cost_recipient(), + *executor::optimism::L1_FEE_RECIPIENT, l1_cost, )? } @@ -240,7 +240,7 @@ where &mut db, &mut post_state, parent_block.number + 1, - executor::optimism::base_fee_recipient(), + *executor::optimism::BASE_FEE_RECIPIENT, U256::from(base_fee.saturating_mul(result.gas_used())), )?; } diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index 6046ad0b22bd..407172e28fc6 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -23,6 +23,8 @@ revm.workspace = true # common tracing.workspace = true +once_cell = { version = "1.17.0", optional = true } + [dev-dependencies] reth-rlp.workspace = true once_cell = "1.17.0" @@ -33,5 +35,6 @@ optimism = [ "reth-revm-primitives/optimism", "reth-consensus-common/optimism", "reth-interfaces/optimism", + "once_cell" ] diff --git a/crates/revm/src/optimism/executor.rs b/crates/revm/src/optimism/executor.rs index b2f7ba50db08..85f266255b0b 100644 --- a/crates/revm/src/optimism/executor.rs +++ b/crates/revm/src/optimism/executor.rs @@ -225,14 +225,14 @@ where if let Some(l1_cost) = l1_cost { self.increment_account_balance( block.number, - super::l1_cost_recipient(), + *super::L1_FEE_RECIPIENT, l1_cost, &mut post_state, )? } self.increment_account_balance( block.number, - super::base_fee_recipient(), + *super::BASE_FEE_RECIPIENT, U256::from( block .base_fee_per_gas diff --git a/crates/revm/src/optimism/mod.rs b/crates/revm/src/optimism/mod.rs index b47536189d47..9cf308ec02e5 100644 --- a/crates/revm/src/optimism/mod.rs +++ b/crates/revm/src/optimism/mod.rs @@ -1,17 +1,30 @@ -use std::{ops::Mul, str::FromStr, sync::Arc}; - -pub mod executor; - +use once_cell::sync::Lazy; use reth_interfaces::executor as reth_executor; use reth_primitives::{Address, Block, Bytes, ChainSpec, Hardfork, TransactionKind, U256}; +use std::{ops::Mul, str::FromStr, sync::Arc}; -const L1_FEE_RECIPIENT: &str = "0x420000000000000000000000000000000000001A"; -const BASE_FEE_RECIPIENT: &str = "0x4200000000000000000000000000000000000019"; -const L1_BLOCK_CONTRACT: &str = "0x4200000000000000000000000000000000000015"; +pub mod executor; const ZERO_BYTE_COST: u64 = 4; const NON_ZERO_BYTE_COST: u64 = 16; +/// The address of L1 fee recipient. +pub static L1_FEE_RECIPIENT: Lazy
= Lazy::new(|| { + Address::from_str("0x420000000000000000000000000000000000001A") + .expect("failed to parse l1 fee recipient address") +}); + +/// The address of the base fee recipient. +pub static BASE_FEE_RECIPIENT: Lazy
= Lazy::new(|| { + Address::from_str("0x4200000000000000000000000000000000000019") + .expect("failed to parse base fee recipient address") +}); + +static L1_BLOCK_CONTRACT: Lazy
= Lazy::new(|| { + Address::from_str("0x4200000000000000000000000000000000000015") + .expect("failed to parse l1 block contract address") +}); + /// L1 block info /// /// We can extract L1 epoch data from each L2 block, by looking at the `setL1BlockValues` @@ -37,12 +50,10 @@ impl TryFrom<&Block> for L1BlockInfo { type Error = reth_executor::BlockExecutionError; fn try_from(block: &Block) -> Result { - let l1_block_contract = Address::from_str(L1_BLOCK_CONTRACT).unwrap(); - let l1_info_tx_data = block .body .iter() - .find(|tx| matches!(tx.kind(), TransactionKind::Call(to) if to == &l1_block_contract)) + .find(|tx| matches!(tx.kind(), TransactionKind::Call(to) if to == &*L1_BLOCK_CONTRACT)) .ok_or(reth_executor::BlockExecutionError::L1BlockInfoError { message: "could not find l1 block info tx in the L2 block".to_string(), }) @@ -138,16 +149,6 @@ impl L1BlockInfo { } } -/// Get the base fee recipient address -pub fn base_fee_recipient() -> Address { - Address::from_str(BASE_FEE_RECIPIENT).unwrap() -} - -/// Get the L1 cost recipient address -pub fn l1_cost_recipient() -> Address { - Address::from_str(L1_FEE_RECIPIENT).unwrap() -} - #[cfg(test)] mod test_l1_fee { #[cfg(feature = "optimism")]