Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zk_toolbox): Add contract verifier support for zk toolbox #2420

Merged
merged 31 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
be655d1
Some features
Deniallugo Jul 10, 2024
ec8cd4c
Add contract verifier support for zk toolbox
matias-gonz Jul 10, 2024
00c9218
Move contract verifier command into subcommand
matias-gonz Jul 10, 2024
f8a7119
Add get_zksolc_releases
matias-gonz Jul 10, 2024
868bd59
add aliases
Deniallugo Jul 10, 2024
529df72
Remove build l1 contract
Deniallugo Jul 10, 2024
f429d33
Print stderr always
Deniallugo Jul 10, 2024
007b97f
Merge branch 'main' into matias-zk-toolbox-contract-verifier
matias-gonz Jul 11, 2024
5b4e634
Improve error messages from Daniels pr
Deniallugo Jul 11, 2024
14e8293
Refactor get_zksolc_releases
matias-gonz Jul 11, 2024
55eef61
Merge branch 'matias-zk-toolbox-contract-verifier' of github.com:matt…
matias-gonz Jul 11, 2024
c3d7c06
Add Vyper version selection
matias-gonz Jul 11, 2024
5794e52
Add ecosystem name
Deniallugo Jul 11, 2024
4c4ffc8
Update zk_toolbox/crates/zk_inception/src/messages.rs
Deniallugo Jul 11, 2024
53561cd
Merge branch 'deniallugo-different-zk_toolbox-fixes' of github.com:ma…
matias-gonz Jul 11, 2024
2f4b1fc
Add download_binary
matias-gonz Jul 11, 2024
385f990
fmt
matias-gonz Jul 11, 2024
b77c44b
Merge branch 'main' into matias-zk-toolbox-contract-verifier
matias-gonz Jul 11, 2024
315361e
Merge branch 'main' into matias-zk-toolbox-contract-verifier
matias-gonz Jul 11, 2024
c119fd9
Download all binaries above version
matias-gonz Jul 12, 2024
8b7f123
Merge branch 'matias-zk-toolbox-contract-verifier' of github.com:matt…
matias-gonz Jul 12, 2024
1200552
Merge branch 'main' into matias-zk-toolbox-contract-verifier
matias-gonz Jul 12, 2024
5832abe
Add get_solc_releases
matias-gonz Jul 12, 2024
3971196
lint
matias-gonz Jul 12, 2024
d1eea82
Add docs
matias-gonz Jul 12, 2024
6709f1d
Show output from contract verifier
matias-gonz Jul 12, 2024
ec6a987
Merge branch 'main' into matias-zk-toolbox-contract-verifier
matias-gonz Jul 12, 2024
0140c58
lint
matias-gonz Jul 12, 2024
33c50a6
Merge branch 'matias-zk-toolbox-contract-verifier' of github.com:matt…
matias-gonz Jul 12, 2024
e6528c7
Merge branch 'main' into matias-zk-toolbox-contract-verifier
matias-gonz Jul 12, 2024
abe468f
lint
matias-gonz Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use std::str::FromStr;

use anyhow::Context;
use clap::{Parser, ValueEnum};
use common::{cmd::Cmd, spinner::Spinner, PromptSelect};
use strum::IntoEnumIterator;
use xshell::{cmd, Shell};

use crate::messages::{
MSG_ARCH_SLECTION_PROMPT, MSG_FETCHING_ZKSOLC_RELEASES_SPINNER, MSG_GET_ZKSOLC_RELEASES_ERR,
MSG_INVALID_ARCH_ERR, MSG_NO_RELEASES_FOUND_ERR, MSG_NO_VERSION_FOUND_ERR,
MSG_ZKSOLC_VERSION_PROMPT,
};

#[derive(Debug, Clone, Parser, Default)]
pub struct InitContractVerifierArgs {
/// Version of zksolc to install
#[clap(long)]
pub version: Option<String>,
/// Architecture of zksolc to install
#[clap(long)]
pub arch: Option<Arch>,
}

#[derive(Debug, Clone)]
pub struct InitContractVerifierArgsFinal {
pub zksolc_version: ZkSolcVersion,
}

#[derive(Debug, Clone)]
pub struct ZkSolcVersion {
pub version: String,
pub arch: Arch,
pub url: String,
}

#[derive(Debug, Clone, ValueEnum, strum::EnumIter, PartialEq, Eq, Copy)]
pub enum Arch {
LinuxAmd,
LinuxArm,
MacosAmd,
MacosArm,
WindowsAmd,
}
matias-gonz marked this conversation as resolved.
Show resolved Hide resolved

