Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor(zkstack): Preload chain and ecosystem config before each chain subcommand #3322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use anyhow::Context;
use common::{forge::ForgeScriptArgs, logger, spinner::Spinner};
use config::zkstack_config::ZkStackConfig;
use config::ChainConfig;
use xshell::Shell;

use crate::{
accept_ownership::accept_admin,
messages::{
MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_NOT_INITIALIZED, MSG_CHAIN_OWNERSHIP_TRANSFERRED,
MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_OWNERSHIP_TRANSFERRED,
MSG_L1_SECRETS_MUST_BE_PRESENTED,
},
};

pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> {
let ecosystem_config = ZkStackConfig::ecosystem(shell)?;
let chain_config = ecosystem_config
.load_current_chain()
.context(MSG_CHAIN_NOT_INITIALIZED)?;
let contracts = chain_config.get_contracts_config()?;
let secrets = chain_config.get_secrets_config()?;
pub async fn run(args: ForgeScriptArgs, shell: &Shell, chain: ChainConfig) -> anyhow::Result<()> {
let contracts = chain.get_contracts_config()?;
let secrets = chain.get_secrets_config()?;
let l1_rpc_url = secrets
.l1
.context(MSG_L1_SECRETS_MUST_BE_PRESENTED)?
Expand All @@ -28,9 +24,9 @@ pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> {
let spinner = Spinner::new(MSG_ACCEPTING_ADMIN_SPINNER);
accept_admin(
shell,
&chain_config.path_to_foundry(),
&chain.path_to_foundry(),
contracts.l1.chain_admin_addr,
&chain_config.get_wallets_config()?.governor,
&chain.get_wallets_config()?.governor,
contracts.l1.diamond_proxy_addr,
&args,
l1_rpc_url.clone(),
Expand Down
46 changes: 24 additions & 22 deletions zkstack_cli/crates/zkstack/src/commands/chain/build_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Context;
use common::{git, logger, spinner::Spinner};
use config::{
copy_configs, traits::SaveConfigWithBasePath, update_from_chain_config,
zkstack_config::ZkStackConfig,
copy_configs, traits::SaveConfigWithBasePath, update_from_chain_config, ChainConfig,
EcosystemConfig,
};
use ethers::utils::hex::ToHex;
use xshell::Shell;
Expand All @@ -12,9 +12,9 @@ use crate::{
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,
MSG_CHAIN_TXN_OUT_PATH_INVALID_ERR, MSG_PREPARING_CONFIG_SPINNER, MSG_SELECTED_CONFIG,
MSG_BUILDING_CHAIN_REGISTRATION_TXNS_SPINNER, MSG_CHAIN_TRANSACTIONS_BUILT,
MSG_CHAIN_TXN_MISSING_CONTRACT_CONFIG, MSG_CHAIN_TXN_OUT_PATH_INVALID_ERR,
MSG_ECOSYSTEM_CONFIG_INVALID_ERR, MSG_PREPARING_CONFIG_SPINNER, MSG_SELECTED_CONFIG,
MSG_WRITING_OUTPUT_FILES_SPINNER,
},
};
Expand All @@ -27,39 +27,41 @@ const SCRIPT_CONFIG_FILE_SRC: &str =
"contracts/l1-contracts/script-config/register-hyperchain.toml";
const SCRIPT_CONFIG_FILE_DST: &str = "register-hyperchain.toml";

pub(crate) async fn run(args: BuildTransactionsArgs, shell: &Shell) -> anyhow::Result<()> {
let config = ZkStackConfig::ecosystem(shell)?;
let chain_config = config
.load_current_chain()
.context(MSG_CHAIN_NOT_FOUND_ERR)?;
pub(crate) async fn run(
args: BuildTransactionsArgs,
shell: &Shell,
chain: ChainConfig,
ecosystem: Option<EcosystemConfig>,
) -> anyhow::Result<()> {
let ecosystem = ecosystem.context(MSG_ECOSYSTEM_CONFIG_INVALID_ERR)?;

let args = args.fill_values_with_prompt(config.default_chain.clone());
let args = args.fill_values_with_prompt(ecosystem.default_chain.clone());

git::submodule_update(shell, config.link_to_code.clone())?;
git::submodule_update(shell, ecosystem.link_to_code.clone())?;

let spinner = Spinner::new(MSG_PREPARING_CONFIG_SPINNER);
copy_configs(shell, &config.link_to_code, &chain_config.configs)?;
copy_configs(shell, &ecosystem.link_to_code, &chain.configs)?;

logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&chain_config));
logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&chain));

let mut genesis_config = chain_config.get_genesis_config()?;
update_from_chain_config(&mut genesis_config, &chain_config)?;
let mut genesis_config = chain.get_genesis_config()?;
update_from_chain_config(&mut genesis_config, &chain)?;

// Copy ecosystem contracts
let mut contracts_config = config
let mut contracts_config = ecosystem
.get_contracts_config()
.context(MSG_CHAIN_TXN_MISSING_CONTRACT_CONFIG)?;
contracts_config.l1.base_token_addr = chain_config.base_token.address;
contracts_config.l1.base_token_addr = chain.base_token.address;
spinner.finish();

let spinner = Spinner::new(MSG_BUILDING_CHAIN_REGISTRATION_TXNS_SPINNER);
let wallets = config.get_wallets()?;
let wallets = ecosystem.get_wallets()?;
let governor: String = wallets.governor.address.encode_hex_upper();

register_chain(
shell,
args.forge_args.clone(),
&chain_config,
&chain,
&mut contracts_config,
&wallets,
args.l1_rpc_url.clone(),
Expand All @@ -77,12 +79,12 @@ pub(crate) async fn run(args: BuildTransactionsArgs, shell: &Shell) -> anyhow::R
.context(MSG_CHAIN_TXN_OUT_PATH_INVALID_ERR)?;

shell.copy_file(
config.link_to_code.join(REGISTER_CHAIN_TXNS_FILE_SRC),
ecosystem.link_to_code.join(REGISTER_CHAIN_TXNS_FILE_SRC),
args.out.join(REGISTER_CHAIN_TXNS_FILE_DST),
)?;

shell.copy_file(
config.link_to_code.join(SCRIPT_CONFIG_FILE_SRC),
ecosystem.link_to_code.join(SCRIPT_CONFIG_FILE_SRC),
args.out.join(SCRIPT_CONFIG_FILE_DST),
)?;
spinner.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ use config::{
script_params::DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS,
},
traits::{ReadConfig, SaveConfig, SaveConfigWithBasePath},
zkstack_config::ZkStackConfig,
ChainConfig, ContractsConfig, WalletsConfig,
ChainConfig, ContractsConfig, EcosystemConfig, WalletsConfig,
};
use xshell::Shell;
use zksync_basic_types::L2ChainId;

use crate::{
messages::{
MSG_CHAIN_NOT_INITIALIZED, MSG_DEPLOYING_L2_CONTRACT_SPINNER,
MSG_L1_SECRETS_MUST_BE_PRESENTED,
},
messages::{MSG_DEPLOYING_L2_CONTRACT_SPINNER, MSG_L1_SECRETS_MUST_BE_PRESENTED},
utils::forge::{check_the_balance, fill_forge_private_key},
};

Expand All @@ -45,88 +41,40 @@ pub async fn run(
args: ForgeScriptArgs,
shell: &Shell,
deploy_option: Deploy2ContractsOption,
chain: ChainConfig,
ecosystem: EcosystemConfig,
) -> anyhow::Result<()> {
let ecosystem_config = ZkStackConfig::ecosystem(shell)?;
let chain_config = ecosystem_config
.load_current_chain()
.context(MSG_CHAIN_NOT_INITIALIZED)?;

let mut contracts = chain_config.get_contracts_config()?;
let era_chain_id = ecosystem_config.era_chain_id;
let wallets = ecosystem_config.get_wallets()?;
let mut contracts = chain.get_contracts_config()?;
let era_chain_id = ecosystem.era_chain_id;
let wallets = ecosystem.get_wallets()?;

let spinner = Spinner::new(MSG_DEPLOYING_L2_CONTRACT_SPINNER);

match deploy_option {
Deploy2ContractsOption::All => {
deploy_l2_contracts(
shell,
&chain_config,
era_chain_id,
&wallets,
&mut contracts,
args,
)
.await?;
deploy_l2_contracts(shell, &chain, era_chain_id, &wallets, &mut contracts, args)
.await?;
}
Deploy2ContractsOption::Upgrader => {
deploy_upgrader(
shell,
&chain_config,
era_chain_id,
&wallets,
&mut contracts,
args,
)
.await?;
deploy_upgrader(shell, &chain, era_chain_id, &wallets, &mut contracts, args).await?;
}
Deploy2ContractsOption::ConsensusRegistry => {
deploy_consensus_registry(
shell,
&chain_config,
era_chain_id,
&wallets,
&mut contracts,
args,
)
.await?;
deploy_consensus_registry(shell, &chain, era_chain_id, &wallets, &mut contracts, args)
.await?;
}
Deploy2ContractsOption::Multicall3 => {
deploy_multicall3(
shell,
&chain_config,
era_chain_id,
&wallets,
&mut contracts,
args,
)
.await?;
deploy_multicall3(shell, &chain, era_chain_id, &wallets, &mut contracts, args).await?;
}
Deploy2ContractsOption::TimestampAsserter => {
deploy_timestamp_asserter(
shell,
&chain_config,
era_chain_id,
&wallets,
&mut contracts,
args,
)
.await?;
deploy_timestamp_asserter(shell, &chain, era_chain_id, &wallets, &mut contracts, args)
.await?;
}
Deploy2ContractsOption::InitiailizeBridges => {
initialize_bridges(
shell,
&chain_config,
era_chain_id,
&wallets,
&mut contracts,
args,
)
.await?
initialize_bridges(shell, &chain, era_chain_id, &wallets, &mut contracts, args).await?
}
}

contracts.save_with_base_path(shell, &chain_config.configs)?;
contracts.save_with_base_path(shell, &chain.configs)?;
spinner.finish();

Ok(())
Expand Down
19 changes: 10 additions & 9 deletions zkstack_cli/crates/zkstack/src/commands/chain/init/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use common::logger;
use config::{
copy_configs, set_l1_rpc_url,
traits::{ReadConfig, SaveConfigWithBasePath},
update_from_chain_config,
zkstack_config::ZkStackConfig,
ChainConfig, ContractsConfig,
update_from_chain_config, ChainConfig, ContractsConfig, EcosystemConfig,
};
use ethers::types::Address;
use xshell::Shell;
Expand All @@ -19,7 +17,7 @@ use crate::{
portal::update_portal_config,
},
messages::{
MSG_CHAIN_CONFIGS_INITIALIZED, MSG_CHAIN_NOT_FOUND_ERR, MSG_CONSENSUS_CONFIG_MISSING_ERR,
MSG_CHAIN_CONFIGS_INITIALIZED, MSG_CONSENSUS_CONFIG_MISSING_ERR,
MSG_PORTAL_FAILED_TO_CREATE_CONFIG_ERR,
},
utils::{
Expand All @@ -28,12 +26,15 @@ use crate::{
},
};

pub async fn run(args: InitConfigsArgs, shell: &Shell) -> anyhow::Result<()> {
let ecosystem_config = ZkStackConfig::ecosystem(shell)?;
let chain_config = ZkStackConfig::current_chain(shell).context(MSG_CHAIN_NOT_FOUND_ERR)?;
let args = args.fill_values_with_prompt(Some(ecosystem_config), &chain_config)?;
pub async fn run(
args: InitConfigsArgs,
shell: &Shell,
chain: ChainConfig,
ecosystem: Option<EcosystemConfig>,
) -> anyhow::Result<()> {
let args = args.fill_values_with_prompt(ecosystem, &chain)?;

init_configs(&args, shell, &chain_config).await?;
init_configs(&args, shell, &chain).await?;
logger::outro(MSG_CHAIN_CONFIGS_INITIALIZED);

Ok(())
Expand Down
38 changes: 23 additions & 15 deletions zkstack_cli/crates/zkstack/src/commands/chain/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use common::{git, logger, spinner::Spinner};
use config::{
get_default_era_chain_id,
traits::{ReadConfig, SaveConfigWithBasePath},
zkstack_config::ZkStackConfig,
ChainConfig, EcosystemConfig, WalletsConfig,
};
use types::BaseToken;
Expand All @@ -27,10 +26,9 @@ use crate::{
},
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_WALLETS_CONFIG_MUST_BE_PRESENT,
MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND,
MSG_DEPLOYING_PAYMASTER, MSG_GENESIS_DATABASE_ERR, MSG_REGISTERING_CHAIN_SPINNER,
MSG_SELECTED_CONFIG, MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER,
MSG_WALLETS_CONFIG_MUST_BE_PRESENT, MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND,
},
};

Expand All @@ -52,23 +50,33 @@ pub struct ChainInitCommand {
args: InitArgs,
}

pub(crate) async fn run(args: ChainInitCommand, shell: &Shell) -> anyhow::Result<()> {
pub(crate) async fn run(
args: ChainInitCommand,
shell: &Shell,
chain: ChainConfig,
ecosystem: Option<EcosystemConfig>,
) -> anyhow::Result<()> {
match args.command {
Some(ChainInitSubcommands::Configs(args)) => configs::run(args, shell).await,
None => run_init(args.args, shell).await,
Some(ChainInitSubcommands::Configs(args)) => {
configs::run(args, shell, chain, ecosystem).await
}
None => run_init(args.args, shell, chain, ecosystem).await,
}
}

async fn run_init(args: InitArgs, shell: &Shell) -> anyhow::Result<()> {
let ecosystem = ZkStackConfig::ecosystem(shell).ok();
let chain_config = ZkStackConfig::current_chain(shell).context(MSG_CHAIN_NOT_FOUND_ERR)?;
let args = args.fill_values_with_prompt(ecosystem.clone(), &chain_config)?;
async fn run_init(
args: InitArgs,
shell: &Shell,
chain: ChainConfig,
ecosystem: Option<EcosystemConfig>,
) -> anyhow::Result<()> {
let args = args.fill_values_with_prompt(ecosystem.clone(), &chain)?;

logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&chain_config));
logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&chain));
logger::info(msg_initializing_chain(""));
git::submodule_update(shell, chain_config.link_to_code.clone())?;
git::submodule_update(shell, chain.link_to_code.clone())?;

init(&args, shell, ecosystem, &chain_config).await?;
init(&args, shell, ecosystem, &chain).await?;

logger::success(MSG_CHAIN_INITIALIZED);
Ok(())
Expand Down
Loading