From 63ba8e7aa7d6aa6272b0045c01d51376bd4f89d8 Mon Sep 17 00:00:00 2001 From: Joaquin Carletti <56092489+ColoCarletti@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:26:12 -0300 Subject: [PATCH] fix(prover_cli): Fix the issues with `home` path (#2104) In this PR: [Pull Request #2022](https://github.com/matter-labs/zksync-era/pull/2022/files), the logic was changed to stop using the $ZKSYNC_HOME environment variable to construct paths relative to the root of zksync-era. But the prover is a separate workspace, so it fails to create the path to the contract with the functions in the main workspace.. ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [x] Code has been formatted via `zk fmt` and `zk lint`. - [x] Spellcheck has been run via `zk spellcheck`. --- etc/pliconfig | 1 + prover/Cargo.lock | 2 + prover/prover_cli/Cargo.toml | 2 + prover/prover_cli/src/commands/status/l1.rs | 13 +++--- prover/prover_cli/src/config/mod.rs | 14 +++--- prover/prover_cli/src/helper.rs | 47 +++++++++++++++++++++ prover/prover_cli/src/lib.rs | 1 + 7 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 etc/pliconfig create mode 100644 prover/prover_cli/src/helper.rs diff --git a/etc/pliconfig b/etc/pliconfig new file mode 100644 index 000000000000..c1ec05c0137f --- /dev/null +++ b/etc/pliconfig @@ -0,0 +1 @@ +PLI__DB_URL=postgres://postgres:notsecurepassword@localhost/prover_local diff --git a/prover/Cargo.lock b/prover/Cargo.lock index 79858448a034..d2de12c5682e 100644 --- a/prover/Cargo.lock +++ b/prover/Cargo.lock @@ -4982,6 +4982,7 @@ dependencies = [ "dialoguer", "hex", "prover_dal", + "serde_json", "sqlx", "strum", "tokio", @@ -4998,6 +4999,7 @@ dependencies = [ "zksync_prover_fri_types", "zksync_prover_interface", "zksync_types", + "zksync_utils", ] [[package]] diff --git a/prover/prover_cli/Cargo.toml b/prover/prover_cli/Cargo.toml index ca6a4d2dd65d..cca26f76113a 100644 --- a/prover/prover_cli/Cargo.toml +++ b/prover/prover_cli/Cargo.toml @@ -29,10 +29,12 @@ prover_dal.workspace = true zksync_eth_client.workspace = true zksync_contracts.workspace = true zksync_dal.workspace = true +zksync_utils.workspace = true strum.workspace = true colored.workspace = true sqlx.workspace = true circuit_definitions.workspace = true +serde_json.workspace = true zkevm_test_harness = { workspace = true, optional = true, features = ["verbose_circuits"] } [features] diff --git a/prover/prover_cli/src/commands/status/l1.rs b/prover/prover_cli/src/commands/status/l1.rs index 5488b1d2f47e..d02e545a4178 100644 --- a/prover/prover_cli/src/commands/status/l1.rs +++ b/prover/prover_cli/src/commands/status/l1.rs @@ -15,6 +15,8 @@ use zksync_eth_client::{ CallFunctionArgs, }; +use crate::helper; + pub(crate) async fn run() -> anyhow::Result<()> { println!(" ====== L1 Status ====== "); let postgres_config = PostgresConfig::from_env().context("PostgresConfig::from_env")?; @@ -27,7 +29,7 @@ pub(crate) async fn run() -> anyhow::Result<()> { let total_batches_committed: U256 = CallFunctionArgs::new("getTotalBatchesCommitted", ()) .for_contract( contracts_config.diamond_proxy_addr, - &zksync_contracts::hyperchain_contract(), + &helper::hyperchain_contract(), ) .call(&query_client) .await?; @@ -35,7 +37,7 @@ pub(crate) async fn run() -> anyhow::Result<()> { let total_batches_verified: U256 = CallFunctionArgs::new("getTotalBatchesVerified", ()) .for_contract( contracts_config.diamond_proxy_addr, - &zksync_contracts::hyperchain_contract(), + &helper::hyperchain_contract(), ) .call(&query_client) .await?; @@ -74,17 +76,14 @@ pub(crate) async fn run() -> anyhow::Result<()> { ); let node_verification_key_hash: H256 = CallFunctionArgs::new("verificationKeyHash", ()) - .for_contract( - contracts_config.verifier_addr, - &zksync_contracts::verifier_contract(), - ) + .for_contract(contracts_config.verifier_addr, &helper::verifier_contract()) .call(&query_client) .await?; let node_verifier_params: VerifierParams = CallFunctionArgs::new("getVerifierParams", ()) .for_contract( contracts_config.diamond_proxy_addr, - &zksync_contracts::hyperchain_contract(), + &helper::hyperchain_contract(), ) .call(&query_client) .await?; diff --git a/prover/prover_cli/src/config/mod.rs b/prover/prover_cli/src/config/mod.rs index 452e1ad9ce01..93af17317c5d 100644 --- a/prover/prover_cli/src/config/mod.rs +++ b/prover/prover_cli/src/config/mod.rs @@ -1,10 +1,12 @@ -use std::io::Write; +use std::{io::Write, path::PathBuf}; -pub fn get_envfile() -> anyhow::Result { +use crate::helper::core_workspace_dir_or_current_dir; + +pub fn get_envfile() -> anyhow::Result { if let Ok(envfile) = std::env::var("PLI__CONFIG") { - return Ok(envfile); + return Ok(envfile.into()); } - Ok(std::env::var("ZKSYNC_HOME").map(|home| home + "/etc/pliconfig")?) + Ok(core_workspace_dir_or_current_dir().join("etc/pliconfig")) } pub fn load_envfile(path: impl AsRef) -> anyhow::Result<()> { @@ -13,7 +15,6 @@ pub fn load_envfile(path: impl AsRef) -> anyhow::Result<()> { .filter(|l| !l.starts_with('#')) .filter_map(|l| l.split_once('=')) .for_each(|(k, v)| std::env::set_var(k, v)); - Ok(()) } @@ -28,7 +29,8 @@ pub fn update_envfile( let mut out = std::io::BufWriter::new(std::fs::File::create_new(&swapfile)?); let mut found = false; - std::fs::read_to_string(path)? + std::fs::read_to_string(path) + .unwrap_or_default() .lines() .map(|l| { if l.starts_with(&prefix) { diff --git a/prover/prover_cli/src/helper.rs b/prover/prover_cli/src/helper.rs new file mode 100644 index 000000000000..352a789baed7 --- /dev/null +++ b/prover/prover_cli/src/helper.rs @@ -0,0 +1,47 @@ +use std::{ + fs::File, + path::{Path, PathBuf}, +}; + +use zksync_types::ethabi::Contract; +use zksync_utils::locate_workspace; + +const ZKSYNC_HYPERCHAIN_CONTRACT_FILE: &str = + "contracts/l1-contracts/artifacts/contracts/state-transition/chain-interfaces/IZkSyncHyperchain.sol/IZkSyncHyperchain.json"; +const VERIFIER_CONTRACT_FILE: &str = + "contracts/l1-contracts/artifacts/contracts/state-transition/Verifier.sol/Verifier.json"; + +pub fn hyperchain_contract() -> Contract { + load_contract_if_present(ZKSYNC_HYPERCHAIN_CONTRACT_FILE) +} + +pub fn verifier_contract() -> Contract { + load_contract_if_present(VERIFIER_CONTRACT_FILE) +} + +fn read_file_to_json_value(path: &PathBuf) -> serde_json::Value { + serde_json::from_reader( + File::open(path).unwrap_or_else(|e| panic!("Failed to open file {:?}: {}", path, e)), + ) + .unwrap_or_else(|e| panic!("Failed to parse file {:?}: {}", path, e)) +} + +fn load_contract_if_present(path: &str) -> Contract { + let home = core_workspace_dir_or_current_dir(); + let path = Path::new(&home).join(path); + path.exists() + .then(|| { + serde_json::from_value(read_file_to_json_value(&path)["abi"].take()).unwrap_or_else( + |e| panic!("Failed to parse contract abi from file {:?}: {}", path, e), + ) + }) + .unwrap_or_else(|| { + panic!("Failed to load contract from {:?}", path); + }) +} + +pub fn core_workspace_dir_or_current_dir() -> PathBuf { + locate_workspace() + .map(|a| a.join("..")) + .unwrap_or_else(|| PathBuf::from(".")) +} diff --git a/prover/prover_cli/src/lib.rs b/prover/prover_cli/src/lib.rs index 3a441e45bdeb..7c1df326d396 100644 --- a/prover/prover_cli/src/lib.rs +++ b/prover/prover_cli/src/lib.rs @@ -1,3 +1,4 @@ pub mod cli; pub mod commands; pub mod config; +pub mod helper;