diff --git a/crates/payload/basic/src/optimism.rs b/crates/payload/basic/src/optimism.rs index 61b50fe357b18..15e30b98fa490 100644 --- a/crates/payload/basic/src/optimism.rs +++ b/crates/payload/basic/src/optimism.rs @@ -54,6 +54,13 @@ where let is_regolith = chain_spec.is_fork_active_at_timestamp(Hardfork::Regolith, attributes.timestamp); + reth_revm::optimism::ensure_create2_deployer(chain_spec.clone(), attributes.timestamp, &mut db) + .map_err(|_| { + PayloadBuilderError::Internal(RethError::Custom( + "Failed to force create2deployer account code".to_string(), + )) + })?; + let mut receipts = Vec::new(); for sequencer_tx in attributes.optimism_payload_attributes.transactions { // Check if the job was cancelled, if so we can exit early. diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 37d242bff0729..bc7f5487f087d 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -252,7 +252,7 @@ pub static OP_GOERLI: Lazy> = Lazy::new(|| { genesis_hash: Some(b256!( "c1fc15cd51159b1f1e5cbc4b82e85c1447ddfa33c52cf1d98d14fba0d6354be1" )), - fork_timestamps: ForkTimestamps::default(), + fork_timestamps: ForkTimestamps::default().shanghai(1699981200).canyon(1699981200), paris_block_and_final_difficulty: Some((0, U256::from(0))), hardforks: BTreeMap::from([ (Hardfork::Frontier, ForkCondition::Block(0)), @@ -295,7 +295,7 @@ pub static BASE_GOERLI: Lazy> = Lazy::new(|| { genesis_hash: Some(b256!( "a3ab140f15ea7f7443a4702da64c10314eb04d488e72974e02e2d728096b4f76" )), - fork_timestamps: ForkTimestamps::default(), + fork_timestamps: ForkTimestamps::default().shanghai(1699981200).canyon(1699981200), paris_block_and_final_difficulty: Some((0, U256::from(0))), hardforks: BTreeMap::from([ (Hardfork::Frontier, ForkCondition::Block(0)), @@ -831,6 +831,12 @@ pub struct ForkTimestamps { pub shanghai: Option, /// The timestamp of the cancun fork pub cancun: Option, + /// The timestamp of the Regolith fork + #[cfg(feature = "optimism")] + pub regolith: Option, + /// The timestamp of the Canyon fork + #[cfg(feature = "optimism")] + pub canyon: Option, } impl ForkTimestamps { @@ -843,6 +849,15 @@ impl ForkTimestamps { if let Some(cancun) = forks.get(&Hardfork::Cancun).and_then(|f| f.as_timestamp()) { timestamps = timestamps.cancun(cancun); } + #[cfg(feature = "optimism")] + { + if let Some(regolith) = forks.get(&Hardfork::Regolith).and_then(|f| f.as_timestamp()) { + timestamps = timestamps.regolith(regolith); + } + if let Some(canyon) = forks.get(&Hardfork::Canyon).and_then(|f| f.as_timestamp()) { + timestamps = timestamps.canyon(canyon); + } + } timestamps } @@ -857,6 +872,20 @@ impl ForkTimestamps { self.cancun = Some(cancun); self } + + /// Sets the given regolith timestamp + #[cfg(feature = "optimism")] + pub fn regolith(mut self, regolith: u64) -> Self { + self.regolith = Some(regolith); + self + } + + /// Sets the given canyon timestamp + #[cfg(feature = "optimism")] + pub fn canyon(mut self, canyon: u64) -> Self { + self.canyon = Some(canyon); + self + } } /// A helper type for compatibility with geth's config diff --git a/crates/revm/src/optimism/mod.rs b/crates/revm/src/optimism/mod.rs index ced44bd91ca96..3e5eee35825a7 100644 --- a/crates/revm/src/optimism/mod.rs +++ b/crates/revm/src/optimism/mod.rs @@ -1,13 +1,29 @@ -use reth_interfaces::executor::{self as reth_executor, BlockExecutionError}; -use reth_primitives::{Block, Bytes, ChainSpec, Hardfork, TransactionKind, U256}; +use std::sync::Arc; + +use reth_interfaces::{ + executor::{self as reth_executor, BlockExecutionError}, + RethError, +}; +use reth_primitives::{ + address, b256, hex, Address, Block, Bytes, ChainSpec, Hardfork, TransactionKind, B256, U256, +}; use revm::{ - primitives::{BedrockSpec, RegolithSpec}, - L1BlockInfo, + primitives::{BedrockSpec, Bytecode, HashMap, RegolithSpec}, + DatabaseCommit, L1BlockInfo, }; +use tracing::trace; /// Optimism-specific processor implementation for the `EVMProcessor` pub mod processor; +/// The address of the create2 deployer +pub const CREATE_2_DEPLOYER_ADDR: Address = address!("13b0D85CcB8bf860b6b79AF3029fCA081AE9beF2"); +/// The codehash of the create2 deployer contract. +pub const CREATE_2_DEPLOYER_CODEHASH: B256 = + b256!("b0550b5b431e30d38000efb7107aaa0ade03d48a7198a140edda9d27134468b2"); +/// The raw bytecode of the create2 deployer contract. +pub const CREATE_2_DEPLOYER_BYTECODE: [u8; 1584] = hex!("6080604052600436106100435760003560e01c8063076c37b21461004f578063481286e61461007157806356299481146100ba57806366cfa057146100da57600080fd5b3661004a57005b600080fd5b34801561005b57600080fd5b5061006f61006a366004610327565b6100fa565b005b34801561007d57600080fd5b5061009161008c366004610327565b61014a565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200160405180910390f35b3480156100c657600080fd5b506100916100d5366004610349565b61015d565b3480156100e657600080fd5b5061006f6100f53660046103ca565b610172565b61014582826040518060200161010f9061031a565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe082820381018352601f90910116604052610183565b505050565b600061015683836102e7565b9392505050565b600061016a8484846102f0565b949350505050565b61017d838383610183565b50505050565b6000834710156101f4576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f437265617465323a20696e73756666696369656e742062616c616e636500000060448201526064015b60405180910390fd5b815160000361025f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820181905260248201527f437265617465323a2062797465636f6465206c656e677468206973207a65726f60448201526064016101eb565b8282516020840186f5905073ffffffffffffffffffffffffffffffffffffffff8116610156576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f437265617465323a204661696c6564206f6e206465706c6f790000000000000060448201526064016101eb565b60006101568383305b6000604051836040820152846020820152828152600b8101905060ff815360559020949350505050565b61014e806104ad83390190565b6000806040838503121561033a57600080fd5b50508035926020909101359150565b60008060006060848603121561035e57600080fd5b8335925060208401359150604084013573ffffffffffffffffffffffffffffffffffffffff8116811461039057600080fd5b809150509250925092565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000806000606084860312156103df57600080fd5b8335925060208401359150604084013567ffffffffffffffff8082111561040557600080fd5b818601915086601f83011261041957600080fd5b81358181111561042b5761042b61039b565b604051601f82017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0908116603f011681019083821181831017156104715761047161039b565b8160405282815289602084870101111561048a57600080fd5b826020860160208301376000602084830101528095505050505050925092509256fe608060405234801561001057600080fd5b5061012e806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063249cb3fa14602d575b600080fd5b603c603836600460b1565b604e565b60405190815260200160405180910390f35b60008281526020818152604080832073ffffffffffffffffffffffffffffffffffffffff8516845290915281205460ff16608857600060aa565b7fa2ef4600d742022d532d4747cb3547474667d6f13804902513b2ec01c848f4b45b9392505050565b6000806040838503121560c357600080fd5b82359150602083013573ffffffffffffffffffffffffffffffffffffffff8116811460ed57600080fd5b80915050925092905056fea26469706673582212205ffd4e6cede7d06a5daf93d48d0541fc68189eeb16608c1999a82063b666eb1164736f6c63430008130033a2646970667358221220fdc4a0fe96e3b21c108ca155438d37c9143fb01278a3c1d274948bad89c564ba64736f6c63430008130033"); + /// Extracts the [L1BlockInfo] from the L2 block. The L1 info transaction is always the first /// transaction in the L2 block. pub fn extract_l1_info(block: &Block) -> Result { @@ -151,6 +167,44 @@ impl RethL1BlockInfo for L1BlockInfo { } } +/// The Canyon hardfork issues an irregular state transition that force-deploys the create2 +/// deployer contract. This is done by directly setting the code of the create2 deployer account +/// prior to executing any transactions on the timestamp activation of the fork. +pub fn ensure_create2_deployer( + chain_spec: Arc, + timestamp: u64, + db: &mut revm::State, +) -> Result<(), RethError> +where + DB: revm::Database, +{ + if chain_spec.fork_timestamps.canyon.unwrap_or_default() == timestamp && + chain_spec.is_optimism() + { + trace!(target: "evm", "Forcing create2 deployer contract deployment on Canyon transition"); + + // Load the create2 deployer account from the cache. + let acc = db + .load_cache_account(CREATE_2_DEPLOYER_ADDR) + .map_err(|_| RethError::Custom("Failed to load account".to_string()))?; + + // Update the account info with the create2 deployer codehash and bytecode. + let mut acc_info = acc.account_info().unwrap_or_default(); + acc_info.code_hash = CREATE_2_DEPLOYER_CODEHASH; + acc_info.code = Some(Bytecode::new_raw(Bytes::from_static(&CREATE_2_DEPLOYER_BYTECODE))); + + // Convert the cache account back into a revm account and mark it as touched. + let mut revm_acc: revm::primitives::Account = acc_info.into(); + revm_acc.mark_touch(); + + // Commit the create2 deployer account to the database. + db.commit(HashMap::from([(CREATE_2_DEPLOYER_ADDR, revm_acc)])); + return Ok(()) + } + + Ok(()) +} + #[cfg(test)] mod test_l1_fee { #[test] diff --git a/crates/revm/src/optimism/processor.rs b/crates/revm/src/optimism/processor.rs index efc3b8cba3688..6a01596d23528 100644 --- a/crates/revm/src/optimism/processor.rs +++ b/crates/revm/src/optimism/processor.rs @@ -1,3 +1,4 @@ +use crate::processor::{verify_receipt, EVMProcessor}; use reth_interfaces::executor::{BlockExecutionError, BlockValidationError}; use reth_primitives::{ revm::compat::into_reth_log, revm_primitives::ResultAndState, Address, Block, Hardfork, @@ -8,8 +9,6 @@ use revm::DatabaseCommit; use std::time::Instant; use tracing::{debug, trace}; -use crate::processor::{verify_receipt, EVMProcessor}; - impl<'a> BlockExecutor for EVMProcessor<'a> { fn execute( &mut self, @@ -70,6 +69,9 @@ impl<'a> BlockExecutor for EVMProcessor<'a> { let is_regolith = self.chain_spec.fork(Hardfork::Regolith).active_at_timestamp(block.timestamp); + super::ensure_create2_deployer(self.chain_spec().clone(), block.timestamp, self.db_mut()) + .map_err(|_| BlockExecutionError::ProviderError)?; + let mut cumulative_gas_used = 0; let mut receipts = Vec::with_capacity(block.body.len()); for (transaction, sender) in block.body.iter().zip(senders) {