Skip to content

Commit

Permalink
Add link_to_code
Browse files Browse the repository at this point in the history
  • Loading branch information
matias-gonz committed Oct 31, 2024
1 parent 070e6a8 commit 38c02dd
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 32 deletions.
15 changes: 9 additions & 6 deletions zkstack_cli/crates/zkstack/src/commands/chain/args/create.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use std::{path::PathBuf, str::FromStr};

use anyhow::{bail, Context};
use clap::{Parser, ValueEnum, ValueHint};
Expand Down Expand Up @@ -34,6 +31,7 @@ use crate::{
MSG_WALLET_CREATION_PROMPT, MSG_WALLET_CREATION_VALIDATOR_ERR, MSG_WALLET_PATH_HELP,
MSG_WALLET_PATH_INVALID_ERR, MSG_WALLET_PATH_PROMPT,
},
utils::link_to_code::{get_link_to_code, resolve_link_to_code},
};

// We need to duplicate it for using enum inside the arguments
Expand Down Expand Up @@ -89,7 +87,7 @@ impl ChainCreateArgs {
number_of_chains: u32,
l1_network: Option<L1Network>,
possible_erc20: Vec<Erc20Token>,
link_to_code: &Path,
link_to_code: Option<String>,
) -> anyhow::Result<ChainCreateArgsFinal> {
let mut chain_name = self
.chain_name
Expand Down Expand Up @@ -232,9 +230,12 @@ impl ChainCreateArgs {
}
};

let link_to_code = link_to_code.unwrap_or_else(|| get_link_to_code(shell));
let link_to_code = resolve_link_to_code(shell, link_to_code)?;

let default_genesis_config = GenesisConfig::read_with_base_path(
shell,
EcosystemConfig::default_configs_path(link_to_code),
EcosystemConfig::default_configs_path(&link_to_code),
)
.context("failed reading genesis config")?;
let has_evm_emulation_support = default_genesis_config.evm_emulator_hash.is_some();
Expand Down Expand Up @@ -268,6 +269,7 @@ impl ChainCreateArgs {
set_as_default,
legacy_bridge: self.legacy_bridge,
evm_emulator,
link_to_code,
})
}
}
Expand All @@ -284,6 +286,7 @@ pub struct ChainCreateArgsFinal {
pub set_as_default: bool,
pub legacy_bridge: bool,
pub evm_emulator: bool,
pub link_to_code: PathBuf,
}

