Skip to content

Commit

Permalink
refactor: implement new compilers api (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
elfedy authored Aug 21, 2024
1 parent c8fe5a7 commit c4f1f03
Show file tree
Hide file tree
Showing 18 changed files with 588 additions and 279 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

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

11 changes: 5 additions & 6 deletions crates/common/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use foundry_compilers::{
multi::MultiCompilerLanguage,
report::{BasicStdoutReporter, NoReporter, Report},
solc::SolcSettings,
zksolc::{ZkSolc, ZkSolcCompiler},
zksync::{
artifact_output::Artifact as ZkArtifact,
artifact_output::zk::ZkArtifactOutput,
compile::output::ProjectCompileOutput as ZkProjectCompileOutput,
},
Artifact, Project, ProjectBuilder, ProjectCompileOutput, ProjectPathsConfig, SolcConfig,
Expand Down Expand Up @@ -280,7 +281,7 @@ impl ProjectCompiler {
/// Compiles the project.
pub fn zksync_compile(
self,
project: &Project<SolcCompiler>,
project: &Project<ZkSolcCompiler, ZkArtifactOutput>,
maybe_avoid_contracts: Option<Vec<globset::GlobMatcher>>,
) -> Result<ZkProjectCompileOutput> {
// TODO: Avoid process::exit
Expand All @@ -298,10 +299,8 @@ impl ProjectCompiler {
let files = self.files.clone();

{
Report::new(SpinnerReporter::spawn_with(format!(
"Using zksolc-{}",
project.zksync_zksolc.version()?
)));
let zksolc_version = ZkSolc::new(project.compiler.zksolc.clone()).version()?;
Report::new(SpinnerReporter::spawn_with(format!("Using zksolc-{zksolc_version}")));
}
self.zksync_compile_with(&project.paths.root, || {
let files_to_compile =
Expand Down
21 changes: 5 additions & 16 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ use foundry_compilers::{
},
error::SolcError,
solc::{CliSettings, SolcSettings},
zksolc::ZkSolcSettings,
ConfigurableArtifacts, Project, ProjectPathsConfig, VyperLanguage,
ArtifactOutput, ConfigurableArtifacts, Project, ProjectPathsConfig, VyperLanguage,
};
use inflector::Inflector;
use regex::Regex;
Expand Down Expand Up @@ -860,7 +859,10 @@ impl Config {
}

/// Cleans the project.
pub fn cleanup<C: Compiler>(&self, project: &Project<C>) -> Result<(), SolcError> {
pub fn cleanup<C: Compiler, T: ArtifactOutput>(
&self,
project: &Project<C, T>,
) -> Result<(), SolcError> {
project.cleanup()?;

// Remove last test run failures file.
Expand Down Expand Up @@ -1358,19 +1360,6 @@ impl Config {
})
}

/// Returns the configured `zksolc` `Settings` that includes:
/// - all libraries
/// - the optimizer (including details, if configured)
/// - evm version
pub fn zksync_zksolc_settings(&self) -> Result<ZkSolcSettings, SolcError> {
let libraries = match self.parsed_libraries() {
Ok(libs) => self.project_paths::<ProjectPathsConfig>().apply_lib_remappings(libs),
Err(e) => return Err(SolcError::msg(format!("Failed to parse libraries: {e}"))),
};

Ok(self.zksync.settings(libraries, self.evm_version, self.via_ir))
}

/// Returns the default figment
///
/// The default figment reads from the following sources, in ascending
Expand Down
1 change: 0 additions & 1 deletion crates/config/src/zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ impl ZkSyncConfig {
per_contract: Some([OutputSelectionFlag::ABI].into()),
}),
},
solc: self.solc_path.clone(),
cli_settings: CliSettings::default(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ impl BuildArgs {
println!("{}", serde_json::to_string_pretty(&output.output())?);
}
} else {
let zk_project = foundry_zksync_compiler::create_project(&config, config.cache, false)?;
let zk_project =
foundry_zksync_compiler::config_create_project(&config, config.cache, false)?;
let zk_compiler = ProjectCompiler::new()
.print_names(self.names)
.print_sizes(self.sizes)
Expand Down
11 changes: 8 additions & 3 deletions crates/forge/bin/cmd/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloy_signer::Signer;
use alloy_transport::{Transport, TransportError};
use clap::{Parser, ValueHint};
use eyre::{Context, Result};
use forge_verify::RetryArgs;
use forge_verify::{zk_provider::CompilerVerificationContext, RetryArgs};
use foundry_cli::{
opts::{CoreBuildArgs, EthereumOpts, EtherscanOpts, TransactionOpts},
utils::{self, read_constructor_args_file, remove_contract, remove_zk_contract, LoadConfig},
Expand Down Expand Up @@ -117,7 +117,8 @@ impl CreateArgs {
};

let config = self.opts.try_load_config_emit_warnings()?;
let zk_project = foundry_zksync_compiler::create_project(&config, config.cache, false)?;
let zk_project =
foundry_zksync_compiler::config_create_project(&config, config.cache, false)?;
let zk_compiler = ProjectCompiler::new()
.quiet(self.json || self.opts.silent)
.files([target_path.clone()]);
Expand Down Expand Up @@ -352,7 +353,11 @@ impl CreateArgs {
verify.etherscan.key =
config.get_etherscan_config_with_chain(Some(chain.into()))?.map(|c| c.key);

let context = verify.resolve_context().await?;
let context = if verify.zksync {
CompilerVerificationContext::Solc(verify.resolve_context().await?)
} else {
CompilerVerificationContext::ZkSolc(verify.zk_resolve_context().await?)
};

verify.verification_provider()?.preflight_check(verify, context).await?;
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ impl TestArgs {
let output = compiler.compile(&project)?;

let (zk_output, dual_compiled_contracts) = if config.zksync.should_compile() {
let zk_project = foundry_zksync_compiler::create_project(&config, config.cache, false)?;
let zk_project =
foundry_zksync_compiler::config_create_project(&config, config.cache, false)?;

let sources_to_compile = self.get_sources_to_compile(&config, &filter)?;
let zk_compiler = ProjectCompiler::new()
Expand All @@ -304,7 +305,7 @@ impl TestArgs {
let zk_output =
zk_compiler.zksync_compile(&zk_project, config.zksync.avoid_contracts())?;
let dual_compiled_contracts =
DualCompiledContracts::new(&output, &zk_output, &project.paths);
DualCompiledContracts::new(&output, &zk_output, &project.paths, &zk_project.paths);

(Some(zk_output), Some(dual_compiled_contracts))
} else {
Expand Down
38 changes: 11 additions & 27 deletions crates/forge/tests/it/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ use forge::{
};
use foundry_compilers::{
artifacts::{EvmVersion, Libraries, Settings},
multi::MultiCompilerLanguage,
solc::SolcCompiler,
utils::RuntimeOrHandle,
zksolc::ZkSolcCompiler,
zksync::{
cache::ZKSYNC_SOLIDITY_FILES_CACHE_FILENAME,
artifact_output::zk::ZkArtifactOutput,
compile::output::ProjectCompileOutput as ZkProjectCompileOutput,
},
Project, ProjectCompileOutput, ProjectPathsConfig, SolcConfig, Vyper,
Project, ProjectCompileOutput, SolcConfig, Vyper,
};
use foundry_config::{
fs_permissions::PathPermission, Config, FsPermissions, FuzzConfig, FuzzDictionaryConfig,
Expand All @@ -25,7 +24,7 @@ use foundry_evm::{
opts::{Env, EvmOpts},
};
use foundry_test_utils::{fd_lock, init_tracing, TestCommand, ZkSyncNode};
use foundry_zksync_compiler::DualCompiledContracts;
use foundry_zksync_compiler::{DualCompiledContracts, ZKSYNC_SOLIDITY_FILES_CACHE_FILENAME};
use once_cell::sync::Lazy;
use semver::Version;
use std::{
Expand All @@ -35,7 +34,7 @@ use std::{
sync::Arc,
};

type ZkProject = Project<SolcCompiler>;
type ZkProject = Project<ZkSolcCompiler, ZkArtifactOutput>;

pub const RE_PATH_SEPARATOR: &str = "/";
const TESTDATA: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../testdata");
Expand Down Expand Up @@ -90,10 +89,10 @@ impl ForgeTestProfile {
pub fn zk_project(&self) -> ZkProject {
let zk_config = self.zk_config();
let mut zk_project =
foundry_zksync_compiler::create_project(&zk_config, zk_config.cache, false)
foundry_zksync_compiler::config_create_project(&zk_config, zk_config.cache, false)
.expect("failed creating zksync project");
zk_project.paths.zksync_artifacts = zk_config.root.as_ref().join("zk").join("zkout");
zk_project.paths.zksync_cache = zk_config
zk_project.paths.artifacts = zk_config.root.as_ref().join("zk").join("zkout");
zk_project.paths.cache = zk_config
.root
.as_ref()
.join("zk")
Expand Down Expand Up @@ -255,23 +254,8 @@ impl ForgeTestData {
let mut project = zk_config.project().expect("failed obtaining project");
let output = get_compiled(&mut project);
let zk_output = get_zk_compiled(&zk_project);
let layout = ProjectPathsConfig {
root: zk_project.paths.root.clone(),
cache: zk_project.paths.cache.clone(),
artifacts: zk_project.paths.artifacts.clone(),
build_infos: zk_project.paths.build_infos.clone(),
sources: zk_project.paths.sources.clone(),
tests: zk_project.paths.tests.clone(),
scripts: zk_project.paths.scripts.clone(),
libraries: zk_project.paths.libraries.clone(),
remappings: zk_project.paths.remappings.clone(),
include_paths: zk_project.paths.include_paths.clone(),
allowed_paths: zk_project.paths.allowed_paths.clone(),
zksync_artifacts: zk_project.paths.zksync_artifacts.clone(),
zksync_cache: zk_project.paths.zksync_cache.clone(),
_l: std::marker::PhantomData::<MultiCompilerLanguage>,
};
let dual_compiled_contracts = DualCompiledContracts::new(&output, &zk_output, &layout);
let dual_compiled_contracts =
DualCompiledContracts::new(&output, &zk_output, &project.paths, &zk_project.paths);
ZkTestData { dual_compiled_contracts, zk_config, zk_project, output, zk_output }
};

Expand Down Expand Up @@ -489,7 +473,7 @@ pub fn get_zk_compiled(zk_project: &ZkProject) -> ZkProjectCompileOutput {
let mut write = None;

let zk_compiler = foundry_common::compile::ProjectCompiler::new();
if zk_project.paths.zksync_cache.exists() || std::fs::read(&lock_file_path).unwrap() == b"1" {
if zk_project.paths.cache.exists() || std::fs::read(&lock_file_path).unwrap() == b"1" {
drop(read);
write = Some(lock.write().unwrap());
}
Expand Down
4 changes: 2 additions & 2 deletions crates/script/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl PreprocessedState {

// ZK
let dual_compiled_contracts = if script_config.config.zksync.should_compile() {
let zk_project = foundry_zksync_compiler::create_project(
let zk_project = foundry_zksync_compiler::config_create_project(
&script_config.config,
script_config.config.cache,
false,
Expand All @@ -215,7 +215,7 @@ impl PreprocessedState {

let zk_output = zk_compiler
.zksync_compile(&zk_project, script_config.config.zksync.avoid_contracts())?;
Some(DualCompiledContracts::new(&output, &zk_output, &project.paths))
Some(DualCompiledContracts::new(&output, &zk_output, &project.paths, &zk_project.paths))
} else {
None
};
Expand Down
Loading

0 comments on commit c4f1f03

Please sign in to comment.