impl std::fmt::Display for Arch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Arch::LinuxAmd => write!(f, "linux-amd64"),
Arch::LinuxArm => write!(f, "linux-arm64"),
Arch::MacosAmd => write!(f, "macos-amd64"),
Arch::MacosArm => write!(f, "macos-arm64"),
Arch::WindowsAmd => write!(f, "windows-amd64"),
}
}
}

impl std::str::FromStr for Arch {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.contains("linux-amd64") {
Ok(Arch::LinuxAmd)
} else if s.contains("linux-arm64") {
Ok(Arch::LinuxArm)
} else if s.contains("macosx-amd64") {
Ok(Arch::MacosAmd)
} else if s.contains("macosx-arm64") {
Ok(Arch::MacosArm)
} else if s.contains("windows-amd64") {
Ok(Arch::WindowsAmd)
} else {
Err(anyhow::anyhow!(MSG_INVALID_ARCH_ERR))
}
}
}

impl InitContractVerifierArgs {
pub fn fill_values_with_prompt(
self,
shell: &Shell,
) -> anyhow::Result<InitContractVerifierArgsFinal> {
let spinner = Spinner::new(MSG_FETCHING_ZKSOLC_RELEASES_SPINNER);
let releases = get_zksolc_releases(shell)?;
spinner.finish();

let arch = self
.arch
.unwrap_or_else(|| PromptSelect::new(MSG_ARCH_SLECTION_PROMPT, Arch::iter()).ask());

let releases = releases
.into_iter()
.filter(|r| r.arch == arch)
.collect::<Vec<_>>();

if releases.is_empty() {
anyhow::bail!(MSG_NO_RELEASES_FOUND_ERR);
}

let version = self.version.unwrap_or_else(|| {
PromptSelect::new(
MSG_ZKSOLC_VERSION_PROMPT,
releases.iter().map(|r| &r.version),
)
.ask()
.into()
});

let zksolc_version = releases
.iter()
.find(|r| r.version == version)
.context(MSG_NO_VERSION_FOUND_ERR)?
.to_owned();

Ok(InitContractVerifierArgsFinal { zksolc_version })
}
}

