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

feat(zk_toolbox): Add update command #2440

Merged
merged 40 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e5011d1
Add update command
matias-gonz Jul 16, 2024
da3be62
Add merge_yaml
matias-gonz Jul 18, 2024
6e729e9
Add save_updated_config
matias-gonz Jul 18, 2024
53fe0d5
Fix diff
matias-gonz Jul 18, 2024
ee9c4eb
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 19, 2024
bd18341
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 23, 2024
29213c2
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 24, 2024
67a86f6
Update all chains
matias-gonz Jul 24, 2024
eb7da9f
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 24, 2024
e13a204
Update name
matias-gonz Jul 24, 2024
fe4564b
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 24, 2024
dbf7528
Merge branch 'matias-zk-toolbox-update' of github.com:matter-labs/zks…
matias-gonz Jul 24, 2024
0573e91
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 25, 2024
263d17b
Merge branch 'matias-zk-toolbox-update' of github.com:matter-labs/zks…
matias-gonz Jul 25, 2024
6d3ca5e
Use shell instead of std
matias-gonz Jul 25, 2024
5178d9c
Make constants public
matias-gonz Jul 25, 2024
bf47f8b
Add check_diff for genesis config
matias-gonz Jul 25, 2024
33795be
Add Diff.print()
matias-gonz Jul 25, 2024
0ac6ff3
Add warnings for contracts and secrets
matias-gonz Jul 25, 2024
a18b204
Add update external node config
matias-gonz Jul 25, 2024
1f87c95
Add update_config
matias-gonz Jul 26, 2024
e6ab62c
Add merge_yaml tests
matias-gonz Jul 26, 2024
3e98763
Add test
matias-gonz Jul 26, 2024
4223adc
Add parent keys to diff
matias-gonz Jul 26, 2024
923de1a
Merge branch 'main' of github.com:matter-labs/zksync-era into matias-…
matias-gonz Jul 26, 2024
f7f55b0
Update ConfigDiff
matias-gonz Jul 26, 2024
4b3330c
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 26, 2024
507d22d
Refactor merge_yaml
matias-gonz Jul 26, 2024
7ef4d35
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 26, 2024
48186f5
Update path_to_external_node_config
matias-gonz Jul 26, 2024
255bf65
Merge branch 'matias-zk-toolbox-update' of github.com:matter-labs/zks…
matias-gonz Jul 26, 2024
55e3c95
Update GeneralConfig.print
matias-gonz Jul 26, 2024
74b6fad
add general
Deniallugo Jul 26, 2024
9b87baf
Update
Deniallugo Jul 26, 2024
c01e4df
Use git pull from origin branch
Deniallugo Jul 26, 2024
7207656
Add only-config argument
matias-gonz Jul 26, 2024
5fd4e6e
Add support for EN general config
matias-gonz Jul 26, 2024
f822a94
Merge branch 'main' into matias-zk-toolbox-update
matias-gonz Jul 26, 2024
73a2ce4
Merge pull request #2514 from matter-labs/deniallugo-tests
matias-gonz Jul 26, 2024
cf633e7
Lint
matias-gonz Jul 26, 2024
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
6 changes: 6 additions & 0 deletions zk_toolbox/crates/common/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ pub fn submodule_update(shell: &Shell, link_to_code: PathBuf) -> anyhow::Result<
.run()?;
Ok(())
}

pub fn pull(shell: &Shell, link_to_code: PathBuf) -> anyhow::Result<()> {
let _dir_guard = shell.push_dir(link_to_code);
Cmd::new(cmd!(shell, "git pull")).run()?;
Ok(())
}
4 changes: 4 additions & 0 deletions zk_toolbox/crates/config/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ impl ChainConfig {
self.configs.join(GENERAL_FILE)
}

pub fn path_to_genesis_config(&self) -> PathBuf {
self.configs.join(GENESIS_FILE)
}

pub fn path_to_secrets_config(&self) -> PathBuf {
self.configs.join(SECRETS_FILE)
}
Expand Down
12 changes: 6 additions & 6 deletions zk_toolbox/crates/config/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ use types::ChainId;
/// Name of the main configuration file
pub(crate) const CONFIG_NAME: &str = "ZkStack.yaml";
/// Name of the wallets file
pub(crate) const WALLETS_FILE: &str = "wallets.yaml";
pub const WALLETS_FILE: &str = "wallets.yaml";
/// Name of the secrets config file
pub(crate) const SECRETS_FILE: &str = "secrets.yaml";
pub const SECRETS_FILE: &str = "secrets.yaml";
/// Name of the general config file
pub(crate) const GENERAL_FILE: &str = "general.yaml";
pub const GENERAL_FILE: &str = "general.yaml";
/// Name of the genesis config file
pub(crate) const GENESIS_FILE: &str = "genesis.yaml";
pub const GENESIS_FILE: &str = "genesis.yaml";

