Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Dec 19, 2024
1 parent 1876c01 commit bbe27b9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 58 deletions.
11 changes: 1 addition & 10 deletions crates/evm/core/src/backend/strategy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
fmt::Debug,
sync::{Arc, Mutex},
};
use std::fmt::Debug;

use super::{BackendInner, Fork, ForkDB, ForkType, FoundryEvmInMemoryDB};
use alloy_primitives::{Address, U256};
Expand All @@ -17,8 +14,6 @@ pub struct BackendStrategyForkInfo<'a> {
pub trait BackendStrategy: Debug + Send + Sync {
fn name(&self) -> &'static str;

fn new_cloned(&self) -> Arc<Mutex<dyn BackendStrategy>>;

/// When creating or switching forks, we update the AccountInfo of the contract
fn update_fork_db(
&self,
Expand Down Expand Up @@ -61,10 +56,6 @@ impl BackendStrategy for EvmBackendStrategy {
"evm"
}

fn new_cloned(&self) -> Arc<Mutex<dyn BackendStrategy>> {
Arc::new(Mutex::new(self.clone()))
}

fn update_fork_db(
&self,
fork_info: BackendStrategyForkInfo<'_>,
Expand Down
32 changes: 12 additions & 20 deletions crates/evm/evm/src/executors/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,22 @@ use revm::{

use super::Executor;

pub trait ExecutorStrategy: Debug + Send + Sync {
pub trait ExecutorStrategy: Debug + Send {
fn name(&self) -> &'static str;

fn new_cloned(&self) -> Box<dyn ExecutorStrategy>;

fn set_balance(
&mut self,
&self,
executor: &mut Executor,
address: Address,
amount: U256,
) -> BackendResult<()>;

fn set_nonce(
&mut self,
executor: &mut Executor,
address: Address,
nonce: u64,
) -> BackendResult<()>;
fn set_nonce(&self, executor: &mut Executor, address: Address, nonce: u64)
-> BackendResult<()>;

fn set_inspect_context(&mut self, other_fields: OtherFields);
fn set_inspect_context(&self, other_fields: OtherFields);

fn call_inspect(
&self,
Expand All @@ -48,7 +44,7 @@ pub trait ExecutorStrategy: Debug + Send + Sync {
) -> eyre::Result<ResultAndState>;

fn transact_inspect(
&mut self,
&self,
db: &mut dyn DatabaseExt,
env: &mut EnvWithHandlerCfg,
_executor_env: &EnvWithHandlerCfg,
Expand All @@ -64,13 +60,9 @@ pub trait ExecutorStrategy: Debug + Send + Sync {
pub trait ExecutorStrategyExt: ExecutorStrategy {
fn new_cloned_ext(&self) -> Box<dyn ExecutorStrategyExt>;

fn zksync_set_dual_compiled_contracts(
&mut self,
_dual_compiled_contracts: DualCompiledContracts,
) {
}
fn zksync_set_dual_compiled_contracts(&self, _dual_compiled_contracts: DualCompiledContracts) {}

fn zksync_set_fork_env(&mut self, _fork_url: &str, _env: &Env) -> Result<()> {
fn zksync_set_fork_env(&self, _fork_url: &str, _env: &Env) -> Result<()> {
Ok(())
}
}
Expand All @@ -87,7 +79,7 @@ impl ExecutorStrategy for EvmExecutorStrategy {
Box::new(self.clone())
}

fn set_inspect_context(&mut self, _other_fields: OtherFields) {}
fn set_inspect_context(&self, _other_fields: OtherFields) {}

/// Executes the configured test call of the `env` without committing state changes.
///
Expand All @@ -114,7 +106,7 @@ impl ExecutorStrategy for EvmExecutorStrategy {
/// Note: in case there are any cheatcodes executed that modify the environment, this will
/// update the given `env` with the new values.
fn transact_inspect(
&mut self,
&self,
db: &mut dyn DatabaseExt,
env: &mut EnvWithHandlerCfg,
_executor_env: &EnvWithHandlerCfg,
Expand All @@ -130,7 +122,7 @@ impl ExecutorStrategy for EvmExecutorStrategy {
}

fn set_balance(
&mut self,
&self,
executor: &mut Executor,
address: Address,
amount: U256,
Expand All @@ -144,7 +136,7 @@ impl ExecutorStrategy for EvmExecutorStrategy {
}

fn set_nonce(
&mut self,
&self,
executor: &mut Executor,
address: Address,
nonce: u64,
Expand Down
9 changes: 1 addition & 8 deletions crates/strategy/zksync/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
collections::hash_map::Entry,
sync::{Arc, Mutex},
};
use std::collections::hash_map::Entry;

use alloy_primitives::{map::HashMap, Address, U256};
use foundry_evm::backend::strategy::BackendStrategyExt;
Expand Down Expand Up @@ -43,10 +40,6 @@ impl BackendStrategy for ZksyncBackendStrategy {
"zk"
}

fn new_cloned(&self) -> Arc<Mutex<dyn BackendStrategy>> {
Arc::new(Mutex::new(self.clone()))
}

/// When creating or switching forks, we update the AccountInfo of the contract.
fn update_fork_db(
&self,
Expand Down
47 changes: 27 additions & 20 deletions crates/strategy/zksync/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::RefCell;

use alloy_primitives::{Address, U256};
use alloy_rpc_types::serde_helpers::OtherFields;
use alloy_zksync::provider::{zksync_provider, ZksyncProvider};
Expand Down Expand Up @@ -28,6 +30,11 @@ use crate::{
#[derive(Debug, Default, Clone)]
pub struct ZksyncExecutorStrategy {
evm: EvmExecutorStrategy,
inner: RefCell<ZksyncExecutorStrategyInner>,
}

#[derive(Debug, Default, Clone)]
struct ZksyncExecutorStrategyInner {
inspect_context: Option<ZkTransactionMetadata>,
persisted_factory_deps: HashMap<H256, Vec<u8>>,
dual_compiled_contracts: DualCompiledContracts,
Expand All @@ -43,13 +50,13 @@ impl ExecutorStrategy for ZksyncExecutorStrategy {
Box::new(self.clone())
}

fn set_inspect_context(&mut self, other_fields: OtherFields) {
fn set_inspect_context(&self, other_fields: OtherFields) {
let maybe_context = get_zksync_transaction_metadata(&other_fields);
self.inspect_context = maybe_context;
self.inner.borrow_mut().inspect_context = maybe_context;
}

fn set_balance(
&mut self,
&self,
executor: &mut Executor,
address: Address,
amount: U256,
Expand All @@ -63,7 +70,7 @@ impl ExecutorStrategy for ZksyncExecutorStrategy {
}

fn set_nonce(
&mut self,
&self,
executor: &mut Executor,
address: Address,
nonce: u64,
Expand All @@ -87,8 +94,8 @@ impl ExecutorStrategy for ZksyncExecutorStrategy {

fn new_cheatcode_inspector_strategy(&self) -> Box<dyn CheatcodeInspectorStrategyExt> {
Box::new(ZksyncCheatcodeInspectorStrategy::new(
self.dual_compiled_contracts.clone(),
self.zk_env.clone(),
self.inner.borrow().dual_compiled_contracts.clone(),
self.inner.borrow().zk_env.clone(),
))
}

Expand All @@ -98,27 +105,30 @@ impl ExecutorStrategy for ZksyncExecutorStrategy {
env: &mut EnvWithHandlerCfg,
inspector: &mut dyn InspectorExt,
) -> eyre::Result<ResultAndState> {
match self.inspect_context.as_ref() {
let self_ = self.inner.borrow();
match self_.inspect_context.as_ref() {
None => self.evm.call_inspect(db, env, inspector),
Some(zk_tx) => foundry_zksync_core::vm::transact(
Some(&mut self.persisted_factory_deps.clone()),
Some(&mut self_.persisted_factory_deps.clone()),
Some(zk_tx.factory_deps.clone()),
zk_tx.paymaster_data.clone(),
env,
&self.zk_env,
&self_.zk_env,
db,
),
}
}

fn transact_inspect(
&mut self,
&self,
db: &mut dyn DatabaseExt,
env: &mut EnvWithHandlerCfg,
executor_env: &EnvWithHandlerCfg,
inspector: &mut dyn InspectorExt,
) -> eyre::Result<ResultAndState> {
match self.inspect_context.take() {
let mut self_ = self.inner.borrow_mut();
let zk_env = self_.zk_env.clone();
match self_.inspect_context.take() {
None => self.evm.transact_inspect(db, env, executor_env, inspector),
Some(zk_tx) => {
// apply fork-related env instead of cheatcode handler
Expand All @@ -127,11 +137,11 @@ impl ExecutorStrategy for ZksyncExecutorStrategy {
env.tx.gas_price = executor_env.tx.gas_price;

foundry_zksync_core::vm::transact(
Some(&mut self.persisted_factory_deps),
Some(&mut self_.persisted_factory_deps),
Some(zk_tx.factory_deps),
zk_tx.paymaster_data,
env,
&self.zk_env,
&zk_env,
db,
)
}
Expand All @@ -144,14 +154,11 @@ impl ExecutorStrategyExt for ZksyncExecutorStrategy {
Box::new(self.clone())
}

fn zksync_set_dual_compiled_contracts(
&mut self,
dual_compiled_contracts: DualCompiledContracts,
) {
self.dual_compiled_contracts = dual_compiled_contracts;
fn zksync_set_dual_compiled_contracts(&self, dual_compiled_contracts: DualCompiledContracts) {
self.inner.borrow_mut().dual_compiled_contracts = dual_compiled_contracts;
}

fn zksync_set_fork_env(&mut self, fork_url: &str, env: &Env) -> Result<()> {
fn zksync_set_fork_env(&self, fork_url: &str, env: &Env) -> Result<()> {
let provider = zksync_provider().with_recommended_fillers().on_http(fork_url.parse()?);
let block_number = env.block.number.try_into()?;
// TODO(zk): switch to getFeeParams call when it is implemented for anvil-zksync
Expand All @@ -162,7 +169,7 @@ impl ExecutorStrategyExt for ZksyncExecutorStrategy {
.flatten();

if let Some(block_details) = maybe_block_details {
self.zk_env = ZkEnv {
self.inner.borrow_mut().zk_env = ZkEnv {
l1_gas_price: block_details
.l1_gas_price
.try_into()
Expand Down

0 comments on commit bbe27b9

Please sign in to comment.