From 39f9024db44aec3e5e73c5d81c4f5981c34f732c Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Wed, 8 Jan 2025 15:26:51 +0400 Subject: [PATCH] refactor: Simplify strategy traits --- crates/cheatcodes/src/inspector.rs | 11 +++------ crates/cheatcodes/src/strategy.rs | 29 ++++-------------------- crates/evm/core/src/backend/cow.rs | 7 +----- crates/evm/core/src/backend/mod.rs | 2 +- crates/evm/core/src/backend/strategy.rs | 24 +++----------------- crates/evm/evm/src/executors/mod.rs | 4 ++-- crates/evm/evm/src/executors/strategy.rs | 28 ++++------------------- crates/strategy/zksync/src/backend.rs | 20 ++++------------ crates/strategy/zksync/src/cheatcode.rs | 18 ++++----------- crates/strategy/zksync/src/executor.rs | 22 +++++------------- 10 files changed, 34 insertions(+), 131 deletions(-) diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index f0a742b5e..d44bb7e7d 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -801,9 +801,7 @@ impl Cheatcodes { }]); } - if let Some(result) = - self.strategy.runner.clone().zksync_try_create(self, ecx, &input, executor) - { + if let Some(result) = self.strategy.runner.zksync_try_create(self, ecx, &input, executor) { return Some(result); } @@ -1222,9 +1220,7 @@ where { }]); } - if let Some(result) = - self.strategy.runner.clone().zksync_try_call(self, ecx, call, executor) - { + if let Some(result) = self.strategy.runner.zksync_try_call(self, ecx, call, executor) { return Some(result); } @@ -2285,8 +2281,7 @@ fn apply_dispatch( } // Apply the cheatcode. - let runner = ccx.state.strategy.runner.new_cloned(); - let mut result = runner.apply_full(cheat, ccx, executor); + let mut result = ccx.state.strategy.runner.apply_full(cheat, ccx, executor); // Format the error message to include the cheatcode name. if let Err(e) = &mut result { diff --git a/crates/cheatcodes/src/strategy.rs b/crates/cheatcodes/src/strategy.rs index 7956664a3..3b0546915 100644 --- a/crates/cheatcodes/src/strategy.rs +++ b/crates/cheatcodes/src/strategy.rs @@ -42,33 +42,26 @@ impl CheatcodeInspectorStrategyContext for () { #[derive(Debug)] pub struct CheatcodeInspectorStrategy { /// Strategy runner. - pub runner: Box, + pub runner: &'static dyn CheatcodeInspectorStrategyRunner, /// Strategy context. pub context: Box, } impl CheatcodeInspectorStrategy { pub fn new_evm() -> Self { - Self { - runner: Box::new(EvmCheatcodeInspectorStrategyRunner::default()), - context: Box::new(()), - } + Self { runner: &EvmCheatcodeInspectorStrategyRunner, context: Box::new(()) } } } impl Clone for CheatcodeInspectorStrategy { fn clone(&self) -> Self { - Self { runner: self.runner.new_cloned(), context: self.context.new_cloned() } + Self { runner: self.runner, context: self.context.new_cloned() } } } pub trait CheatcodeInspectorStrategyRunner: Debug + Send + Sync + CheatcodeInspectorStrategyExt { - fn name(&self) -> &'static str; - - fn new_cloned(&self) -> Box; - fn apply_full( &self, cheatcode: &dyn DynCheatcode, @@ -171,17 +164,9 @@ pub trait CheatcodeInspectorStrategyExt { } #[derive(Debug, Default, Clone)] -pub struct EvmCheatcodeInspectorStrategyRunner {} +pub struct EvmCheatcodeInspectorStrategyRunner; impl CheatcodeInspectorStrategyRunner for EvmCheatcodeInspectorStrategyRunner { - fn name(&self) -> &'static str { - "evm" - } - - fn new_cloned(&self) -> Box { - Box::new(self.clone()) - } - fn record_broadcastable_create_transactions( &self, _ctx: &mut dyn CheatcodeInspectorStrategyContext, @@ -256,11 +241,5 @@ impl CheatcodeInspectorStrategyRunner for EvmCheatcodeInspectorStrategyRunner { impl CheatcodeInspectorStrategyExt for EvmCheatcodeInspectorStrategyRunner {} -impl Clone for Box { - fn clone(&self) -> Self { - self.new_cloned() - } -} - struct _ObjectSafe0(dyn CheatcodeInspectorStrategyRunner); struct _ObjectSafe1(dyn CheatcodeInspectorStrategyExt); diff --git a/crates/evm/core/src/backend/cow.rs b/crates/evm/core/src/backend/cow.rs index 44e91a2a8..972184c35 100644 --- a/crates/evm/core/src/backend/cow.rs +++ b/crates/evm/core/src/backend/cow.rs @@ -72,12 +72,7 @@ impl<'a> CowBackend<'a> { self.is_initialized = false; self.spec_id = env.handler_cfg.spec_id; - self.backend.strategy.runner.clone().inspect( - self.backend.to_mut(), - env, - inspector, - inspect_ctx, - ) + self.backend.strategy.runner.inspect(self.backend.to_mut(), env, inspector, inspect_ctx) } /// Returns whether there was a state snapshot failure in the backend. diff --git a/crates/evm/core/src/backend/mod.rs b/crates/evm/core/src/backend/mod.rs index 7d168d9d5..33a7d9256 100644 --- a/crates/evm/core/src/backend/mod.rs +++ b/crates/evm/core/src/backend/mod.rs @@ -794,7 +794,7 @@ impl Backend { inspect_ctx: Box, ) -> eyre::Result { self.initialize(env); - self.strategy.runner.clone().inspect(self, env, inspector, inspect_ctx) + self.strategy.runner.inspect(self, env, inspector, inspect_ctx) } /// Returns the `EnvWithHandlerCfg` with the current `spec_id` set. diff --git a/crates/evm/core/src/backend/strategy.rs b/crates/evm/core/src/backend/strategy.rs index 3025fdfbc..571e3cd69 100644 --- a/crates/evm/core/src/backend/strategy.rs +++ b/crates/evm/core/src/backend/strategy.rs @@ -46,7 +46,7 @@ impl BackendStrategyContext for () { #[derive(Debug)] pub struct BackendStrategy { /// Strategy runner. - pub runner: Box, + pub runner: &'static dyn BackendStrategyRunner, /// Strategy context. pub context: Box, } @@ -54,21 +54,17 @@ pub struct BackendStrategy { impl BackendStrategy { /// Create a new instance of [BackendStrategy] pub fn new_evm() -> Self { - Self { runner: Box::new(EvmBackendStrategyRunner), context: Box::new(()) } + Self { runner: &EvmBackendStrategyRunner, context: Box::new(()) } } } impl Clone for BackendStrategy { fn clone(&self) -> Self { - Self { runner: self.runner.new_cloned(), context: self.context.new_cloned() } + Self { runner: self.runner, context: self.context.new_cloned() } } } pub trait BackendStrategyRunner: Debug + Send + Sync + BackendStrategyRunnerExt { - fn name(&self) -> &'static str; - - fn new_cloned(&self) -> Box; - fn inspect( &self, backend: &mut Backend, @@ -128,14 +124,6 @@ struct _ObjectSafe(dyn BackendStrategyRunner); pub struct EvmBackendStrategyRunner; impl BackendStrategyRunner for EvmBackendStrategyRunner { - fn name(&self) -> &'static str { - "evm" - } - - fn new_cloned(&self) -> Box { - Box::new(self.clone()) - } - fn inspect( &self, backend: &mut Backend, @@ -298,9 +286,3 @@ impl EvmBackendMergeStrategy { fork_db.accounts.insert(addr, acc); } } - -impl Clone for Box { - fn clone(&self) -> Self { - self.new_cloned() - } -} diff --git a/crates/evm/evm/src/executors/mod.rs b/crates/evm/evm/src/executors/mod.rs index 0e6c8975b..158fdac8d 100644 --- a/crates/evm/evm/src/executors/mod.rs +++ b/crates/evm/evm/src/executors/mod.rs @@ -248,7 +248,7 @@ impl Executor { /// Set the balance of an account. pub fn set_balance(&mut self, address: Address, amount: U256) -> BackendResult<()> { trace!(?address, ?amount, "setting account balance"); - self.strategy.runner.clone().set_balance(self, address, amount) + self.strategy.runner.set_balance(self, address, amount) } /// Gets the balance of an account @@ -258,7 +258,7 @@ impl Executor { /// Set the nonce of an account. pub fn set_nonce(&mut self, address: Address, nonce: u64) -> BackendResult<()> { - self.strategy.runner.clone().set_nonce(self, address, nonce) + self.strategy.runner.set_nonce(self, address, nonce) } /// Returns the nonce of an account. diff --git a/crates/evm/evm/src/executors/strategy.rs b/crates/evm/evm/src/executors/strategy.rs index 6c6c6af03..a719e841a 100644 --- a/crates/evm/evm/src/executors/strategy.rs +++ b/crates/evm/evm/src/executors/strategy.rs @@ -43,28 +43,24 @@ impl ExecutorStrategyContext for () { #[derive(Debug)] pub struct ExecutorStrategy { /// Strategy runner. - pub runner: Box, + pub runner: &'static dyn ExecutorStrategyRunner, /// Strategy context. pub context: Box, } impl ExecutorStrategy { pub fn new_evm() -> Self { - Self { runner: Box::new(EvmExecutorStrategyRunner::default()), context: Box::new(()) } + Self { runner: &EvmExecutorStrategyRunner, context: Box::new(()) } } } impl Clone for ExecutorStrategy { fn clone(&self) -> Self { - Self { runner: self.runner.new_cloned(), context: self.context.new_cloned() } + Self { runner: self.runner, context: self.context.new_cloned() } } } pub trait ExecutorStrategyRunner: Debug + Send + Sync + ExecutorStrategyExt { - fn name(&self) -> &'static str; - - fn new_cloned(&self) -> Box; - fn set_balance( &self, executor: &mut Executor, @@ -140,17 +136,9 @@ pub trait ExecutorStrategyExt { /// Implements [ExecutorStrategyRunner] for EVM. #[derive(Debug, Default, Clone)] -pub struct EvmExecutorStrategyRunner {} +pub struct EvmExecutorStrategyRunner; impl ExecutorStrategyRunner for EvmExecutorStrategyRunner { - fn name(&self) -> &'static str { - "evm" - } - - fn new_cloned(&self) -> Box { - Box::new(self.clone()) - } - fn set_balance( &self, executor: &mut Executor, @@ -209,16 +197,10 @@ impl ExecutorStrategyRunner for EvmExecutorStrategyRunner { _ctx: &dyn ExecutorStrategyContext, ) -> CheatcodeInspectorStrategy { CheatcodeInspectorStrategy { - runner: Box::new(EvmCheatcodeInspectorStrategyRunner::default()), + runner: &EvmCheatcodeInspectorStrategyRunner, context: Box::new(()), } } } impl ExecutorStrategyExt for EvmExecutorStrategyRunner {} - -impl Clone for Box { - fn clone(&self) -> Self { - self.new_cloned() - } -} diff --git a/crates/strategy/zksync/src/backend.rs b/crates/strategy/zksync/src/backend.rs index a0ec8ce09..bc955116e 100644 --- a/crates/strategy/zksync/src/backend.rs +++ b/crates/strategy/zksync/src/backend.rs @@ -66,19 +66,9 @@ impl BackendStrategyContext for ZksyncBackendStrategyContext { /// ZKsync implementation for [BackendStrategyRunner]. #[derive(Debug, Default, Clone, Serialize, Deserialize)] -pub struct ZksyncBackendStrategyRunner { - evm: EvmBackendStrategyRunner, -} +pub struct ZksyncBackendStrategyRunner; impl BackendStrategyRunner for ZksyncBackendStrategyRunner { - fn name(&self) -> &'static str { - "zk" - } - - fn new_cloned(&self) -> Box { - Box::new(self.clone()) - } - fn inspect( &self, backend: &mut Backend, @@ -87,7 +77,7 @@ impl BackendStrategyRunner for ZksyncBackendStrategyRunner { inspect_ctx: Box, ) -> Result { if !is_zksync_inspect_context(inspect_ctx.as_ref()) { - return self.evm.inspect(backend, env, inspector, inspect_ctx); + return EvmBackendStrategyRunner.inspect(backend, env, inspector, inspect_ctx); } let inspect_ctx = get_inspect_context(inspect_ctx); @@ -137,7 +127,7 @@ impl BackendStrategyRunner for ZksyncBackendStrategyRunner { active_journaled_state: &JournaledState, fork_journaled_state: &mut JournaledState, ) { - self.evm.merge_journaled_state_data( + EvmBackendStrategyRunner.merge_journaled_state_data( ctx, addr, active_journaled_state, @@ -161,7 +151,7 @@ impl BackendStrategyRunner for ZksyncBackendStrategyRunner { active: &ForkDB, fork_db: &mut ForkDB, ) { - self.evm.merge_db_account_data(ctx, addr, active, fork_db); + EvmBackendStrategyRunner.merge_db_account_data(ctx, addr, active, fork_db); let ctx = get_context(ctx); let zk_state = &ZksyncMergeState { persistent_immutable_keys: &ctx.persistent_immutable_keys }; @@ -235,7 +225,7 @@ pub trait ZksyncBackendStrategyBuilder { impl ZksyncBackendStrategyBuilder for BackendStrategy { fn new_zksync() -> Self { Self { - runner: Box::new(ZksyncBackendStrategyRunner::default()), + runner: &ZksyncBackendStrategyRunner, context: Box::new(ZksyncBackendStrategyContext::default()), } } diff --git a/crates/strategy/zksync/src/cheatcode.rs b/crates/strategy/zksync/src/cheatcode.rs index a651cd22a..d4acb4ec4 100644 --- a/crates/strategy/zksync/src/cheatcode.rs +++ b/crates/strategy/zksync/src/cheatcode.rs @@ -91,9 +91,7 @@ macro_rules! bail { /// ZKsync implementation for [CheatcodeInspectorStrategyRunner]. #[derive(Debug, Default, Clone)] -pub struct ZksyncCheatcodeInspectorStrategyRunner { - evm: EvmCheatcodeInspectorStrategyRunner, -} +pub struct ZksyncCheatcodeInspectorStrategyRunner; /// Context for [ZksyncCheatcodeInspectorStrategyRunner]. #[derive(Debug, Default, Clone)] @@ -246,14 +244,6 @@ impl ZkPersistNonceUpdate { } impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner { - fn name(&self) -> &'static str { - "zk" - } - - fn new_cloned(&self) -> Box { - Box::new(self.clone()) - } - fn base_contract_deployed(&self, ctx: &mut dyn CheatcodeInspectorStrategyContext) { let ctx = get_context(ctx); @@ -565,7 +555,7 @@ impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner ) { let ctx_zk = get_context(ctx); if !ctx_zk.using_zk_vm { - return self.evm.record_broadcastable_create_transactions( + return EvmCheatcodeInspectorStrategyRunner.record_broadcastable_create_transactions( ctx, config, input, @@ -692,7 +682,7 @@ impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner let ctx_zk = get_context(ctx); if !ctx_zk.using_zk_vm { - return self.evm.record_broadcastable_call_transactions( + return EvmCheatcodeInspectorStrategyRunner.record_broadcastable_call_transactions( ctx, config, call, @@ -1536,7 +1526,7 @@ pub trait ZksyncCheatcodeInspectorStrategyBuilder { impl ZksyncCheatcodeInspectorStrategyBuilder for CheatcodeInspectorStrategy { fn new_zksync(dual_compiled_contracts: DualCompiledContracts, zk_env: ZkEnv) -> Self { Self { - runner: Box::new(ZksyncCheatcodeInspectorStrategyRunner::default()), + runner: &ZksyncCheatcodeInspectorStrategyRunner, context: Box::new(ZksyncCheatcodeInspectorStrategyContext::new( dual_compiled_contracts, zk_env, diff --git a/crates/strategy/zksync/src/executor.rs b/crates/strategy/zksync/src/executor.rs index 715f7ca07..3679631fd 100644 --- a/crates/strategy/zksync/src/executor.rs +++ b/crates/strategy/zksync/src/executor.rs @@ -50,9 +50,7 @@ impl ExecutorStrategyContext for ZksyncExecutorStrategyContext { /// Defines the [ExecutorStrategyRunner] strategy for ZKsync. #[derive(Debug, Default, Clone)] -pub struct ZksyncExecutorStrategyRunner { - evm: EvmExecutorStrategyRunner, -} +pub struct ZksyncExecutorStrategyRunner; fn get_context_ref(ctx: &dyn ExecutorStrategyContext) -> &ZksyncExecutorStrategyContext { ctx.as_any_ref().downcast_ref().expect("expected ZksyncExecutorStrategyContext") @@ -63,21 +61,13 @@ fn get_context(ctx: &mut dyn ExecutorStrategyContext) -> &mut ZksyncExecutorStra } impl ExecutorStrategyRunner for ZksyncExecutorStrategyRunner { - fn name(&self) -> &'static str { - "zk" - } - - fn new_cloned(&self) -> Box { - Box::new(self.clone()) - } - fn set_balance( &self, executor: &mut Executor, address: Address, amount: U256, ) -> BackendResult<()> { - self.evm.set_balance(executor, address, amount)?; + EvmExecutorStrategyRunner.set_balance(executor, address, amount)?; let (address, slot) = foundry_zksync_core::state::get_balance_storage(address); executor.backend.insert_account_storage(address, slot, amount)?; @@ -91,7 +81,7 @@ impl ExecutorStrategyRunner for ZksyncExecutorStrategyRunner { address: Address, nonce: u64, ) -> BackendResult<()> { - self.evm.set_nonce(executor, address, nonce)?; + EvmExecutorStrategyRunner.set_nonce(executor, address, nonce)?; let (address, slot) = foundry_zksync_core::state::get_nonce_storage(address); // fetch the full nonce to preserve account's deployment nonce @@ -130,7 +120,7 @@ impl ExecutorStrategyRunner for ZksyncExecutorStrategyRunner { let ctx = get_context_ref(ctx); match ctx.transaction_context.as_ref() { - None => self.evm.call(ctx, backend, env, executor_env, inspector), + None => EvmExecutorStrategyRunner.call(ctx, backend, env, executor_env, inspector), Some(zk_tx) => backend.inspect( env, inspector, @@ -154,7 +144,7 @@ impl ExecutorStrategyRunner for ZksyncExecutorStrategyRunner { let ctx = get_context(ctx); match ctx.transaction_context.take() { - None => self.evm.transact(ctx, backend, env, executor_env, inspector), + None => EvmExecutorStrategyRunner.transact(ctx, backend, env, executor_env, inspector), Some(zk_tx) => { // apply fork-related env instead of cheatcode handler // since it won't be set by zkEVM @@ -256,7 +246,7 @@ pub trait ZksyncExecutorStrategyBuilder { impl ZksyncExecutorStrategyBuilder for ExecutorStrategy { fn new_zksync() -> Self { Self { - runner: Box::new(ZksyncExecutorStrategyRunner::default()), + runner: &ZksyncExecutorStrategyRunner, context: Box::new(ZksyncExecutorStrategyContext::default()), } }