Skip to content

Commit

Permalink
feat(zk_toolbox): Implement default upgrader deployment
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Jul 29, 2024
1 parent a12699a commit 1812312
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 120 deletions.
11 changes: 10 additions & 1 deletion zk_toolbox/crates/config/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
consts::CONTRACTS_FILE,
forge_interface::{
deploy_ecosystem::output::DeployL1Output,
initialize_bridges::output::InitializeBridgeOutput,
deploy_l2_contracts::output::{DefaultL2UpgradeOutput, InitializeBridgeOutput},
register_chain::output::RegisterChainOutput,
},
traits::{FileConfigWithDefaultName, ZkToolboxConfig},
Expand Down Expand Up @@ -83,6 +83,14 @@ impl ContractsConfig {
self.bridges.erc20.l2_address = Some(initialize_bridges_output.l2_shared_bridge_proxy);
Ok(())
}

pub fn set_default_l2_upgrade(
&mut self,
default_upgrade_output: &DefaultL2UpgradeOutput,
) -> anyhow::Result<()> {
self.l2.default_l2_upgrader = default_upgrade_output.l2_default_upgrader;
Ok(())
}
}

impl FileConfigWithDefaultName for ContractsConfig {
Expand Down Expand Up @@ -131,4 +139,5 @@ pub struct L1Contracts {
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct L2Contracts {
pub testnet_paymaster_addr: Address,
pub default_l2_upgrader: Address,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use zksync_basic_types::L2ChainId;

use crate::{traits::ZkToolboxConfig, ChainConfig};

impl ZkToolboxConfig for InitializeBridgeInput {}
impl ZkToolboxConfig for DeployL2ContractsInput {}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InitializeBridgeInput {
pub struct DeployL2ContractsInput {
pub era_chain_id: L2ChainId,
pub chain_id: L2ChainId,
pub l1_shared_bridge: Address,
Expand All @@ -16,7 +16,7 @@ pub struct InitializeBridgeInput {
pub erc20_bridge: Address,
}

impl InitializeBridgeInput {
impl DeployL2ContractsInput {
pub fn new(chain_config: &ChainConfig, era_chain_id: L2ChainId) -> anyhow::Result<Self> {
let contracts = chain_config.get_contracts_config()?;
let wallets = chain_config.get_wallets_config()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ use crate::traits::ZkToolboxConfig;

impl ZkToolboxConfig for InitializeBridgeOutput {}

impl ZkToolboxConfig for DefaultL2UpgradeOutput {}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InitializeBridgeOutput {
pub l2_shared_bridge_implementation: Address,
pub l2_shared_bridge_proxy: Address,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DefaultL2UpgradeOutput {
pub l2_default_upgrader: Address,
}
2 changes: 1 addition & 1 deletion zk_toolbox/crates/config/src/forge_interface/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod accept_ownership;
pub mod deploy_ecosystem;
pub mod initialize_bridges;
pub mod deploy_l2_contracts;
pub mod paymaster;
pub mod register_chain;
pub mod script_params;
8 changes: 4 additions & 4 deletions zk_toolbox/crates/config/src/forge_interface/script_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub const DEPLOY_ECOSYSTEM_SCRIPT_PARAMS: ForgeScriptParams = ForgeScriptParams
script_path: "deploy-scripts/DeployL1.s.sol",
};

pub const INITIALIZE_BRIDGES_SCRIPT_PARAMS: ForgeScriptParams = ForgeScriptParams {
input: "script-config/config-initialize-shared-bridges.toml",
output: "script-out/output-initialize-shared-bridges.toml",
script_path: "deploy-scripts/InitializeSharedBridgeOnL2.sol",
pub const DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS: ForgeScriptParams = ForgeScriptParams {
input: "script-config/config-deploy-l2-contracts.toml",
output: "script-out/output-deploy-l2-contracts.toml",
script_path: "deploy-scripts/DeployL2Contracts.sol",
};

pub const REGISTER_CHAIN_SCRIPT_PARAMS: ForgeScriptParams = ForgeScriptParams {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
use std::path::Path;

use anyhow::Context;
use common::{
cmd::Cmd,
config::global_config,
forge::{Forge, ForgeScriptArgs},
spinner::Spinner,
};
use config::{
forge_interface::{
deploy_l2_contracts::{
input::DeployL2ContractsInput,
output::{DefaultL2UpgradeOutput, InitializeBridgeOutput},
},
script_params::DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS,
},
traits::{ReadConfig, SaveConfig, SaveConfigWithBasePath},
ChainConfig, ContractsConfig, EcosystemConfig,
};
use xshell::{cmd, Shell};

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

pub enum Deploy2ContractsOption {
All,
Upgrader,
IntiailizeBridges,
}

pub async fn run(
args: ForgeScriptArgs,
shell: &Shell,
deploy_option: Deploy2ContractsOption,
) -> 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 spinner = Spinner::new(MSG_DEPLOYING_L2_CONTRACT_SPINNER);

match deploy_option {
Deploy2ContractsOption::All => {
deploy_l2_contracts(
shell,
&chain_config,
&ecosystem_config,
&mut contracts,
args,
)
.await?;
}
Deploy2ContractsOption::Upgrader => {
deploy_upgrader(
shell,
&chain_config,
&ecosystem_config,
&mut contracts,
args,
)
.await?;
}
Deploy2ContractsOption::IntiailizeBridges => initialize_bridges(
shell,
&chain_config,
&ecosystem_config,
&mut contracts,
args,
),
}

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

Ok(())
}

pub async fn initialize_bridges(
shell: &Shell,
chain_config: &ChainConfig,
ecosystem_config: &EcosystemConfig,
contracts_config: &mut ContractsConfig,
forge_args: ForgeScriptArgs,
) -> anyhow::Result<()> {
build_l2_contracts(shell, &ecosystem_config.link_to_code)?;
call_forge(
shell,
chain_config,
ecosystem_config,
forge_args,
Some("runDeploySharedBridge"),
)
.await?;
let output = InitializeBridgeOutput::read(
shell,
DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS.output(&chain_config.link_to_code),
)?;

contracts_config.set_l2_shared_bridge(&output)?;
Ok(())
}

pub async fn deploy_upgrader(
shell: &Shell,
chain_config: &ChainConfig,
ecosystem_config: &EcosystemConfig,
contracts_config: &mut ContractsConfig,
forge_args: ForgeScriptArgs,
) -> anyhow::Result<()> {
build_l2_contracts(shell, &ecosystem_config.link_to_code)?;
call_forge(
shell,
chain_config,
ecosystem_config,
forge_args,
Some("runDefaultUpgrader"),
)
.await?;
let output = DefaultL2UpgradeOutput::read(
shell,
DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS.output(&chain_config.link_to_code),
)?;

contracts_config.set_default_l2_upgrade(&output)?;
Ok(())
}

pub async fn deploy_l2_contracts(
shell: &Shell,
chain_config: &ChainConfig,
ecosystem_config: &EcosystemConfig,
contracts_config: &mut ContractsConfig,
forge_args: ForgeScriptArgs,
) -> anyhow::Result<()> {
build_l2_contracts(shell, &ecosystem_config.link_to_code)?;
call_forge(shell, chain_config, ecosystem_config, forge_args, None).await?;
let output = InitializeBridgeOutput::read(
shell,
DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS.output(&chain_config.link_to_code),
)?;

contracts_config.set_l2_shared_bridge(&output)?;

let output = DefaultL2UpgradeOutput::read(
shell,
DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS.output(&chain_config.link_to_code),
)?;

contracts_config.set_default_l2_upgrade(&output)?;

Ok(())
}

async fn call_forge(
shell: &Shell,
chain_config: &ChainConfig,
ecosystem_config: &EcosystemConfig,
forge_args: ForgeScriptArgs,
signature: Option<&str>,
) -> anyhow::Result<()> {
let input = DeployL2ContractsInput::new(chain_config, ecosystem_config.era_chain_id)?;
let foundry_contracts_path = chain_config.path_to_foundry();
let secrets = chain_config.get_secrets_config()?;
input.save(
shell,
DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS.input(&chain_config.link_to_code),
)?;

let mut forge = Forge::new(&foundry_contracts_path)
.script(
&DEPLOY_L2_CONTRACTS_SCRIPT_PARAMS.script(),
forge_args.clone(),
)
.with_ffi()
.with_rpc_url(
secrets
.l1
.context(MSG_L1_SECRETS_MUST_BE_PRESENTED)?
.l1_rpc_url
.expose_str()
.to_string(),
)
.with_broadcast();

if let Some(signature) = signature {
forge = forge.with_signature(signature);
}

forge = fill_forge_private_key(
forge,
ecosystem_config.get_wallets()?.governor_private_key(),
)?;

check_the_balance(&forge).await?;
forge.run(shell)?;
Ok(())
}

fn build_l2_contracts(shell: &Shell, link_to_code: &Path) -> anyhow::Result<()> {
let _dir_guard = shell.push_dir(link_to_code.join("contracts"));
Ok(Cmd::new(cmd!(shell, "yarn l2 build")).run()?)
}
5 changes: 2 additions & 3 deletions zk_toolbox/crates/zk_inception/src/commands/chain/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ use crate::{
accept_ownership::accept_admin,
commands::chain::{
args::init::{InitArgs, InitArgsFinal},
deploy_paymaster,
deploy_l2_contracts, deploy_paymaster,
genesis::genesis,
initialize_bridges,
},
messages::{
msg_initializing_chain, MSG_ACCEPTING_ADMIN_SPINNER, MSG_CHAIN_INITIALIZED,
Expand Down Expand Up @@ -103,7 +102,7 @@ pub async fn init(
.await?;
spinner.finish();

initialize_bridges::initialize_bridges(
deploy_l2_contracts::deploy_l2_contracts(
shell,
chain_config,
ecosystem_config,
Expand Down
Loading

0 comments on commit 1812312

Please sign in to comment.