Skip to content

Commit

Permalink
added ecosystem-only init
Browse files Browse the repository at this point in the history
  • Loading branch information
sanekmelnikov committed Sep 24, 2024
1 parent 93bc66f commit 859ce92
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -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(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl ChainCreateArgs {
.ask()
});

let wallet_path: Option<PathBuf> = if self.wallet_creation == Some(WalletCreation::InFile) {
let wallet_path: Option<PathBuf> = if wallet_creation == WalletCreation::InFile {
Some(self.wallet_path.unwrap_or_else(|| {
Prompt::new(MSG_WALLET_PATH_PROMPT)
.validate_with(|val: &String| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub struct InitArgs {
pub l1_rpc_url: Option<String>,
#[clap(long, help = MSG_PORT_OFFSET_HELP)]
pub port_offset: Option<PortOffset>,
/// Only create chain configs
#[clap(long, default_value_t = false)]
pub configs_only: bool,
}

impl InitArgs {
Expand Down Expand Up @@ -90,6 +93,7 @@ impl InitArgs {
.port_offset
.unwrap_or(PortOffset::from_chain_id(config.id as u16))
.into(),
configs_only: self.configs_only,
}
}
}
Expand All @@ -101,4 +105,5 @@ pub struct InitArgsFinal {
pub deploy_paymaster: bool,
pub l1_rpc_url: String,
pub port_offset: u16,
pub configs_only: bool,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
58 changes: 2 additions & 56 deletions zk_toolbox/crates/zk_inception/src/commands/chain/common.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
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(&REGISTER_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(&register_chain_output);
Ok(())
}

// Distribute eth to the chain wallets for localhost environment
pub async fn distribute_eth(
ecosystem_config: &EcosystemConfig,
Expand Down
93 changes: 62 additions & 31 deletions zk_toolbox/crates/zk_inception/src/commands/chain/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ 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::{
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,
},
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,
},
Expand All @@ -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(())
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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<ContractsConfig> {
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");
Expand Down
Loading

0 comments on commit 859ce92

Please sign in to comment.