Skip to content

Commit

Permalink
Merge branch 'main' into nish-upstream-5e72c69-fix-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec authored Jan 27, 2025
2 parents a500951 + b69695a commit 5920457
Show file tree
Hide file tree
Showing 37 changed files with 2,217 additions and 529 deletions.
7 changes: 6 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 31 additions & 23 deletions crates/config/src/zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ pub fn config_zksolc_settings(config: &Config) -> Result<ZkSolcSettings, SolcErr
Ok(config.zksync.settings(libraries, config.evm_version, config.via_ir))
}

/// Return the configured `zksolc` compiler
///
/// If not `offline`, will install the default version automatically
/// Will fallback to `zksolc` present in the environment
pub fn config_zksolc_compiler(config: &Config) -> Result<ZkSolcCompiler, SolcError> {
let zksolc = if let Some(zksolc) =
config_ensure_zksolc(config.zksync.zksolc.as_ref(), config.offline)?
{
zksolc
} else if !config.offline {
let default_version = semver::Version::new(1, 5, 10);
let mut zksolc = ZkSolc::find_installed_version(&default_version)?;
if zksolc.is_none() {
ZkSolc::blocking_install(&default_version)?;
zksolc = ZkSolc::find_installed_version(&default_version)?;
}
zksolc.unwrap_or_else(|| panic!("Could not install zksolc v{default_version}"))
} else {
"zksolc".into()
};

Ok(ZkSolcCompiler { zksolc, solc: config_solc_compiler(config)? })
}

/// Create a new zkSync project
pub fn config_create_project(
config: &Config,
Expand Down Expand Up @@ -193,23 +217,7 @@ pub fn config_create_project(
builder = builder.sparse_output(filter);
}

let zksolc = if let Some(zksolc) =
config_ensure_zksolc(config.zksync.zksolc.as_ref(), config.offline)?
{
zksolc
} else if !config.offline {
let default_version = semver::Version::new(1, 5, 10);
let mut zksolc = ZkSolc::find_installed_version(&default_version)?;
if zksolc.is_none() {
ZkSolc::blocking_install(&default_version)?;
zksolc = ZkSolc::find_installed_version(&default_version)?;
}
zksolc.unwrap_or_else(|| panic!("Could not install zksolc v{default_version}"))
} else {
"zksolc".into()
};

let zksolc_compiler = ZkSolcCompiler { zksolc, solc: config_solc_compiler(config)? };
let zksolc_compiler = config_zksolc_compiler(config)?;

let project = builder.build(zksolc_compiler)?;

Expand All @@ -229,12 +237,12 @@ pub fn config_create_project(
fn config_solc_compiler(config: &Config) -> Result<SolcCompiler, SolcError> {
if let Some(path) = &config.zksync.solc_path {
if !path.is_file() {
return Err(SolcError::msg(format!("`solc` {} does not exist", path.display())))
return Err(SolcError::msg(format!("`solc` {} does not exist", path.display())));
}
let version = get_solc_version_info(path)?.version;
let solc =
Solc::new_with_version(path, Version::new(version.major, version.minor, version.patch));
return Ok(SolcCompiler::Specific(solc))
return Ok(SolcCompiler::Specific(solc));
}

if let Some(ref solc) = config.solc {
Expand All @@ -256,7 +264,7 @@ fn config_solc_compiler(config: &Config) -> Result<SolcCompiler, SolcError> {
}
SolcReq::Local(path) => {
if !path.is_file() {
return Err(SolcError::msg(format!("`solc` {} does not exist", path.display())))
return Err(SolcError::msg(format!("`solc` {} does not exist", path.display())));
}
let version = get_solc_version_info(path)?.version;
Solc::new_with_version(
Expand Down Expand Up @@ -307,7 +315,7 @@ pub fn config_ensure_zksolc(
if offline {
return Err(SolcError::msg(format!(
"can't install missing zksolc {version} in offline mode"
)))
)));
}
ZkSolc::blocking_install(version)?;
zksolc = ZkSolc::find_installed_version(version)?;
Expand All @@ -319,12 +327,12 @@ pub fn config_ensure_zksolc(
return Err(SolcError::msg(format!(
"`zksolc` {} does not exist",
zksolc.display()
)))
)));
}
Some(zksolc.clone())
}
};
return Ok(zksolc)
return Ok(zksolc);
}

Ok(None)
Expand Down
1 change: 1 addition & 0 deletions crates/evm/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ foundry-evm-core.workspace = true
foundry-evm-coverage.workspace = true
foundry-evm-fuzz.workspace = true
foundry-evm-traces.workspace = true
foundry-linking.workspace = true
foundry-zksync-core.workspace = true
foundry-zksync-compilers.workspace = true
foundry-zksync-inspectors.workspace = true
Expand Down
19 changes: 18 additions & 1 deletion crates/evm/evm/src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::{
borrow::Cow,
time::{Duration, Instant},
};
use strategy::ExecutorStrategy;
use strategy::{DeployLibKind, DeployLibResult, ExecutorStrategy};

