Skip to content

Commit

Permalink
feat(zk_toolbox): Add setup keys step to prover init (#2811)
Browse files Browse the repository at this point in the history
## What ❔
Add setup keys step to prover init
  • Loading branch information
matias-gonz authored Sep 9, 2024
1 parent b8d4424 commit 0a9e096
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use clap::Parser;
use common::Prompt;

use crate::messages::MSG_SETUP_COMPRESSOR_KEY_PATH_PROMPT;

#[derive(Debug, Clone, Parser, Default)]
pub struct CompressorKeysArgs {
#[clap(long)]
pub path: Option<String>,
}

impl CompressorKeysArgs {
pub fn fill_values_with_prompt(self, default: &str) -> CompressorKeysArgs {
let path = self.path.unwrap_or_else(|| {
Prompt::new(MSG_SETUP_COMPRESSOR_KEY_PATH_PROMPT)
.default(default)
.ask()
});

CompressorKeysArgs { path: Some(path) }
}
}
121 changes: 72 additions & 49 deletions zk_toolbox/crates/zk_inception/src/commands/prover/args/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use url::Url;
use xshell::Shell;
use zksync_config::configs::fri_prover::CloudConnectionMode;

use super::init_bellman_cuda::InitBellmanCudaArgs;
use super::{
compressor_keys::CompressorKeysArgs, init_bellman_cuda::InitBellmanCudaArgs,
setup_keys::SetupKeysArgs,
};
use crate::{
commands::prover::gcs::get_project_ids,
consts::{DEFAULT_CREDENTIALS_FILE, DEFAULT_PROOF_STORE_DIR},
Expand All @@ -18,25 +21,24 @@ use crate::{
MSG_CREATE_GCS_BUCKET_LOCATION_PROMPT, MSG_CREATE_GCS_BUCKET_NAME_PROMTP,
MSG_CREATE_GCS_BUCKET_PROJECT_ID_NO_PROJECTS_PROMPT,
MSG_CREATE_GCS_BUCKET_PROJECT_ID_PROMPT, MSG_CREATE_GCS_BUCKET_PROMPT,
MSG_DOWNLOAD_SETUP_KEY_PROMPT, MSG_GETTING_PROOF_STORE_CONFIG,
MSG_GETTING_PUBLIC_STORE_CONFIG, MSG_PROOF_STORE_CONFIG_PROMPT, MSG_PROOF_STORE_DIR_PROMPT,
MSG_DOWNLOAD_SETUP_COMPRESSOR_KEY_PROMPT, MSG_GETTING_PROOF_STORE_CONFIG,
MSG_GETTING_PUBLIC_STORE_CONFIG, MSG_INITIALIZE_BELLMAN_CUDA_PROMPT,
MSG_PROOF_STORE_CONFIG_PROMPT, MSG_PROOF_STORE_DIR_PROMPT,
MSG_PROOF_STORE_GCS_BUCKET_BASE_URL_ERR, MSG_PROOF_STORE_GCS_BUCKET_BASE_URL_PROMPT,
MSG_PROOF_STORE_GCS_CREDENTIALS_FILE_PROMPT, MSG_PROVER_DB_NAME_HELP,
MSG_PROVER_DB_URL_HELP, MSG_SAVE_TO_PUBLIC_BUCKET_PROMPT, MSG_SETUP_KEY_PATH_PROMPT,
MSG_PROVER_DB_URL_HELP, MSG_SAVE_TO_PUBLIC_BUCKET_PROMPT, MSG_SETUP_KEYS_PROMPT,
MSG_USE_DEFAULT_DATABASES_HELP,
},
};

#[derive(Debug, Clone, Serialize, Deserialize, Parser, Default)]
#[derive(Debug, Clone, Parser, Default)]
pub struct ProverInitArgs {
// Proof store object
#[clap(long)]
pub proof_store_dir: Option<String>,
#[clap(flatten)]
#[serde(flatten)]
pub proof_store_gcs_config: ProofStorageGCSTmp,
#[clap(flatten)]
#[serde(flatten)]
pub create_gcs_bucket_config: ProofStorageGCSCreateBucketTmp,

// Public store object
Expand All @@ -45,20 +47,25 @@ pub struct ProverInitArgs {
#[clap(long)]
pub public_store_dir: Option<String>,
#[clap(flatten)]
#[serde(flatten)]
pub public_store_gcs_config: PublicStorageGCSTmp,
#[clap(flatten)]
#[serde(flatten)]
pub public_create_gcs_bucket_config: PublicStorageGCSCreateBucketTmp,

// Bellman cuda
#[clap(flatten)]
#[serde(flatten)]
pub bellman_cuda_config: InitBellmanCudaArgs,
#[clap(long, default_missing_value = "true", num_args = 0..=1)]
pub bellman_cuda: Option<bool>,

#[clap(long, default_missing_value = "true", num_args = 0..=1)]
pub setup_compressor_keys: Option<bool>,
#[clap(flatten)]
pub compressor_keys_args: CompressorKeysArgs,

#[clap(flatten)]
#[serde(flatten)]
pub setup_key_config: SetupKeyConfigTmp,
pub setup_keys_args: SetupKeysArgs,
#[clap(long, default_missing_value = "true", num_args = 0..=1)]
pub setup_keys: Option<bool>,

#[clap(long)]
pub setup_database: Option<bool>,
Expand Down Expand Up @@ -137,7 +144,7 @@ pub struct PublicStorageGCSCreateBucketTmp {
}

#[derive(Clone, Debug, Serialize, Deserialize, Parser, Default)]
pub struct SetupKeyConfigTmp {
pub struct SetupCompressorKeyConfigTmp {
#[clap(long)]
pub download_key: Option<bool>,
#[clap(long)]
Expand Down Expand Up @@ -171,12 +178,6 @@ pub enum ProofStorageConfig {
GCSCreateBucket(ProofStorageGCSCreateBucket),
}

#[derive(Debug, Clone)]
pub struct SetupKeyConfig {
pub download_key: bool,
pub setup_key_path: String,
}

#[derive(Debug, Clone)]
pub struct ProverDatabaseConfig {
pub database_config: DatabaseConfig,
Expand All @@ -187,8 +188,9 @@ pub struct ProverDatabaseConfig {
pub struct ProverInitArgsFinal {
pub proof_store: ProofStorageConfig,
pub public_store: Option<ProofStorageConfig>,
pub setup_key_config: SetupKeyConfig,
pub bellman_cuda_config: InitBellmanCudaArgs,
pub compressor_key_args: Option<CompressorKeysArgs>,
pub setup_keys: Option<SetupKeysArgs>,
pub bellman_cuda_config: Option<InitBellmanCudaArgs>,
pub cloud_type: CloudConnectionMode,
pub database_config: Option<ProverDatabaseConfig>,
}
Expand All @@ -197,20 +199,23 @@ impl ProverInitArgs {
pub(crate) fn fill_values_with_prompt(
&self,
shell: &Shell,
setup_key_path: &str,
default_compressor_key_path: &str,
chain_config: &ChainConfig,
) -> anyhow::Result<ProverInitArgsFinal> {
let proof_store = self.fill_proof_storage_values_with_prompt(shell)?;
let public_store = self.fill_public_storage_values_with_prompt(shell)?;
let setup_key_config = self.fill_setup_key_values_with_prompt(setup_key_path);
let bellman_cuda_config = self.fill_bellman_cuda_values_with_prompt()?;
let compressor_key_args =
self.fill_setup_compressor_key_values_with_prompt(default_compressor_key_path);
let bellman_cuda_config = self.fill_bellman_cuda_values_with_prompt();
let cloud_type = self.get_cloud_type_with_prompt();
let database_config = self.fill_database_values_with_prompt(chain_config);
let setup_keys = self.fill_setup_keys_values_with_prompt();

Ok(ProverInitArgsFinal {
proof_store,
public_store,
setup_key_config,
compressor_key_args,
setup_keys,
bellman_cuda_config,
cloud_type,
database_config,
Expand Down Expand Up @@ -336,29 +341,38 @@ impl ProverInitArgs {
}
}

fn fill_setup_key_values_with_prompt(&self, setup_key_path: &str) -> SetupKeyConfig {
let download_key = self
.clone()
.setup_key_config
.download_key
.unwrap_or_else(|| {
PromptConfirm::new(MSG_DOWNLOAD_SETUP_KEY_PROMPT)
.default(true)
.ask()
});
let setup_key_path = self
.clone()
.setup_key_config
.setup_key_path
.unwrap_or_else(|| {
Prompt::new(MSG_SETUP_KEY_PATH_PROMPT)
.default(setup_key_path)
.ask()
});
fn fill_setup_compressor_key_values_with_prompt(
&self,
default_path: &str,
) -> Option<CompressorKeysArgs> {
let download_key = self.clone().setup_compressor_keys.unwrap_or_else(|| {
PromptConfirm::new(MSG_DOWNLOAD_SETUP_COMPRESSOR_KEY_PROMPT)
.default(false)
.ask()
});

SetupKeyConfig {
download_key,
setup_key_path,
if download_key {
Some(
self.compressor_keys_args
.clone()
.fill_values_with_prompt(default_path),
)
} else {
None
}
}

fn fill_setup_keys_values_with_prompt(&self) -> Option<SetupKeysArgs> {
let args = self.setup_keys_args.clone();

if self.setup_keys.unwrap_or_else(|| {
PromptConfirm::new(MSG_SETUP_KEYS_PROMPT)
.default(false)
.ask()
}) {
Some(args)
} else {
None
}
}

Expand Down Expand Up @@ -460,8 +474,17 @@ impl ProverInitArgs {
})
}

fn fill_bellman_cuda_values_with_prompt(&self) -> anyhow::Result<InitBellmanCudaArgs> {
self.bellman_cuda_config.clone().fill_values_with_prompt()
fn fill_bellman_cuda_values_with_prompt(&self) -> Option<InitBellmanCudaArgs> {
let args = self.bellman_cuda_config.clone();
if self.bellman_cuda.unwrap_or_else(|| {
PromptConfirm::new(MSG_INITIALIZE_BELLMAN_CUDA_PROMPT)
.default(false)
.ask()
}) {
Some(args)
} else {
None
}
}

fn get_cloud_type_with_prompt(&self) -> CloudConnectionMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl std::fmt::Display for BellmanCudaPathSelection {
}

impl InitBellmanCudaArgs {
pub fn fill_values_with_prompt(self) -> anyhow::Result<InitBellmanCudaArgs> {
pub fn fill_values_with_prompt(self) -> InitBellmanCudaArgs {
let bellman_cuda_dir = self.bellman_cuda_dir.unwrap_or_else(|| {
match PromptSelect::new(
MSG_BELLMAN_CUDA_ORIGIN_SELECT,
Expand All @@ -43,8 +43,8 @@ impl InitBellmanCudaArgs {
}
});

Ok(InitBellmanCudaArgs {
InitBellmanCudaArgs {
bellman_cuda_dir: Some(bellman_cuda_dir),
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod compressor_keys;
pub mod init;
pub mod init_bellman_cuda;
pub mod run;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{Parser, ValueEnum};
use common::PromptSelect;
use strum::{EnumIter, IntoEnumIterator};

use crate::messages::{MSG_SETUP_KEYS_DOWNLOAD_HELP, MSG_SETUP_KEYS_REGION_PROMPT};
use crate::messages::{MSG_SETUP_KEYS_DOWNLOAD_SELECTION_PROMPT, MSG_SETUP_KEYS_REGION_PROMPT};

#[derive(Debug, Clone, Parser, Default)]
pub struct SetupKeysArgs {
Expand Down Expand Up @@ -33,9 +33,9 @@ pub enum Region {

impl SetupKeysArgs {
pub fn fill_values_with_prompt(self) -> SetupKeysArgsFinal {
let mode = self
.mode
.unwrap_or_else(|| PromptSelect::new(MSG_SETUP_KEYS_DOWNLOAD_HELP, Mode::iter()).ask());
let mode = self.mode.unwrap_or_else(|| {
PromptSelect::new(MSG_SETUP_KEYS_DOWNLOAD_SELECTION_PROMPT, Mode::iter()).ask()
});

if mode == Mode::Download {
let region = self.region.unwrap_or_else(|| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use anyhow::Context;
use common::{
check_prerequisites, cmd::Cmd, config::global_config, spinner::Spinner, WGET_PREREQUISITES,
};
use config::{EcosystemConfig, GeneralConfig};
use xshell::{cmd, Shell};

use super::{args::compressor_keys::CompressorKeysArgs, utils::get_link_to_prover};
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,
};

pub(crate) async fn run(shell: &Shell, args: CompressorKeysArgs) -> anyhow::Result<()> {
let ecosystem_config = EcosystemConfig::from_file(shell)?;
let chain_config = ecosystem_config
.load_chain(global_config().chain_name.clone())
.context(MSG_CHAIN_NOT_FOUND_ERR)?;
let mut general_config = chain_config.get_general_config()?;

let default_path = get_default_compressor_keys_path(&ecosystem_config)?;
let args = args.fill_values_with_prompt(&default_path);

download_compressor_key(
shell,
&mut general_config,
&args.path.context(MSG_SETUP_KEY_PATH_ERROR)?,
)?;

chain_config.save_general_config(&general_config)?;

Ok(())
}

pub(crate) fn download_compressor_key(
shell: &Shell,
general_config: &mut GeneralConfig,
path: &str,
) -> anyhow::Result<()> {
check_prerequisites(shell, &WGET_PREREQUISITES, false);
let spinner = Spinner::new(MSG_DOWNLOADING_SETUP_COMPRESSOR_KEY_SPINNER);
let mut compressor_config: zksync_config::configs::FriProofCompressorConfig = general_config
.proof_compressor_config
.as_ref()
.expect(MSG_PROOF_COMPRESSOR_CONFIG_NOT_FOUND_ERR)
.clone();
compressor_config.universal_setup_path = path.to_string();
general_config.proof_compressor_config = Some(compressor_config.clone());

let url = compressor_config.universal_setup_download_url;
let path = std::path::Path::new(path);
let parent = path.parent().expect(MSG_SETUP_KEY_PATH_ERROR);
let file_name = path.file_name().expect(MSG_SETUP_KEY_PATH_ERROR);

Cmd::new(cmd!(shell, "wget {url} -P {parent}")).run()?;

if file_name != "setup_2^24.key" {
Cmd::new(cmd!(shell, "mv {parent}/setup_2^24.key {path}")).run()?;
}

spinner.finish();
Ok(())
}

pub fn get_default_compressor_keys_path(
ecosystem_config: &EcosystemConfig,
) -> anyhow::Result<String> {
let link_to_prover = get_link_to_prover(ecosystem_config);
let path = link_to_prover.join("keys/setup/setup_2^24.key");
let string = path.to_str().unwrap();

Ok(String::from(string))
}
Loading

0 comments on commit 0a9e096

Please sign in to comment.