From 3506731d1702bdec8c6b5b41cabca9a257f0269b Mon Sep 17 00:00:00 2001 From: Artem Fomiuk <88630083+Artemka374@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:11:03 +0300 Subject: [PATCH] feat(zk_toolbox): `zk_supervisor prover` subcommand (#2820) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Add prover subcommand for `zk_supervisor`. Add the following subcommand: * `zk_supervisor prover info` - Prints information about current prover setup. * `zk_supervisor prover insert-version` - Insert new protocol version in prover database(integration with `prover_cli`). * `zk_supervisor prover insert-batch` - Insert new batch in prover database(integration with `prover_cli`). Add automatic creation of `prover/artifacts/witness_inputs` dirs if the storage is file backed on init. ## Why ❔ To improve UX of working with provers. ## Checklist - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. --- prover/crates/bin/prover_cli/README.md | 6 ++ zk_toolbox/crates/common/src/lib.rs | 4 +- zk_toolbox/crates/common/src/prerequisites.rs | 10 +- zk_toolbox/crates/config/src/ecosystem.rs | 7 ++ .../src/commands/prover/compressor_keys.rs | 8 +- .../zk_inception/src/commands/prover/gcs.rs | 4 +- .../zk_inception/src/commands/prover/init.rs | 45 +++++++-- .../zk_inception/src/commands/prover/mod.rs | 1 - .../zk_inception/src/commands/prover/run.rs | 7 +- .../src/commands/prover/setup_keys.rs | 8 +- .../zk_inception/src/commands/prover/utils.rs | 10 -- zk_toolbox/crates/zk_supervisor/README.md | 34 ++++++- .../src/commands/database/reset.rs | 4 +- .../crates/zk_supervisor/src/commands/mod.rs | 2 +- .../src/commands/prover/args/insert_batch.rs | 40 ++++++++ .../commands/prover/args/insert_version.rs | 49 ++++++++++ .../src/commands/prover/args/mod.rs | 2 + .../zk_supervisor/src/commands/prover/info.rs | 95 +++++++++++++++++++ .../src/commands/prover/insert_batch.rs | 38 ++++++++ .../src/commands/prover/insert_version.rs | 38 ++++++++ .../zk_supervisor/src/commands/prover/mod.rs | 22 +++++ .../src/commands/prover_version.rs | 40 -------- zk_toolbox/crates/zk_supervisor/src/main.rs | 8 +- 23 files changed, 391 insertions(+), 91 deletions(-) delete mode 100644 zk_toolbox/crates/zk_inception/src/commands/prover/utils.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_batch.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_version.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/args/mod.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/info.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_batch.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_version.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover/mod.rs delete mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/prover_version.rs diff --git a/prover/crates/bin/prover_cli/README.md b/prover/crates/bin/prover_cli/README.md index 2d57e0b56495..e0dd1697bf6d 100644 --- a/prover/crates/bin/prover_cli/README.md +++ b/prover/crates/bin/prover_cli/README.md @@ -9,6 +9,12 @@ git clone git@github.com:matter-labs/zksync-era.git cargo install prover_cli ``` +Or + +``` +cargo +nightly-2024-08-01 install --git https://github.com/matter-labs/zksync-era/ --locked prover_cli --force +``` + ## Usage ``` diff --git a/zk_toolbox/crates/common/src/lib.rs b/zk_toolbox/crates/common/src/lib.rs index 5a6f63e3a51f..7be4af740700 100644 --- a/zk_toolbox/crates/common/src/lib.rs +++ b/zk_toolbox/crates/common/src/lib.rs @@ -15,8 +15,8 @@ pub mod server; pub mod wallets; pub use prerequisites::{ - check_general_prerequisites, check_prerequisites, GCLOUD_PREREQUISITES, GPU_PREREQUISITES, - WGET_PREREQUISITES, + check_general_prerequisites, check_prerequisites, GCLOUD_PREREQUISITE, GPU_PREREQUISITES, + PROVER_CLI_PREREQUISITE, WGET_PREREQUISITE, }; pub use prompt::{init_prompt_theme, Prompt, PromptConfirm, PromptSelect}; pub use term::{error, logger, spinner}; diff --git a/zk_toolbox/crates/common/src/prerequisites.rs b/zk_toolbox/crates/common/src/prerequisites.rs index 87ec396d0e63..665096d8486e 100644 --- a/zk_toolbox/crates/common/src/prerequisites.rs +++ b/zk_toolbox/crates/common/src/prerequisites.rs @@ -45,16 +45,22 @@ pub const GPU_PREREQUISITES: [Prerequisite; 3] = [ }, // CUDA GPU driver ]; -pub const WGET_PREREQUISITES: [Prerequisite; 1] = [Prerequisite { +pub const WGET_PREREQUISITE: [Prerequisite; 1] = [Prerequisite { name: "wget", download_link: "https://www.gnu.org/software/wget/", }]; -pub const GCLOUD_PREREQUISITES: [Prerequisite; 1] = [Prerequisite { +pub const GCLOUD_PREREQUISITE: [Prerequisite; 1] = [Prerequisite { name: "gcloud", download_link: "https://cloud.google.com/sdk/docs/install", }]; +pub const PROVER_CLI_PREREQUISITE: [Prerequisite; 1] = [Prerequisite { + name: "prover_cli", + download_link: + "https://github.com/matter-labs/zksync-era/tree/main/prover/crates/bin/prover_cli", +}]; + pub struct Prerequisite { name: &'static str, download_link: &'static str, diff --git a/zk_toolbox/crates/config/src/ecosystem.rs b/zk_toolbox/crates/config/src/ecosystem.rs index 76d85bb41e92..a0412fbc4733 100644 --- a/zk_toolbox/crates/config/src/ecosystem.rs +++ b/zk_toolbox/crates/config/src/ecosystem.rs @@ -290,3 +290,10 @@ fn find_file(shell: &Shell, path_buf: PathBuf, file_name: &str) -> Result PathBuf { + let link_to_code = config.link_to_code.clone(); + let mut link_to_prover = link_to_code.into_os_string(); + link_to_prover.push("/prover"); + link_to_prover.into() +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/compressor_keys.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/compressor_keys.rs index 1f39c91a2e2e..fd83fccfebfa 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/compressor_keys.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/prover/compressor_keys.rs @@ -1,11 +1,11 @@ use anyhow::Context; use common::{ - check_prerequisites, cmd::Cmd, config::global_config, spinner::Spinner, WGET_PREREQUISITES, + check_prerequisites, cmd::Cmd, config::global_config, spinner::Spinner, WGET_PREREQUISITE, }; -use config::{EcosystemConfig, GeneralConfig}; +use config::{get_link_to_prover, EcosystemConfig, GeneralConfig}; use xshell::{cmd, Shell}; -use super::{args::compressor_keys::CompressorKeysArgs, utils::get_link_to_prover}; +use super::args::compressor_keys::CompressorKeysArgs; use crate::messages::{ MSG_CHAIN_NOT_FOUND_ERR, MSG_DOWNLOADING_SETUP_COMPRESSOR_KEY_SPINNER, MSG_PROOF_COMPRESSOR_CONFIG_NOT_FOUND_ERR, MSG_SETUP_KEY_PATH_ERROR, @@ -37,7 +37,7 @@ pub(crate) fn download_compressor_key( general_config: &mut GeneralConfig, path: &str, ) -> anyhow::Result<()> { - check_prerequisites(shell, &WGET_PREREQUISITES, false); + check_prerequisites(shell, &WGET_PREREQUISITE, false); let spinner = Spinner::new(MSG_DOWNLOADING_SETUP_COMPRESSOR_KEY_SPINNER); let mut compressor_config: zksync_config::configs::FriProofCompressorConfig = general_config .proof_compressor_config diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/gcs.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/gcs.rs index 700209f5ffc8..f28c44504b56 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/gcs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/prover/gcs.rs @@ -1,4 +1,4 @@ -use common::{check_prerequisites, cmd::Cmd, logger, spinner::Spinner, GCLOUD_PREREQUISITES}; +use common::{check_prerequisites, cmd::Cmd, logger, spinner::Spinner, GCLOUD_PREREQUISITE}; use xshell::{cmd, Shell}; use zksync_config::{configs::object_store::ObjectStoreMode, ObjectStoreConfig}; @@ -14,7 +14,7 @@ pub(crate) fn create_gcs_bucket( shell: &Shell, config: ProofStorageGCSCreateBucket, ) -> anyhow::Result { - check_prerequisites(shell, &GCLOUD_PREREQUISITES, false); + check_prerequisites(shell, &GCLOUD_PREREQUISITE, false); let bucket_name = config.bucket_name; let location = config.location; diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/init.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/init.rs index c8636381f203..1d92357635c5 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/prover/init.rs @@ -2,13 +2,17 @@ use std::path::PathBuf; use anyhow::Context; use common::{ + cmd::Cmd, config::global_config, db::{drop_db_if_exists, init_db, migrate_db, DatabaseConfig}, logger, spinner::Spinner, }; -use config::{copy_configs, set_prover_database, traits::SaveConfigWithBasePath, EcosystemConfig}; -use xshell::Shell; +use config::{ + copy_configs, get_link_to_prover, set_prover_database, traits::SaveConfigWithBasePath, + EcosystemConfig, +}; +use xshell::{cmd, Shell}; use zksync_config::{configs::object_store::ObjectStoreMode, ObjectStoreConfig}; use super::{ @@ -19,6 +23,7 @@ use super::{ setup_keys, }; use crate::{ + commands::prover::args::init::ProofStorageFileBacked, consts::{PROVER_MIGRATIONS, PROVER_STORE_MAX_RETRIES}, messages::{ MSG_CHAIN_NOT_FOUND_ERR, MSG_FAILED_TO_DROP_PROVER_DATABASE_ERR, @@ -105,13 +110,11 @@ fn get_object_store_config( config: Option, ) -> anyhow::Result> { let object_store = match config { - Some(ProofStorageConfig::FileBacked(config)) => Some(ObjectStoreConfig { - mode: ObjectStoreMode::FileBacked { - file_backed_base_path: config.proof_store_dir, - }, - max_retries: PROVER_STORE_MAX_RETRIES, - local_mirror_path: None, - }), + Some(ProofStorageConfig::FileBacked(config)) => Some(init_file_backed_proof_storage( + shell, + &EcosystemConfig::from_file(shell)?, + config, + )?), Some(ProofStorageConfig::GCS(config)) => Some(ObjectStoreConfig { mode: ObjectStoreMode::GCSWithCredentialFile { bucket_base_url: config.bucket_base_url, @@ -154,3 +157,27 @@ async fn initialize_prover_database( Ok(()) } + +fn init_file_backed_proof_storage( + shell: &Shell, + ecosystem_config: &EcosystemConfig, + config: ProofStorageFileBacked, +) -> anyhow::Result { + let proof_store_dir = config.proof_store_dir; + let prover_path = get_link_to_prover(ecosystem_config); + + let proof_store_dir = prover_path.join(proof_store_dir).join("witness_inputs"); + + let cmd = Cmd::new(cmd!(shell, "mkdir -p {proof_store_dir}")); + cmd.run()?; + + let object_store_config = ObjectStoreConfig { + mode: ObjectStoreMode::FileBacked { + file_backed_base_path: proof_store_dir.into_os_string().into_string().unwrap(), + }, + max_retries: PROVER_STORE_MAX_RETRIES, + local_mirror_path: None, + }; + + Ok(object_store_config) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/mod.rs index 2b771c8ad201..d9e443cdae0d 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/prover/mod.rs @@ -14,7 +14,6 @@ mod init; mod init_bellman_cuda; mod run; mod setup_keys; -mod utils; #[derive(Subcommand, Debug)] pub enum ProverCommands { diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/run.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/run.rs index 78116e40d6c7..8f72da03f3b3 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/run.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/prover/run.rs @@ -2,13 +2,10 @@ use std::path::PathBuf; use anyhow::{anyhow, Context}; use common::{check_prerequisites, cmd::Cmd, config::global_config, logger, GPU_PREREQUISITES}; -use config::EcosystemConfig; +use config::{get_link_to_prover, EcosystemConfig}; use xshell::{cmd, Shell}; -use super::{ - args::run::{ProverComponent, ProverRunArgs}, - utils::get_link_to_prover, -}; +use super::args::run::{ProverComponent, ProverRunArgs}; use crate::messages::{ MSG_BELLMAN_CUDA_DIR_ERR, MSG_CHAIN_NOT_FOUND_ERR, MSG_MISSING_COMPONENT_ERR, MSG_RUNNING_COMPRESSOR, MSG_RUNNING_COMPRESSOR_ERR, MSG_RUNNING_PROVER, MSG_RUNNING_PROVER_ERR, diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/setup_keys.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/setup_keys.rs index 09d9f76a47cf..ae0480e872dd 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/setup_keys.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/prover/setup_keys.rs @@ -1,12 +1,10 @@ use anyhow::Ok; use common::{ - check_prerequisites, cmd::Cmd, logger, spinner::Spinner, GCLOUD_PREREQUISITES, - GPU_PREREQUISITES, + check_prerequisites, cmd::Cmd, logger, spinner::Spinner, GCLOUD_PREREQUISITE, GPU_PREREQUISITES, }; -use config::EcosystemConfig; +use config::{get_link_to_prover, EcosystemConfig}; use xshell::{cmd, Shell}; -use super::utils::get_link_to_prover; use crate::{ commands::prover::args::setup_keys::{Mode, Region, SetupKeysArgs}, messages::{MSG_GENERATING_SK_SPINNER, MSG_SK_GENERATED}, @@ -33,7 +31,7 @@ pub(crate) async fn run(args: SetupKeysArgs, shell: &Shell) -> anyhow::Result<() spinner.finish(); logger::outro(MSG_SK_GENERATED); } else { - check_prerequisites(shell, &GCLOUD_PREREQUISITES, false); + check_prerequisites(shell, &GCLOUD_PREREQUISITE, false); let link_to_setup_keys = get_link_to_prover(&ecosystem_config).join("data/keys"); let path_to_keys_buckets = diff --git a/zk_toolbox/crates/zk_inception/src/commands/prover/utils.rs b/zk_toolbox/crates/zk_inception/src/commands/prover/utils.rs deleted file mode 100644 index 4dae70863dc9..000000000000 --- a/zk_toolbox/crates/zk_inception/src/commands/prover/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::path::PathBuf; - -use config::EcosystemConfig; - -pub(crate) fn get_link_to_prover(config: &EcosystemConfig) -> PathBuf { - let link_to_code = config.link_to_code.clone(); - let mut link_to_prover = link_to_code.into_os_string(); - link_to_prover.push("/prover"); - link_to_prover.into() -} diff --git a/zk_toolbox/crates/zk_supervisor/README.md b/zk_toolbox/crates/zk_supervisor/README.md index c3fac876ace6..865bd2f0d579 100644 --- a/zk_toolbox/crates/zk_supervisor/README.md +++ b/zk_toolbox/crates/zk_supervisor/README.md @@ -32,7 +32,9 @@ This document contains the help content for the `zk_supervisor` command-line pro - [`zk_supervisor fmt rustfmt`↴](#zk_supervisor-fmt-rustfmt) - [`zk_supervisor fmt contract`↴](#zk_supervisor-fmt-contract) - [`zk_supervisor fmt prettier`↴](#zk_supervisor-fmt-prettier) -- [`zk_supervisor prover-version`↴](#zk_supervisor-prover-version) +- [`zk_supervisor prover info`↴](#zk_supervisor-prover-info) +- [`zk_supervisor prover insert-version`↴](#zk_supervisor-prover-insert-version) +- [`zk_supervisor prover insert-batch`↴](#zk_supervisor-prover-insert-batch) ## `zk_supervisor` @@ -348,11 +350,35 @@ Format code Possible values: `md`, `sol`, `js`, `ts`, `rs` -## `zk_supervisor prover-version` +## `zk_supervisor prover info` -Protocol version used by provers +Prints prover protocol version, snark wrapper and prover database URL -**Usage:** `zk_supervisor prover-version` +**Usage:** `zk_supervisor prover info` + +## `zk_supervisor prover insert-version` + +Inserts protocol version into prover database. + +**Usage:** `zk_supervisor prover insert-version [OPTIONS]` + +###### **Options:** + +- `--version ` — Protocol version in semantic format(`x.y.z`). Major version should be 0. +- `--snark-wrapper ` — Snark wrapper hash. +- `--default` - use default values for protocol version and snark wrapper hash (the ones found in zksync-era). + +## `zk_supervisor prover insert-batch` + +Inserts batch into prover database. + +**Usage:** `zk_supervisor prover insert-batch` + +###### **Options:** + +- `--number ` — Number of the batch to insert. +- `--version ` — Protocol version in semantic format(`x.y.z`). Major version should be 0. +- `--default` - use default value for protocol version (the one found in zksync-era).
diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs index 5e32a8e5ae4e..f0262cecb959 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs @@ -20,14 +20,14 @@ pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> return Ok(()); } - let ecoseystem_config = EcosystemConfig::from_file(shell)?; + let ecosystem_config = EcosystemConfig::from_file(shell)?; logger::info(msg_database_info(MSG_DATABASE_RESET_GERUND)); let dals = get_dals(shell, &args.selected_dals, &args.urls)?; for dal in dals { logger::info(msg_database_loading(MSG_DATABASE_RESET_GERUND, &dal.path)); - reset_database(shell, ecoseystem_config.link_to_code.clone(), dal).await?; + reset_database(shell, ecosystem_config.link_to_code.clone(), dal).await?; } logger::outro(msg_database_success(MSG_DATABASE_RESET_PAST)); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs index e45512d50d89..875f2982c959 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs @@ -4,6 +4,6 @@ pub mod database; pub mod fmt; pub mod lint; pub(crate) mod lint_utils; -pub mod prover_version; +pub mod prover; pub mod snapshot; pub mod test; diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_batch.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_batch.rs new file mode 100644 index 000000000000..e837bbe9eb86 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_batch.rs @@ -0,0 +1,40 @@ +use clap::Parser; + +#[derive(Debug, Parser)] +pub struct InsertBatchArgs { + #[clap(long)] + pub number: Option, + #[clap(long, default_value = "false")] + pub default: bool, + #[clap(long)] + pub version: Option, +} + +#[derive(Debug)] +pub struct InsertBatchArgsFinal { + pub number: u32, + pub version: String, +} + +impl InsertBatchArgs { + pub(crate) fn fill_values_with_prompts(self, era_version: String) -> InsertBatchArgsFinal { + let number = self.number.unwrap_or_else(|| { + common::Prompt::new("Enter the number of the batch to insert").ask() + }); + + if self.default { + return InsertBatchArgsFinal { + number, + version: era_version, + }; + } + + let version = self.version.unwrap_or_else(|| { + common::Prompt::new("Enter the version of the batch to insert") + .default(&era_version) + .ask() + }); + + InsertBatchArgsFinal { number, version } + } +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_version.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_version.rs new file mode 100644 index 000000000000..97e60fb38f8c --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/insert_version.rs @@ -0,0 +1,49 @@ +use clap::Parser; + +#[derive(Debug, Parser)] +pub struct InsertVersionArgs { + #[clap(long, default_value = "false")] + pub default: bool, + #[clap(long)] + pub version: Option, + #[clap(long)] + pub snark_wrapper: Option, +} + +#[derive(Debug)] +pub struct InsertVersionArgsFinal { + pub snark_wrapper: String, + pub version: String, +} + +impl InsertVersionArgs { + pub(crate) fn fill_values_with_prompts( + self, + era_version: String, + snark_wrapper: String, + ) -> InsertVersionArgsFinal { + if self.default { + return InsertVersionArgsFinal { + snark_wrapper, + version: era_version, + }; + } + + let version = self.version.unwrap_or_else(|| { + common::Prompt::new("Enter the version of the protocol to insert") + .default(&era_version) + .ask() + }); + + let snark_wrapper = self.snark_wrapper.unwrap_or_else(|| { + common::Prompt::new("Enter the snark wrapper of the protocol to insert") + .default(&snark_wrapper) + .ask() + }); + + InsertVersionArgsFinal { + snark_wrapper, + version, + } + } +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/mod.rs new file mode 100644 index 000000000000..0984546136c9 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/args/mod.rs @@ -0,0 +1,2 @@ +pub mod insert_batch; +pub mod insert_version; diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/info.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/info.rs new file mode 100644 index 000000000000..05964cf689fd --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/info.rs @@ -0,0 +1,95 @@ +use std::{ + fs, + path::{Path, PathBuf}, +}; + +use anyhow::Context as _; +use common::{config::global_config, logger}; +use config::EcosystemConfig; +use xshell::{cmd, Shell}; + +use crate::messages::MSG_CHAIN_NOT_FOUND_ERR; + +pub async fn run(shell: &Shell) -> anyhow::Result<()> { + let link_to_code = EcosystemConfig::from_file(shell)?.link_to_code; + let link_to_prover = link_to_code.join("prover"); + + let protocol_version = get_protocol_version(shell, &link_to_prover).await?; + let snark_wrapper = get_snark_wrapper(&link_to_prover).await?; + let prover_url = get_database_url(shell).await?; + + logger::info(format!( + " +=============================== \n +Current prover setup information: \n +Protocol version: {} \n +Snark wrapper: {} \n +Database URL: {}\n +===============================", + protocol_version, snark_wrapper, prover_url + )); + + Ok(()) +} + +pub(crate) async fn get_protocol_version( + shell: &Shell, + link_to_prover: &PathBuf, +) -> anyhow::Result { + shell.change_dir(link_to_prover); + let protocol_version = cmd!(shell, "cargo run --release --bin prover_version").read()?; + + Ok(protocol_version) +} + +pub(crate) async fn get_snark_wrapper(link_to_prover: &Path) -> anyhow::Result { + let path = link_to_prover.join("data/keys/commitments.json"); + let file = fs::File::open(path).expect("Could not find commitments file in zksync-era"); + let json: serde_json::Value = + serde_json::from_reader(file).expect("Could not parse commitments.json"); + + let snark_wrapper = json + .get("snark_wrapper") + .expect("Could not find snark_wrapper in commitments.json"); + + let mut snark_wrapper = snark_wrapper.to_string(); + snark_wrapper.pop(); + snark_wrapper.remove(0); + + Ok(snark_wrapper) +} + +pub(crate) async fn get_database_url(shell: &Shell) -> anyhow::Result { + let ecosystem = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem + .load_chain(global_config().chain_name.clone()) + .context(MSG_CHAIN_NOT_FOUND_ERR)?; + + let prover_url = chain_config + .get_secrets_config()? + .database + .context("Database secrets not found")? + .prover_url()? + .expose_url() + .to_string(); + Ok(prover_url) +} + +pub fn parse_version(version: &str) -> anyhow::Result<(&str, &str)> { + let splitted: Vec<&str> = version.split(".").collect(); + + assert_eq!(splitted.len(), 3, "Invalid version format"); + assert_eq!(splitted[0], "0", "Invalid major version, expected 0"); + + splitted[1] + .parse::() + .context("Could not parse minor version")?; + splitted[2] + .parse::() + .context("Could not parse patch version")?; + + let minor = splitted[1]; + let patch = splitted[2]; + + Ok((minor, patch)) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_batch.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_batch.rs new file mode 100644 index 000000000000..2c4a1cf97513 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_batch.rs @@ -0,0 +1,38 @@ +use common::{check_prerequisites, cmd::Cmd, logger, PROVER_CLI_PREREQUISITE}; +use config::{get_link_to_prover, EcosystemConfig}; +use xshell::{cmd, Shell}; + +use crate::commands::prover::{ + args::insert_batch::{InsertBatchArgs, InsertBatchArgsFinal}, + info, +}; + +pub async fn run(shell: &Shell, args: InsertBatchArgs) -> anyhow::Result<()> { + check_prerequisites(shell, &PROVER_CLI_PREREQUISITE, false); + + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + let version = info::get_protocol_version(shell, &get_link_to_prover(&ecosystem_config)).await?; + let prover_url = info::get_database_url(shell).await?; + + let InsertBatchArgsFinal { number, version } = args.fill_values_with_prompts(version); + + let (minor, patch) = info::parse_version(&version)?; + + logger::info(format!( + "Inserting protocol version {}, batch number {} into the database", + version, number + )); + + let number = number.to_string(); + + let cmd = Cmd::new(cmd!( + shell, + "prover_cli {prover_url} insert-batch --version={minor} --patch={patch} --number={number}" + )); + cmd.run()?; + + logger::info("Done."); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_version.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_version.rs new file mode 100644 index 000000000000..ab28efca9446 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/insert_version.rs @@ -0,0 +1,38 @@ +use common::{check_prerequisites, cmd::Cmd, logger, PROVER_CLI_PREREQUISITE}; +use config::{get_link_to_prover, EcosystemConfig}; +use xshell::{cmd, Shell}; + +use crate::commands::prover::{ + args::insert_version::{InsertVersionArgs, InsertVersionArgsFinal}, + info, +}; + +pub async fn run(shell: &Shell, args: InsertVersionArgs) -> anyhow::Result<()> { + check_prerequisites(shell, &PROVER_CLI_PREREQUISITE, false); + + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + let version = info::get_protocol_version(shell, &get_link_to_prover(&ecosystem_config)).await?; + let snark_wrapper = info::get_snark_wrapper(&get_link_to_prover(&ecosystem_config)).await?; + + let prover_url = info::get_database_url(shell).await?; + + let InsertVersionArgsFinal { + version, + snark_wrapper, + } = args.fill_values_with_prompts(version, snark_wrapper); + + let (minor, patch) = info::parse_version(&version)?; + + logger::info(format!( + "Inserting protocol version {}, snark wrapper {} into the database", + version, snark_wrapper + )); + + let cmd = Cmd::new(cmd!(shell, "prover_cli {prover_url} insert-version --version={minor} --patch={patch} --snark-wrapper={snark_wrapper}")); + cmd.run()?; + + logger::info("Done."); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover/mod.rs new file mode 100644 index 000000000000..364f8fe93efc --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/prover/mod.rs @@ -0,0 +1,22 @@ +use clap::Subcommand; +use xshell::Shell; + +mod args; +pub mod info; +pub mod insert_batch; +pub mod insert_version; + +#[derive(Subcommand, Debug)] +pub enum ProverCommands { + Info, + InsertBatch(args::insert_batch::InsertBatchArgs), + InsertVersion(args::insert_version::InsertVersionArgs), +} + +pub async fn run(shell: &Shell, args: ProverCommands) -> anyhow::Result<()> { + match args { + ProverCommands::Info => info::run(shell).await, + ProverCommands::InsertBatch(args) => insert_batch::run(shell, args).await, + ProverCommands::InsertVersion(args) => insert_version::run(shell, args).await, + } +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/prover_version.rs b/zk_toolbox/crates/zk_supervisor/src/commands/prover_version.rs deleted file mode 100644 index 8740e7c873a9..000000000000 --- a/zk_toolbox/crates/zk_supervisor/src/commands/prover_version.rs +++ /dev/null @@ -1,40 +0,0 @@ -use std::{fs, path::Path}; - -use common::logger; -use config::EcosystemConfig; -use xshell::{cmd, Shell}; - -pub async fn run(shell: &Shell) -> anyhow::Result<()> { - let link_to_code = EcosystemConfig::from_file(shell)?.link_to_code; - let link_to_prover = link_to_code.join("prover"); - - let protocol_version = get_protocol_version(shell, &link_to_prover).await?; - let snark_wrapper = get_snark_wrapper(&link_to_prover).await?; - - logger::info(format!( - "Current protocol version found in zksync-era: {}, snark_wrapper: {}", - protocol_version, snark_wrapper - )); - - Ok(()) -} - -async fn get_protocol_version(shell: &Shell, link_to_prover: &Path) -> anyhow::Result { - shell.change_dir(link_to_prover); - let protocol_version = cmd!(shell, "cargo run --release --bin prover_version").read()?; - - Ok(protocol_version) -} - -async fn get_snark_wrapper(link_to_prover: &Path) -> anyhow::Result { - let path = link_to_prover.join("data/keys/commitments.json"); - let file = fs::File::open(path).expect("Could not find commitments file in zksync-era"); - let json: serde_json::Value = - serde_json::from_reader(file).expect("Could not parse commitments.json"); - - let snark_wrapper = json - .get("snark_wrapper") - .expect("Could not find snark_wrapper in commitments.json"); - - Ok(snark_wrapper.to_string()) -} diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index a8722787b5ff..32aefa7fcad9 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -1,6 +1,6 @@ use clap::{Parser, Subcommand}; use commands::{ - contracts::ContractsArgs, database::DatabaseCommands, lint::LintArgs, + contracts::ContractsArgs, database::DatabaseCommands, lint::LintArgs, prover::ProverCommands, snapshot::SnapshotCommands, test::TestCommands, }; use common::{ @@ -49,8 +49,8 @@ enum SupervisorSubcommands { Fmt(FmtArgs), #[command(hide = true)] Markdown, - #[command(about = MSG_PROVER_VERSION_ABOUT)] - ProverVersion, + #[command(subcommand, about = MSG_PROVER_VERSION_ABOUT)] + Prover(ProverCommands), #[command(about = MSG_CONTRACTS_ABOUT)] Contracts(ContractsArgs), } @@ -109,7 +109,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> { } SupervisorSubcommands::Lint(args) => commands::lint::run(shell, args)?, SupervisorSubcommands::Fmt(args) => commands::fmt::run(shell.clone(), args).await?, - SupervisorSubcommands::ProverVersion => commands::prover_version::run(shell).await?, + SupervisorSubcommands::Prover(command) => commands::prover::run(shell, command).await?, SupervisorSubcommands::Contracts(args) => commands::contracts::run(shell, args)?, } Ok(())