// Name of external node specific config
pub(crate) const EN_CONFIG_FILE: &str = "external_node.yaml";
pub const EN_CONFIG_FILE: &str = "external_node.yaml";
pub(crate) const ERC20_CONFIGS_FILE: &str = "erc20.yaml";
/// Name of the initial deployments config file
pub(crate) const INITIAL_DEPLOYMENT_FILE: &str = "initial_deployments.yaml";
/// Name of the erc20 deployments config file
pub(crate) const ERC20_DEPLOYMENT_FILE: &str = "erc20_deployments.yaml";
/// Name of the contracts file
pub(crate) const CONTRACTS_FILE: &str = "contracts.yaml";
pub const CONTRACTS_FILE: &str = "contracts.yaml";
/// Main repository for the ZKsync project
pub const ZKSYNC_ERA_GIT_REPO: &str = "https://github.com/matter-labs/zksync-era";
/// Name of the docker-compose file inside zksync repository
Expand Down
2 changes: 1 addition & 1 deletion zk_toolbox/crates/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use chain::*;
pub use consts::{DOCKER_COMPOSE_FILE, ZKSYNC_ERA_GIT_REPO};
pub use consts::*;
pub use contracts::*;
pub use ecosystem::*;
pub use file_config::*;
Expand Down
1 change: 1 addition & 0 deletions zk_toolbox/crates/zk_inception/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod ecosystem;
pub mod external_node;
pub mod prover;
pub mod server;
pub mod update;
155 changes: 155 additions & 0 deletions zk_toolbox/crates/zk_inception/src/commands/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use std::path::Path;

use anyhow::Context;
use common::{
git::{pull, submodule_update},
logger,
spinner::Spinner,
};
use config::{ChainConfig, EcosystemConfig, GENERAL_FILE, GENESIS_FILE};
use xshell::Shell;

use crate::messages::{
msg_diff_genesis_config, msg_updating_chain, MSG_CHAIN_NOT_FOUND_ERR,
MSG_PULLING_ZKSYNC_CODE_SPINNER, MSG_SHOW_DIFF, MSG_SHOW_DIFF_ADDED_FIELDS,
MSG_UPDATING_GENERAL_CONFIG, MSG_UPDATING_SUBMODULES_SPINNER, MSG_UPDATING_ZKSYNC,
MSG_ZKSYNC_UPDATED,
};

#[derive(Default)]
struct ConfigDiff {
pub value_diff: serde_yaml::Mapping,
pub added_fields: serde_yaml::Mapping,
}
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved

pub fn run(shell: &Shell) -> anyhow::Result<()> {
logger::info(MSG_UPDATING_ZKSYNC);
let ecosystem = EcosystemConfig::from_file(shell)?;
let link_to_code = ecosystem.link_to_code.clone();

let spinner = Spinner::new(MSG_PULLING_ZKSYNC_CODE_SPINNER);
pull(shell, link_to_code.clone())?;
spinner.finish();
let spinner = Spinner::new(MSG_UPDATING_SUBMODULES_SPINNER);
submodule_update(shell, link_to_code.clone())?;
spinner.finish();

let general_config = serde_yaml::from_str(
&shell.read_file(ecosystem.get_default_configs_path().join(GENERAL_FILE))?,
)?;

let genesis_config = serde_yaml::from_str(
&shell.read_file(ecosystem.get_default_configs_path().join(GENESIS_FILE))?,
)?;

for chain in ecosystem.list_of_chains() {
logger::info(msg_updating_chain(&chain));
let chain = ecosystem
.load_chain(Some(chain))
.context(MSG_CHAIN_NOT_FOUND_ERR)?;
update_chain(shell, &chain, &general_config, &genesis_config)?;
}

logger::outro(MSG_ZKSYNC_UPDATED);

Ok(())
}

fn merge_yaml_internal(
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
a: &mut serde_yaml::Value,
b: serde_yaml::Value,
current_key: serde_yaml::Value,
diff: &mut ConfigDiff,
) -> anyhow::Result<()> {
match (a, b) {
(serde_yaml::Value::Mapping(a), serde_yaml::Value::Mapping(b)) => {
for (key, value) in b {
if a.contains_key(&key) {
merge_yaml_internal(a.get_mut(&key).unwrap(), value, key, diff)?;
} else {
a.insert(key.clone(), value.clone());
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
diff.added_fields.insert(key, value);
}
}
}
(a, b) => {
if a != &b {
diff.value_diff.insert(current_key, b);
}
}
}
Ok(())
}

