diff --git a/crates/engine/invalid-block-hooks/src/witness.rs b/crates/engine/invalid-block-hooks/src/witness.rs index 51978311fa..bb227e3041 100644 --- a/crates/engine/invalid-block-hooks/src/witness.rs +++ b/crates/engine/invalid-block-hooks/src/witness.rs @@ -84,7 +84,8 @@ where EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default()), ); - let mut system_caller = SystemCaller::new(&self.evm_config, self.provider.chain_spec()); + let mut system_caller = + SystemCaller::new(self.evm_config.clone(), self.provider.chain_spec()); // Apply pre-block system contract calls. system_caller.apply_pre_execution_changes(&block.clone().unseal(), &mut evm)?; diff --git a/crates/engine/util/src/reorg.rs b/crates/engine/util/src/reorg.rs index bd7b1b9564..611095101f 100644 --- a/crates/engine/util/src/reorg.rs +++ b/crates/engine/util/src/reorg.rs @@ -286,7 +286,7 @@ where let mut evm = evm_config.evm_with_env(&mut state, env); // apply eip-4788 pre block contract call - let mut system_caller = SystemCaller::new(evm_config, chain_spec); + let mut system_caller = SystemCaller::new(evm_config.clone(), chain_spec); system_caller.apply_beacon_root_contract_call( reorg_target.timestamp, diff --git a/crates/ethereum/evm/src/execute.rs b/crates/ethereum/evm/src/execute.rs index 6513cf75fa..108e1f87c4 100644 --- a/crates/ethereum/evm/src/execute.rs +++ b/crates/ethereum/evm/src/execute.rs @@ -141,10 +141,12 @@ where where DB: Database, DB::Error: Into + Display, - F: OnStateHook, + F: OnStateHook + 'static, { - let mut system_caller = - SystemCaller::new(&self.evm_config, &self.chain_spec).with_state_hook(state_hook); + let mut system_caller = SystemCaller::new(self.evm_config.clone(), &self.chain_spec); + if let Some(hook) = state_hook { + system_caller.with_state_hook(Some(Box::new(hook) as Box)); + } system_caller.apply_pre_execution_changes(block, &mut evm)?; @@ -290,7 +292,7 @@ where state_hook: Option, ) -> Result where - F: OnStateHook, + F: OnStateHook + 'static, { // 1. prepare state on new block self.on_new_block(&block.header); @@ -396,7 +398,7 @@ where state_hook: F, ) -> Result where - F: OnStateHook, + F: OnStateHook + 'static, { let BlockExecutionInput { block, total_difficulty } = input; let EthExecuteOutput { receipts, requests, gas_used } = self diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index 97237efa8b..248aa3486d 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -160,7 +160,7 @@ where let block_number = initialized_block_env.number.to::(); - let mut system_caller = SystemCaller::new(&evm_config, chain_spec.clone()); + let mut system_caller = SystemCaller::new(evm_config.clone(), chain_spec.clone()); // apply eip-4788 pre block contract call system_caller diff --git a/crates/evm/src/either.rs b/crates/evm/src/either.rs index 8022c68c43..82f84301f0 100644 --- a/crates/evm/src/either.rs +++ b/crates/evm/src/either.rs @@ -97,7 +97,7 @@ where state_hook: F, ) -> Result where - F: OnStateHook, + F: OnStateHook + 'static, { match self { Self::Left(a) => a.execute_with_state_hook(input, state_hook), diff --git a/crates/evm/src/execute.rs b/crates/evm/src/execute.rs index 145eca29c9..476c695e7a 100644 --- a/crates/evm/src/execute.rs +++ b/crates/evm/src/execute.rs @@ -56,7 +56,7 @@ pub trait Executor { state_hook: F, ) -> Result where - F: OnStateHook; + F: OnStateHook + 'static; } /// A general purpose executor that can execute multiple inputs in sequence, validate the outputs, diff --git a/crates/evm/src/system_calls/mod.rs b/crates/evm/src/system_calls/mod.rs index 43baa1c766..5dc3f35bd3 100644 --- a/crates/evm/src/system_calls/mod.rs +++ b/crates/evm/src/system_calls/mod.rs @@ -1,7 +1,7 @@ //! System contract call functions. use crate::ConfigureEvm; -use alloc::vec::Vec; +use alloc::{boxed::Box, vec::Vec}; use core::fmt::Display; use reth_chainspec::EthereumHardforks; use reth_execution_errors::BlockExecutionError; @@ -42,27 +42,26 @@ impl OnStateHook for NoopHook { /// /// This can be used to chain system transaction calls. #[allow(missing_debug_implementations)] -pub struct SystemCaller<'a, EvmConfig, Chainspec, Hook = NoopHook> { - evm_config: &'a EvmConfig, +pub struct SystemCaller { + evm_config: EvmConfig, chain_spec: Chainspec, /// Optional hook to be called after each state change. - hook: Option, + hook: Option>, } -impl<'a, EvmConfig, Chainspec> SystemCaller<'a, EvmConfig, Chainspec, NoopHook> { +impl SystemCaller { /// Create a new system caller with the given EVM config, database, and chain spec, and creates /// the EVM with the given initialized config and block environment. - pub const fn new(evm_config: &'a EvmConfig, chain_spec: Chainspec) -> Self { + pub const fn new(evm_config: EvmConfig, chain_spec: Chainspec) -> Self { Self { evm_config, chain_spec, hook: None } } + /// Installs a custom hook to be called after each state change. - pub fn with_state_hook( - self, - hook: Option, - ) -> SystemCaller<'a, EvmConfig, Chainspec, H> { - let Self { evm_config, chain_spec, .. } = self; - SystemCaller { evm_config, chain_spec, hook } + pub fn with_state_hook(&mut self, hook: Option>) -> &mut Self { + self.hook = hook; + self } + /// Convenience method to consume the type and drop borrowed fields pub fn finish(self) {} } @@ -85,11 +84,10 @@ where .build() } -impl SystemCaller<'_, EvmConfig, Chainspec, Hook> +impl SystemCaller where EvmConfig: ConfigureEvm
, Chainspec: EthereumHardforks, - Hook: OnStateHook, { /// Apply pre execution changes. pub fn apply_pre_execution_changes( diff --git a/crates/optimism/evm/src/execute.rs b/crates/optimism/evm/src/execute.rs index f4abb8c887..ee0d028e42 100644 --- a/crates/optimism/evm/src/execute.rs +++ b/crates/optimism/evm/src/execute.rs @@ -121,10 +121,12 @@ where ) -> Result<(Vec, u64), BlockExecutionError> where DB: Database + Display>, - F: OnStateHook, + F: OnStateHook + 'static, { - let mut system_caller = - SystemCaller::new(&self.evm_config, &self.chain_spec).with_state_hook(state_hook); + let mut system_caller = SystemCaller::new(self.evm_config.clone(), &self.chain_spec); + if let Some(hook) = state_hook { + system_caller.with_state_hook(Some(Box::new(hook) as Box)); + } // apply pre execution changes system_caller.apply_beacon_root_contract_call( @@ -306,7 +308,7 @@ where state_hook: Option, ) -> Result<(Vec, u64), BlockExecutionError> where - F: OnStateHook, + F: OnStateHook + 'static, { // 1. prepare state on new block self.on_new_block(&block.header); @@ -410,7 +412,7 @@ where state_hook: F, ) -> Result where - F: OnStateHook, + F: OnStateHook + 'static, { let BlockExecutionInput { block, total_difficulty } = input; let (receipts, gas_used) = self.execute_without_verification_with_state_hook( diff --git a/crates/optimism/payload/src/builder.rs b/crates/optimism/payload/src/builder.rs index 2ff3d33420..0a8dcdb124 100644 --- a/crates/optimism/payload/src/builder.rs +++ b/crates/optimism/payload/src/builder.rs @@ -201,7 +201,7 @@ where ); // apply eip-4788 pre block contract call - let mut system_caller = SystemCaller::new(&evm_config, &chain_spec); + let mut system_caller = SystemCaller::new(evm_config.clone(), &chain_spec); system_caller .pre_block_beacon_root_contract_call( diff --git a/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs b/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs index 85ad7fd181..26e0ffc741 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs @@ -260,8 +260,7 @@ pub trait LoadPendingBlock: EthApiTypes { let chain_spec = self.provider().chain_spec(); - let evm_config = self.evm_config().clone(); - let mut system_caller = SystemCaller::new(&evm_config, chain_spec.clone()); + let mut system_caller = SystemCaller::new(self.evm_config().clone(), chain_spec.clone()); let parent_beacon_block_root = if origin.is_actual_pending() { // apply eip-4788 pre block contract call if we got the block from the CL with the real diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs index 2dc1c5b474..457cbb4811 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/trace.rs @@ -199,7 +199,7 @@ pub trait Trace: LoadState { // apply relevant system calls let mut system_caller = SystemCaller::new( - Trace::evm_config(&this), + Trace::evm_config(&this).clone(), LoadState::provider(&this).chain_spec(), ); system_caller @@ -332,7 +332,7 @@ pub trait Trace: LoadState { // apply relevant system calls let mut system_caller = SystemCaller::new( - Trace::evm_config(&this), + Trace::evm_config(&this).clone(), LoadState::provider(&this).chain_spec(), ); system_caller