From 859ce92f977a3ca64945193702a198c829584de2 Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Mon, 23 Sep 2024 10:21:35 -0600 Subject: [PATCH 1/7] added ecosystem-only init --- .../commands/chain/accept_chain_ownership.rs | 43 ++++++++ .../src/commands/chain/args/create.rs | 2 +- .../src/commands/chain/args/genesis.rs | 7 ++ .../src/commands/chain/args/init.rs | 5 + .../src/commands/chain/build_transactions.rs | 5 +- .../zk_inception/src/commands/chain/common.rs | 58 +--------- .../zk_inception/src/commands/chain/init.rs | 93 ++++++++++------ .../zk_inception/src/commands/chain/mod.rs | 23 +++- .../src/commands/chain/register_chain.rs | 98 +++++++++++++++++ .../src/commands/ecosystem/args/init.rs | 31 +++--- .../src/commands/ecosystem/init.rs | 102 +++++++++++------- .../crates/zk_inception/src/messages.rs | 10 +- 12 files changed, 326 insertions(+), 151 deletions(-) create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs new file mode 100644 index 000000000000..bfa91063004c --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs @@ -0,0 +1,43 @@ +use anyhow::Context; +use common::{config::global_config, forge::ForgeScriptArgs, logger, spinner::Spinner}; +use config::EcosystemConfig; +use xshell::Shell; + +use crate::{ + accept_ownership::accept_admin, + messages::{ + MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_NOT_INITIALIZED, MSG_CHAIN_OWNERSHIP_TRANSFERRED, + MSG_L1_SECRETS_MUST_BE_PRESENTED, + }, +}; + +pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_INITIALIZED)?; + let contracts = chain_config.get_contracts_config()?; + let secrets = chain_config.get_secrets_config()?; + let l1_rpc_url = secrets + .l1 + .context(MSG_L1_SECRETS_MUST_BE_PRESENTED)? + .l1_rpc_url + .expose_str() + .to_string(); + + let spinner = Spinner::new(MSG_ACCEPTING_ADMIN_SPINNER); + accept_admin( + shell, + &ecosystem_config, + contracts.l1.chain_admin_addr, + chain_config.get_wallets_config()?.governor_private_key(), + contracts.l1.diamond_proxy_addr, + &args, + l1_rpc_url.clone(), + ) + .await?; + spinner.finish(); + logger::success(MSG_CHAIN_OWNERSHIP_TRANSFERRED); + Ok(()) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/create.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/create.rs index 3ea15d10f8be..5fc46c1b227a 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/create.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/create.rs @@ -127,7 +127,7 @@ impl ChainCreateArgs { .ask() }); - let wallet_path: Option = if self.wallet_creation == Some(WalletCreation::InFile) { + let wallet_path: Option = if wallet_creation == WalletCreation::InFile { Some(self.wallet_path.unwrap_or_else(|| { Prompt::new(MSG_WALLET_PATH_PROMPT) .validate_with(|val: &String| { diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs index 483b78e9b267..fa1ed28588ca 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs @@ -77,6 +77,13 @@ impl GenesisArgs { } } } + + pub fn reset_db_names(&mut self) { + self.prover_db_name = None; + self.prover_db_url = None; + self.server_db_name = None; + self.server_db_url = None; + } } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs index 9dd6c490bd78..ecfbc69986fb 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs @@ -57,6 +57,9 @@ pub struct InitArgs { pub l1_rpc_url: Option, #[clap(long, help = MSG_PORT_OFFSET_HELP)] pub port_offset: Option, + /// Only create chain configs + #[clap(long, default_value_t = false)] + pub configs_only: bool, } impl InitArgs { @@ -90,6 +93,7 @@ impl InitArgs { .port_offset .unwrap_or(PortOffset::from_chain_id(config.id as u16)) .into(), + configs_only: self.configs_only, } } } @@ -101,4 +105,5 @@ pub struct InitArgsFinal { pub deploy_paymaster: bool, pub l1_rpc_url: String, pub port_offset: u16, + pub configs_only: bool, } diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/build_transactions.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/build_transactions.rs index 68cb7a9a0742..6c630da06208 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/build_transactions.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/build_transactions.rs @@ -6,9 +6,10 @@ use config::{ use ethers::utils::hex::ToHex; use xshell::Shell; -use super::common::register_chain; use crate::{ - commands::chain::args::build_transactions::BuildTransactionsArgs, + commands::chain::{ + args::build_transactions::BuildTransactionsArgs, register_chain::register_chain, + }, messages::{ MSG_BUILDING_CHAIN_REGISTRATION_TXNS_SPINNER, MSG_CHAIN_NOT_FOUND_ERR, MSG_CHAIN_TRANSACTIONS_BUILT, MSG_CHAIN_TXN_MISSING_CONTRACT_CONFIG, diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/common.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/common.rs index ec70d6122d23..9e34aafa6631 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/common.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/common.rs @@ -1,66 +1,12 @@ -use common::{ - forge::{Forge, ForgeScriptArgs}, - spinner::Spinner, -}; -use config::{ - forge_interface::{ - register_chain::{input::RegisterChainL1Config, output::RegisterChainOutput}, - script_params::REGISTER_CHAIN_SCRIPT_PARAMS, - }, - traits::{ReadConfig, SaveConfig}, - ChainConfig, ContractsConfig, EcosystemConfig, -}; +use common::spinner::Spinner; +use config::{ChainConfig, EcosystemConfig}; use types::{BaseToken, L1Network, WalletCreation}; -use xshell::Shell; use crate::{ consts::AMOUNT_FOR_DISTRIBUTION_TO_WALLETS, messages::{MSG_DISTRIBUTING_ETH_SPINNER, MSG_MINT_BASE_TOKEN_SPINNER}, - utils::forge::{check_the_balance, fill_forge_private_key}, }; -#[allow(clippy::too_many_arguments)] -pub async fn register_chain( - shell: &Shell, - forge_args: ForgeScriptArgs, - config: &EcosystemConfig, - chain_config: &ChainConfig, - contracts: &mut ContractsConfig, - l1_rpc_url: String, - sender: Option, - broadcast: bool, -) -> anyhow::Result<()> { - let deploy_config_path = REGISTER_CHAIN_SCRIPT_PARAMS.input(&config.link_to_code); - - let deploy_config = RegisterChainL1Config::new(chain_config, contracts)?; - deploy_config.save(shell, deploy_config_path)?; - - let mut forge = Forge::new(&config.path_to_foundry()) - .script(®ISTER_CHAIN_SCRIPT_PARAMS.script(), forge_args.clone()) - .with_ffi() - .with_rpc_url(l1_rpc_url); - - if broadcast { - forge = forge.with_broadcast(); - } - - if let Some(address) = sender { - forge = forge.with_sender(address); - } else { - forge = fill_forge_private_key(forge, config.get_wallets()?.governor_private_key())?; - check_the_balance(&forge).await?; - } - - forge.run(shell)?; - - let register_chain_output = RegisterChainOutput::read( - shell, - REGISTER_CHAIN_SCRIPT_PARAMS.output(&chain_config.link_to_code), - )?; - contracts.set_chain_contracts(®ister_chain_output); - Ok(()) -} - // Distribute eth to the chain wallets for localhost environment pub async fn distribute_eth( ecosystem_config: &EcosystemConfig, diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs index fa2388a69be8..7b2a97051b60 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs @@ -2,12 +2,14 @@ use anyhow::{bail, Context}; use common::{config::global_config, git, logger, spinner::Spinner}; use config::{ copy_configs, ports_config, set_l1_rpc_url, traits::SaveConfigWithBasePath, - update_from_chain_config, update_ports, ChainConfig, EcosystemConfig, GeneralConfig, + update_from_chain_config, update_ports, ChainConfig, ContractsConfig, EcosystemConfig, + GeneralConfig, }; +use ethers::types::Address; use types::BaseToken; use xshell::Shell; -use super::common::{distribute_eth, mint_base_token, register_chain}; +use super::common::{distribute_eth, mint_base_token}; use crate::{ accept_ownership::accept_admin, commands::{ @@ -15,15 +17,16 @@ use crate::{ args::init::{InitArgs, InitArgsFinal}, deploy_l2_contracts, deploy_paymaster, genesis::genesis, + register_chain::register_chain, set_token_multiplier_setter::set_token_multiplier_setter, setup_legacy_bridge::setup_legacy_bridge, }, portal::update_portal_config, }, messages::{ - msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_INITIALIZED, - MSG_CHAIN_NOT_FOUND_ERR, MSG_DEPLOYING_PAYMASTER, MSG_GENESIS_DATABASE_ERR, - MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR, MSG_PORTS_CONFIG_ERR, + msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_CONFIGS_INITIALIZED, + MSG_CHAIN_INITIALIZED, MSG_CHAIN_NOT_FOUND_ERR, MSG_DEPLOYING_PAYMASTER, + MSG_GENESIS_DATABASE_ERR, MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR, MSG_PORTS_CONFIG_ERR, MSG_REGISTERING_CHAIN_SPINNER, MSG_SELECTED_CONFIG, MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER, MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND, }, @@ -44,7 +47,11 @@ pub(crate) async fn run(args: InitArgs, shell: &Shell) -> anyhow::Result<()> { init(&mut args, shell, &config, &chain_config).await?; - logger::success(MSG_CHAIN_INITIALIZED); + if args.configs_only { + logger::success(MSG_CHAIN_CONFIGS_INITIALIZED); + } else { + logger::success(MSG_CHAIN_INITIALIZED); + } Ok(()) } @@ -54,35 +61,15 @@ pub async fn init( ecosystem_config: &EcosystemConfig, chain_config: &ChainConfig, ) -> anyhow::Result<()> { - copy_configs(shell, &ecosystem_config.link_to_code, &chain_config.configs)?; - - let mut general_config = chain_config.get_general_config()?; - apply_port_offset(init_args.port_offset, &mut general_config)?; - let ports = ports_config(&general_config).context(MSG_PORTS_CONFIG_ERR)?; - - let consensus_keys = generate_consensus_keys(); - let consensus_config = - get_consensus_config(chain_config, ports, Some(consensus_keys.clone()), None)?; - general_config.consensus_config = Some(consensus_config); - general_config.save_with_base_path(shell, &chain_config.configs)?; - - let mut genesis_config = chain_config.get_genesis_config()?; - update_from_chain_config(&mut genesis_config, chain_config); - genesis_config.save_with_base_path(shell, &chain_config.configs)?; - - // Copy ecosystem contracts - let mut contracts_config = ecosystem_config.get_contracts_config()?; - contracts_config.l1.base_token_addr = chain_config.base_token.address; - contracts_config.save_with_base_path(shell, &chain_config.configs)?; + let mut contracts_config = init_configs(init_args, shell, ecosystem_config, chain_config)?; + if init_args.configs_only { + return Ok(()); + } distribute_eth(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; mint_base_token(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; - let mut secrets = chain_config.get_secrets_config()?; - set_l1_rpc_url(&mut secrets, init_args.l1_rpc_url.clone())?; - secrets.consensus = Some(get_consensus_secrets(&consensus_keys)); - secrets.save_with_base_path(shell, &chain_config.configs)?; - + // Register chain on BridgeHub (run by L1 Governor) let spinner = Spinner::new(MSG_REGISTERING_CHAIN_SPINNER); register_chain( shell, @@ -97,6 +84,8 @@ pub async fn init( .await?; contracts_config.save_with_base_path(shell, &chain_config.configs)?; spinner.finish(); + + // Accept ownership for DiamondProxy (run by L2 Governor) let spinner = Spinner::new(MSG_ACCEPTING_ADMIN_SPINNER); accept_admin( shell, @@ -110,6 +99,7 @@ pub async fn init( .await?; spinner.finish(); + // Set token multiplier setter address (run by L2 Governor) if chain_config.base_token != BaseToken::eth() { let spinner = Spinner::new(MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER); set_token_multiplier_setter( @@ -130,6 +120,7 @@ pub async fn init( spinner.finish(); } + // Deploy L2 contracts: L2 Shared Bridge, L2 Default Upgrader, etc. (run by L2 Governor) deploy_l2_contracts::deploy_l2_contracts( shell, chain_config, @@ -140,6 +131,7 @@ pub async fn init( .await?; contracts_config.save_with_base_path(shell, &chain_config.configs)?; + // Setup legacy bridge - shouldn't be used for new chains (run by L1 Governor) if let Some(true) = chain_config.legacy_bridge { setup_legacy_bridge( shell, @@ -151,6 +143,7 @@ pub async fn init( .await?; } + // Deploy Paymaster contract (run by L2 Governor) if init_args.deploy_paymaster { let spinner = Spinner::new(MSG_DEPLOYING_PAYMASTER); deploy_paymaster::deploy_paymaster( @@ -177,6 +170,44 @@ pub async fn init( Ok(()) } +fn init_configs( + init_args: &mut InitArgsFinal, + shell: &Shell, + ecosystem_config: &EcosystemConfig, + chain_config: &ChainConfig, +) -> anyhow::Result { + copy_configs(shell, &ecosystem_config.link_to_code, &chain_config.configs)?; + + let mut general_config = chain_config.get_general_config()?; + apply_port_offset(init_args.port_offset, &mut general_config)?; + let ports = ports_config(&general_config).context(MSG_PORTS_CONFIG_ERR)?; + + let consensus_keys = generate_consensus_keys(); + let consensus_config = + get_consensus_config(chain_config, ports, Some(consensus_keys.clone()), None)?; + general_config.consensus_config = Some(consensus_config); + general_config.save_with_base_path(shell, &chain_config.configs)?; + + let mut genesis_config = chain_config.get_genesis_config()?; + update_from_chain_config(&mut genesis_config, chain_config); + genesis_config.save_with_base_path(shell, &chain_config.configs)?; + + // Copy ecosystem contracts + let mut contracts_config = ecosystem_config.get_contracts_config()?; + contracts_config.l1.diamond_proxy_addr = Address::zero(); + contracts_config.l1.governance_addr = Address::zero(); + contracts_config.l1.chain_admin_addr = Address::zero(); + contracts_config.l1.base_token_addr = chain_config.base_token.address; + contracts_config.save_with_base_path(shell, &chain_config.configs)?; + + let mut secrets = chain_config.get_secrets_config()?; + set_l1_rpc_url(&mut secrets, init_args.l1_rpc_url.clone())?; + secrets.consensus = Some(get_consensus_secrets(&consensus_keys)); + secrets.save_with_base_path(shell, &chain_config.configs)?; + + Ok(contracts_config) +} + fn apply_port_offset(port_offset: u16, general_config: &mut GeneralConfig) -> anyhow::Result<()> { let Some(mut ports_config) = ports_config(general_config) else { bail!("Missing ports config"); diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs index 4ddc4bf58569..f2d8b3d57578 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs @@ -10,6 +10,7 @@ use crate::commands::chain::{ deploy_l2_contracts::Deploy2ContractsOption, }; +mod accept_chain_ownership; pub(crate) mod args; mod build_transactions; mod common; @@ -18,6 +19,7 @@ pub mod deploy_l2_contracts; pub mod deploy_paymaster; pub mod genesis; pub(crate) mod init; +pub mod register_chain; mod set_token_multiplier_setter; mod setup_legacy_bridge; @@ -31,12 +33,23 @@ pub enum ChainCommands { Init(InitArgs), /// Run server genesis Genesis(GenesisArgs), - /// Initialize bridges on l2 - #[command(alias = "bridge")] - InitializeBridges(ForgeScriptArgs), - /// Deploy all l2 contracts + /// Register a new chain on L1 (executed by L1 governor). + /// This command deploys and configures Governance, ChainAdmin, and DiamondProxy contracts, + /// registers chain with BridgeHub and sets pending admin for DiamondProxy. + /// Note: After completion, L2 governor can accept ownership by running `accept-chain-ownership` + #[command(alias = "register")] + RegisterChain(ForgeScriptArgs), + /// Deploy all L2 contracts (executed by L1 governor). #[command(alias = "l2")] DeployL2Contracts(ForgeScriptArgs), + /// Accept ownership of L2 chain (executed by L2 governor). + /// This command should be run after `register-chain` to accept ownership of newly created + /// DiamondProxy contract. + #[command(alias = "accept-ownership")] + AcceptChainOwnership(ForgeScriptArgs), + /// Initialize bridges on L2 + #[command(alias = "bridge")] + InitializeBridges(ForgeScriptArgs), /// Deploy L2 consensus registry #[command(alias = "consensus")] DeployConsensusRegistry(ForgeScriptArgs), @@ -55,9 +68,11 @@ pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<() ChainCommands::Init(args) => init::run(args, shell).await, ChainCommands::BuildTransactions(args) => build_transactions::run(args, shell).await, ChainCommands::Genesis(args) => genesis::run(args, shell).await, + ChainCommands::RegisterChain(args) => register_chain::run(args, shell).await, ChainCommands::DeployL2Contracts(args) => { deploy_l2_contracts::run(args, shell, Deploy2ContractsOption::All).await } + ChainCommands::AcceptChainOwnership(args) => accept_chain_ownership::run(args, shell).await, ChainCommands::DeployConsensusRegistry(args) => { deploy_l2_contracts::run(args, shell, Deploy2ContractsOption::ConsensusRegistry).await } diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs new file mode 100644 index 000000000000..e2ce133dfa83 --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs @@ -0,0 +1,98 @@ +use anyhow::Context; +use common::{ + config::global_config, + forge::{Forge, ForgeScriptArgs}, + logger, + spinner::Spinner, +}; +use config::{ + forge_interface::{ + register_chain::{input::RegisterChainL1Config, output::RegisterChainOutput}, + script_params::REGISTER_CHAIN_SCRIPT_PARAMS, + }, + traits::{ReadConfig, SaveConfig, SaveConfigWithBasePath}, + ChainConfig, ContractsConfig, EcosystemConfig, +}; +use xshell::Shell; + +use crate::{ + messages::{ + MSG_CHAIN_NOT_INITIALIZED, MSG_CHAIN_REGISTERED, MSG_L1_SECRETS_MUST_BE_PRESENTED, + MSG_REGISTERING_CHAIN_SPINNER, + }, + utils::forge::{check_the_balance, fill_forge_private_key}, +}; + +pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_INITIALIZED)?; + let mut contracts = chain_config.get_contracts_config()?; + let secrets = chain_config.get_secrets_config()?; + let l1_rpc_url = secrets + .l1 + .context(MSG_L1_SECRETS_MUST_BE_PRESENTED)? + .l1_rpc_url + .expose_str() + .to_string(); + let spinner = Spinner::new(MSG_REGISTERING_CHAIN_SPINNER); + register_chain( + shell, + args, + &ecosystem_config, + &chain_config, + &mut contracts, + l1_rpc_url, + None, + true, + ) + .await?; + contracts.save_with_base_path(shell, chain_config.configs)?; + spinner.finish(); + logger::success(MSG_CHAIN_REGISTERED); + Ok(()) +} + +#[allow(clippy::too_many_arguments)] +pub async fn register_chain( + shell: &Shell, + forge_args: ForgeScriptArgs, + config: &EcosystemConfig, + chain_config: &ChainConfig, + contracts: &mut ContractsConfig, + l1_rpc_url: String, + sender: Option, + broadcast: bool, +) -> anyhow::Result<()> { + let deploy_config_path = REGISTER_CHAIN_SCRIPT_PARAMS.input(&config.link_to_code); + + let deploy_config = RegisterChainL1Config::new(chain_config, contracts)?; + deploy_config.save(shell, deploy_config_path)?; + + let mut forge = Forge::new(&config.path_to_foundry()) + .script(®ISTER_CHAIN_SCRIPT_PARAMS.script(), forge_args.clone()) + .with_ffi() + .with_rpc_url(l1_rpc_url); + + if broadcast { + forge = forge.with_broadcast(); + } + + if let Some(address) = sender { + forge = forge.with_sender(address); + } else { + forge = fill_forge_private_key(forge, config.get_wallets()?.governor_private_key())?; + check_the_balance(&forge).await?; + } + + forge.run(shell)?; + + let register_chain_output = RegisterChainOutput::read( + shell, + REGISTER_CHAIN_SCRIPT_PARAMS.output(&chain_config.link_to_code), + )?; + contracts.set_chain_contracts(®ister_chain_output); + Ok(()) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/init.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/init.rs index 2411e4b95588..3bd172cea96d 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/init.rs @@ -10,8 +10,8 @@ use crate::{ commands::chain::args::genesis::GenesisArgs, defaults::LOCAL_RPC_URL, messages::{ - MSG_DEPLOY_ECOSYSTEM_PROMPT, MSG_DEPLOY_ERC20_PROMPT, MSG_DEPLOY_PAYMASTER_PROMPT, - MSG_DEV_ARG_HELP, MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, MSG_L1_RPC_URL_INVALID_ERR, + MSG_DEPLOY_ECOSYSTEM_PROMPT, MSG_DEPLOY_ERC20_PROMPT, MSG_DEV_ARG_HELP, + MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, MSG_L1_RPC_URL_INVALID_ERR, MSG_L1_RPC_URL_PROMPT, MSG_OBSERVABILITY_HELP, MSG_OBSERVABILITY_PROMPT, }, }; @@ -73,9 +73,6 @@ pub struct EcosystemArgsFinal { #[derive(Debug, Clone, Serialize, Deserialize, Parser)] pub struct EcosystemInitArgs { - /// Deploy Paymaster contract - #[clap(long, default_missing_value = "true", num_args = 0..=1)] - pub deploy_paymaster: Option, /// Deploy ERC20 contracts #[clap(long, default_missing_value = "true", num_args = 0..=1)] pub deploy_erc20: Option, @@ -85,9 +82,15 @@ pub struct EcosystemInitArgs { #[clap(flatten)] #[serde(flatten)] pub forge_args: ForgeScriptArgs, + /// Deploy Paymaster contract + #[clap(long, default_missing_value = "true", num_args = 0..=1)] + pub deploy_paymaster: Option, #[clap(flatten, next_help_heading = MSG_GENESIS_ARGS_HELP)] #[serde(flatten)] pub genesis_args: GenesisArgs, + /// Initialize ecosystem only and skip chain initialization (chain can be initialized later with `chain init` subcommand) + #[clap(long, default_value_t = false)] + pub ecosystem_only: bool, #[clap(long, help = MSG_DEV_ARG_HELP)] pub dev: bool, #[clap(long, short = 'o', help = MSG_OBSERVABILITY_HELP, default_missing_value = "true", num_args = 0..=1)] @@ -96,20 +99,14 @@ pub struct EcosystemInitArgs { impl EcosystemInitArgs { pub fn fill_values_with_prompt(self, l1_network: L1Network) -> EcosystemInitArgsFinal { - let (deploy_paymaster, deploy_erc20) = if self.dev { - (true, true) + let deploy_erc20 = if self.dev { + true } else { - let deploy_paymaster = self.deploy_paymaster.unwrap_or_else(|| { - PromptConfirm::new(MSG_DEPLOY_PAYMASTER_PROMPT) - .default(true) - .ask() - }); - let deploy_erc20 = self.deploy_erc20.unwrap_or_else(|| { + self.deploy_erc20.unwrap_or_else(|| { PromptConfirm::new(MSG_DEPLOY_ERC20_PROMPT) .default(true) .ask() - }); - (deploy_paymaster, deploy_erc20) + }) }; let ecosystem = self.ecosystem.fill_values_with_prompt(l1_network, self.dev); let observability = if self.dev { @@ -123,22 +120,22 @@ impl EcosystemInitArgs { }; EcosystemInitArgsFinal { - deploy_paymaster, deploy_erc20, ecosystem, forge_args: self.forge_args.clone(), dev: self.dev, observability, + ecosystem_only: self.ecosystem_only, } } } #[derive(Debug, Serialize, Deserialize)] pub struct EcosystemInitArgsFinal { - pub deploy_paymaster: bool, pub deploy_erc20: bool, pub ecosystem: EcosystemArgsFinal, pub forge_args: ForgeScriptArgs, pub dev: bool, pub observability: bool, + pub ecosystem_only: bool, } diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs index 2d31aad10336..0a5a0628022b 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs @@ -37,9 +37,8 @@ use crate::{ }, }, messages::{ - msg_ecosystem_initialized, msg_ecosystem_no_found_preexisting_contract, - msg_initializing_chain, MSG_CHAIN_NOT_INITIALIZED, - MSG_DEPLOYING_ECOSYSTEM_CONTRACTS_SPINNER, MSG_DEPLOYING_ERC20, + msg_chain_load_err, msg_ecosystem_initialized, msg_ecosystem_no_found_preexisting_contract, + msg_initializing_chain, MSG_DEPLOYING_ECOSYSTEM_CONTRACTS_SPINNER, MSG_DEPLOYING_ERC20, MSG_DEPLOYING_ERC20_SPINNER, MSG_ECOSYSTEM_CONTRACTS_PATH_INVALID_ERR, MSG_ECOSYSTEM_CONTRACTS_PATH_PROMPT, MSG_INITIALIZING_ECOSYSTEM, MSG_INTALLING_DEPS_SPINNER, @@ -57,11 +56,9 @@ pub async fn run(args: EcosystemInitArgs, shell: &Shell) -> anyhow::Result<()> { Err(_) => create_initial_deployments_config(shell, &ecosystem_config.config)?, }; - let mut genesis_args = args.genesis_args.clone(); - if args.dev { - genesis_args.use_default = true; - } - let mut final_ecosystem_args = args.fill_values_with_prompt(ecosystem_config.l1_network); + let mut final_ecosystem_args = args + .clone() + .fill_values_with_prompt(ecosystem_config.l1_network); logger::info(MSG_INITIALIZING_ECOSYSTEM); @@ -69,7 +66,7 @@ pub async fn run(args: EcosystemInitArgs, shell: &Shell) -> anyhow::Result<()> { setup_observability::run(shell)?; } - let contracts_config = init( + let contracts_config = init_ecosystem( &mut final_ecosystem_args, shell, &ecosystem_config, @@ -94,42 +91,18 @@ pub async fn run(args: EcosystemInitArgs, shell: &Shell) -> anyhow::Result<()> { .await?; } - // If the name of chain passed then we deploy exactly this chain otherwise deploy all chains - let list_of_chains = if let Some(name) = global_config().chain_name.clone() { - vec![name] - } else { - ecosystem_config.list_of_chains() - }; - - for chain_name in &list_of_chains { - logger::info(msg_initializing_chain(chain_name)); - let chain_config = ecosystem_config - .load_chain(Some(chain_name.clone())) - .context(MSG_CHAIN_NOT_INITIALIZED)?; - - let mut chain_init_args = chain::args::init::InitArgsFinal { - forge_args: final_ecosystem_args.forge_args.clone(), - genesis_args: genesis_args.clone().fill_values_with_prompt(&chain_config), - deploy_paymaster: final_ecosystem_args.deploy_paymaster, - l1_rpc_url: final_ecosystem_args.ecosystem.l1_rpc_url.clone(), - port_offset: PortOffset::from_chain_id(chain_config.id as u16).into(), - }; - - chain::init::init( - &mut chain_init_args, - shell, - &ecosystem_config, - &chain_config, - ) - .await?; + // Initialize chain(s) + let mut chains: Vec = vec![]; + if !final_ecosystem_args.ecosystem_only { + chains = init_chains(&args, &final_ecosystem_args, shell, &ecosystem_config).await?; } - logger::outro(msg_ecosystem_initialized(&list_of_chains.join(","))); + logger::outro(msg_ecosystem_initialized(&chains.join(","))); Ok(()) } -async fn init( +async fn init_ecosystem( init_args: &mut EcosystemInitArgsFinal, shell: &Shell, ecosystem_config: &EcosystemConfig, @@ -358,3 +331,54 @@ async fn deploy_ecosystem_inner( Ok(contracts_config) } + +async fn init_chains( + init_args: &EcosystemInitArgs, + final_init_args: &EcosystemInitArgsFinal, + shell: &Shell, + ecosystem_config: &EcosystemConfig, +) -> anyhow::Result> { + // If the name of chain passed then we deploy exactly this chain otherwise deploy all chains + let list_of_chains = if let Some(name) = global_config().chain_name.clone() { + vec![name] + } else { + ecosystem_config.list_of_chains() + }; + // Set default values for dev mode + let mut deploy_paymaster = init_args.deploy_paymaster; + let mut genesis_args = init_args.genesis_args.clone(); + if final_init_args.dev { + deploy_paymaster = Some(true); + genesis_args.use_default = true; + } + // Can't initialize multiple chains with the same DB + if list_of_chains.len() > 1 { + genesis_args.reset_db_names(); + } + // Initialize chains + for chain_name in &list_of_chains { + logger::info(msg_initializing_chain(chain_name)); + let chain_config = ecosystem_config + .load_chain(Some(chain_name.clone())) + .context(msg_chain_load_err(chain_name))?; + + let chain_init_args = chain::args::init::InitArgs { + forge_args: final_init_args.forge_args.clone(), + genesis_args: genesis_args.clone(), + deploy_paymaster, + l1_rpc_url: Some(final_init_args.ecosystem.l1_rpc_url.clone()), + port_offset: PortOffset::from_chain_id(chain_config.id as u16).into(), + configs_only: false, + }; + let mut final_chain_init_args = chain_init_args.fill_values_with_prompt(&chain_config); + + chain::init::init( + &mut final_chain_init_args, + shell, + ecosystem_config, + &chain_config, + ) + .await?; + } + Ok(list_of_chains) +} diff --git a/zk_toolbox/crates/zk_inception/src/messages.rs b/zk_toolbox/crates/zk_inception/src/messages.rs index 3bbac066dbb6..bec613af4eaf 100644 --- a/zk_toolbox/crates/zk_inception/src/messages.rs +++ b/zk_toolbox/crates/zk_inception/src/messages.rs @@ -71,6 +71,10 @@ pub(super) const MSG_CHAIN_NOT_FOUND_ERR: &str = "Chain not found"; pub(super) const MSG_INITIALIZING_ECOSYSTEM: &str = "Initializing ecosystem"; pub(super) const MSG_DEPLOYING_ERC20: &str = "Deploying ERC20 contracts"; pub(super) const MSG_CHAIN_INITIALIZED: &str = "Chain initialized successfully"; +pub(super) const MSG_CHAIN_CONFIGS_INITIALIZED: &str = "Chain configs were initialized"; +pub(super) const MSG_CHAIN_OWNERSHIP_TRANSFERRED: &str = + "Chain ownership was transferred successfully"; +pub(super) const MSG_CHAIN_REGISTERED: &str = "Chain registraion was successful"; pub(super) const MSG_DISTRIBUTING_ETH_SPINNER: &str = "Distributing eth..."; pub(super) const MSG_MINT_BASE_TOKEN_SPINNER: &str = "Minting base token to the governance addresses..."; @@ -99,7 +103,11 @@ pub(super) fn msg_initializing_chain(chain_name: &str) -> String { } pub(super) fn msg_ecosystem_initialized(chains: &str) -> String { - format!("Ecosystem initialized successfully with chains {chains}") + if chains.is_empty() { + "Ecosystem initialized successfully. You can initialize chain with `chain init`".to_string() + } else { + format!("Ecosystem initialized successfully with chains {chains}") + } } /// Ecosystem default related messages From 3b09f5b55880779973433f0df69ea1df7bee5e81 Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Thu, 26 Sep 2024 09:10:40 -0600 Subject: [PATCH 2/7] genesis split --- docs/guides/external-node/00_quick_start.md | 3 +- yarn.lock | 33 +---- .../src/commands/chain/args/genesis.rs | 34 +++++ .../src/commands/chain/args/init.rs | 5 - .../src/commands/chain/args/init_configs.rs | 66 +++++++++ .../src/commands/chain/args/mod.rs | 1 + .../chain/{genesis.rs => genesis/database.rs} | 128 ++++++------------ .../src/commands/chain/genesis/mod.rs | 15 ++ .../src/commands/chain/genesis/server.rs | 48 +++++++ .../src/commands/chain/genesis_all.rs | 65 +++++++++ .../zk_inception/src/commands/chain/init.rs | 105 +++----------- .../src/commands/chain/init_configs.rs | 92 +++++++++++++ .../zk_inception/src/commands/chain/mod.rs | 37 ++++- .../src/commands/ecosystem/init.rs | 1 - .../crates/zk_inception/src/messages.rs | 1 + 15 files changed, 424 insertions(+), 210 deletions(-) create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs rename zk_toolbox/crates/zk_inception/src/commands/chain/{genesis.rs => genesis/database.rs} (63%) create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs diff --git a/docs/guides/external-node/00_quick_start.md b/docs/guides/external-node/00_quick_start.md index 75d8ba891512..287a4d2d47c0 100644 --- a/docs/guides/external-node/00_quick_start.md +++ b/docs/guides/external-node/00_quick_start.md @@ -34,8 +34,7 @@ cd docker-compose-examples docker compose --file testnet-external-node-docker-compose.yml down --volumes ``` -You can see the status of the node (after recovery) in -[local grafana dashboard](http://localhost:3000/dashboards). +You can see the status of the node (after recovery) in [local grafana dashboard](http://localhost:3000/dashboards). Those commands start ZKsync node locally inside docker. diff --git a/yarn.lock b/yarn.lock index b70e64f148a1..531f49abc000 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6903,7 +6903,7 @@ jest-each@^29.7.0: jest-util "^29.7.0" pretty-format "^29.7.0" -jest-environment-node@^29.7.0: +jest-environment-node@^29.0.3, jest-environment-node@^29.7.0: version "29.7.0" resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-29.7.0.tgz#0b93e111dda8ec120bc8300e6d1fb9576e164376" integrity sha512-DOSwCRqXirTOyheM+4d5YZOrWcdu0LNZ87ewUoywbcb2XR4wKgqiG8vNeYwhjFMbEkfju7wx2GYH0P2gevGvFw== @@ -9816,7 +9816,7 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0": +"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -9833,15 +9833,6 @@ string-width@^2.1.0, string-width@^2.1.1: is-fullwidth-code-point "^2.0.0" strip-ansi "^4.0.0" -string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3: - version "4.2.3" - resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" - integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== - dependencies: - emoji-regex "^8.0.0" - is-fullwidth-code-point "^3.0.0" - strip-ansi "^6.0.1" - string-width@^5.0.1, string-width@^5.1.2: version "5.1.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794" @@ -9908,7 +9899,7 @@ string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1": +"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -9929,13 +9920,6 @@ strip-ansi@^5.1.0: dependencies: ansi-regex "^4.1.0" -strip-ansi@^6.0.0, strip-ansi@^6.0.1: - version "6.0.1" - resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" - integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== - dependencies: - ansi-regex "^5.0.1" - strip-ansi@^7.0.1: version "7.1.0" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" @@ -10781,16 +10765,7 @@ workerpool@6.2.1: resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.2.1.tgz#46fc150c17d826b86a008e5a4508656777e9c343" integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": - version "7.0.0" - resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" - integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== - dependencies: - ansi-styles "^4.0.0" - string-width "^4.1.0" - strip-ansi "^6.0.0" - -wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs index fa1ed28588ca..21796b3179de 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use clap::Parser; use common::{db::DatabaseConfig, Prompt}; use config::ChainConfig; @@ -78,6 +79,39 @@ impl GenesisArgs { } } + pub fn fill_values_with_secrets( + mut self, + chain_config: &ChainConfig, + ) -> anyhow::Result { + let secrets = chain_config.get_secrets_config()?; + let database = secrets + .database + .context("Database secrets must be present")?; + + let (server_db_url, server_db_name) = if let Some(db_full_url) = database.server_url { + let db_config = DatabaseConfig::from_url(db_full_url.expose_url()) + .context("Invalid server database URL")?; + (Some(db_config.url), Some(db_config.name)) + } else { + (None, None) + }; + + let (prover_db_url, prover_db_name) = if let Some(db_full_url) = database.prover_url { + let db_config = DatabaseConfig::from_url(db_full_url.expose_url()) + .context("Invalid prover database URL")?; + (Some(db_config.url), Some(db_config.name)) + } else { + (None, None) + }; + + self.server_db_url = self.server_db_url.or(server_db_url); + self.server_db_name = self.server_db_name.or(server_db_name); + self.prover_db_url = self.prover_db_url.or(prover_db_url); + self.prover_db_name = self.prover_db_name.or(prover_db_name); + + Ok(self.fill_values_with_prompt(chain_config)) + } + pub fn reset_db_names(&mut self) { self.prover_db_name = None; self.prover_db_url = None; diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs index ecfbc69986fb..9dd6c490bd78 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs @@ -57,9 +57,6 @@ pub struct InitArgs { pub l1_rpc_url: Option, #[clap(long, help = MSG_PORT_OFFSET_HELP)] pub port_offset: Option, - /// Only create chain configs - #[clap(long, default_value_t = false)] - pub configs_only: bool, } impl InitArgs { @@ -93,7 +90,6 @@ impl InitArgs { .port_offset .unwrap_or(PortOffset::from_chain_id(config.id as u16)) .into(), - configs_only: self.configs_only, } } } @@ -105,5 +101,4 @@ pub struct InitArgsFinal { pub deploy_paymaster: bool, pub l1_rpc_url: String, pub port_offset: u16, - pub configs_only: bool, } diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs new file mode 100644 index 000000000000..1e9e2ee7d3b9 --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs @@ -0,0 +1,66 @@ +use clap::Parser; +use common::Prompt; +use config::ChainConfig; +use serde::{Deserialize, Serialize}; +use types::L1Network; +use url::Url; + +use super::{genesis::GenesisArgsFinal, init::InitArgsFinal}; +use crate::{ + commands::chain::args::{genesis::GenesisArgs, init::PortOffset}, + defaults::LOCAL_RPC_URL, + messages::{ + MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, MSG_L1_RPC_URL_INVALID_ERR, + MSG_L1_RPC_URL_PROMPT, + }, +}; + +#[derive(Debug, Clone, Serialize, Deserialize, Parser)] +pub struct InitConfigsArgs { + #[clap(flatten, next_help_heading = MSG_GENESIS_ARGS_HELP)] + #[serde(flatten)] + pub genesis_args: GenesisArgs, + #[clap(long, help = MSG_L1_RPC_URL_HELP)] + pub l1_rpc_url: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct InitConfigsArgsFinal { + pub genesis_args: GenesisArgsFinal, + pub l1_rpc_url: String, + pub port_offset: u16, +} + +impl InitConfigsArgs { + pub fn fill_values_with_prompt(self, config: &ChainConfig) -> InitConfigsArgsFinal { + let l1_rpc_url = self.l1_rpc_url.unwrap_or_else(|| { + let mut prompt = Prompt::new(MSG_L1_RPC_URL_PROMPT); + if config.l1_network == L1Network::Localhost { + prompt = prompt.default(LOCAL_RPC_URL); + } + prompt + .validate_with(|val: &String| -> Result<(), String> { + Url::parse(val) + .map(|_| ()) + .map_err(|_| MSG_L1_RPC_URL_INVALID_ERR.to_string()) + }) + .ask() + }); + + InitConfigsArgsFinal { + genesis_args: self.genesis_args.fill_values_with_prompt(config), + l1_rpc_url, + port_offset: PortOffset::from_chain_id(config.id as u16).into(), + } + } +} + +impl InitConfigsArgsFinal { + pub fn from_chain_init_args(init_args: &InitArgsFinal) -> InitConfigsArgsFinal { + InitConfigsArgsFinal { + genesis_args: init_args.genesis_args.clone(), + l1_rpc_url: init_args.l1_rpc_url.clone(), + port_offset: init_args.port_offset.clone(), + } + } +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs index f2a5f6b8be1f..7deb24617874 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs @@ -2,3 +2,4 @@ pub mod build_transactions; pub mod create; pub mod genesis; pub mod init; +pub mod init_configs; diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs similarity index 63% rename from zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs rename to zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs index 187af41489d9..171d0915015e 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs @@ -5,33 +5,26 @@ use common::{ config::global_config, db::{drop_db_if_exists, init_db, migrate_db, DatabaseConfig}, logger, - server::{Server, ServerMode}, - spinner::Spinner, }; use config::{ override_config, set_databases, set_file_artifacts, set_rocks_db_config, - traits::{FileConfigWithDefaultName, SaveConfigWithBasePath}, - ChainConfig, ContractsConfig, EcosystemConfig, FileArtifacts, GeneralConfig, GenesisConfig, - SecretsConfig, WalletsConfig, + traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig, FileArtifacts, }; use types::ProverMode; use xshell::Shell; use zksync_basic_types::commitment::L1BatchCommitmentMode; -use super::args::genesis::GenesisArgsFinal; use crate::{ - commands::chain::args::genesis::GenesisArgs, + commands::chain::args::genesis::{GenesisArgs, GenesisArgsFinal}, consts::{ PATH_TO_ONLY_REAL_PROOFS_OVERRIDE_CONFIG, PATH_TO_VALIDIUM_OVERRIDE_CONFIG, PROVER_MIGRATIONS, SERVER_MIGRATIONS, }, messages::{ MSG_CHAIN_NOT_INITIALIZED, MSG_FAILED_TO_DROP_PROVER_DATABASE_ERR, - MSG_FAILED_TO_DROP_SERVER_DATABASE_ERR, MSG_FAILED_TO_RUN_SERVER_ERR, - MSG_GENESIS_COMPLETED, MSG_INITIALIZING_DATABASES_SPINNER, + MSG_FAILED_TO_DROP_SERVER_DATABASE_ERR, MSG_GENESIS_DATABASES_INITIALIZED, MSG_INITIALIZING_PROVER_DATABASE, MSG_INITIALIZING_SERVER_DATABASE, - MSG_RECREATE_ROCKS_DB_ERRROR, MSG_SELECTED_CONFIG, MSG_STARTING_GENESIS, - MSG_STARTING_GENESIS_SPINNER, + MSG_RECREATE_ROCKS_DB_ERRROR, }, utils::rocks_db::{recreate_rocksdb_dirs, RocksDBDirOption}, }; @@ -42,79 +35,26 @@ pub async fn run(args: GenesisArgs, shell: &Shell) -> anyhow::Result<()> { let chain_config = ecosystem_config .load_chain(chain_name) .context(MSG_CHAIN_NOT_INITIALIZED)?; - let args = args.fill_values_with_prompt(&chain_config); - genesis(args, shell, &chain_config).await?; - logger::outro(MSG_GENESIS_COMPLETED); - - Ok(()) -} - -pub async fn genesis( - args: GenesisArgsFinal, - shell: &Shell, - config: &ChainConfig, -) -> anyhow::Result<()> { - shell.create_dir(&config.rocks_db_path)?; - - let link_to_code = config.link_to_code.clone(); - let rocks_db = recreate_rocksdb_dirs(shell, &config.rocks_db_path, RocksDBDirOption::Main) - .context(MSG_RECREATE_ROCKS_DB_ERRROR)?; - let mut general = config.get_general_config()?; - let file_artifacts = FileArtifacts::new(config.artifacts.clone()); - set_rocks_db_config(&mut general, rocks_db)?; - set_file_artifacts(&mut general, file_artifacts); - general.save_with_base_path(shell, &config.configs)?; - - if config.prover_version != ProverMode::NoProofs { - override_config( - shell, - link_to_code.join(PATH_TO_ONLY_REAL_PROOFS_OVERRIDE_CONFIG), - config, - )?; - } - - if config.l1_batch_commit_data_generator_mode == L1BatchCommitmentMode::Validium { - override_config( - shell, - link_to_code.join(PATH_TO_VALIDIUM_OVERRIDE_CONFIG), - config, - )?; - } - - let mut secrets = config.get_secrets_config()?; + let mut secrets = chain_config.get_secrets_config()?; + let args = args.fill_values_with_secrets(&chain_config)?; set_databases(&mut secrets, &args.server_db, &args.prover_db)?; - secrets.save_with_base_path(shell, &config.configs)?; - - logger::note( - MSG_SELECTED_CONFIG, - logger::object_to_string(serde_json::json!({ - "chain_config": config, - "server_db_config": args.server_db, - "prover_db_config": args.prover_db, - })), - ); - logger::info(MSG_STARTING_GENESIS); + secrets.save_with_base_path(shell, &chain_config.configs)?; - let spinner = Spinner::new(MSG_INITIALIZING_DATABASES_SPINNER); initialize_databases( shell, &args.server_db, &args.prover_db, - config.link_to_code.clone(), + chain_config.link_to_code.clone(), args.dont_drop, ) .await?; - spinner.finish(); - - let spinner = Spinner::new(MSG_STARTING_GENESIS_SPINNER); - run_server_genesis(config, shell)?; - spinner.finish(); + logger::outro(MSG_GENESIS_DATABASES_INITIALIZED); Ok(()) } -async fn initialize_databases( +pub async fn initialize_databases( shell: &Shell, server_db_config: &DatabaseConfig, prover_db_config: &DatabaseConfig, @@ -159,18 +99,40 @@ async fn initialize_databases( Ok(()) } -fn run_server_genesis(chain_config: &ChainConfig, shell: &Shell) -> anyhow::Result<()> { - let server = Server::new(None, chain_config.link_to_code.clone(), false); - server - .run( +pub fn update_configs( + args: GenesisArgsFinal, + shell: &Shell, + config: &ChainConfig, +) -> anyhow::Result<()> { + shell.create_dir(&config.rocks_db_path)?; + + // Update general config + let mut general = config.get_general_config()?; + let rocks_db = recreate_rocksdb_dirs(shell, &config.rocks_db_path, RocksDBDirOption::Main) + .context(MSG_RECREATE_ROCKS_DB_ERRROR)?; + let file_artifacts = FileArtifacts::new(config.artifacts.clone()); + set_rocks_db_config(&mut general, rocks_db)?; + set_file_artifacts(&mut general, file_artifacts); + general.save_with_base_path(shell, &config.configs)?; + + let mut secrets = config.get_secrets_config()?; + set_databases(&mut secrets, &args.server_db, &args.prover_db)?; + secrets.save_with_base_path(shell, &config.configs)?; + + let link_to_code = config.link_to_code.clone(); + if config.prover_version != ProverMode::NoProofs { + override_config( + shell, + link_to_code.join(PATH_TO_ONLY_REAL_PROOFS_OVERRIDE_CONFIG), + config, + )?; + } + if config.l1_batch_commit_data_generator_mode == L1BatchCommitmentMode::Validium { + override_config( shell, - ServerMode::Genesis, - GenesisConfig::get_path_with_base_path(&chain_config.configs), - WalletsConfig::get_path_with_base_path(&chain_config.configs), - GeneralConfig::get_path_with_base_path(&chain_config.configs), - SecretsConfig::get_path_with_base_path(&chain_config.configs), - ContractsConfig::get_path_with_base_path(&chain_config.configs), - vec![], - ) - .context(MSG_FAILED_TO_RUN_SERVER_ERR) + link_to_code.join(PATH_TO_VALIDIUM_OVERRIDE_CONFIG), + config, + )?; + } + Ok(()) } diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs new file mode 100644 index 000000000000..8f313efff1ea --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs @@ -0,0 +1,15 @@ +use clap::Subcommand; + +use crate::commands::chain::args::genesis::GenesisArgs; + +pub mod database; +pub mod server; + +#[derive(Subcommand, Debug, Clone)] +pub enum GenesisSubcommands { + /// Initialize database + #[command(alias = "db")] + InitDatabase(GenesisArgs), + /// Runs server genesis + Server, +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs new file mode 100644 index 000000000000..bf682f139daf --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs @@ -0,0 +1,48 @@ +use anyhow::Context; +use common::{ + config::global_config, + logger, + server::{Server, ServerMode}, + spinner::Spinner, +}; +use config::{ + traits::FileConfigWithDefaultName, ChainConfig, ContractsConfig, EcosystemConfig, + GeneralConfig, GenesisConfig, SecretsConfig, WalletsConfig, +}; +use xshell::Shell; + +use crate::messages::{ + MSG_CHAIN_NOT_INITIALIZED, MSG_FAILED_TO_RUN_SERVER_ERR, MSG_GENESIS_COMPLETED, + MSG_STARTING_GENESIS_SPINNER, +}; + +pub async fn run(shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_INITIALIZED)?; + + let spinner = Spinner::new(MSG_STARTING_GENESIS_SPINNER); + run_server_genesis(&chain_config, shell)?; + spinner.finish(); + logger::outro(MSG_GENESIS_COMPLETED); + + Ok(()) +} + +pub fn run_server_genesis(chain_config: &ChainConfig, shell: &Shell) -> anyhow::Result<()> { + let server = Server::new(None, chain_config.link_to_code.clone(), false); + server + .run( + shell, + ServerMode::Genesis, + GenesisConfig::get_path_with_base_path(&chain_config.configs), + WalletsConfig::get_path_with_base_path(&chain_config.configs), + GeneralConfig::get_path_with_base_path(&chain_config.configs), + SecretsConfig::get_path_with_base_path(&chain_config.configs), + ContractsConfig::get_path_with_base_path(&chain_config.configs), + vec![], + ) + .context(MSG_FAILED_TO_RUN_SERVER_ERR) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs new file mode 100644 index 000000000000..0678941ce097 --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs @@ -0,0 +1,65 @@ +use anyhow::Context; +use common::{config::global_config, logger, spinner::Spinner}; +use config::{ChainConfig, EcosystemConfig}; +use xshell::Shell; + +use super::{args::genesis::GenesisArgsFinal, genesis}; +use crate::{ + commands::chain::{ + args::genesis::GenesisArgs, + genesis::{database::initialize_databases, server::run_server_genesis}, + }, + messages::{ + MSG_CHAIN_NOT_INITIALIZED, MSG_GENESIS_COMPLETED, MSG_INITIALIZING_DATABASES_SPINNER, + MSG_SELECTED_CONFIG, MSG_STARTING_GENESIS, MSG_STARTING_GENESIS_SPINNER, + }, +}; + +pub async fn run(args: GenesisArgs, shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_INITIALIZED)?; + let args = args.fill_values_with_prompt(&chain_config); + + genesis(args, shell, &chain_config).await?; + logger::outro(MSG_GENESIS_COMPLETED); + + Ok(()) +} + +pub async fn genesis( + args: GenesisArgsFinal, + shell: &Shell, + config: &ChainConfig, +) -> anyhow::Result<()> { + genesis::database::update_configs(args.clone(), shell, config)?; + + logger::note( + MSG_SELECTED_CONFIG, + logger::object_to_string(serde_json::json!({ + "chain_config": config, + "server_db_config": args.server_db, + "prover_db_config": args.prover_db, + })), + ); + logger::info(MSG_STARTING_GENESIS); + + let spinner = Spinner::new(MSG_INITIALIZING_DATABASES_SPINNER); + initialize_databases( + shell, + &args.server_db, + &args.prover_db, + config.link_to_code.clone(), + args.dont_drop, + ) + .await?; + spinner.finish(); + + let spinner = Spinner::new(MSG_STARTING_GENESIS_SPINNER); + run_server_genesis(config, shell)?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs index 7b2a97051b60..2be030ee79f8 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs @@ -1,36 +1,30 @@ -use anyhow::{bail, Context}; +use anyhow::Context; use common::{config::global_config, git, logger, spinner::Spinner}; -use config::{ - copy_configs, ports_config, set_l1_rpc_url, traits::SaveConfigWithBasePath, - update_from_chain_config, update_ports, ChainConfig, ContractsConfig, EcosystemConfig, - GeneralConfig, -}; -use ethers::types::Address; +use config::{traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig}; use types::BaseToken; use xshell::Shell; use super::common::{distribute_eth, mint_base_token}; use crate::{ accept_ownership::accept_admin, - commands::{ - chain::{ - args::init::{InitArgs, InitArgsFinal}, - deploy_l2_contracts, deploy_paymaster, - genesis::genesis, - register_chain::register_chain, - set_token_multiplier_setter::set_token_multiplier_setter, - setup_legacy_bridge::setup_legacy_bridge, + commands::chain::{ + args::{ + init::{InitArgs, InitArgsFinal}, + init_configs::InitConfigsArgsFinal, }, - portal::update_portal_config, + deploy_l2_contracts, deploy_paymaster, + genesis_all::genesis, + init_configs::init_configs, + register_chain::register_chain, + set_token_multiplier_setter::set_token_multiplier_setter, + setup_legacy_bridge::setup_legacy_bridge, }, messages::{ - msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_CONFIGS_INITIALIZED, - MSG_CHAIN_INITIALIZED, MSG_CHAIN_NOT_FOUND_ERR, MSG_DEPLOYING_PAYMASTER, - MSG_GENESIS_DATABASE_ERR, MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR, MSG_PORTS_CONFIG_ERR, + msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_INITIALIZED, + MSG_CHAIN_NOT_FOUND_ERR, MSG_DEPLOYING_PAYMASTER, MSG_GENESIS_DATABASE_ERR, MSG_REGISTERING_CHAIN_SPINNER, MSG_SELECTED_CONFIG, MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER, MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND, }, - utils::consensus::{generate_consensus_keys, get_consensus_config, get_consensus_secrets}, }; pub(crate) async fn run(args: InitArgs, shell: &Shell) -> anyhow::Result<()> { @@ -47,25 +41,22 @@ pub(crate) async fn run(args: InitArgs, shell: &Shell) -> anyhow::Result<()> { init(&mut args, shell, &config, &chain_config).await?; - if args.configs_only { - logger::success(MSG_CHAIN_CONFIGS_INITIALIZED); - } else { - logger::success(MSG_CHAIN_INITIALIZED); - } + logger::success(MSG_CHAIN_INITIALIZED); Ok(()) } pub async fn init( - init_args: &mut InitArgsFinal, + init_args: &InitArgsFinal, shell: &Shell, ecosystem_config: &EcosystemConfig, chain_config: &ChainConfig, ) -> anyhow::Result<()> { - let mut contracts_config = init_configs(init_args, shell, ecosystem_config, chain_config)?; - if init_args.configs_only { - return Ok(()); - } + // Initialize configs + let init_configs_args = InitConfigsArgsFinal::from_chain_init_args(&init_args); + let mut contracts_config = + init_configs(&init_configs_args, shell, ecosystem_config, chain_config).await?; + // Fund some wallet addresses with ETH or base token (only for Localhost) distribute_eth(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; mint_base_token(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; @@ -163,59 +154,5 @@ pub async fn init( .await .context(MSG_GENESIS_DATABASE_ERR)?; - update_portal_config(shell, chain_config) - .await - .context(MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR)?; - - Ok(()) -} - -fn init_configs( - init_args: &mut InitArgsFinal, - shell: &Shell, - ecosystem_config: &EcosystemConfig, - chain_config: &ChainConfig, -) -> anyhow::Result { - copy_configs(shell, &ecosystem_config.link_to_code, &chain_config.configs)?; - - let mut general_config = chain_config.get_general_config()?; - apply_port_offset(init_args.port_offset, &mut general_config)?; - let ports = ports_config(&general_config).context(MSG_PORTS_CONFIG_ERR)?; - - let consensus_keys = generate_consensus_keys(); - let consensus_config = - get_consensus_config(chain_config, ports, Some(consensus_keys.clone()), None)?; - general_config.consensus_config = Some(consensus_config); - general_config.save_with_base_path(shell, &chain_config.configs)?; - - let mut genesis_config = chain_config.get_genesis_config()?; - update_from_chain_config(&mut genesis_config, chain_config); - genesis_config.save_with_base_path(shell, &chain_config.configs)?; - - // Copy ecosystem contracts - let mut contracts_config = ecosystem_config.get_contracts_config()?; - contracts_config.l1.diamond_proxy_addr = Address::zero(); - contracts_config.l1.governance_addr = Address::zero(); - contracts_config.l1.chain_admin_addr = Address::zero(); - contracts_config.l1.base_token_addr = chain_config.base_token.address; - contracts_config.save_with_base_path(shell, &chain_config.configs)?; - - let mut secrets = chain_config.get_secrets_config()?; - set_l1_rpc_url(&mut secrets, init_args.l1_rpc_url.clone())?; - secrets.consensus = Some(get_consensus_secrets(&consensus_keys)); - secrets.save_with_base_path(shell, &chain_config.configs)?; - - Ok(contracts_config) -} - -fn apply_port_offset(port_offset: u16, general_config: &mut GeneralConfig) -> anyhow::Result<()> { - let Some(mut ports_config) = ports_config(general_config) else { - bail!("Missing ports config"); - }; - - ports_config.apply_offset(port_offset); - - update_ports(general_config, &ports_config)?; - Ok(()) } diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs new file mode 100644 index 000000000000..f287895a1fde --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs @@ -0,0 +1,92 @@ +use anyhow::{bail, Context}; +use common::{config::global_config, logger}; +use config::{ + copy_configs, ports_config, set_l1_rpc_url, traits::SaveConfigWithBasePath, + update_from_chain_config, update_ports, ChainConfig, ContractsConfig, EcosystemConfig, + GeneralConfig, +}; +use ethers::types::Address; +use xshell::Shell; + +use super::genesis; +use crate::{ + commands::{ + chain::args::init_configs::{InitConfigsArgs, InitConfigsArgsFinal}, + portal::update_portal_config, + }, + messages::{ + MSG_CHAIN_CONFIGS_INITIALIZED, MSG_CHAIN_NOT_FOUND_ERR, + MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR, MSG_PORTS_CONFIG_ERR, + }, + utils::consensus::{generate_consensus_keys, get_consensus_config, get_consensus_secrets}, +}; + +pub async fn run(args: InitConfigsArgs, shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_FOUND_ERR)?; + let args = args.fill_values_with_prompt(&chain_config); + + init_configs(&args, shell, &ecosystem_config, &chain_config).await?; + logger::outro(MSG_CHAIN_CONFIGS_INITIALIZED); + + Ok(()) +} + +pub async fn init_configs( + init_args: &InitConfigsArgsFinal, + shell: &Shell, + ecosystem_config: &EcosystemConfig, + chain_config: &ChainConfig, +) -> anyhow::Result { + copy_configs(shell, &ecosystem_config.link_to_code, &chain_config.configs)?; + + // Initialize general config + let mut general_config = chain_config.get_general_config()?; + apply_port_offset(init_args.port_offset, &mut general_config)?; + let ports = ports_config(&general_config).context(MSG_PORTS_CONFIG_ERR)?; + + let consensus_keys = generate_consensus_keys(); + let consensus_config = + get_consensus_config(chain_config, ports, Some(consensus_keys.clone()), None)?; + general_config.consensus_config = Some(consensus_config); + + let mut genesis_config = chain_config.get_genesis_config()?; + update_from_chain_config(&mut genesis_config, chain_config); + genesis_config.save_with_base_path(shell, &chain_config.configs)?; + + // Copy ecosystem contracts + let mut contracts_config = ecosystem_config.get_contracts_config()?; + contracts_config.l1.diamond_proxy_addr = Address::zero(); + contracts_config.l1.governance_addr = Address::zero(); + contracts_config.l1.chain_admin_addr = Address::zero(); + contracts_config.l1.base_token_addr = chain_config.base_token.address; + contracts_config.save_with_base_path(shell, &chain_config.configs)?; + + let mut secrets = chain_config.get_secrets_config()?; + set_l1_rpc_url(&mut secrets, init_args.l1_rpc_url.clone())?; + secrets.consensus = Some(get_consensus_secrets(&consensus_keys)); + secrets.save_with_base_path(shell, &chain_config.configs)?; + + genesis::database::update_configs(init_args.genesis_args.clone(), shell, &chain_config)?; + + update_portal_config(shell, chain_config) + .await + .context(MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR)?; + + Ok(contracts_config) +} + +fn apply_port_offset(port_offset: u16, general_config: &mut GeneralConfig) -> anyhow::Result<()> { + let Some(mut ports_config) = ports_config(general_config) else { + bail!("Missing ports config"); + }; + + ports_config.apply_offset(port_offset); + + update_ports(general_config, &ports_config)?; + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs index f2d8b3d57578..16aa41b5f6f1 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs @@ -1,13 +1,17 @@ use ::common::forge::ForgeScriptArgs; use args::build_transactions::BuildTransactionsArgs; pub(crate) use args::create::ChainCreateArgsFinal; -use clap::Subcommand; +use clap::{command, Parser, Subcommand}; pub(crate) use create::create_chain_inner; use xshell::Shell; use crate::commands::chain::{ - args::{create::ChainCreateArgs, genesis::GenesisArgs, init::InitArgs}, + args::{ + create::ChainCreateArgs, genesis::GenesisArgs, init::InitArgs, + init_configs::InitConfigsArgs, + }, deploy_l2_contracts::Deploy2ContractsOption, + genesis::GenesisSubcommands, }; mod accept_chain_ownership; @@ -18,11 +22,22 @@ mod create; pub mod deploy_l2_contracts; pub mod deploy_paymaster; pub mod genesis; +pub mod genesis_all; pub(crate) mod init; +mod init_configs; pub mod register_chain; mod set_token_multiplier_setter; mod setup_legacy_bridge; +#[derive(Parser, Debug)] +#[command()] +pub struct GenesisCommand { + #[command(subcommand)] + command: Option, + #[clap(flatten)] + args: GenesisArgs, +} + #[derive(Subcommand, Debug)] pub enum ChainCommands { /// Create a new chain, setting the necessary configurations for later initialization @@ -32,7 +47,7 @@ pub enum ChainCommands { /// Initialize chain, deploying necessary contracts and performing on-chain operations Init(InitArgs), /// Run server genesis - Genesis(GenesisArgs), + Genesis(GenesisCommand), /// Register a new chain on L1 (executed by L1 governor). /// This command deploys and configures Governance, ChainAdmin, and DiamondProxy contracts, /// registers chain with BridgeHub and sets pending admin for DiamondProxy. @@ -54,12 +69,15 @@ pub enum ChainCommands { #[command(alias = "consensus")] DeployConsensusRegistry(ForgeScriptArgs), /// Deploy Default Upgrader - Upgrader(ForgeScriptArgs), + #[command(alias = "upgrader")] + DeployUpgrader(ForgeScriptArgs), /// Deploy paymaster smart contract #[command(alias = "paymaster")] DeployPaymaster(ForgeScriptArgs), /// Update Token Multiplier Setter address on L1 UpdateTokenMultiplierSetter(ForgeScriptArgs), + /// Initializes chain configs + InitConfigs(InitConfigsArgs), } pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<()> { @@ -67,7 +85,13 @@ pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<() ChainCommands::Create(args) => create::run(args, shell), ChainCommands::Init(args) => init::run(args, shell).await, ChainCommands::BuildTransactions(args) => build_transactions::run(args, shell).await, - ChainCommands::Genesis(args) => genesis::run(args, shell).await, + ChainCommands::Genesis(args) => match args.command { + Some(GenesisSubcommands::InitDatabase(args)) => { + genesis::database::run(args, shell).await + } + Some(GenesisSubcommands::Server) => genesis::server::run(shell).await, + None => genesis_all::run(args.args, shell).await, + }, ChainCommands::RegisterChain(args) => register_chain::run(args, shell).await, ChainCommands::DeployL2Contracts(args) => { deploy_l2_contracts::run(args, shell, Deploy2ContractsOption::All).await @@ -76,7 +100,7 @@ pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<() ChainCommands::DeployConsensusRegistry(args) => { deploy_l2_contracts::run(args, shell, Deploy2ContractsOption::ConsensusRegistry).await } - ChainCommands::Upgrader(args) => { + ChainCommands::DeployUpgrader(args) => { deploy_l2_contracts::run(args, shell, Deploy2ContractsOption::Upgrader).await } ChainCommands::InitializeBridges(args) => { @@ -86,5 +110,6 @@ pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<() ChainCommands::UpdateTokenMultiplierSetter(args) => { set_token_multiplier_setter::run(args, shell).await } + ChainCommands::InitConfigs(args) => init_configs::run(args, shell).await, } } diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs index 0a5a0628022b..0b1d7237fa8b 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs @@ -368,7 +368,6 @@ async fn init_chains( deploy_paymaster, l1_rpc_url: Some(final_init_args.ecosystem.l1_rpc_url.clone()), port_offset: PortOffset::from_chain_id(chain_config.id as u16).into(), - configs_only: false, }; let mut final_chain_init_args = chain_init_args.fill_values_with_prompt(&chain_config); diff --git a/zk_toolbox/crates/zk_inception/src/messages.rs b/zk_toolbox/crates/zk_inception/src/messages.rs index bec613af4eaf..7582c8a18abc 100644 --- a/zk_toolbox/crates/zk_inception/src/messages.rs +++ b/zk_toolbox/crates/zk_inception/src/messages.rs @@ -194,6 +194,7 @@ pub(super) const MSG_INITIALIZING_SERVER_DATABASE: &str = "Initializing server d pub(super) const MSG_FAILED_TO_DROP_SERVER_DATABASE_ERR: &str = "Failed to drop server database"; pub(super) const MSG_INITIALIZING_PROVER_DATABASE: &str = "Initializing prover database"; pub(super) const MSG_FAILED_TO_DROP_PROVER_DATABASE_ERR: &str = "Failed to drop prover database"; +pub(super) const MSG_GENESIS_DATABASES_INITIALIZED: &str = "Databases initialized successfully"; /// Chain update related messages pub(super) const MSG_WALLETS_CONFIG_MUST_BE_PRESENT: &str = "Wallets configuration must be present"; From 79c8cb81e5530a7a7d08298ef35a62692ae031fa Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Fri, 27 Sep 2024 11:55:14 -0600 Subject: [PATCH 3/7] init-configs to init configs --- .../args/{init_configs.rs => init/configs.rs} | 9 +++-- .../commands/chain/args/{ => init}/init.rs | 3 +- .../src/commands/chain/args/init/mod.rs | 4 +++ .../src/commands/chain/args/mod.rs | 2 +- .../src/commands/chain/genesis/database.rs | 9 ++--- .../{genesis_all.rs => genesis/genesis.rs} | 5 ++- .../src/commands/chain/genesis/mod.rs | 26 ++++++++++++-- .../{init_configs.rs => init/configs.rs} | 16 ++++++--- .../src/commands/chain/{ => init}/init.rs | 11 +++--- .../src/commands/chain/init/mod.rs | 32 +++++++++++++++++ .../zk_inception/src/commands/chain/mod.rs | 36 ++++--------------- 11 files changed, 95 insertions(+), 58 deletions(-) rename zk_toolbox/crates/zk_inception/src/commands/chain/args/{init_configs.rs => init/configs.rs} (86%) rename zk_toolbox/crates/zk_inception/src/commands/chain/args/{ => init}/init.rs (97%) create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs rename zk_toolbox/crates/zk_inception/src/commands/chain/{genesis_all.rs => genesis/genesis.rs} (91%) rename zk_toolbox/crates/zk_inception/src/commands/chain/{init_configs.rs => init/configs.rs} (88%) rename zk_toolbox/crates/zk_inception/src/commands/chain/{ => init}/init.rs (95%) create mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/configs.rs similarity index 86% rename from zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs rename to zk_toolbox/crates/zk_inception/src/commands/chain/args/init/configs.rs index 1e9e2ee7d3b9..167728dd9531 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init_configs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/configs.rs @@ -5,9 +5,11 @@ use serde::{Deserialize, Serialize}; use types::L1Network; use url::Url; -use super::{genesis::GenesisArgsFinal, init::InitArgsFinal}; use crate::{ - commands::chain::args::{genesis::GenesisArgs, init::PortOffset}, + commands::chain::args::{ + genesis::{GenesisArgs, GenesisArgsFinal}, + init::InitArgsFinal, + }, defaults::LOCAL_RPC_URL, messages::{ MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, MSG_L1_RPC_URL_INVALID_ERR, @@ -28,7 +30,6 @@ pub struct InitConfigsArgs { pub struct InitConfigsArgsFinal { pub genesis_args: GenesisArgsFinal, pub l1_rpc_url: String, - pub port_offset: u16, } impl InitConfigsArgs { @@ -50,7 +51,6 @@ impl InitConfigsArgs { InitConfigsArgsFinal { genesis_args: self.genesis_args.fill_values_with_prompt(config), l1_rpc_url, - port_offset: PortOffset::from_chain_id(config.id as u16).into(), } } } @@ -60,7 +60,6 @@ impl InitConfigsArgsFinal { InitConfigsArgsFinal { genesis_args: init_args.genesis_args.clone(), l1_rpc_url: init_args.l1_rpc_url.clone(), - port_offset: init_args.port_offset.clone(), } } } diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs similarity index 97% rename from zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs rename to zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs index 9dd6c490bd78..6addd7d86dd1 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs @@ -7,9 +7,8 @@ use serde::{Deserialize, Serialize}; use types::L1Network; use url::Url; -use super::genesis::GenesisArgsFinal; use crate::{ - commands::chain::args::genesis::GenesisArgs, + commands::chain::args::genesis::{GenesisArgs, GenesisArgsFinal}, defaults::LOCAL_RPC_URL, messages::{ MSG_DEPLOY_PAYMASTER_PROMPT, MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs new file mode 100644 index 000000000000..7eb8e8ab76a0 --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs @@ -0,0 +1,4 @@ +pub mod configs; +pub mod init; + +pub use init::*; diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs index 7deb24617874..b42f9835dba3 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs @@ -2,4 +2,4 @@ pub mod build_transactions; pub mod create; pub mod genesis; pub mod init; -pub mod init_configs; +// pub mod init_all; diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs index 171d0915015e..0bf257dcd7a9 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/database.rs @@ -106,6 +106,11 @@ pub fn update_configs( ) -> anyhow::Result<()> { shell.create_dir(&config.rocks_db_path)?; + // Update secrets configs + let mut secrets = config.get_secrets_config()?; + set_databases(&mut secrets, &args.server_db, &args.prover_db)?; + secrets.save_with_base_path(shell, &config.configs)?; + // Update general config let mut general = config.get_general_config()?; let rocks_db = recreate_rocksdb_dirs(shell, &config.rocks_db_path, RocksDBDirOption::Main) @@ -115,10 +120,6 @@ pub fn update_configs( set_file_artifacts(&mut general, file_artifacts); general.save_with_base_path(shell, &config.configs)?; - let mut secrets = config.get_secrets_config()?; - set_databases(&mut secrets, &args.server_db, &args.prover_db)?; - secrets.save_with_base_path(shell, &config.configs)?; - let link_to_code = config.link_to_code.clone(); if config.prover_version != ProverMode::NoProofs { override_config( diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs similarity index 91% rename from zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs rename to zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs index 0678941ce097..696674110972 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis_all.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs @@ -3,11 +3,10 @@ use common::{config::global_config, logger, spinner::Spinner}; use config::{ChainConfig, EcosystemConfig}; use xshell::Shell; -use super::{args::genesis::GenesisArgsFinal, genesis}; use crate::{ commands::chain::{ - args::genesis::GenesisArgs, - genesis::{database::initialize_databases, server::run_server_genesis}, + args::genesis::{GenesisArgs, GenesisArgsFinal}, + genesis::{self, database::initialize_databases, server::run_server_genesis}, }, messages::{ MSG_CHAIN_NOT_INITIALIZED, MSG_GENESIS_COMPLETED, MSG_INITIALIZING_DATABASES_SPINNER, diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs index 8f313efff1ea..5095f011f058 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs @@ -1,15 +1,37 @@ -use clap::Subcommand; +use clap::{command, Parser, Subcommand}; +use xshell::Shell; use crate::commands::chain::args::genesis::GenesisArgs; +// Standard genesis command +mod genesis; +pub use genesis::*; +// Genesis subcommands pub mod database; pub mod server; #[derive(Subcommand, Debug, Clone)] pub enum GenesisSubcommands { - /// Initialize database + /// Initialize databases #[command(alias = "db")] InitDatabase(GenesisArgs), /// Runs server genesis Server, } + +#[derive(Parser, Debug)] +#[command()] +pub struct GenesisCommand { + #[command(subcommand)] + command: Option, + #[clap(flatten)] + args: GenesisArgs, +} + +pub(crate) async fn run(args: GenesisCommand, shell: &Shell) -> anyhow::Result<()> { + match args.command { + Some(GenesisSubcommands::InitDatabase(args)) => database::run(args, shell).await, + Some(GenesisSubcommands::Server) => server::run(shell).await, + None => genesis::run(args.args, shell).await, + } +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs similarity index 88% rename from zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs rename to zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs index f287895a1fde..f74b971d2319 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init_configs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs @@ -8,10 +8,15 @@ use config::{ use ethers::types::Address; use xshell::Shell; -use super::genesis; use crate::{ commands::{ - chain::args::init_configs::{InitConfigsArgs, InitConfigsArgsFinal}, + chain::{ + args::init::{ + configs::{InitConfigsArgs, InitConfigsArgsFinal}, + PortOffset, + }, + genesis, + }, portal::update_portal_config, }, messages::{ @@ -45,7 +50,8 @@ pub async fn init_configs( // Initialize general config let mut general_config = chain_config.get_general_config()?; - apply_port_offset(init_args.port_offset, &mut general_config)?; + let port_offset = PortOffset::from_chain_id(chain_config.id as u16).into(); + apply_port_offset(port_offset, &mut general_config)?; let ports = ports_config(&general_config).context(MSG_PORTS_CONFIG_ERR)?; let consensus_keys = generate_consensus_keys(); @@ -53,11 +59,12 @@ pub async fn init_configs( get_consensus_config(chain_config, ports, Some(consensus_keys.clone()), None)?; general_config.consensus_config = Some(consensus_config); + // Initialize genesis config let mut genesis_config = chain_config.get_genesis_config()?; update_from_chain_config(&mut genesis_config, chain_config); genesis_config.save_with_base_path(shell, &chain_config.configs)?; - // Copy ecosystem contracts + // Initialize contracts config let mut contracts_config = ecosystem_config.get_contracts_config()?; contracts_config.l1.diamond_proxy_addr = Address::zero(); contracts_config.l1.governance_addr = Address::zero(); @@ -65,6 +72,7 @@ pub async fn init_configs( contracts_config.l1.base_token_addr = chain_config.base_token.address; contracts_config.save_with_base_path(shell, &chain_config.configs)?; + // Initialize secrets config let mut secrets = chain_config.get_secrets_config()?; set_l1_rpc_url(&mut secrets, init_args.l1_rpc_url.clone())?; secrets.consensus = Some(get_consensus_secrets(&consensus_keys)); diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs similarity index 95% rename from zk_toolbox/crates/zk_inception/src/commands/chain/init.rs rename to zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs index 2be030ee79f8..8445b265f325 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs @@ -4,17 +4,14 @@ use config::{traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig}; use types::BaseToken; use xshell::Shell; -use super::common::{distribute_eth, mint_base_token}; use crate::{ accept_ownership::accept_admin, commands::chain::{ - args::{ - init::{InitArgs, InitArgsFinal}, - init_configs::InitConfigsArgsFinal, - }, + args::init::{configs::InitConfigsArgsFinal, InitArgs, InitArgsFinal}, + common::{distribute_eth, mint_base_token}, deploy_l2_contracts, deploy_paymaster, - genesis_all::genesis, - init_configs::init_configs, + genesis::genesis, + init::configs::init_configs, register_chain::register_chain, set_token_multiplier_setter::set_token_multiplier_setter, setup_legacy_bridge::setup_legacy_bridge, diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs new file mode 100644 index 000000000000..ece70384b292 --- /dev/null +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs @@ -0,0 +1,32 @@ +use clap::{command, Parser, Subcommand}; +use xshell::Shell; + +use crate::commands::chain::args::init::{configs::InitConfigsArgs, InitArgs}; + +// Standard init command +mod init; +pub use init::*; +// Init subcommands +pub mod configs; + +#[derive(Subcommand, Debug, Clone)] +pub enum ChainInitSubcommands { + /// Initialize chain configs + Configs(InitConfigsArgs), +} + +#[derive(Parser, Debug)] +#[command()] +pub struct ChainInitCommand { + #[command(subcommand)] + command: Option, + #[clap(flatten)] + args: InitArgs, +} + +pub(crate) async fn run(args: ChainInitCommand, shell: &Shell) -> anyhow::Result<()> { + match args.command { + Some(ChainInitSubcommands::Configs(args)) => configs::run(args, shell).await, + None => init::run(args.args, shell).await, + } +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs index 16aa41b5f6f1..5d835a5b6d54 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs @@ -1,17 +1,13 @@ use ::common::forge::ForgeScriptArgs; use args::build_transactions::BuildTransactionsArgs; pub(crate) use args::create::ChainCreateArgsFinal; -use clap::{command, Parser, Subcommand}; +use clap::{command, Subcommand}; pub(crate) use create::create_chain_inner; use xshell::Shell; use crate::commands::chain::{ - args::{ - create::ChainCreateArgs, genesis::GenesisArgs, init::InitArgs, - init_configs::InitConfigsArgs, - }, - deploy_l2_contracts::Deploy2ContractsOption, - genesis::GenesisSubcommands, + args::create::ChainCreateArgs, deploy_l2_contracts::Deploy2ContractsOption, + genesis::GenesisCommand, init::ChainInitCommand, }; mod accept_chain_ownership; @@ -22,22 +18,11 @@ mod create; pub mod deploy_l2_contracts; pub mod deploy_paymaster; pub mod genesis; -pub mod genesis_all; -pub(crate) mod init; -mod init_configs; +pub mod init; pub mod register_chain; mod set_token_multiplier_setter; mod setup_legacy_bridge; -#[derive(Parser, Debug)] -#[command()] -pub struct GenesisCommand { - #[command(subcommand)] - command: Option, - #[clap(flatten)] - args: GenesisArgs, -} - #[derive(Subcommand, Debug)] pub enum ChainCommands { /// Create a new chain, setting the necessary configurations for later initialization @@ -45,7 +30,7 @@ pub enum ChainCommands { /// Create unsigned transactions for chain deployment BuildTransactions(BuildTransactionsArgs), /// Initialize chain, deploying necessary contracts and performing on-chain operations - Init(InitArgs), + Init(ChainInitCommand), /// Run server genesis Genesis(GenesisCommand), /// Register a new chain on L1 (executed by L1 governor). @@ -76,8 +61,6 @@ pub enum ChainCommands { DeployPaymaster(ForgeScriptArgs), /// Update Token Multiplier Setter address on L1 UpdateTokenMultiplierSetter(ForgeScriptArgs), - /// Initializes chain configs - InitConfigs(InitConfigsArgs), } pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<()> { @@ -85,13 +68,7 @@ pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<() ChainCommands::Create(args) => create::run(args, shell), ChainCommands::Init(args) => init::run(args, shell).await, ChainCommands::BuildTransactions(args) => build_transactions::run(args, shell).await, - ChainCommands::Genesis(args) => match args.command { - Some(GenesisSubcommands::InitDatabase(args)) => { - genesis::database::run(args, shell).await - } - Some(GenesisSubcommands::Server) => genesis::server::run(shell).await, - None => genesis_all::run(args.args, shell).await, - }, + ChainCommands::Genesis(args) => genesis::run(args, shell).await, ChainCommands::RegisterChain(args) => register_chain::run(args, shell).await, ChainCommands::DeployL2Contracts(args) => { deploy_l2_contracts::run(args, shell, Deploy2ContractsOption::All).await @@ -110,6 +87,5 @@ pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<() ChainCommands::UpdateTokenMultiplierSetter(args) => { set_token_multiplier_setter::run(args, shell).await } - ChainCommands::InitConfigs(args) => init_configs::run(args, shell).await, } } From 2371245f5aef68450c77b351fcf62a847fc4021d Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Fri, 27 Sep 2024 12:34:05 -0600 Subject: [PATCH 4/7] lint: no same name file and folder --- .../src/commands/chain/args/init/init.rs | 103 ----------- .../src/commands/chain/args/init/mod.rs | 105 +++++++++++- .../src/commands/chain/genesis/genesis.rs | 64 ------- .../src/commands/chain/genesis/mod.rs | 72 +++++++- .../src/commands/chain/init/configs.rs | 2 +- .../src/commands/chain/init/init.rs | 155 ----------------- .../src/commands/chain/init/mod.rs | 162 +++++++++++++++++- .../zk_inception/src/commands/chain/mod.rs | 4 +- .../src/commands/ecosystem/init.rs | 4 +- zk_toolbox/crates/zk_inception/src/main.rs | 8 +- 10 files changed, 334 insertions(+), 345 deletions(-) delete mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs delete mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs delete mode 100644 zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs deleted file mode 100644 index 6addd7d86dd1..000000000000 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/init.rs +++ /dev/null @@ -1,103 +0,0 @@ -use std::str::FromStr; - -use clap::Parser; -use common::{forge::ForgeScriptArgs, Prompt}; -use config::ChainConfig; -use serde::{Deserialize, Serialize}; -use types::L1Network; -use url::Url; - -use crate::{ - commands::chain::args::genesis::{GenesisArgs, GenesisArgsFinal}, - defaults::LOCAL_RPC_URL, - messages::{ - MSG_DEPLOY_PAYMASTER_PROMPT, MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, - MSG_L1_RPC_URL_INVALID_ERR, MSG_L1_RPC_URL_PROMPT, MSG_PORT_OFFSET_HELP, - }, -}; - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct PortOffset(u16); - -impl PortOffset { - pub fn from_chain_id(chain_id: u16) -> Self { - Self((chain_id - 1) * 100) - } -} - -impl FromStr for PortOffset { - type Err = String; - - fn from_str(s: &str) -> Result { - s.parse::() - .map(PortOffset) - .map_err(|_| "Invalid port offset".to_string()) - } -} - -impl From for u16 { - fn from(port_offset: PortOffset) -> Self { - port_offset.0 - } -} - -#[derive(Debug, Clone, Serialize, Deserialize, Parser)] -pub struct InitArgs { - /// All ethereum environment related arguments - #[clap(flatten)] - #[serde(flatten)] - pub forge_args: ForgeScriptArgs, - #[clap(flatten, next_help_heading = MSG_GENESIS_ARGS_HELP)] - #[serde(flatten)] - pub genesis_args: GenesisArgs, - #[clap(long, default_missing_value = "true", num_args = 0..=1)] - pub deploy_paymaster: Option, - #[clap(long, help = MSG_L1_RPC_URL_HELP)] - pub l1_rpc_url: Option, - #[clap(long, help = MSG_PORT_OFFSET_HELP)] - pub port_offset: Option, -} - -impl InitArgs { - pub fn fill_values_with_prompt(self, config: &ChainConfig) -> InitArgsFinal { - let deploy_paymaster = self.deploy_paymaster.unwrap_or_else(|| { - common::PromptConfirm::new(MSG_DEPLOY_PAYMASTER_PROMPT) - .default(true) - .ask() - }); - - let l1_rpc_url = self.l1_rpc_url.unwrap_or_else(|| { - let mut prompt = Prompt::new(MSG_L1_RPC_URL_PROMPT); - if config.l1_network == L1Network::Localhost { - prompt = prompt.default(LOCAL_RPC_URL); - } - prompt - .validate_with(|val: &String| -> Result<(), String> { - Url::parse(val) - .map(|_| ()) - .map_err(|_| MSG_L1_RPC_URL_INVALID_ERR.to_string()) - }) - .ask() - }); - - InitArgsFinal { - forge_args: self.forge_args, - genesis_args: self.genesis_args.fill_values_with_prompt(config), - deploy_paymaster, - l1_rpc_url, - port_offset: self - .port_offset - .unwrap_or(PortOffset::from_chain_id(config.id as u16)) - .into(), - } - } -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct InitArgsFinal { - pub forge_args: ForgeScriptArgs, - pub genesis_args: GenesisArgsFinal, - pub deploy_paymaster: bool, - pub l1_rpc_url: String, - pub port_offset: u16, -} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs index 7eb8e8ab76a0..5ed1df4d30e8 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/init/mod.rs @@ -1,4 +1,105 @@ +use std::str::FromStr; + +use clap::Parser; +use common::{forge::ForgeScriptArgs, Prompt}; +use config::ChainConfig; +use serde::{Deserialize, Serialize}; +use types::L1Network; +use url::Url; + +use crate::{ + commands::chain::args::genesis::{GenesisArgs, GenesisArgsFinal}, + defaults::LOCAL_RPC_URL, + messages::{ + MSG_DEPLOY_PAYMASTER_PROMPT, MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP, + MSG_L1_RPC_URL_INVALID_ERR, MSG_L1_RPC_URL_PROMPT, MSG_PORT_OFFSET_HELP, + }, +}; + pub mod configs; -pub mod init; -pub use init::*; +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PortOffset(u16); + +impl PortOffset { + pub fn from_chain_id(chain_id: u16) -> Self { + Self((chain_id - 1) * 100) + } +} + +impl FromStr for PortOffset { + type Err = String; + + fn from_str(s: &str) -> Result { + s.parse::() + .map(PortOffset) + .map_err(|_| "Invalid port offset".to_string()) + } +} + +impl From for u16 { + fn from(port_offset: PortOffset) -> Self { + port_offset.0 + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, Parser)] +pub struct InitArgs { + /// All ethereum environment related arguments + #[clap(flatten)] + #[serde(flatten)] + pub forge_args: ForgeScriptArgs, + #[clap(flatten, next_help_heading = MSG_GENESIS_ARGS_HELP)] + #[serde(flatten)] + pub genesis_args: GenesisArgs, + #[clap(long, default_missing_value = "true", num_args = 0..=1)] + pub deploy_paymaster: Option, + #[clap(long, help = MSG_L1_RPC_URL_HELP)] + pub l1_rpc_url: Option, + #[clap(long, help = MSG_PORT_OFFSET_HELP)] + pub port_offset: Option, +} + +impl InitArgs { + pub fn fill_values_with_prompt(self, config: &ChainConfig) -> InitArgsFinal { + let deploy_paymaster = self.deploy_paymaster.unwrap_or_else(|| { + common::PromptConfirm::new(MSG_DEPLOY_PAYMASTER_PROMPT) + .default(true) + .ask() + }); + + let l1_rpc_url = self.l1_rpc_url.unwrap_or_else(|| { + let mut prompt = Prompt::new(MSG_L1_RPC_URL_PROMPT); + if config.l1_network == L1Network::Localhost { + prompt = prompt.default(LOCAL_RPC_URL); + } + prompt + .validate_with(|val: &String| -> Result<(), String> { + Url::parse(val) + .map(|_| ()) + .map_err(|_| MSG_L1_RPC_URL_INVALID_ERR.to_string()) + }) + .ask() + }); + + InitArgsFinal { + forge_args: self.forge_args, + genesis_args: self.genesis_args.fill_values_with_prompt(config), + deploy_paymaster, + l1_rpc_url, + port_offset: self + .port_offset + .unwrap_or(PortOffset::from_chain_id(config.id as u16)) + .into(), + } + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct InitArgsFinal { + pub forge_args: ForgeScriptArgs, + pub genesis_args: GenesisArgsFinal, + pub deploy_paymaster: bool, + pub l1_rpc_url: String, + pub port_offset: u16, +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs deleted file mode 100644 index 696674110972..000000000000 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/genesis.rs +++ /dev/null @@ -1,64 +0,0 @@ -use anyhow::Context; -use common::{config::global_config, logger, spinner::Spinner}; -use config::{ChainConfig, EcosystemConfig}; -use xshell::Shell; - -use crate::{ - commands::chain::{ - args::genesis::{GenesisArgs, GenesisArgsFinal}, - genesis::{self, database::initialize_databases, server::run_server_genesis}, - }, - messages::{ - MSG_CHAIN_NOT_INITIALIZED, MSG_GENESIS_COMPLETED, MSG_INITIALIZING_DATABASES_SPINNER, - MSG_SELECTED_CONFIG, MSG_STARTING_GENESIS, MSG_STARTING_GENESIS_SPINNER, - }, -}; - -pub async fn run(args: GenesisArgs, shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); - let ecosystem_config = EcosystemConfig::from_file(shell)?; - let chain_config = ecosystem_config - .load_chain(chain_name) - .context(MSG_CHAIN_NOT_INITIALIZED)?; - let args = args.fill_values_with_prompt(&chain_config); - - genesis(args, shell, &chain_config).await?; - logger::outro(MSG_GENESIS_COMPLETED); - - Ok(()) -} - -pub async fn genesis( - args: GenesisArgsFinal, - shell: &Shell, - config: &ChainConfig, -) -> anyhow::Result<()> { - genesis::database::update_configs(args.clone(), shell, config)?; - - logger::note( - MSG_SELECTED_CONFIG, - logger::object_to_string(serde_json::json!({ - "chain_config": config, - "server_db_config": args.server_db, - "prover_db_config": args.prover_db, - })), - ); - logger::info(MSG_STARTING_GENESIS); - - let spinner = Spinner::new(MSG_INITIALIZING_DATABASES_SPINNER); - initialize_databases( - shell, - &args.server_db, - &args.prover_db, - config.link_to_code.clone(), - args.dont_drop, - ) - .await?; - spinner.finish(); - - let spinner = Spinner::new(MSG_STARTING_GENESIS_SPINNER); - run_server_genesis(config, shell)?; - spinner.finish(); - - Ok(()) -} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs index 5095f011f058..4e1d5e1d43a7 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs @@ -1,11 +1,20 @@ +use anyhow::Context; use clap::{command, Parser, Subcommand}; +use common::{config::global_config, logger, spinner::Spinner}; +use config::{ChainConfig, EcosystemConfig}; use xshell::Shell; -use crate::commands::chain::args::genesis::GenesisArgs; +use crate::{ + commands::chain::{ + args::genesis::{GenesisArgs, GenesisArgsFinal}, + genesis::{self, database::initialize_databases, server::run_server_genesis}, + }, + messages::{ + MSG_CHAIN_NOT_INITIALIZED, MSG_GENESIS_COMPLETED, MSG_INITIALIZING_DATABASES_SPINNER, + MSG_SELECTED_CONFIG, MSG_STARTING_GENESIS, MSG_STARTING_GENESIS_SPINNER, + }, +}; -// Standard genesis command -mod genesis; -pub use genesis::*; // Genesis subcommands pub mod database; pub mod server; @@ -14,7 +23,7 @@ pub mod server; pub enum GenesisSubcommands { /// Initialize databases #[command(alias = "db")] - InitDatabase(GenesisArgs), + InitDatabase(Box), /// Runs server genesis Server, } @@ -30,8 +39,57 @@ pub struct GenesisCommand { pub(crate) async fn run(args: GenesisCommand, shell: &Shell) -> anyhow::Result<()> { match args.command { - Some(GenesisSubcommands::InitDatabase(args)) => database::run(args, shell).await, + Some(GenesisSubcommands::InitDatabase(args)) => database::run(*args, shell).await, Some(GenesisSubcommands::Server) => server::run(shell).await, - None => genesis::run(args.args, shell).await, + None => run_genesis(args.args, shell).await, } } + +pub async fn run_genesis(args: GenesisArgs, shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_INITIALIZED)?; + let args = args.fill_values_with_prompt(&chain_config); + + genesis(args, shell, &chain_config).await?; + logger::outro(MSG_GENESIS_COMPLETED); + + Ok(()) +} + +pub async fn genesis( + args: GenesisArgsFinal, + shell: &Shell, + config: &ChainConfig, +) -> anyhow::Result<()> { + genesis::database::update_configs(args.clone(), shell, config)?; + + logger::note( + MSG_SELECTED_CONFIG, + logger::object_to_string(serde_json::json!({ + "chain_config": config, + "server_db_config": args.server_db, + "prover_db_config": args.prover_db, + })), + ); + logger::info(MSG_STARTING_GENESIS); + + let spinner = Spinner::new(MSG_INITIALIZING_DATABASES_SPINNER); + initialize_databases( + shell, + &args.server_db, + &args.prover_db, + config.link_to_code.clone(), + args.dont_drop, + ) + .await?; + spinner.finish(); + + let spinner = Spinner::new(MSG_STARTING_GENESIS_SPINNER); + run_server_genesis(config, shell)?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs index f74b971d2319..1212418726dc 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs @@ -78,7 +78,7 @@ pub async fn init_configs( secrets.consensus = Some(get_consensus_secrets(&consensus_keys)); secrets.save_with_base_path(shell, &chain_config.configs)?; - genesis::database::update_configs(init_args.genesis_args.clone(), shell, &chain_config)?; + genesis::database::update_configs(init_args.genesis_args.clone(), shell, chain_config)?; update_portal_config(shell, chain_config) .await diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs deleted file mode 100644 index 8445b265f325..000000000000 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init/init.rs +++ /dev/null @@ -1,155 +0,0 @@ -use anyhow::Context; -use common::{config::global_config, git, logger, spinner::Spinner}; -use config::{traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig}; -use types::BaseToken; -use xshell::Shell; - -use crate::{ - accept_ownership::accept_admin, - commands::chain::{ - args::init::{configs::InitConfigsArgsFinal, InitArgs, InitArgsFinal}, - common::{distribute_eth, mint_base_token}, - deploy_l2_contracts, deploy_paymaster, - genesis::genesis, - init::configs::init_configs, - register_chain::register_chain, - set_token_multiplier_setter::set_token_multiplier_setter, - setup_legacy_bridge::setup_legacy_bridge, - }, - messages::{ - msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_INITIALIZED, - MSG_CHAIN_NOT_FOUND_ERR, MSG_DEPLOYING_PAYMASTER, MSG_GENESIS_DATABASE_ERR, - MSG_REGISTERING_CHAIN_SPINNER, MSG_SELECTED_CONFIG, - MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER, MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND, - }, -}; - -pub(crate) async fn run(args: InitArgs, shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); - let config = EcosystemConfig::from_file(shell)?; - let chain_config = config - .load_chain(chain_name) - .context(MSG_CHAIN_NOT_FOUND_ERR)?; - let mut args = args.fill_values_with_prompt(&chain_config); - - logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&chain_config)); - logger::info(msg_initializing_chain("")); - git::submodule_update(shell, config.link_to_code.clone())?; - - init(&mut args, shell, &config, &chain_config).await?; - - logger::success(MSG_CHAIN_INITIALIZED); - Ok(()) -} - -pub async fn init( - init_args: &InitArgsFinal, - shell: &Shell, - ecosystem_config: &EcosystemConfig, - chain_config: &ChainConfig, -) -> anyhow::Result<()> { - // Initialize configs - let init_configs_args = InitConfigsArgsFinal::from_chain_init_args(&init_args); - let mut contracts_config = - init_configs(&init_configs_args, shell, ecosystem_config, chain_config).await?; - - // Fund some wallet addresses with ETH or base token (only for Localhost) - distribute_eth(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; - mint_base_token(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; - - // Register chain on BridgeHub (run by L1 Governor) - let spinner = Spinner::new(MSG_REGISTERING_CHAIN_SPINNER); - register_chain( - shell, - init_args.forge_args.clone(), - ecosystem_config, - chain_config, - &mut contracts_config, - init_args.l1_rpc_url.clone(), - None, - true, - ) - .await?; - contracts_config.save_with_base_path(shell, &chain_config.configs)?; - spinner.finish(); - - // Accept ownership for DiamondProxy (run by L2 Governor) - let spinner = Spinner::new(MSG_ACCEPTING_ADMIN_SPINNER); - accept_admin( - shell, - ecosystem_config, - contracts_config.l1.chain_admin_addr, - chain_config.get_wallets_config()?.governor_private_key(), - contracts_config.l1.diamond_proxy_addr, - &init_args.forge_args.clone(), - init_args.l1_rpc_url.clone(), - ) - .await?; - spinner.finish(); - - // Set token multiplier setter address (run by L2 Governor) - if chain_config.base_token != BaseToken::eth() { - let spinner = Spinner::new(MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER); - set_token_multiplier_setter( - shell, - ecosystem_config, - chain_config.get_wallets_config()?.governor_private_key(), - contracts_config.l1.chain_admin_addr, - chain_config - .get_wallets_config() - .unwrap() - .token_multiplier_setter - .context(MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND)? - .address, - &init_args.forge_args.clone(), - init_args.l1_rpc_url.clone(), - ) - .await?; - spinner.finish(); - } - - // Deploy L2 contracts: L2 Shared Bridge, L2 Default Upgrader, etc. (run by L2 Governor) - deploy_l2_contracts::deploy_l2_contracts( - shell, - chain_config, - ecosystem_config, - &mut contracts_config, - init_args.forge_args.clone(), - ) - .await?; - contracts_config.save_with_base_path(shell, &chain_config.configs)?; - - // Setup legacy bridge - shouldn't be used for new chains (run by L1 Governor) - if let Some(true) = chain_config.legacy_bridge { - setup_legacy_bridge( - shell, - chain_config, - ecosystem_config, - &contracts_config, - init_args.forge_args.clone(), - ) - .await?; - } - - // Deploy Paymaster contract (run by L2 Governor) - if init_args.deploy_paymaster { - let spinner = Spinner::new(MSG_DEPLOYING_PAYMASTER); - deploy_paymaster::deploy_paymaster( - shell, - chain_config, - &mut contracts_config, - init_args.forge_args.clone(), - None, - true, - ) - .await?; - contracts_config.save_with_base_path(shell, &chain_config.configs)?; - spinner.finish(); - } - - genesis(init_args.genesis_args.clone(), shell, chain_config) - .await - .context(MSG_GENESIS_DATABASE_ERR)?; - - Ok(()) -} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs index ece70384b292..ddebe13f93dd 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs @@ -1,11 +1,33 @@ +use anyhow::Context; use clap::{command, Parser, Subcommand}; +use common::{config::global_config, git, logger, spinner::Spinner}; +use config::{traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig}; +use types::BaseToken; use xshell::Shell; -use crate::commands::chain::args::init::{configs::InitConfigsArgs, InitArgs}; +use crate::{ + accept_ownership::accept_admin, + commands::chain::{ + args::init::{ + configs::{InitConfigsArgs, InitConfigsArgsFinal}, + InitArgs, InitArgsFinal, + }, + common::{distribute_eth, mint_base_token}, + deploy_l2_contracts, deploy_paymaster, + genesis::genesis, + init::configs::init_configs, + register_chain::register_chain, + set_token_multiplier_setter::set_token_multiplier_setter, + setup_legacy_bridge::setup_legacy_bridge, + }, + messages::{ + msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_INITIALIZED, + MSG_CHAIN_NOT_FOUND_ERR, MSG_DEPLOYING_PAYMASTER, MSG_GENESIS_DATABASE_ERR, + MSG_REGISTERING_CHAIN_SPINNER, MSG_SELECTED_CONFIG, + MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER, MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND, + }, +}; -// Standard init command -mod init; -pub use init::*; // Init subcommands pub mod configs; @@ -27,6 +49,136 @@ pub struct ChainInitCommand { pub(crate) async fn run(args: ChainInitCommand, shell: &Shell) -> anyhow::Result<()> { match args.command { Some(ChainInitSubcommands::Configs(args)) => configs::run(args, shell).await, - None => init::run(args.args, shell).await, + None => run_init(args.args, shell).await, } } + +async fn run_init(args: InitArgs, shell: &Shell) -> anyhow::Result<()> { + let chain_name = global_config().chain_name.clone(); + let config = EcosystemConfig::from_file(shell)?; + let chain_config = config + .load_chain(chain_name) + .context(MSG_CHAIN_NOT_FOUND_ERR)?; + let args = args.fill_values_with_prompt(&chain_config); + + logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&chain_config)); + logger::info(msg_initializing_chain("")); + git::submodule_update(shell, config.link_to_code.clone())?; + + init(&args, shell, &config, &chain_config).await?; + + logger::success(MSG_CHAIN_INITIALIZED); + Ok(()) +} + +pub async fn init( + init_args: &InitArgsFinal, + shell: &Shell, + ecosystem_config: &EcosystemConfig, + chain_config: &ChainConfig, +) -> anyhow::Result<()> { + // Initialize configs + let init_configs_args = InitConfigsArgsFinal::from_chain_init_args(init_args); + let mut contracts_config = + init_configs(&init_configs_args, shell, ecosystem_config, chain_config).await?; + + // Fund some wallet addresses with ETH or base token (only for Localhost) + distribute_eth(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; + mint_base_token(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?; + + // Register chain on BridgeHub (run by L1 Governor) + let spinner = Spinner::new(MSG_REGISTERING_CHAIN_SPINNER); + register_chain( + shell, + init_args.forge_args.clone(), + ecosystem_config, + chain_config, + &mut contracts_config, + init_args.l1_rpc_url.clone(), + None, + true, + ) + .await?; + contracts_config.save_with_base_path(shell, &chain_config.configs)?; + spinner.finish(); + + // Accept ownership for DiamondProxy (run by L2 Governor) + let spinner = Spinner::new(MSG_ACCEPTING_ADMIN_SPINNER); + accept_admin( + shell, + ecosystem_config, + contracts_config.l1.chain_admin_addr, + chain_config.get_wallets_config()?.governor_private_key(), + contracts_config.l1.diamond_proxy_addr, + &init_args.forge_args.clone(), + init_args.l1_rpc_url.clone(), + ) + .await?; + spinner.finish(); + + // Set token multiplier setter address (run by L2 Governor) + if chain_config.base_token != BaseToken::eth() { + let spinner = Spinner::new(MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER); + set_token_multiplier_setter( + shell, + ecosystem_config, + chain_config.get_wallets_config()?.governor_private_key(), + contracts_config.l1.chain_admin_addr, + chain_config + .get_wallets_config() + .unwrap() + .token_multiplier_setter + .context(MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND)? + .address, + &init_args.forge_args.clone(), + init_args.l1_rpc_url.clone(), + ) + .await?; + spinner.finish(); + } + + // Deploy L2 contracts: L2 Shared Bridge, L2 Default Upgrader, etc. (run by L2 Governor) + deploy_l2_contracts::deploy_l2_contracts( + shell, + chain_config, + ecosystem_config, + &mut contracts_config, + init_args.forge_args.clone(), + ) + .await?; + contracts_config.save_with_base_path(shell, &chain_config.configs)?; + + // Setup legacy bridge - shouldn't be used for new chains (run by L1 Governor) + if let Some(true) = chain_config.legacy_bridge { + setup_legacy_bridge( + shell, + chain_config, + ecosystem_config, + &contracts_config, + init_args.forge_args.clone(), + ) + .await?; + } + + // Deploy Paymaster contract (run by L2 Governor) + if init_args.deploy_paymaster { + let spinner = Spinner::new(MSG_DEPLOYING_PAYMASTER); + deploy_paymaster::deploy_paymaster( + shell, + chain_config, + &mut contracts_config, + init_args.forge_args.clone(), + None, + true, + ) + .await?; + contracts_config.save_with_base_path(shell, &chain_config.configs)?; + spinner.finish(); + } + + genesis(init_args.genesis_args.clone(), shell, chain_config) + .await + .context(MSG_GENESIS_DATABASE_ERR)?; + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs index 5d835a5b6d54..378309a07cb1 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/mod.rs @@ -30,7 +30,7 @@ pub enum ChainCommands { /// Create unsigned transactions for chain deployment BuildTransactions(BuildTransactionsArgs), /// Initialize chain, deploying necessary contracts and performing on-chain operations - Init(ChainInitCommand), + Init(Box), /// Run server genesis Genesis(GenesisCommand), /// Register a new chain on L1 (executed by L1 governor). @@ -66,7 +66,7 @@ pub enum ChainCommands { pub(crate) async fn run(shell: &Shell, args: ChainCommands) -> anyhow::Result<()> { match args { ChainCommands::Create(args) => create::run(args, shell), - ChainCommands::Init(args) => init::run(args, shell).await, + ChainCommands::Init(args) => init::run(*args, shell).await, ChainCommands::BuildTransactions(args) => build_transactions::run(args, shell).await, ChainCommands::Genesis(args) => genesis::run(args, shell).await, ChainCommands::RegisterChain(args) => register_chain::run(args, shell).await, diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs index 0b1d7237fa8b..a2dbeed9f032 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs @@ -369,10 +369,10 @@ async fn init_chains( l1_rpc_url: Some(final_init_args.ecosystem.l1_rpc_url.clone()), port_offset: PortOffset::from_chain_id(chain_config.id as u16).into(), }; - let mut final_chain_init_args = chain_init_args.fill_values_with_prompt(&chain_config); + let final_chain_init_args = chain_init_args.fill_values_with_prompt(&chain_config); chain::init::init( - &mut final_chain_init_args, + &final_chain_init_args, shell, ecosystem_config, &chain_config, diff --git a/zk_toolbox/crates/zk_inception/src/main.rs b/zk_toolbox/crates/zk_inception/src/main.rs index f1ca348df386..50ba74b3ef48 100644 --- a/zk_toolbox/crates/zk_inception/src/main.rs +++ b/zk_toolbox/crates/zk_inception/src/main.rs @@ -42,10 +42,10 @@ struct Inception { pub enum InceptionSubcommands { /// Ecosystem related commands #[command(subcommand, alias = "e")] - Ecosystem(EcosystemCommands), + Ecosystem(Box), /// Chain related commands #[command(subcommand, alias = "c")] - Chain(ChainCommands), + Chain(Box), /// Prover related commands #[command(subcommand, alias = "p")] Prover(ProverCommands), @@ -119,8 +119,8 @@ async fn main() -> anyhow::Result<()> { async fn run_subcommand(inception_args: Inception, shell: &Shell) -> anyhow::Result<()> { match inception_args.command { - InceptionSubcommands::Ecosystem(args) => commands::ecosystem::run(shell, args).await?, - InceptionSubcommands::Chain(args) => commands::chain::run(shell, args).await?, + InceptionSubcommands::Ecosystem(args) => commands::ecosystem::run(shell, *args).await?, + InceptionSubcommands::Chain(args) => commands::chain::run(shell, *args).await?, InceptionSubcommands::Prover(args) => commands::prover::run(shell, args).await?, InceptionSubcommands::Server(args) => commands::server::run(shell, args)?, InceptionSubcommands::Containers(args) => commands::containers::run(shell, args)?, From dd5d651a39c69e402906e389268e8728e6d6644b Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Fri, 27 Sep 2024 14:23:42 -0600 Subject: [PATCH 5/7] db to database --- zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs | 1 - .../crates/zk_inception/src/commands/chain/genesis/mod.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs index b42f9835dba3..f2a5f6b8be1f 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/mod.rs @@ -2,4 +2,3 @@ pub mod build_transactions; pub mod create; pub mod genesis; pub mod init; -// pub mod init_all; diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs index 4e1d5e1d43a7..f40da2cdfc92 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs @@ -22,7 +22,7 @@ pub mod server; #[derive(Subcommand, Debug, Clone)] pub enum GenesisSubcommands { /// Initialize databases - #[command(alias = "db")] + #[command(alias = "database")] InitDatabase(Box), /// Runs server genesis Server, From 7e084a201097749d4bede9b1d2f69622c636efc9 Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Tue, 1 Oct 2024 11:29:49 -0600 Subject: [PATCH 6/7] load_current_chain --- .../src/commands/chain/accept_chain_ownership.rs | 5 ++--- .../crates/zk_inception/src/commands/chain/genesis/mod.rs | 5 ++--- .../crates/zk_inception/src/commands/chain/genesis/server.rs | 4 +--- .../crates/zk_inception/src/commands/chain/init/configs.rs | 5 ++--- .../crates/zk_inception/src/commands/chain/init/mod.rs | 2 +- .../crates/zk_inception/src/commands/chain/register_chain.rs | 4 +--- 6 files changed, 9 insertions(+), 16 deletions(-) diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs index bfa91063004c..37d69fcf5bcc 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/accept_chain_ownership.rs @@ -1,5 +1,5 @@ use anyhow::Context; -use common::{config::global_config, forge::ForgeScriptArgs, logger, spinner::Spinner}; +use common::{forge::ForgeScriptArgs, logger, spinner::Spinner}; use config::EcosystemConfig; use xshell::Shell; @@ -12,10 +12,9 @@ use crate::{ }; pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); let ecosystem_config = EcosystemConfig::from_file(shell)?; let chain_config = ecosystem_config - .load_chain(chain_name) + .load_current_chain() .context(MSG_CHAIN_NOT_INITIALIZED)?; let contracts = chain_config.get_contracts_config()?; let secrets = chain_config.get_secrets_config()?; diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs index f40da2cdfc92..01842c2916ae 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/mod.rs @@ -1,6 +1,6 @@ use anyhow::Context; use clap::{command, Parser, Subcommand}; -use common::{config::global_config, logger, spinner::Spinner}; +use common::{logger, spinner::Spinner}; use config::{ChainConfig, EcosystemConfig}; use xshell::Shell; @@ -46,10 +46,9 @@ pub(crate) async fn run(args: GenesisCommand, shell: &Shell) -> anyhow::Result<( } pub async fn run_genesis(args: GenesisArgs, shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); let ecosystem_config = EcosystemConfig::from_file(shell)?; let chain_config = ecosystem_config - .load_chain(chain_name) + .load_current_chain() .context(MSG_CHAIN_NOT_INITIALIZED)?; let args = args.fill_values_with_prompt(&chain_config); diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs index bf682f139daf..50a74b7ea9e4 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis/server.rs @@ -1,6 +1,5 @@ use anyhow::Context; use common::{ - config::global_config, logger, server::{Server, ServerMode}, spinner::Spinner, @@ -17,10 +16,9 @@ use crate::messages::{ }; pub async fn run(shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); let ecosystem_config = EcosystemConfig::from_file(shell)?; let chain_config = ecosystem_config - .load_chain(chain_name) + .load_current_chain() .context(MSG_CHAIN_NOT_INITIALIZED)?; let spinner = Spinner::new(MSG_STARTING_GENESIS_SPINNER); diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs index 0c3a7694732e..8edcd9d1c7ce 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs @@ -1,5 +1,5 @@ use anyhow::Context; -use common::{config::global_config, logger}; +use common::logger; use config::{ copy_configs, set_l1_rpc_url, update_from_chain_config, ChainConfig, ContractsConfig, EcosystemConfig, @@ -29,10 +29,9 @@ use crate::{ }; pub async fn run(args: InitConfigsArgs, shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); let ecosystem_config = EcosystemConfig::from_file(shell)?; let chain_config = ecosystem_config - .load_chain(chain_name) + .load_current_chain() .context(MSG_CHAIN_NOT_FOUND_ERR)?; let args = args.fill_values_with_prompt(&chain_config); diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs index 8527f9a03a24..8a36f4e32b2f 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/mod.rs @@ -136,7 +136,7 @@ pub async fn init( spinner.finish(); } - // Deploy L2 contracts: L2 Shared Bridge, L2 Default Upgrader, etc. (run by L2 Governor) + // Deploy L2 contracts: L2SharedBridge, L2DefaultUpgrader, ... (run by L1 Governor) deploy_l2_contracts::deploy_l2_contracts( shell, chain_config, diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs index e2ce133dfa83..9f2ff41f897e 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/register_chain.rs @@ -1,6 +1,5 @@ use anyhow::Context; use common::{ - config::global_config, forge::{Forge, ForgeScriptArgs}, logger, spinner::Spinner, @@ -24,10 +23,9 @@ use crate::{ }; pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> { - let chain_name = global_config().chain_name.clone(); let ecosystem_config = EcosystemConfig::from_file(shell)?; let chain_config = ecosystem_config - .load_chain(chain_name) + .load_current_chain() .context(MSG_CHAIN_NOT_INITIALIZED)?; let mut contracts = chain_config.get_contracts_config()?; let secrets = chain_config.get_secrets_config()?; From 3d5794f2df135cba547c6217b823044437fc6c55 Mon Sep 17 00:00:00 2001 From: Alexander Melnikov Date: Tue, 1 Oct 2024 12:48:36 -0600 Subject: [PATCH 7/7] ports scanner fix --- .../crates/zk_inception/src/commands/chain/init/configs.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs index 8edcd9d1c7ce..e6b9fa7117d4 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init/configs.rs @@ -47,9 +47,10 @@ pub async fn init_configs( ecosystem_config: &EcosystemConfig, chain_config: &ChainConfig, ) -> anyhow::Result { + // Port scanner should run before copying configs to avoid marking initial ports as assigned + let mut ecosystem_ports = EcosystemPortsScanner::scan(shell)?; copy_configs(shell, &ecosystem_config.link_to_code, &chain_config.configs)?; - let mut ecosystem_ports = EcosystemPortsScanner::scan(shell)?; if !init_args.no_port_reallocation { ecosystem_ports.allocate_ports_in_yaml( shell,