fn get_zksolc_releases(shell: &Shell) -> anyhow::Result<Vec<ZkSolcVersion>> {
let response: std::process::Output = Cmd::new(cmd!(
shell,
"curl https://api.github.com/repos/matter-labs/zksolc-bin/releases"
))
.run_with_output()?;
let response = String::from_utf8(response.stdout)?;
let response: serde_json::Value = serde_json::from_str(&response)?;

let mut releases = vec![];

for r in response.as_array().context(MSG_GET_ZKSOLC_RELEASES_ERR)? {
let version = r["name"]
.as_str()
.context(MSG_GET_ZKSOLC_RELEASES_ERR)?
.to_string();
let assets = r["assets"]
.as_array()
.context(MSG_GET_ZKSOLC_RELEASES_ERR)?;
for a in assets.iter() {
let arch = <Arch as FromStr>::from_str(
a["name"].as_str().context(MSG_GET_ZKSOLC_RELEASES_ERR)?,
)?;
let url = a["browser_download_url"]
.as_str()
.context(MSG_GET_ZKSOLC_RELEASES_ERR)?
.to_string();
releases.push(ZkSolcVersion {
version: version.clone(),
arch,
url,
});
}
}

matias-gonz marked this conversation as resolved.
Show resolved Hide resolved
Ok(releases)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod init;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use xshell::Shell;

use super::args::init::InitContractVerifierArgs;

pub(crate) async fn run(shell: &Shell, args: InitContractVerifierArgs) -> anyhow::Result<()> {
args.fill_values_with_prompt(shell)?;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use args::init::InitContractVerifierArgs;
use clap::Subcommand;
use xshell::Shell;

pub mod args;
pub mod init;
pub mod run;

#[derive(Subcommand, Debug)]
pub enum ContractVerifierCommands {
/// Run contract verifier
Run,
Init(InitContractVerifierArgs),
}

pub(crate) async fn run(shell: &Shell, args: ContractVerifierCommands) -> anyhow::Result<()> {
match args {
ContractVerifierCommands::Run => run::run(shell).await,
ContractVerifierCommands::Init(args) => init::run(shell, args).await,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::Context;
use common::{cmd::Cmd, logger};
use config::EcosystemConfig;
use xshell::{cmd, Shell};

use crate::messages::{
MSG_CHAIN_NOT_FOUND_ERR, MSG_FAILED_TO_RUN_CONTRACT_VERIFIER_ERR, MSG_RUNNING_CONTRACT_VERIFIER,
};

pub(crate) async fn run(shell: &Shell) -> anyhow::Result<()> {
let ecosystem = EcosystemConfig::from_file(shell)?;
let chain = ecosystem
.load_chain(Some(ecosystem.default_chain.clone()))
.context(MSG_CHAIN_NOT_FOUND_ERR)?;

let config_path = chain.path_to_general_config();
let secrets_path = chain.path_to_secrets_config();

let _dir_guard = shell.push_dir(&chain.link_to_code);

logger::info(MSG_RUNNING_CONTRACT_VERIFIER);

Cmd::new(cmd!(
shell,
"cargo run --bin zksync_contract_verifier -- --config-path={config_path} --secrets-path={secrets_path}"
)).run().context(MSG_FAILED_TO_RUN_CONTRACT_VERIFIER_ERR)
}
1 change: 1 addition & 0 deletions zk_toolbox/crates/zk_inception/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod args;
pub mod chain;
pub mod containers;
pub mod contract_verifier;
pub mod ecosystem;
pub mod external_node;
pub mod prover;
Expand Down
2 changes: 1 addition & 1 deletion zk_toolbox/crates/zk_inception/src/commands/prover/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::messages::{
};

pub(crate) async fn run(args: ProverRunArgs, shell: &Shell) -> anyhow::Result<()> {
check_prover_prequisites(shell);
let args = args.fill_values_with_prompt()?;
let ecosystem_config = EcosystemConfig::from_file(shell)?;
let chain = ecosystem_config
Expand All @@ -42,7 +43,6 @@ pub(crate) async fn run(args: ProverRunArgs, shell: &Shell) -> anyhow::Result<()
}

fn run_gateway(shell: &Shell, chain: &ChainConfig) -> anyhow::Result<()> {
check_prover_prequisites(shell);
logger::info(MSG_RUNNING_PROVER_GATEWAY);
let config_path = chain.path_to_general_config();
let secrets_path = chain.path_to_secrets_config();
Expand Down
7 changes: 7 additions & 0 deletions zk_toolbox/crates/zk_inception/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{command, Parser, Subcommand};
use commands::contract_verifier::ContractVerifierCommands;
use common::{
check_general_prerequisites,
config::{global_config, init_global_config, GlobalConfig},
Expand Down Expand Up @@ -48,6 +49,9 @@ pub enum InceptionSubcommands {
ExternalNode(ExternalNodeCommands),
/// Run containers for local development
Containers,
/// Run contract verifier
#[command(subcommand)]
ContractVerifier(ContractVerifierCommands),
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -102,6 +106,9 @@ async fn run_subcommand(inception_args: Inception, shell: &Shell) -> anyhow::Res
InceptionSubcommands::ExternalNode(args) => {
commands::external_node::run(shell, args).await?
}
InceptionSubcommands::ContractVerifier(args) => {
commands::contract_verifier::run(shell, args).await?
}
}
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions zk_toolbox/crates/zk_inception/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,15 @@ pub(super) const MSG_BELLMAN_CUDA_SELECTION_PATH: &str = "I have the code alread
pub(super) fn msg_bucket_created(bucket_name: &str) -> String {
format!("Bucket created successfully with url: gs://{bucket_name}")
}

/// Contract verifier related messages
pub(super) const MSG_RUNNING_CONTRACT_VERIFIER: &str = "Running contract verifier";
pub(super) const MSG_FAILED_TO_RUN_CONTRACT_VERIFIER_ERR: &str = "Failed to run contract verifier";
pub(super) const MSG_INVALID_ARCH_ERR: &str = "Invalid arch";
pub(super) const MSG_GET_ZKSOLC_RELEASES_ERR: &str = "Failed to get zksolc releases";
pub(super) const MSG_FETCHING_ZKSOLC_RELEASES_SPINNER: &str = "Fetching zksolc releases...";
pub(super) const MSG_ARCH_SLECTION_PROMPT: &str = "Select your architecture:";
pub(super) const MSG_ZKSOLC_VERSION_PROMPT: &str = "Select the zksolc version:";
pub(super) const MSG_NO_RELEASES_FOUND_ERR: &str =
"No releases found for the selected architecture";
pub(super) const MSG_NO_VERSION_FOUND_ERR: &str = "No version found";
Loading