mod builder;
pub use builder::ExecutorBuilder;
Expand Down Expand Up @@ -303,6 +303,23 @@ impl Executor {
self.deploy_with_env(env, rd)
}

/// Deploys a library contract and commits the new state to the underlying database.
///
/// Executes a `deploy_kind` transaction with the provided parameters
/// and persistent database state modifications.
///
/// Will return a list of deployment results and transaction requests
/// Will also ensure nonce is increased for the sender
pub fn deploy_library(
&mut self,
from: Address,
kind: DeployLibKind,
value: U256,
rd: Option<&RevertDecoder>,
) -> Result<Vec<DeployLibResult>, EvmError> {
self.strategy.runner.deploy_library(self, from, kind, value, rd)
}

/// Deploys a contract using the given `env` and commits the new state to the underlying
/// database.
///
Expand Down
81 changes: 77 additions & 4 deletions crates/evm/evm/src/executors/strategy.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
use std::{any::Any, fmt::Debug};
use std::{any::Any, fmt::Debug, path::Path};

use alloy_primitives::{Address, U256};
use alloy_serde::OtherFields;
use eyre::Result;
use foundry_cheatcodes::strategy::{
CheatcodeInspectorStrategy, EvmCheatcodeInspectorStrategyRunner,
};
use foundry_evm_core::backend::{strategy::BackendStrategy, Backend, BackendResult, CowBackend};
use foundry_zksync_compilers::dual_compiled_contracts::DualCompiledContracts;
use foundry_compilers::ProjectCompileOutput;
use foundry_config::Config;
use foundry_evm_core::{
backend::{strategy::BackendStrategy, Backend, BackendResult, CowBackend},
decode::RevertDecoder,
};
use foundry_linking::LinkerError;
use foundry_zksync_compilers::{
compilers::{artifact_output::zk::ZkArtifactOutput, zksolc::ZkSolcCompiler},
dual_compiled_contracts::DualCompiledContracts,
};
use revm::{
primitives::{Env, EnvWithHandlerCfg, ResultAndState},
DatabaseRef,
};

use crate::inspectors::InspectorStack;

use super::Executor;
use super::{EvmError, Executor};

mod libraries;
pub use libraries::*;

pub trait ExecutorStrategyContext: Debug + Send + Sync + Any {
/// Clone the strategy context.
Expand Down Expand Up @@ -68,9 +80,32 @@ pub trait ExecutorStrategyRunner: Debug + Send + Sync + ExecutorStrategyExt {
amount: U256,
) -> BackendResult<()>;

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

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

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

fn link(
&self,
ctx: &mut dyn ExecutorStrategyContext,
config: &Config,
root: &Path,
input: &ProjectCompileOutput,
deployer: Address,
) -> Result<LinkOutput, LinkerError>;

/// Deploys a library, applying state changes
fn deploy_library(
&self,
executor: &mut Executor,
from: Address,
input: DeployLibKind,
value: U256,
rd: Option<&RevertDecoder>,
) -> Result<Vec<DeployLibResult>, EvmError>;

/// Execute a transaction and *WITHOUT* applying state changes.
fn call(
&self,
Expand Down Expand Up @@ -110,6 +145,13 @@ pub trait ExecutorStrategyExt {
) {
}

fn zksync_set_compilation_output(
&self,
_ctx: &mut dyn ExecutorStrategyContext,
_output: ProjectCompileOutput<ZkSolcCompiler, ZkArtifactOutput>,
) {
}

/// Set the fork environment on the context.
fn zksync_set_fork_env(
&self,
Expand Down Expand Up @@ -153,6 +195,10 @@ impl ExecutorStrategyRunner for EvmExecutorStrategyRunner {
Ok(())
}

fn get_balance(&self, executor: &mut Executor, address: Address) -> BackendResult<U256> {
executor.get_balance(address)
}

fn set_nonce(
&self,
executor: &mut Executor,
Expand All @@ -166,6 +212,33 @@ impl ExecutorStrategyRunner for EvmExecutorStrategyRunner {
Ok(())
}

fn get_nonce(&self, executor: &mut Executor, address: Address) -> BackendResult<u64> {
executor.get_nonce(address)
}

fn link(
&self,
_: &mut dyn ExecutorStrategyContext,
_: &Config,
root: &Path,
input: &ProjectCompileOutput,
deployer: Address,
) -> Result<LinkOutput, LinkerError> {
self.link_impl(root, input, deployer)
}

/// Deploys a library, applying state changes
fn deploy_library(
&self,
executor: &mut Executor,
from: Address,
kind: DeployLibKind,
value: U256,
rd: Option<&RevertDecoder>,
) -> Result<Vec<DeployLibResult>, EvmError> {
self.deploy_library_impl(executor, from, kind, value, rd)
}

fn call(
&self,
_ctx: &dyn ExecutorStrategyContext,
Expand Down
Loading

0 comments on commit 5920457

Please sign in to comment.