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 9 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(())
}
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;
116 changes: 116 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,116 @@
use std::path::Path;

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

use crate::{
consts::GENERAL_FILE,
messages::{
msg_updating_chain, MSG_CHAIN_NOT_FOUND_ERR, MSG_PULLING_ZKSYNC_CODE_SPINNER,
MSG_SHOW_DIFF, 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 updated_config_path = ecosystem.get_default_configs_path().join(GENERAL_FILE);
let general_config = serde_yaml::from_reader(std::fs::File::open(updated_config_path)?)?;

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(&chain, &general_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(
config: serde_yaml::Value,
path: &Path,
diff: ConfigDiff,
) -> anyhow::Result<()> {
if diff.added_fields.is_empty() {
return Ok(());
}

logger::info(MSG_SHOW_DIFF);
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)?;
std::fs::write(path, general_config)?;
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

fn update_chain(chain: &ChainConfig, general_config: &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_reader(std::fs::File::open(current_general_config_path.clone())?)?;
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
let diff = merge_yaml(&mut current_general_config, general_config.clone())?;
save_updated_config(current_general_config, &current_general_config_path, diff)?;
Ok(())
}
2 changes: 2 additions & 0 deletions zk_toolbox/crates/zk_inception/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pub const PROVER_STORE_MAX_RETRIES: u16 = 10;
pub const DEFAULT_CREDENTIALS_FILE: &str = "~/.config/gcloud/application_default_credentials.json";
pub const DEFAULT_PROOF_STORE_DIR: &str = "artifacts";
pub const BELLMAN_CUDA_DIR: &str = "era-bellman-cuda";

pub const GENERAL_FILE: &str = "general.yaml";
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 13 additions & 0 deletions zk_toolbox/crates/zk_inception/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,16 @@ 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: &str = "Added the following values:";

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