Skip to content

Commit

Permalink
chore: moves zk_solc.rs and zksolc_manager.rs to common crate (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dutterbutter authored Nov 3, 2023
1 parent 3aa13ee commit 29cab92
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 151 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ ethers-etherscan = { workspace = true, features = ["ethers-solc"] }

# zksync
zksync-web3-rs = {git = "https://github.com/lambdaclass/zksync-web3-rs.git", rev = "70327ae5413c517bd4d27502507cdd96ee40cd22"}
era_revm = { git = "https://github.com/matter-labs/era-revm.git", tag = "v0.0.1-alpha" }
anyhow = {version = "1.0.70"}
dirs = {version = "5.0.0"}
ansi_term = "0.12.1"

# io
reqwest = { version = "0.11", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub mod shell;
pub mod term;
pub mod traits;
pub mod transactions;
pub mod zk_compile;
pub mod zk_utils;
pub mod zksolc_manager;

pub use constants::*;
pub use contracts::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// This module provides the implementation of the ZkSolc compiler for Solidity contracts.
#![allow(missing_docs)]
//! This module provides the implementation of the ZkSolc compiler for Solidity contracts.
/// ZkSolc is a specialized compiler that supports zero-knowledge (ZK) proofs for smart
/// contracts.
///
Expand Down Expand Up @@ -29,18 +30,17 @@
///
/// - Artifact Path Generation: The `build_artifacts_path` and `build_artifacts_file` methods
/// construct the path and file for saving the compiler output artifacts.
use crate::zksolc_manager::ZkSolcManager;
use ansi_term::Colour::{Red, Yellow};
use ethers::{
prelude::{artifacts::Source, remappings::RelativeRemapping, Solc},
solc::{
artifacts::{
output_selection::FileOutputSelection, CompactBytecode, CompactDeployedBytecode,
LosslessAbi, StandardJsonCompilerInput,
},
ArtifactFile, Artifacts, ConfigurableContractArtifact, Graph, Project,
ProjectCompileOutput,
use ethers_core::types::Bytes;
use ethers_solc::{
artifacts::{
output_selection::FileOutputSelection, CompactBytecode, CompactDeployedBytecode,
LosslessAbi, Source, StandardJsonCompilerInput,
},
types::Bytes,
remappings::RelativeRemapping,
ArtifactFile, Artifacts, ConfigurableContractArtifact, Graph, Project, ProjectCompileOutput,
Solc,
};
use eyre::{Context, ContextCompat, Result};
use regex::Regex;
Expand Down Expand Up @@ -138,6 +138,42 @@ impl fmt::Display for ZkSolc {
}
}

/// The `compile_smart_contracts` function initiates the contract compilation process.
///
/// It follows these steps:
/// 1. Create an instance of `ZkSolcOpts` with the appropriate options.
/// 2. Instantiate `ZkSolc` with the created options and the project.
/// 3. Initiate the contract compilation process.
///
/// The function returns `Ok(())` if the compilation process completes successfully, or an error
/// if it fails.
pub fn compile_smart_contracts(
is_system: bool,
is_legacy: bool,
zksolc_manager: ZkSolcManager,
project: Project,
remappings: Vec<RelativeRemapping>,
) -> eyre::Result<()> {
let zksolc_opts = ZkSolcOpts {
compiler_path: zksolc_manager.get_full_compiler_path(),
is_system,
force_evmla: is_legacy,
remappings,
};

let mut zksolc = ZkSolc::new(zksolc_opts, project);

match zksolc.compile() {
Ok(_) => {
println!("Compiled Successfully");
Ok(())
}
Err(err) => {
eyre::bail!("Failed to compile smart contracts with zksolc: {}", err);
}
}
}

impl ZkSolc {
pub fn new(opts: ZkSolcOpts, project: Project) -> Self {
Self {
Expand Down Expand Up @@ -448,15 +484,15 @@ impl ZkSolc {

let mut art = ConfigurableContractArtifact {
bytecode: Some(CompactBytecode {
object: ethers::solc::artifacts::BytecodeObject::Bytecode(
object: ethers_solc::artifacts::BytecodeObject::Bytecode(
packed_bytecode.clone(),
),
source_map: None,
link_references: Default::default(),
}),
deployed_bytecode: Some(CompactDeployedBytecode {
bytecode: Some(CompactBytecode {
object: ethers::solc::artifacts::BytecodeObject::Bytecode(
object: ethers_solc::artifacts::BytecodeObject::Bytecode(
packed_bytecode,
),
source_map: None,
Expand Down Expand Up @@ -941,9 +977,8 @@ mod tests {
/// Basic test to analyze the single Counter.sol artifact.
#[test]
pub fn test_artifacts_extraction() {
let data = include_str!("../../../../testdata/artifacts-counter/artifacts.json")
.as_bytes()
.to_vec();
let data =
include_str!("../../../testdata/artifacts-counter/artifacts.json").as_bytes().to_vec();
let mut displayed_warnings = HashSet::new();
let source = "src/Counter.sol".to_owned();
let result = ZkSolc::handle_output(data, &source, &mut displayed_warnings, None);
Expand All @@ -958,14 +993,13 @@ mod tests {
}
#[test]
pub fn test_json_parsing() {
let data = include_str!("../../../../testdata/artifacts-counter/artifacts.json")
.as_bytes()
.to_vec();
let data =
include_str!("../../../testdata/artifacts-counter/artifacts.json").as_bytes().to_vec();
let _parsed: ZkSolcCompilerOutput = serde_json::from_slice(&data).unwrap();

// Contract that has almost no data (and many fields missing).
let almost_empty_data =
include_str!("../../../../testdata/artifacts-counter/empty.json").as_bytes().to_vec();
include_str!("../../../testdata/artifacts-counter/empty.json").as_bytes().to_vec();
let _parsed_empty: ZkSolcCompilerOutput =
serde_json::from_slice(&almost_empty_data).unwrap();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// The `ZkSolcManager` module manages the downloading and setup of the `zksolc` Solidity
#![allow(missing_docs)]
//! The `ZkSolcManager` module manages the downloading and setup of the `zksolc` Solidity
/// compiler. This module provides functionalities to interact with different versions of the
/// zksolc compiler as well as supporting different operating systems.
///
Expand Down Expand Up @@ -409,6 +410,42 @@ impl fmt::Display for ZkSolcManager {
}
}

/// The `setup_zksolc_manager` function creates and prepares an instance of `ZkSolcManager`.
///
/// It follows these steps:
/// 1. Instantiate `ZkSolcManagerOpts` and `ZkSolcManagerBuilder` with the specified zkSync Solidity
/// compiler.
/// 2. Create a `ZkSolcManager` using the builder.
/// 3. Check if the setup compilers directory is properly set up. If not, it raises an error.
/// 4. If the zkSync Solidity compiler does not exist in the compilers directory, it triggers its
/// download.
///
/// The function returns the `ZkSolcManager` if all steps are successful, or an error if any
/// step fails.
pub fn setup_zksolc_manager(zksolc_version: String) -> eyre::Result<ZkSolcManager> {
let zksolc_manager_opts = ZkSolcManagerOpts::new(zksolc_version);
let zksolc_manager_builder = ZkSolcManagerBuilder::new(zksolc_manager_opts);
let zksolc_manager = zksolc_manager_builder
.build()
.map_err(|e| eyre::eyre!("Error building zksolc_manager: {}", e))?;

if let Err(err) = zksolc_manager.check_setup_compilers_dir() {
eyre::bail!("Failed to setup compilers directory: {}", err);
}

if !zksolc_manager.exists() {
println!(
"Downloading zksolc compiler from {:?}",
zksolc_manager.get_full_download_url().unwrap().to_string()
);
zksolc_manager
.download()
.map_err(|err| eyre::eyre!("Failed to download the file: {}", err))?;
}

Ok(zksolc_manager)
}

impl ZkSolcManager {
/// Constructs a new instance of `ZkSolcManager` with the specified configuration options.
///
Expand Down
4 changes: 0 additions & 4 deletions crates/zkforge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ tracing.workspace = true
yansi = "0.5"

# zksync
era_revm = { git = "https://github.com/matter-labs/era-revm.git", tag = "v0.0.1-alpha" }
zksync-web3-rs = {git = "https://github.com/lambdaclass/zksync-web3-rs.git", rev = "70327ae5413c517bd4d27502507cdd96ee40cd22"}
ansi_term = "0.12.1"
anyhow = {version = "1.0.70"}
dirs = {version = "5.0.0"}
url = "2.3.1"

# bin
Expand Down
2 changes: 0 additions & 2 deletions crates/zkforge/bin/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,3 @@ pub mod verify;
pub mod watch;
pub mod zk_build;
pub mod zk_create;
pub mod zk_solc;
pub mod zksolc_manager;
12 changes: 6 additions & 6 deletions crates/zkforge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use super::{install, test::filter::ProjectPathsAwareFilter, watch::WatchArgs};
use crate::cmd::{
zk_solc::{ZkSolc, ZkSolcOpts},
zksolc_manager::{ZkSolcManagerBuilder, ZkSolcManagerOpts, DEFAULT_ZKSOLC_VERSION},
};
use alloy_primitives::U256;
use clap::Parser;
use eyre::Result;
Expand All @@ -11,8 +7,12 @@ use foundry_cli::{
utils::{self, LoadConfig},
};
use foundry_common::{
compact_to_contract, compile::ContractSources, evm::EvmArgs, get_contract_name, get_file_name,
shell,
compact_to_contract,
compile::ContractSources,
evm::EvmArgs,
get_contract_name, get_file_name, shell,
zk_compile::{ZkSolc, ZkSolcOpts},
zksolc_manager::{ZkSolcManagerBuilder, ZkSolcManagerOpts, DEFAULT_ZKSOLC_VERSION},
};
use foundry_config::{
figment,
Expand Down
25 changes: 13 additions & 12 deletions crates/zkforge/bin/cmd/watch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{snapshot::SnapshotArgs, test::TestArgs};
use crate::cmd::zk_build::ZkBuildArgs;
use clap::Parser;
use eyre::Result;
use foundry_cli::utils::{self, FoundryPathExt};
Expand Down Expand Up @@ -82,22 +83,22 @@ impl WatchArgs {

/// Executes a [`Watchexec`] that listens for changes in the project's src dir and reruns `forge
/// build`
// pub async fn watch_build(args: ZkBuildArgs) -> Result<()> {
// let (init, mut runtime) = args.watchexec_config()?;
// let cmd = cmd_args(args.watch.watch.as_ref().map(|paths| paths.len()).unwrap_or_default());
pub async fn watch_build(args: ZkBuildArgs) -> Result<()> {
let (init, mut runtime) = args.watchexec_config()?;
let cmd = cmd_args(args.watch.watch.as_ref().map(|paths| paths.len()).unwrap_or_default());

// trace!("watch build cmd={:?}", cmd);
// runtime.command(watch_command(cmd.clone()));
trace!("watch build cmd={:?}", cmd);
runtime.command(watch_command(cmd.clone()));

// let wx = Watchexec::new(init, runtime.clone())?;
// on_action(args.watch, runtime, Arc::clone(&wx), cmd, (), |_| {});
let wx = Watchexec::new(init, runtime.clone())?;
on_action(args.watch, runtime, Arc::clone(&wx), cmd, (), |_| {});

// // start executing the command immediately
// wx.send_event(Event::default(), Priority::default()).await?;
// wx.main().await??;
// start executing the command immediately
wx.send_event(Event::default(), Priority::default()).await?;
wx.main().await??;

// Ok(())
// }
Ok(())
}

/// Executes a [`Watchexec`] that listens for changes in the project's src dir and reruns `forge
/// snapshot`
Expand Down
Loading

0 comments on commit 29cab92

Please sign in to comment.