fn merge_yaml(a: &mut serde_yaml::Value, b: serde_yaml::Value) -> anyhow::Result<ConfigDiff> {
let mut diff = ConfigDiff::default();
merge_yaml_internal(a, b, "".into(), &mut diff)?;
Ok(diff)
}

fn save_updated_config(
shell: &Shell,
config: serde_yaml::Value,
path: &Path,
diff: ConfigDiff,
) -> anyhow::Result<()> {
if diff.added_fields.is_empty() {
return Ok(());
}

logger::info(MSG_SHOW_DIFF_ADDED_FIELDS);
for (key, value) in diff.added_fields {
let key = key.as_str().unwrap();
logger::info(format!("{}: {:?}", key, value));
}

let general_config = serde_yaml::to_string(&config)?;
shell.write_file(path, general_config)?;

Ok(())
}

fn check_diff(diff: &ConfigDiff, msg: &str) -> anyhow::Result<()> {
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
if diff.value_diff.is_empty() && diff.added_fields.is_empty() {
return Ok(());
}

logger::warn(msg);
logger::info(MSG_SHOW_DIFF);
for (key, value) in diff.added_fields.iter() {
let key = key.as_str().unwrap();
logger::info(format!("{}: {:?}", key, value));
}
for (key, value) in diff.value_diff.iter() {
let key = key.as_str().unwrap();
logger::info(format!("{}: {:?}", key, value));
}

Ok(())
}

fn update_chain(
shell: &Shell,
chain: &ChainConfig,
general: &serde_yaml::Value,
genesis: &serde_yaml::Value,
) -> anyhow::Result<()> {
logger::info(MSG_UPDATING_GENERAL_CONFIG);
let current_general_config_path = chain.path_to_general_config();
let mut current_general_config =
serde_yaml::from_str(&shell.read_file(current_general_config_path.clone())?)?;
let diff = merge_yaml(&mut current_general_config, general.clone())?;
save_updated_config(
shell,
current_general_config,
&current_general_config_path,
diff,
)?;

let mut current_genesis_config =
serde_yaml::from_str(&shell.read_file(chain.path_to_genesis_config())?)?;
let diff = merge_yaml(&mut current_genesis_config, genesis.clone())?;
check_diff(&diff, &msg_diff_genesis_config(&chain.name))?;

Ok(())
}
3 changes: 3 additions & 0 deletions zk_toolbox/crates/zk_inception/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub enum InceptionSubcommands {
/// Run contract verifier
#[command(subcommand)]
ContractVerifier(ContractVerifierCommands),
/// Update zkSync
Update,
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -110,6 +112,7 @@ async fn run_subcommand(inception_args: Inception, shell: &Shell) -> anyhow::Res
InceptionSubcommands::ContractVerifier(args) => {
commands::contract_verifier::run(shell, args).await?
}
InceptionSubcommands::Update => commands::update::run(shell)?,
}
Ok(())
}
Expand Down
21 changes: 21 additions & 0 deletions zk_toolbox/crates/zk_inception/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,24 @@ pub(super) fn msg_binary_already_exists(name: &str, version: &str) -> String {
pub(super) fn msg_downloading_binary_spinner(name: &str, version: &str) -> String {
format!("Downloading {} {} binary", name, version)
}

/// Update related messages

pub(super) const MSG_UPDATING_ZKSYNC: &str = "Updating ZkSync";
pub(super) const MSG_ZKSYNC_UPDATED: &str = "ZkSync updated successfully";
pub(super) const MSG_PULLING_ZKSYNC_CODE_SPINNER: &str = "Pulling zksync-era repo...";
pub(super) const MSG_UPDATING_SUBMODULES_SPINNER: &str = "Updating submodules...";
pub(super) const MSG_UPDATING_GENERAL_CONFIG: &str = "Updating general config";
pub(super) const MSG_SHOW_DIFF_ADDED_FIELDS: &str = "Added the following values:";
pub(super) const MSG_SHOW_DIFF: &str = "Found differences in the following fields:";

pub(super) fn msg_diff_genesis_config(chain: &str) -> String {
format!(
"Found differences between chain {} and era genesis configs. Consider updating the chain {} genesis config and re-running genesis.",
chain, chain
)
}

pub(super) fn msg_updating_chain(chain: &str) -> String {
format!("Updating chain: {}", chain)
}
Loading