#[derive(Debug, Clone, EnumIter, Display, PartialEq, Eq)]
Expand Down
39 changes: 15 additions & 24 deletions zkstack_cli/crates/zkstack/src/commands/chain/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::cell::OnceCell;
use anyhow::Context;
use common::{logger, spinner::Spinner};
use config::{
create_local_configs_dir, create_wallets, traits::SaveConfigWithBasePath, ChainConfig,
EcosystemConfig,
create_local_configs_dir, create_wallets, get_default_era_chain_id,
traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig,
};
use xshell::Shell;
use zksync_basic_types::L2ChainId;
Expand Down Expand Up @@ -43,16 +43,10 @@ fn create(

let link_to_code = ecosystem
.as_ref()
.map(|ecosystem| ecosystem.link_to_code.clone());
.map(|ecosystem| ecosystem.link_to_code.clone().display().to_string());

let args = args
.fill_values_with_prompt(
shell,
number_of_chains,
l1_network,
tokens,
&link_to_code.unwrap(),
)
.fill_values_with_prompt(shell, number_of_chains, l1_network, tokens, link_to_code)
.context(MSG_ARGS_VALIDATOR_ERR)?;

logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&args));
Expand All @@ -62,7 +56,7 @@ fn create(
let name = args.chain_name.clone();
let set_as_default = args.set_as_default;

create_chain_inner(args, &ecosystem.clone().unwrap(), shell)?;
create_chain_inner(args, shell)?;

if let Some(ecosystem) = ecosystem.as_mut() {
if set_as_default {
Expand All @@ -78,34 +72,31 @@ fn create(
Ok(())
}

pub(crate) fn create_chain_inner(
args: ChainCreateArgsFinal,
ecosystem_config: &EcosystemConfig,
shell: &Shell,
) -> anyhow::Result<()> {
pub(crate) fn create_chain_inner(args: ChainCreateArgsFinal, shell: &Shell) -> anyhow::Result<()> {
if args.legacy_bridge {
logger::warn("WARNING!!! You are creating a chain with legacy bridge, use it only for testing compatibility")
}
let default_chain_name = args.chain_name.clone();
let chain_path = ecosystem_config.chains.join(&default_chain_name);
let chain_path = shell.current_dir(); // ecosystem_config.chains.join(&default_chain_name);
let chain_configs_path = create_local_configs_dir(shell, &chain_path)?;
let (chain_id, legacy_bridge) = if args.legacy_bridge {
// Legacy bridge is distinguished by using the same chain id as ecosystem
(ecosystem_config.era_chain_id, Some(true))
(get_default_era_chain_id(), Some(true)) // (ecosystem_config.era_chain_id, Some(true))
} else {
(L2ChainId::from(args.chain_id), None)
};
let internal_id = ecosystem_config.list_of_chains().len() as u32;
let internal_id = 0; // ecosystem_config.list_of_chains().len() as u32;
let link_to_code = args.link_to_code;

let chain_config = ChainConfig {
id: internal_id,
name: default_chain_name.clone(),
chain_id,
prover_version: args.prover_version,
l1_network: ecosystem_config.l1_network,
link_to_code: ecosystem_config.link_to_code.clone(),
rocks_db_path: ecosystem_config.get_chain_rocks_db_path(&default_chain_name),
artifacts: ecosystem_config.get_chain_artifacts_path(&default_chain_name),
l1_network: Default::default(), // ecosystem_config.l1_network,
link_to_code: link_to_code.clone(),
rocks_db_path: Default::default(), // ecosystem_config.get_chain_rocks_db_path(&default_chain_name),
artifacts: Default::default(), // ecosystem_config.get_chain_artifacts_path(&default_chain_name),
configs: chain_configs_path.clone(),
external_node_config_path: None,
l1_batch_commit_data_generator_mode: args.l1_batch_commit_data_generator_mode,
Expand All @@ -119,7 +110,7 @@ pub(crate) fn create_chain_inner(
create_wallets(
shell,
&chain_config.configs,
&ecosystem_config.link_to_code,
&link_to_code,
internal_id,
args.wallet_creation,
args.wallet_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl EcosystemCreateArgs {
0,
Some(l1_network),
vec![],
Path::new(&link_to_code),
Some(link_to_code.clone()),
)?;

let start_containers = self.start_containers.unwrap_or_else(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn create(args: EcosystemCreateArgs, shell: &Shell) -> anyhow::Result<()> {
spinner.finish();

let spinner = Spinner::new(MSG_CREATING_DEFAULT_CHAIN_SPINNER);
create_chain_inner(chain_config, &ecosystem_config, shell)?;
create_chain_inner(chain_config, shell)?;
spinner.finish();

if args.start_containers {
Expand Down
104 changes: 104 additions & 0 deletions zkstack_cli/crates/zkstack/src/utils/link_to_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{
path::{Path, PathBuf},
str::FromStr,
};

use anyhow::bail;
use common::{cmd::Cmd, git, logger, spinner::Spinner, Prompt, PromptConfirm, PromptSelect};
use config::ZKSYNC_ERA_GIT_REPO;
use strum::{EnumIter, IntoEnumIterator};
use xshell::{cmd, Shell};

use crate::messages::{
msg_path_to_zksync_does_not_exist_err, MSG_CLONING_ERA_REPO_SPINNER,
MSG_CONFIRM_STILL_USE_FOLDER, MSG_LINK_TO_CODE_PROMPT, MSG_LINK_TO_CODE_SELECTION_CLONE,
MSG_LINK_TO_CODE_SELECTION_PATH, MSG_NOT_MAIN_REPO_OR_FORK_ERR, MSG_REPOSITORY_ORIGIN_PROMPT,
};

#[derive(Debug, Clone, EnumIter, PartialEq, Eq)]
enum LinkToCodeSelection {
Clone,
Path,
}

impl std::fmt::Display for LinkToCodeSelection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LinkToCodeSelection::Clone => write!(f, "{MSG_LINK_TO_CODE_SELECTION_CLONE}"),
LinkToCodeSelection::Path => write!(f, "{MSG_LINK_TO_CODE_SELECTION_PATH}"),
}
}
}

fn check_link_to_code(shell: &Shell, path: &str) -> anyhow::Result<()> {
let path = Path::new(path);
if !shell.path_exists(path) {
bail!(msg_path_to_zksync_does_not_exist_err(
path.to_str().unwrap()
));
}

let _guard = shell.push_dir(path);
let out = String::from_utf8(
Cmd::new(cmd!(shell, "git remote -v"))
.run_with_output()?
.stdout,
)?;

if !out.contains("matter-labs/zksync-era") {
bail!(MSG_NOT_MAIN_REPO_OR_FORK_ERR);
}

Ok(())
}

fn pick_new_link_to_code(shell: &Shell) -> String {
let link_to_code: String = Prompt::new(MSG_LINK_TO_CODE_PROMPT).ask();
match check_link_to_code(shell, &link_to_code) {
Ok(_) => link_to_code,
Err(err) => {
logger::warn(err);
if !PromptConfirm::new(MSG_CONFIRM_STILL_USE_FOLDER).ask() {
pick_new_link_to_code(shell)
} else {
link_to_code
}
}
}
}

pub fn get_link_to_code(shell: &Shell) -> String {
let link_to_code_selection =
PromptSelect::new(MSG_REPOSITORY_ORIGIN_PROMPT, LinkToCodeSelection::iter()).ask();
match link_to_code_selection {
LinkToCodeSelection::Clone => "".to_string(),
LinkToCodeSelection::Path => {
let mut path: String = Prompt::new(MSG_LINK_TO_CODE_PROMPT).ask();
if let Err(err) = check_link_to_code(shell, &path) {
logger::warn(err);
if !PromptConfirm::new(MSG_CONFIRM_STILL_USE_FOLDER).ask() {
path = pick_new_link_to_code(shell);
}
}
path
}
}
}

pub fn resolve_link_to_code(shell: &Shell, link_to_code: String) -> anyhow::Result<PathBuf> {
if link_to_code.is_empty() {
let spinner = Spinner::new(MSG_CLONING_ERA_REPO_SPINNER);
let link_to_code = git::clone(
shell,
shell.current_dir(),
ZKSYNC_ERA_GIT_REPO,
"zksync-era",
)?;
spinner.finish();
Ok(link_to_code)
} else {
let path = PathBuf::from_str(&link_to_code)?;
git::submodule_update(shell, path.clone())?;
Ok(path)
}
}
1 change: 1 addition & 0 deletions zkstack_cli/crates/zkstack/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod consensus;
pub mod forge;
pub mod link_to_code;
pub mod ports;
pub mod rocks_db;

0 comments on commit 38c02dd

Please sign in to comment.