diff --git a/bin/reth/src/commands/debug_cmd/build_block.rs b/bin/reth/src/commands/debug_cmd/build_block.rs index c7a2f7e68..d4d9ee60a 100644 --- a/bin/reth/src/commands/debug_cmd/build_block.rs +++ b/bin/reth/src/commands/debug_cmd/build_block.rs @@ -19,7 +19,7 @@ use reth_evm::execute::{BlockExecutorProvider, Executor}; use reth_execution_types::ExecutionOutcome; use reth_fs_util as fs; use reth_node_api::{NodeTypesWithDB, NodeTypesWithEngine, PayloadBuilderAttributes}; -use reth_node_ethereum::EthExecutorProvider; +use reth_node_ethereum::{EthEvmConfig, EthExecutorProvider}; use reth_payload_builder::database::CachedReads; use reth_primitives::{ revm_primitives::KzgSettings, Address, BlobTransaction, BlobTransactionSidecar, Bytes, @@ -243,7 +243,9 @@ impl> Command { None, ); - let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::default(); + let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new( + EthEvmConfig::new(provider_factory.chain_spec()), + ); match payload_builder.try_build(args)? { BuildOutcome::Better { payload, .. } => { diff --git a/bin/reth/src/commands/debug_cmd/replay_engine.rs b/bin/reth/src/commands/debug_cmd/replay_engine.rs index fa2cc18fe..d26d36a41 100644 --- a/bin/reth/src/commands/debug_cmd/replay_engine.rs +++ b/bin/reth/src/commands/debug_cmd/replay_engine.rs @@ -19,7 +19,7 @@ use reth_fs_util as fs; use reth_network::{BlockDownloaderProvider, NetworkHandle}; use reth_network_api::NetworkInfo; use reth_node_api::{NodeTypesWithDB, NodeTypesWithDBAdapter, NodeTypesWithEngine}; -use reth_node_ethereum::{EthEngineTypes, EthExecutorProvider}; +use reth_node_ethereum::{EthEngineTypes, EthEvmConfig, EthExecutorProvider}; use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; use reth_provider::{ providers::BlockchainProvider, CanonStateSubscriptions, ChainSpecProvider, ProviderFactory, @@ -117,7 +117,9 @@ impl> Command { .await?; // Set up payload builder - let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::default(); + let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new( + EthEvmConfig::new(provider_factory.chain_spec()), + ); let payload_generator = BasicPayloadJobGenerator::with_builder( blockchain_db.clone(), diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index a97217e90..d0c113f88 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -963,7 +963,7 @@ impl From<&Arc> for ChainSpecBuilder { } /// `PoS` deposit contract details. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DepositContract { /// Deposit Contract Address pub address: Address, diff --git a/crates/engine/invalid-block-hooks/src/witness.rs b/crates/engine/invalid-block-hooks/src/witness.rs index d22dc2b21..ead7d8e79 100644 --- a/crates/engine/invalid-block-hooks/src/witness.rs +++ b/crates/engine/invalid-block-hooks/src/witness.rs @@ -64,13 +64,7 @@ where // Setup environment for the execution. let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default()); let mut block_env = BlockEnv::default(); - self.evm_config.fill_cfg_and_block_env( - &mut cfg, - &mut block_env, - &self.provider.chain_spec(), - block.header(), - U256::MAX, - ); + self.evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, block.header(), U256::MAX); // Setup EVM let mut evm = self.evm_config.evm_with_env( diff --git a/crates/engine/util/src/reorg.rs b/crates/engine/util/src/reorg.rs index b952050aa..0459ff341 100644 --- a/crates/engine/util/src/reorg.rs +++ b/crates/engine/util/src/reorg.rs @@ -279,13 +279,7 @@ where // Configure environments let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default()); let mut block_env = BlockEnv::default(); - evm_config.fill_cfg_and_block_env( - &mut cfg, - &mut block_env, - chain_spec, - &reorg_target.header, - U256::MAX, - ); + evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, &reorg_target.header, U256::MAX); let env = EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default()); let mut evm = evm_config.evm_with_env(&mut state, env); diff --git a/crates/ethereum/evm/src/execute.rs b/crates/ethereum/evm/src/execute.rs index 8c15dee96..cdac05ac4 100644 --- a/crates/ethereum/evm/src/execute.rs +++ b/crates/ethereum/evm/src/execute.rs @@ -47,7 +47,7 @@ pub struct EthExecutorProvider { impl EthExecutorProvider { /// Creates a new default ethereum executor provider. pub fn ethereum(chain_spec: Arc) -> Self { - Self::new(chain_spec, Default::default()) + Self::new(chain_spec.clone(), EthEvmConfig::new(chain_spec)) } /// Returns a new provider for the mainnet. @@ -285,7 +285,6 @@ where self.executor.evm_config.fill_cfg_and_block_env( &mut cfg, &mut block_env, - self.chain_spec(), header, total_difficulty, ); @@ -522,7 +521,7 @@ mod tests { } fn executor_provider(chain_spec: Arc) -> EthExecutorProvider { - EthExecutorProvider { chain_spec, evm_config: Default::default() } + EthExecutorProvider { evm_config: EthEvmConfig::new(chain_spec.clone()), chain_spec } } #[test] diff --git a/crates/ethereum/evm/src/lib.rs b/crates/ethereum/evm/src/lib.rs index bead8ae39..9c72c5a8e 100644 --- a/crates/ethereum/evm/src/lib.rs +++ b/crates/ethereum/evm/src/lib.rs @@ -12,6 +12,8 @@ #[cfg(not(feature = "std"))] extern crate alloc; +use std::sync::Arc; + use reth_chainspec::{ChainSpec, Head}; use reth_evm::{ConfigureEvm, ConfigureEvmEnv}; use reth_primitives::{transaction::FillTxEnv, Address, Header, TransactionSigned, U256}; @@ -32,20 +34,33 @@ pub mod dao_fork; pub mod eip6110; /// Ethereum-related EVM configuration. -#[derive(Debug, Clone, Copy, Default)] +#[derive(Debug, Clone)] #[non_exhaustive] -pub struct EthEvmConfig; +pub struct EthEvmConfig { + chain_spec: Arc, +} + +impl EthEvmConfig { + /// Creates a new Ethereum EVM configuration with the given chain spec. + pub const fn new(chain_spec: Arc) -> Self { + Self { chain_spec } + } + + /// Returns the chain spec associated with this configuration. + pub fn chain_spec(&self) -> &ChainSpec { + &self.chain_spec + } +} impl ConfigureEvmEnv for EthEvmConfig { fn fill_cfg_env( &self, cfg_env: &mut CfgEnvWithHandlerCfg, - chain_spec: &ChainSpec, header: &Header, total_difficulty: U256, ) { let spec_id = config::revm_spec( - chain_spec, + self.chain_spec(), &Head { number: header.number, timestamp: header.timestamp, @@ -55,7 +70,7 @@ impl ConfigureEvmEnv for EthEvmConfig { }, ); - cfg_env.chain_id = chain_spec.chain().id(); + cfg_env.chain_id = self.chain_spec.chain().id(); cfg_env.perf_analyse_created_bytecodes = AnalysisKind::Analyse; cfg_env.handler_cfg.spec_id = spec_id; @@ -115,7 +130,7 @@ impl ConfigureEvm for EthEvmConfig { #[cfg(test)] mod tests { use super::*; - use reth_chainspec::{Chain, ChainSpec}; + use reth_chainspec::{Chain, ChainSpec, MAINNET}; use reth_evm::execute::ProviderError; use reth_primitives::{ revm_primitives::{BlockEnv, CfgEnv, SpecId}, @@ -155,10 +170,9 @@ mod tests { // Use the `EthEvmConfig` to fill the `cfg_env` and `block_env` based on the ChainSpec, // Header, and total difficulty - EthEvmConfig::default().fill_cfg_and_block_env( + EthEvmConfig::new(Arc::new(chain_spec.clone())).fill_cfg_and_block_env( &mut cfg_env, &mut block_env, - &chain_spec, &header, total_difficulty, ); @@ -172,7 +186,7 @@ mod tests { #[allow(clippy::needless_update)] fn test_evm_configure() { // Create a default `EthEvmConfig` - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); // Initialize an empty database wrapped in CacheDB let db = CacheDB::>::default(); @@ -210,7 +224,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_default_spec() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); @@ -231,7 +245,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_custom_cfg() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); @@ -262,7 +276,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_custom_block_and_tx() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); @@ -296,7 +310,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_spec_id() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); @@ -319,7 +333,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_inspector() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); @@ -361,7 +375,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_and_default_inspector() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); let env_with_handler = EnvWithHandlerCfg::default(); @@ -381,7 +395,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_inspector_and_custom_cfg() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); let cfg = CfgEnv::default().with_chain_id(111); @@ -406,7 +420,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_inspector_and_custom_block_tx() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); // Create custom block and tx environment @@ -438,7 +452,7 @@ mod tests { #[test] #[allow(clippy::needless_update)] fn test_evm_with_env_inspector_and_spec_id() { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); let db = CacheDB::>::default(); let handler_cfg = HandlerCfg { spec_id: SpecId::CONSTANTINOPLE, ..Default::default() }; diff --git a/crates/ethereum/node/src/node.rs b/crates/ethereum/node/src/node.rs index 5e902a93a..939f3db1e 100644 --- a/crates/ethereum/node/src/node.rs +++ b/crates/ethereum/node/src/node.rs @@ -11,14 +11,14 @@ use reth_ethereum_engine_primitives::{ }; use reth_evm_ethereum::execute::EthExecutorProvider; use reth_network::NetworkHandle; -use reth_node_api::{FullNodeComponents, NodeAddOns}; +use reth_node_api::{ConfigureEvm, FullNodeComponents, NodeAddOns}; use reth_node_builder::{ components::{ ComponentsBuilder, ConsensusBuilder, ExecutorBuilder, NetworkBuilder, PayloadServiceBuilder, PoolBuilder, }, node::{FullNodeTypes, NodeTypes, NodeTypesWithEngine}, - BuilderContext, ConfigureEvm, Node, PayloadBuilderConfig, PayloadTypes, + BuilderContext, Node, PayloadBuilderConfig, PayloadTypes, }; use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; use reth_provider::CanonStateSubscriptions; @@ -57,7 +57,7 @@ impl EthereumNode { ComponentsBuilder::default() .node_types::() .pool(EthereumPoolBuilder::default()) - .payload(EthereumPayloadBuilder::new(EthEvmConfig::default())) + .payload(EthereumPayloadBuilder::default()) .network(EthereumNetworkBuilder::default()) .executor(EthereumExecutorBuilder::default()) .consensus(EthereumConsensusBuilder::default()) @@ -120,8 +120,8 @@ where ctx: &BuilderContext, ) -> eyre::Result<(Self::EVM, Self::Executor)> { let chain_spec = ctx.chain_spec(); - let evm_config = EthEvmConfig::default(); - let executor = EthExecutorProvider::new(chain_spec, evm_config); + let evm_config = EthEvmConfig::new(ctx.chain_spec()); + let executor = EthExecutorProvider::new(chain_spec, evm_config.clone()); Ok((evm_config, executor)) } @@ -204,37 +204,29 @@ where /// A basic ethereum payload service. #[derive(Debug, Default, Clone)] #[non_exhaustive] -pub struct EthereumPayloadBuilder { - /// The EVM configuration to use for the payload builder. - pub evm_config: Evm, -} +pub struct EthereumPayloadBuilder; -impl EthereumPayloadBuilder { - /// Create a new instance with the given evm config. - pub const fn new(evm_config: EVM) -> Self { - Self { evm_config } - } -} - -impl PayloadServiceBuilder for EthereumPayloadBuilder -where - Types: NodeTypesWithEngine, - Node: FullNodeTypes, - Evm: ConfigureEvm, - Pool: TransactionPool + Unpin + 'static, - Types::Engine: PayloadTypes< - BuiltPayload = EthBuiltPayload, - PayloadAttributes = EthPayloadAttributes, - PayloadBuilderAttributes = EthPayloadBuilderAttributes, - >, -{ - async fn spawn_payload_service( +impl EthereumPayloadBuilder { + /// A helper method initializing [`PayloadBuilderService`] with the given EVM config. + pub fn spawn( self, + evm_config: Evm, ctx: &BuilderContext, pool: Pool, - ) -> eyre::Result> { + ) -> eyre::Result> + where + Types: NodeTypesWithEngine, + Node: FullNodeTypes, + Evm: ConfigureEvm, + Pool: TransactionPool + Unpin + 'static, + Types::Engine: PayloadTypes< + BuiltPayload = EthBuiltPayload, + PayloadAttributes = EthPayloadAttributes, + PayloadBuilderAttributes = EthPayloadBuilderAttributes, + >, + { let payload_builder = - reth_ethereum_payload_builder::EthereumPayloadBuilder::new(self.evm_config); + reth_ethereum_payload_builder::EthereumPayloadBuilder::new(evm_config); let conf = ctx.payload_builder_config(); let payload_job_config = BasicPayloadJobGeneratorConfig::default() @@ -260,6 +252,26 @@ where } } +impl PayloadServiceBuilder for EthereumPayloadBuilder +where + Types: NodeTypesWithEngine, + Node: FullNodeTypes, + Pool: TransactionPool + Unpin + 'static, + Types::Engine: PayloadTypes< + BuiltPayload = EthBuiltPayload, + PayloadAttributes = EthPayloadAttributes, + PayloadBuilderAttributes = EthPayloadBuilderAttributes, + >, +{ + async fn spawn_payload_service( + self, + ctx: &BuilderContext, + pool: Pool, + ) -> eyre::Result> { + self.spawn(EthEvmConfig::new(ctx.chain_spec()), ctx, pool) + } +} + /// A basic ethereum payload service. #[derive(Debug, Default, Clone, Copy)] pub struct EthereumNetworkBuilder { diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index baebd19fa..3849575d6 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -63,12 +63,6 @@ impl EthereumPayloadBuilder { } } -impl Default for EthereumPayloadBuilder { - fn default() -> Self { - Self::new(EthEvmConfig::default()) - } -} - // Default implementation of [PayloadBuilder] for unit type impl PayloadBuilder for EthereumPayloadBuilder where diff --git a/crates/evm/src/lib.rs b/crates/evm/src/lib.rs index 08c343b1a..83e3606d8 100644 --- a/crates/evm/src/lib.rs +++ b/crates/evm/src/lib.rs @@ -15,7 +15,6 @@ extern crate alloc; use core::ops::Deref; use crate::builder::RethEvmBuilder; -use reth_chainspec::ChainSpec; use reth_primitives::{Address, Header, TransactionSigned, TransactionSignedEcRecovered, U256}; use revm::{Database, Evm, GetInspector}; use revm_primitives::{ @@ -133,7 +132,6 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static { fn fill_cfg_env( &self, cfg_env: &mut CfgEnvWithHandlerCfg, - chain_spec: &ChainSpec, header: &Header, total_difficulty: U256, ); @@ -165,11 +163,10 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static { &self, cfg: &mut CfgEnvWithHandlerCfg, block_env: &mut BlockEnv, - chain_spec: &ChainSpec, header: &Header, total_difficulty: U256, ) { - self.fill_cfg_env(cfg, chain_spec, header, total_difficulty); + self.fill_cfg_env(cfg, header, total_difficulty); let after_merge = cfg.handler_cfg.spec_id >= SpecId::MERGE; self.fill_block_env(block_env, header, after_merge); } diff --git a/crates/exex/test-utils/src/lib.rs b/crates/exex/test-utils/src/lib.rs index dd4907649..b93c263a4 100644 --- a/crates/exex/test-utils/src/lib.rs +++ b/crates/exex/test-utils/src/lib.rs @@ -76,16 +76,16 @@ pub struct TestExecutorBuilder; impl ExecutorBuilder for TestExecutorBuilder where - Node: FullNodeTypes, + Node: FullNodeTypes>, { type EVM = EthEvmConfig; type Executor = MockExecutorProvider; async fn build_evm( self, - _ctx: &BuilderContext, + ctx: &BuilderContext, ) -> eyre::Result<(Self::EVM, Self::Executor)> { - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(ctx.chain_spec()); let executor = MockExecutorProvider::default(); Ok((evm_config, executor)) @@ -238,7 +238,7 @@ pub async fn test_exex_context_with_chain_spec( chain_spec: Arc, ) -> eyre::Result<(ExExContext, TestExExHandle)> { let transaction_pool = testing_pool(); - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(chain_spec.clone()); let executor = MockExecutorProvider::default(); let consensus = Arc::new(TestConsensus::default()); diff --git a/crates/optimism/evm/Cargo.toml b/crates/optimism/evm/Cargo.toml index 48282cdaf..9ff3278ad 100644 --- a/crates/optimism/evm/Cargo.toml +++ b/crates/optimism/evm/Cargo.toml @@ -26,6 +26,7 @@ alloy-primitives.workspace = true # Optimism reth-optimism-consensus.workspace = true +reth-optimism-chainspec.workspace = true # revm revm.workspace = true diff --git a/crates/optimism/evm/src/execute.rs b/crates/optimism/evm/src/execute.rs index 659d7128c..b5199f69e 100644 --- a/crates/optimism/evm/src/execute.rs +++ b/crates/optimism/evm/src/execute.rs @@ -1,6 +1,8 @@ //! Optimism block executor. -use crate::{l1::ensure_create2_deployer, OptimismBlockExecutionError, OptimismEvmConfig}; +use crate::{ + l1::ensure_create2_deployer, OpChainSpec, OptimismBlockExecutionError, OptimismEvmConfig, +}; use alloy_primitives::{BlockNumber, U256}; use reth_chainspec::{ChainSpec, EthereumHardforks, OptimismHardfork}; use reth_evm::{ @@ -36,7 +38,10 @@ pub struct OpExecutorProvider { impl OpExecutorProvider { /// Creates a new default optimism executor provider. pub fn optimism(chain_spec: Arc) -> Self { - Self::new(chain_spec, Default::default()) + Self::new( + chain_spec.clone(), + OptimismEvmConfig::new(Arc::new(OpChainSpec { inner: (*chain_spec).clone() })), + ) } } @@ -270,7 +275,6 @@ where self.executor.evm_config.fill_cfg_and_block_env( &mut cfg, &mut block_env, - self.chain_spec(), header, total_difficulty, ); @@ -442,6 +446,7 @@ where #[cfg(test)] mod tests { use super::*; + use crate::OpChainSpec; use alloy_primitives::{b256, Address, StorageKey, StorageValue}; use reth_chainspec::ChainSpecBuilder; use reth_primitives::{ @@ -480,7 +485,12 @@ mod tests { } fn executor_provider(chain_spec: Arc) -> OpExecutorProvider { - OpExecutorProvider { chain_spec, evm_config: Default::default() } + OpExecutorProvider { + evm_config: OptimismEvmConfig::new(Arc::new(OpChainSpec { + inner: (*chain_spec).clone(), + })), + chain_spec, + } } #[test] diff --git a/crates/optimism/evm/src/lib.rs b/crates/optimism/evm/src/lib.rs index 0e46e3464..95399ada4 100644 --- a/crates/optimism/evm/src/lib.rs +++ b/crates/optimism/evm/src/lib.rs @@ -10,14 +10,15 @@ #![cfg(feature = "optimism")] use alloy_primitives::{Address, U256}; -use reth_chainspec::ChainSpec; use reth_evm::{ConfigureEvm, ConfigureEvmEnv}; +use reth_optimism_chainspec::OpChainSpec; use reth_primitives::{ revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv}, transaction::FillTxEnv, Head, Header, TransactionSigned, }; use reth_revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector}; +use std::sync::Arc; mod config; pub use config::{revm_spec, revm_spec_by_timestamp_after_bedrock}; @@ -31,9 +32,23 @@ pub use error::OptimismBlockExecutionError; use revm_primitives::{Bytes, Env, OptimismFields, TxKind}; /// Optimism-related EVM configuration. -#[derive(Debug, Default, Clone, Copy)] +#[derive(Debug, Clone)] #[non_exhaustive] -pub struct OptimismEvmConfig; +pub struct OptimismEvmConfig { + chain_spec: Arc, +} + +impl OptimismEvmConfig { + /// Creates a new [`OptimismEvmConfig`] with the given chain spec. + pub const fn new(chain_spec: Arc) -> Self { + Self { chain_spec } + } + + /// Returns the chain spec associated with this configuration. + pub fn chain_spec(&self) -> &OpChainSpec { + &self.chain_spec + } +} impl ConfigureEvmEnv for OptimismEvmConfig { fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) { @@ -88,12 +103,11 @@ impl ConfigureEvmEnv for OptimismEvmConfig { fn fill_cfg_env( &self, cfg_env: &mut CfgEnvWithHandlerCfg, - chain_spec: &ChainSpec, header: &Header, total_difficulty: U256, ) { let spec_id = revm_spec( - chain_spec, + self.chain_spec(), &Head { number: header.number, timestamp: header.timestamp, @@ -103,11 +117,11 @@ impl ConfigureEvmEnv for OptimismEvmConfig { }, ); - cfg_env.chain_id = chain_spec.chain().id(); + cfg_env.chain_id = self.chain_spec.chain().id(); cfg_env.perf_analyse_created_bytecodes = AnalysisKind::Analyse; cfg_env.handler_cfg.spec_id = spec_id; - cfg_env.handler_cfg.is_optimism = chain_spec.is_optimism(); + cfg_env.handler_cfg.is_optimism = self.chain_spec.is_optimism(); } } @@ -142,7 +156,7 @@ mod tests { use reth_evm::execute::ProviderError; use reth_primitives::{ revm_primitives::{BlockEnv, CfgEnv, SpecId}, - Genesis, Header, KECCAK_EMPTY, + Genesis, Header, BASE_MAINNET, KECCAK_EMPTY, }; use reth_revm::{ db::{CacheDB, EmptyDBTyped}, @@ -150,7 +164,11 @@ mod tests { JournaledState, }; use revm_primitives::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg}; - use std::collections::HashSet; + use std::{collections::HashSet, sync::Arc}; + + fn test_evm_config() -> OptimismEvmConfig { + OptimismEvmConfig::new(BASE_MAINNET.clone()) + } #[test] fn test_fill_cfg_and_block_env() { @@ -178,13 +196,8 @@ mod tests { // Use the `OptimismEvmConfig` to fill the `cfg_env` and `block_env` based on the ChainSpec, // Header, and total difficulty - OptimismEvmConfig::default().fill_cfg_and_block_env( - &mut cfg_env, - &mut block_env, - &chain_spec, - &header, - total_difficulty, - ); + OptimismEvmConfig::new(Arc::new(OpChainSpec { inner: chain_spec.clone() })) + .fill_cfg_and_block_env(&mut cfg_env, &mut block_env, &header, total_difficulty); // Assert that the chain ID in the `cfg_env` is correctly set to the chain ID of the // ChainSpec @@ -194,7 +207,7 @@ mod tests { #[test] fn test_evm_configure() { // Create a default `OptimismEvmConfig` - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); // Initialize an empty database wrapped in CacheDB let db = CacheDB::>::default(); @@ -234,7 +247,7 @@ mod tests { #[test] fn test_evm_with_env_default_spec() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); @@ -254,7 +267,7 @@ mod tests { #[test] fn test_evm_with_env_custom_cfg() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); @@ -284,7 +297,7 @@ mod tests { #[test] fn test_evm_with_env_custom_block_and_tx() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); @@ -317,7 +330,7 @@ mod tests { #[test] fn test_evm_with_spec_id() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); @@ -336,7 +349,7 @@ mod tests { #[test] fn test_evm_with_inspector() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); @@ -380,7 +393,7 @@ mod tests { #[test] fn test_evm_with_env_and_default_inspector() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); let env_with_handler = EnvWithHandlerCfg::default(); @@ -399,7 +412,7 @@ mod tests { #[test] fn test_evm_with_env_inspector_and_custom_cfg() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); let cfg = CfgEnv::default().with_chain_id(111); @@ -423,7 +436,7 @@ mod tests { #[test] fn test_evm_with_env_inspector_and_custom_block_tx() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); // Create custom block and tx environment @@ -454,7 +467,7 @@ mod tests { #[test] fn test_evm_with_env_inspector_and_spec_id() { - let evm_config = OptimismEvmConfig::default(); + let evm_config = test_evm_config(); let db = CacheDB::>::default(); let handler_cfg = HandlerCfg { spec_id: SpecId::ECOTONE, ..Default::default() }; diff --git a/crates/optimism/node/src/node.rs b/crates/optimism/node/src/node.rs index 74549b315..d4fabdbf1 100644 --- a/crates/optimism/node/src/node.rs +++ b/crates/optimism/node/src/node.rs @@ -16,6 +16,7 @@ use reth_node_builder::{ node::{FullNodeTypes, NodeTypes, NodeTypesWithEngine}, BuilderContext, Node, PayloadBuilderConfig, }; +use reth_optimism_chainspec::OpChainSpec; use reth_optimism_consensus::OptimismBeaconConsensus; use reth_optimism_rpc::OpEthApi; use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; @@ -66,10 +67,7 @@ impl OptimismNode { ComponentsBuilder::default() .node_types::() .pool(OptimismPoolBuilder::default()) - .payload(OptimismPayloadBuilder::new( - compute_pending_block, - OptimismEvmConfig::default(), - )) + .payload(OptimismPayloadBuilder::new(compute_pending_block)) .network(OptimismNetworkBuilder { disable_txpool_gossip, disable_discovery_v4: !discovery_v4, @@ -136,8 +134,9 @@ where ctx: &BuilderContext, ) -> eyre::Result<(Self::EVM, Self::Executor)> { let chain_spec = ctx.chain_spec(); - let evm_config = OptimismEvmConfig::default(); - let executor = OpExecutorProvider::new(chain_spec, evm_config); + let evm_config = + OptimismEvmConfig::new(Arc::new(OpChainSpec { inner: (*chain_spec).clone() })); + let executor = OpExecutorProvider::new(chain_spec, evm_config.clone()); Ok((evm_config, executor)) } @@ -225,7 +224,7 @@ where /// A basic optimism payload service builder #[derive(Debug, Default, Clone)] -pub struct OptimismPayloadBuilder { +pub struct OptimismPayloadBuilder { /// By default the pending block equals the latest block /// to save resources and not leak txs from the tx-pool, /// this flag enables computing of the pending block @@ -235,32 +234,30 @@ pub struct OptimismPayloadBuilder { /// will use the payload attributes from the latest block. Note /// that this flag is not yet functional. pub compute_pending_block: bool, - /// The EVM configuration to use for the payload builder. - pub evm_config: EVM, } -impl OptimismPayloadBuilder { - /// Create a new instance with the given `compute_pending_block` flag and evm config. - pub const fn new(compute_pending_block: bool, evm_config: EVM) -> Self { - Self { compute_pending_block, evm_config } +impl OptimismPayloadBuilder { + /// Create a new instance with the given `compute_pending_block` flag. + pub const fn new(compute_pending_block: bool) -> Self { + Self { compute_pending_block } } -} -impl PayloadServiceBuilder for OptimismPayloadBuilder -where - Node: FullNodeTypes< - Types: NodeTypesWithEngine, - >, - Pool: TransactionPool + Unpin + 'static, - EVM: ConfigureEvm, -{ - async fn spawn_payload_service( + /// A helper method to initialize [`PayloadBuilderService`] with the given EVM config. + pub fn spawn( self, + evm_config: Evm, ctx: &BuilderContext, pool: Pool, - ) -> eyre::Result> { + ) -> eyre::Result> + where + Node: FullNodeTypes< + Types: NodeTypesWithEngine, + >, + Pool: TransactionPool + Unpin + 'static, + Evm: ConfigureEvm, + { let payload_builder = - reth_optimism_payload_builder::OptimismPayloadBuilder::new(self.evm_config) + reth_optimism_payload_builder::OptimismPayloadBuilder::new(evm_config) .set_compute_pending_block(self.compute_pending_block); let conf = ctx.payload_builder_config(); @@ -288,6 +285,26 @@ where } } +impl PayloadServiceBuilder for OptimismPayloadBuilder +where + Node: FullNodeTypes< + Types: NodeTypesWithEngine, + >, + Pool: TransactionPool + Unpin + 'static, +{ + async fn spawn_payload_service( + self, + ctx: &BuilderContext, + pool: Pool, + ) -> eyre::Result> { + self.spawn( + OptimismEvmConfig::new(Arc::new(OpChainSpec { inner: (*ctx.chain_spec()).clone() })), + ctx, + pool, + ) + } +} + /// A basic optimism network builder. #[derive(Debug, Default, Clone)] pub struct OptimismNetworkBuilder { diff --git a/crates/rpc/rpc-builder/tests/it/utils.rs b/crates/rpc/rpc-builder/tests/it/utils.rs index 141044920..ee7a064ab 100644 --- a/crates/rpc/rpc-builder/tests/it/utils.rs +++ b/crates/rpc/rpc-builder/tests/it/utils.rs @@ -130,5 +130,5 @@ pub fn test_rpc_builder() -> RpcModuleBuilder< .with_network(NoopNetwork::default()) .with_executor(TokioTaskExecutor::default()) .with_events(TestCanonStateSubscriptions::default()) - .with_evm_config(EthEvmConfig::default()) + .with_evm_config(EthEvmConfig::new(MAINNET.clone())) } diff --git a/crates/rpc/rpc/src/eth/core.rs b/crates/rpc/rpc/src/eth/core.rs index 3868bfcab..b1848d9a4 100644 --- a/crates/rpc/rpc/src/eth/core.rs +++ b/crates/rpc/rpc/src/eth/core.rs @@ -384,8 +384,8 @@ mod tests { >( provider: P, ) -> EthApi { - let evm_config = EthEvmConfig::default(); - let cache = EthStateCache::spawn(provider.clone(), Default::default(), evm_config); + let evm_config = EthEvmConfig::new(provider.chain_spec()); + let cache = EthStateCache::spawn(provider.clone(), Default::default(), evm_config.clone()); let fee_history_cache = FeeHistoryCache::new(cache.clone(), FeeHistoryCacheConfig::default()); diff --git a/crates/rpc/rpc/src/eth/helpers/state.rs b/crates/rpc/rpc/src/eth/helpers/state.rs index 9446d235d..2d2ce5d5a 100644 --- a/crates/rpc/rpc/src/eth/helpers/state.rs +++ b/crates/rpc/rpc/src/eth/helpers/state.rs @@ -44,6 +44,7 @@ where mod tests { use super::*; use alloy_primitives::{Address, StorageKey, StorageValue, U256}; + use reth_chainspec::MAINNET; use reth_evm_ethereum::EthEvmConfig; use reth_primitives::{constants::ETHEREUM_BLOCK_GAS_LIMIT, KECCAK_EMPTY}; use reth_provider::test_utils::{ExtendedAccount, MockEthProvider, NoopProvider}; @@ -58,9 +59,10 @@ mod tests { fn noop_eth_api() -> EthApi { let pool = testing_pool(); - let evm_config = EthEvmConfig::default(); + let evm_config = EthEvmConfig::new(MAINNET.clone()); - let cache = EthStateCache::spawn(NoopProvider::default(), Default::default(), evm_config); + let cache = + EthStateCache::spawn(NoopProvider::default(), Default::default(), evm_config.clone()); EthApi::new( NoopProvider::default(), pool, @@ -80,12 +82,13 @@ mod tests { accounts: HashMap, ) -> EthApi { let pool = testing_pool(); - let evm_config = EthEvmConfig::default(); - let mock_provider = MockEthProvider::default(); + + let evm_config = EthEvmConfig::new(mock_provider.chain_spec()); mock_provider.extend_accounts(accounts); - let cache = EthStateCache::spawn(mock_provider.clone(), Default::default(), evm_config); + let cache = + EthStateCache::spawn(mock_provider.clone(), Default::default(), evm_config.clone()); EthApi::new( mock_provider.clone(), pool, diff --git a/crates/rpc/rpc/src/eth/helpers/transaction.rs b/crates/rpc/rpc/src/eth/helpers/transaction.rs index 2bf64536b..7188ecf0a 100644 --- a/crates/rpc/rpc/src/eth/helpers/transaction.rs +++ b/crates/rpc/rpc/src/eth/helpers/transaction.rs @@ -53,6 +53,7 @@ where #[cfg(test)] mod tests { use alloy_primitives::{hex_literal::hex, Bytes}; + use reth_chainspec::ChainSpecProvider; use reth_evm_ethereum::EthEvmConfig; use reth_network_api::noop::NoopNetwork; use reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT; @@ -74,8 +75,8 @@ mod tests { let pool = testing_pool(); - let evm_config = EthEvmConfig::default(); - let cache = EthStateCache::spawn(noop_provider, Default::default(), evm_config); + let evm_config = EthEvmConfig::new(noop_provider.chain_spec()); + let cache = EthStateCache::spawn(noop_provider, Default::default(), evm_config.clone()); let fee_history_cache = FeeHistoryCache::new(cache.clone(), FeeHistoryCacheConfig::default()); let eth_api = EthApi::new( diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index adf73bd70..29fd29fdc 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -1014,13 +1014,7 @@ impl EvmEnvProvider for BlockchainProvider2 { let total_difficulty = self .header_td_by_number(header.number)? .ok_or_else(|| ProviderError::HeaderNotFound(header.number.into()))?; - evm_config.fill_cfg_and_block_env( - cfg, - block_env, - &self.database.chain_spec(), - header, - total_difficulty, - ); + evm_config.fill_cfg_and_block_env(cfg, block_env, header, total_difficulty); Ok(()) } @@ -1050,7 +1044,7 @@ impl EvmEnvProvider for BlockchainProvider2 { let total_difficulty = self .header_td_by_number(header.number)? .ok_or_else(|| ProviderError::HeaderNotFound(header.number.into()))?; - evm_config.fill_cfg_env(cfg, &self.database.chain_spec(), header, total_difficulty); + evm_config.fill_cfg_env(cfg, header, total_difficulty); Ok(()) } } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 666edb148..fa608d2c1 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -2480,13 +2480,7 @@ impl EvmEnvProvider for DatabaseProvider { let total_difficulty = self .header_td_by_number(header.number)? .ok_or_else(|| ProviderError::HeaderNotFound(header.number.into()))?; - evm_config.fill_cfg_and_block_env( - cfg, - block_env, - &self.chain_spec, - header, - total_difficulty, - ); + evm_config.fill_cfg_and_block_env(cfg, block_env, header, total_difficulty); Ok(()) } @@ -2516,7 +2510,7 @@ impl EvmEnvProvider for DatabaseProvider { let total_difficulty = self .header_td_by_number(header.number)? .ok_or_else(|| ProviderError::HeaderNotFound(header.number.into()))?; - evm_config.fill_cfg_env(cfg, &self.chain_spec, header, total_difficulty); + evm_config.fill_cfg_env(cfg, header, total_difficulty); Ok(()) } } diff --git a/examples/custom-engine-types/src/main.rs b/examples/custom-engine-types/src/main.rs index a8f9496f0..274fb4aae 100644 --- a/examples/custom-engine-types/src/main.rs +++ b/examples/custom-engine-types/src/main.rs @@ -45,9 +45,12 @@ use reth_node_api::{ validate_version_specific_fields, EngineTypes, PayloadAttributes, PayloadBuilderAttributes, }; use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig}; -use reth_node_ethereum::node::{ - EthereumAddOns, EthereumConsensusBuilder, EthereumExecutorBuilder, EthereumNetworkBuilder, - EthereumPoolBuilder, +use reth_node_ethereum::{ + node::{ + EthereumAddOns, EthereumConsensusBuilder, EthereumExecutorBuilder, EthereumNetworkBuilder, + EthereumPoolBuilder, + }, + EthEvmConfig, }; use reth_payload_builder::{ error::PayloadBuilderError, EthBuiltPayload, EthPayloadBuilderAttributes, PayloadBuilderHandle, @@ -302,7 +305,10 @@ where // This reuses the default EthereumPayloadBuilder to build the payload // but any custom logic can be implemented here - reth_ethereum_payload_builder::EthereumPayloadBuilder::default().try_build(BuildArguments { + reth_ethereum_payload_builder::EthereumPayloadBuilder::new(EthEvmConfig::new( + chain_spec.clone(), + )) + .try_build(BuildArguments { client, pool, cached_reads, @@ -332,7 +338,7 @@ where attributes, chain_spec, } = config; - >::build_empty_payload(&reth_ethereum_payload_builder::EthereumPayloadBuilder::default(),client, + >::build_empty_payload(&reth_ethereum_payload_builder::EthereumPayloadBuilder::new(EthEvmConfig::new(chain_spec.clone())),client, PayloadConfig { initialized_block_env, initialized_cfg, parent_block, extra_data, attributes: attributes.0, chain_spec }) } } diff --git a/examples/custom-evm/src/main.rs b/examples/custom-evm/src/main.rs index 5986aaac3..1ddc53d1e 100644 --- a/examples/custom-evm/src/main.rs +++ b/examples/custom-evm/src/main.rs @@ -4,7 +4,11 @@ use alloy_genesis::Genesis; use reth::{ - builder::{components::ExecutorBuilder, BuilderContext, NodeBuilder}, + builder::{ + components::{ExecutorBuilder, PayloadServiceBuilder}, + BuilderContext, NodeBuilder, + }, + payload::{EthBuiltPayload, EthPayloadBuilderAttributes}, primitives::{ address, revm_primitives::{Env, PrecompileResult}, @@ -16,27 +20,40 @@ use reth::{ precompile::{Precompile, PrecompileOutput, PrecompileSpecId}, ContextPrecompiles, Database, Evm, EvmBuilder, GetInspector, }, + rpc::types::engine::PayloadAttributes, tasks::TaskManager, + transaction_pool::TransactionPool, }; -use reth_chainspec::{Chain, ChainSpec, Head}; +use reth_chainspec::{Chain, ChainSpec}; use reth_evm_ethereum::EthEvmConfig; -use reth_node_api::{ConfigureEvm, ConfigureEvmEnv, FullNodeTypes, NodeTypes}; +use reth_node_api::{ + ConfigureEvm, ConfigureEvmEnv, FullNodeTypes, NodeTypes, NodeTypesWithEngine, PayloadTypes, +}; use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig}; use reth_node_ethereum::{ node::{EthereumAddOns, EthereumPayloadBuilder}, EthExecutorProvider, EthereumNode, }; use reth_primitives::{ - revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv}, + revm_primitives::{CfgEnvWithHandlerCfg, TxEnv}, Address, Header, TransactionSigned, U256, }; use reth_tracing::{RethTracer, Tracer}; use std::sync::Arc; /// Custom EVM configuration -#[derive(Debug, Clone, Copy, Default)] +#[derive(Debug, Clone)] #[non_exhaustive] -pub struct MyEvmConfig; +pub struct MyEvmConfig { + /// Wrapper around mainnet configuration + inner: EthEvmConfig, +} + +impl MyEvmConfig { + pub const fn new(chain_spec: Arc) -> Self { + Self { inner: EthEvmConfig::new(chain_spec) } + } +} impl MyEvmConfig { /// Sets the precompiles to the EVM handler @@ -73,29 +90,14 @@ impl ConfigureEvmEnv for MyEvmConfig { fn fill_cfg_env( &self, cfg_env: &mut CfgEnvWithHandlerCfg, - chain_spec: &ChainSpec, header: &Header, total_difficulty: U256, ) { - let spec_id = reth_evm_ethereum::revm_spec( - chain_spec, - &Head { - number: header.number, - timestamp: header.timestamp, - difficulty: header.difficulty, - total_difficulty, - hash: Default::default(), - }, - ); - - cfg_env.chain_id = chain_spec.chain().id(); - cfg_env.perf_analyse_created_bytecodes = AnalysisKind::Analyse; - - cfg_env.handler_cfg.spec_id = spec_id; + self.inner.fill_cfg_env(cfg_env, header, total_difficulty); } fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) { - EthEvmConfig::default().fill_tx_env(tx_env, transaction, sender) + self.inner.fill_tx_env(tx_env, transaction, sender); } fn fill_tx_env_system_contract_call( @@ -105,7 +107,7 @@ impl ConfigureEvmEnv for MyEvmConfig { contract: Address, data: Bytes, ) { - EthEvmConfig::default().fill_tx_env_system_contract_call(env, caller, contract, data) + self.inner.fill_tx_env_system_contract_call(env, caller, contract, data); } } @@ -154,12 +156,38 @@ where ctx: &BuilderContext, ) -> eyre::Result<(Self::EVM, Self::Executor)> { Ok(( - MyEvmConfig::default(), - EthExecutorProvider::new(ctx.chain_spec(), MyEvmConfig::default()), + MyEvmConfig::new(ctx.chain_spec()), + EthExecutorProvider::new(ctx.chain_spec(), MyEvmConfig::new(ctx.chain_spec())), )) } } +/// Builds a regular ethereum block executor that uses the custom EVM. +#[derive(Debug, Default, Clone)] +#[non_exhaustive] +pub struct MyPayloadBuilder { + inner: EthereumPayloadBuilder, +} + +impl PayloadServiceBuilder for MyPayloadBuilder +where + Types: NodeTypesWithEngine, + Node: FullNodeTypes, + Pool: TransactionPool + Unpin + 'static, + Types::Engine: PayloadTypes< + BuiltPayload = EthBuiltPayload, + PayloadAttributes = PayloadAttributes, + PayloadBuilderAttributes = EthPayloadBuilderAttributes, + >, +{ + async fn spawn_payload_service( + self, + ctx: &BuilderContext, + pool: Pool, + ) -> eyre::Result> { + self.inner.spawn(MyEvmConfig::new(ctx.chain_spec()), ctx, pool) + } +} #[tokio::main] async fn main() -> eyre::Result<()> { let _guard = RethTracer::new().init()?; @@ -187,7 +215,7 @@ async fn main() -> eyre::Result<()> { .with_components( EthereumNode::components() .executor(MyExecutorBuilder::default()) - .payload(EthereumPayloadBuilder::new(MyEvmConfig::default())), + .payload(MyPayloadBuilder::default()), ) .with_add_ons::() .launch() diff --git a/examples/custom-payload-builder/src/main.rs b/examples/custom-payload-builder/src/main.rs index 1b612252b..4861f8284 100644 --- a/examples/custom-payload-builder/src/main.rs +++ b/examples/custom-payload-builder/src/main.rs @@ -22,7 +22,7 @@ use reth::{ use reth_basic_payload_builder::BasicPayloadJobGeneratorConfig; use reth_chainspec::ChainSpec; use reth_node_api::NodeTypesWithEngine; -use reth_node_ethereum::{node::EthereumAddOns, EthEngineTypes, EthereumNode}; +use reth_node_ethereum::{node::EthereumAddOns, EthEngineTypes, EthEvmConfig, EthereumNode}; use reth_payload_builder::PayloadBuilderService; pub mod generator; @@ -57,7 +57,9 @@ where ctx.task_executor().clone(), payload_job_config, ctx.chain_spec().clone(), - reth_ethereum_payload_builder::EthereumPayloadBuilder::default(), + reth_ethereum_payload_builder::EthereumPayloadBuilder::new(EthEvmConfig::new( + ctx.chain_spec(), + )), ); let (payload_service, payload_builder) = diff --git a/examples/rpc-db/src/main.rs b/examples/rpc-db/src/main.rs index 7c978cf08..e7409417f 100644 --- a/examples/rpc-db/src/main.rs +++ b/examples/rpc-db/src/main.rs @@ -66,7 +66,7 @@ async fn main() -> eyre::Result<()> { .with_noop_pool() .with_noop_network() .with_executor(TokioTaskExecutor::default()) - .with_evm_config(EthEvmConfig::default()) + .with_evm_config(EthEvmConfig::new(spec)) .with_events(TestCanonStateSubscriptions::default()); // Pick which namespaces to expose. diff --git a/examples/stateful-precompile/src/main.rs b/examples/stateful-precompile/src/main.rs index e6d83acf0..ae5b9feab 100644 --- a/examples/stateful-precompile/src/main.rs +++ b/examples/stateful-precompile/src/main.rs @@ -50,13 +50,19 @@ pub struct PrecompileCache { } /// Custom EVM configuration -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] #[non_exhaustive] pub struct MyEvmConfig { + inner: EthEvmConfig, precompile_cache: Arc>, } impl MyEvmConfig { + /// Creates a new instance. + pub fn new(chain_spec: Arc) -> Self { + Self { inner: EthEvmConfig::new(chain_spec), precompile_cache: Default::default() } + } + /// Sets the precompiles to the EVM handler /// /// This will be invoked when the EVM is created via [ConfigureEvm::evm] or @@ -139,17 +145,16 @@ impl StatefulPrecompileMut for WrappedPrecompile { impl ConfigureEvmEnv for MyEvmConfig { fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) { - EthEvmConfig::default().fill_tx_env(tx_env, transaction, sender) + self.inner.fill_tx_env(tx_env, transaction, sender) } fn fill_cfg_env( &self, cfg_env: &mut CfgEnvWithHandlerCfg, - chain_spec: &ChainSpec, header: &Header, total_difficulty: U256, ) { - EthEvmConfig::default().fill_cfg_env(cfg_env, chain_spec, header, total_difficulty) + self.inner.fill_cfg_env(cfg_env, header, total_difficulty) } fn fill_tx_env_system_contract_call( @@ -159,7 +164,7 @@ impl ConfigureEvmEnv for MyEvmConfig { contract: Address, data: Bytes, ) { - EthEvmConfig::default().fill_tx_env_system_contract_call(env, caller, contract, data) + self.inner.fill_tx_env_system_contract_call(env, caller, contract, data) } } @@ -216,7 +221,10 @@ where self, ctx: &BuilderContext, ) -> eyre::Result<(Self::EVM, Self::Executor)> { - let evm_config = MyEvmConfig { precompile_cache: self.precompile_cache.clone() }; + let evm_config = MyEvmConfig { + inner: EthEvmConfig::new(ctx.chain_spec()), + precompile_cache: self.precompile_cache.clone(), + }; Ok((evm_config.clone(), EthExecutorProvider::new(ctx.chain_spec(), evm_config))) } }