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(toolbox): refactor config to its own crate #2063

Merged
merged 13 commits into from
May 30, 2024
33 changes: 32 additions & 1 deletion zk_toolbox/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion zk_toolbox/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[workspace]
members = ["crates/common",
members = [
"crates/common",
"crates/config",
"crates/types",
"crates/zk_inception",
"crates/zk_supervisor",
]
Expand All @@ -20,6 +23,8 @@ keywords = ["zk", "cryptography", "blockchain", "ZKStack", "zkSync"]
[workspace.dependencies]
# Local dependencies
common = { path = "crates/common" }
config = { path = "crates/config" }
types = { path = "crates/types" }

# External dependencies
anyhow = "1.0.82"
Expand All @@ -29,6 +34,7 @@ console = "0.15.8"
ethers = "2.0"
human-panic = "2.0"
once_cell = "1.19.0"
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.9"
Expand Down
3 changes: 1 addition & 2 deletions zk_toolbox/crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
sqlx.workspace = true
strum.workspace = true
strum_macros.workspace = true
toml.workspace = true
url.workspace = true
xshell.workspace = true
futures.workspace = true
futures.workspace = true
2 changes: 1 addition & 1 deletion zk_toolbox/crates/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use once_cell::sync::OnceCell;
static CONFIG: OnceCell<GlobalConfig> = OnceCell::new();

pub fn init_global_config(config: GlobalConfig) {
CONFIG.set(config).unwrap();
CONFIG.set(config).expect("GlobalConfig already initialized");
}

pub fn global_config() -> &'static GlobalConfig {
Expand Down
6 changes: 2 additions & 4 deletions zk_toolbox/crates/common/src/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{ops::Add, time::Duration};

use ethers::prelude::Signer;
use ethers::{
core::k256::ecdsa::SigningKey,
middleware::MiddlewareBuilder,
prelude::{Http, LocalWallet, Provider},
prelude::{SignerMiddleware, H256},
prelude::{Http, LocalWallet, Provider, Signer, SignerMiddleware},
providers::Middleware,
types::{Address, TransactionRequest},
types::{Address, TransactionRequest, H256},
};

use crate::wallets::Wallet;
Expand Down
29 changes: 28 additions & 1 deletion zk_toolbox/crates/common/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
use std::path::Path;

use serde::Serialize;
use serde::{de::DeserializeOwned, Serialize};
use xshell::Shell;

pub fn read_yaml_file<T>(shell: &Shell, file_path: impl AsRef<Path>) -> anyhow::Result<T>
where
T: DeserializeOwned,
{
let content = shell.read_file(file_path)?;
let yaml = serde_yaml::from_str(&content)?;
Ok(yaml)
}

pub fn read_toml_file<T>(shell: &Shell, file_path: impl AsRef<Path>) -> anyhow::Result<T>
where
T: DeserializeOwned,
{
let content = shell.read_file(file_path)?;
let toml = toml::from_str(&content)?;
Ok(toml)
}

pub fn read_json_file<T>(shell: &Shell, file_path: impl AsRef<Path>) -> anyhow::Result<T>
where
T: DeserializeOwned,
{
let content = shell.read_file(file_path)?;
let json = serde_json::from_str(&content)?;
Ok(json)
}

pub fn save_yaml_file(
shell: &Shell,
file_path: impl AsRef<Path>,
Expand Down
21 changes: 12 additions & 9 deletions zk_toolbox/crates/common/src/forge.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{
path::{Path, PathBuf},
str::FromStr,
};

use clap::{Parser, ValueEnum};
use ethers::abi::Address;
use ethers::middleware::Middleware;
use ethers::prelude::{LocalWallet, Signer, U256};
use ethers::{abi::AbiEncode, types::H256};
use ethers::{
middleware::Middleware,
prelude::{LocalWallet, Signer},
types::{Address, H256, U256},
utils::hex::ToHex,
};
use serde::{Deserialize, Serialize};
use strum_macros::Display;
use xshell::{cmd, Shell};

use crate::cmd::Cmd;
use crate::ethereum::create_ethers_client;
use crate::{cmd::Cmd, ethereum::create_ethers_client};

/// Forge is a wrapper around the forge binary.
pub struct Forge {
Expand Down Expand Up @@ -123,7 +126,7 @@ impl ForgeScript {
self.private_key().and_then(|a| {
LocalWallet::from_bytes(a.as_bytes())
.ok()
.map(|a| a.address())
.map(|a| Address::from_slice(a.address().as_bytes()))
})
}

Expand Down
2 changes: 1 addition & 1 deletion zk_toolbox/crates/common/src/slugify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub fn slugify(data: &str) -> String {
data.trim().replace(" ", "-")
data.trim().replace(' ', "-")
}
14 changes: 7 additions & 7 deletions zk_toolbox/crates/common/src/wallets.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use ethers::{
core::rand::Rng,
signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer},
types::{H160, H256},
types::{Address, H256},
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Wallet {
pub address: H160,
pub address: Address,
pub private_key: Option<H256>,
}

impl Wallet {
pub fn random(rng: &mut impl Rng) -> Self {
let private_key = H256(rng.gen());
let private_key = H256::random_using(rng);
let local_wallet = LocalWallet::from_bytes(private_key.as_bytes()).unwrap();

Self {
address: local_wallet.address(),
address: Address::from_slice(local_wallet.address().as_bytes()),
private_key: Some(private_key),
}
}

pub fn new_with_key(private_key: H256) -> Self {
let local_wallet = LocalWallet::from_bytes(private_key.as_bytes()).unwrap();
Self {
address: local_wallet.address(),
address: Address::from_slice(local_wallet.address().as_bytes()),
private_key: Some(private_key),
}
}
Expand All @@ -41,7 +41,7 @@ impl Wallet {

pub fn empty() -> Self {
Self {
address: H160::zero(),
address: Address::zero(),
private_key: Some(H256::zero()),
}
}
Expand All @@ -57,7 +57,7 @@ fn test_load_localhost_wallets() {
.unwrap();
assert_eq!(
wallet.address,
H160::from_slice(
Address::from_slice(
&ethers::utils::hex::decode("0xa61464658AfeAf65CccaaFD3a512b69A83B77618").unwrap()
)
);
Expand Down
26 changes: 26 additions & 0 deletions zk_toolbox/crates/config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "config"
version.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
authors.workspace = true
exclude.workspace = true
repository.workspace = true
description.workspace = true
keywords.workspace = true

[dependencies]
anyhow.workspace = true
clap.workspace = true
common.workspace = true
ethers.workspace = true
rand.workspace = true
serde.workspace = true
serde_json.workspace = true
strum.workspace = true
strum_macros.workspace = true
thiserror.workspace = true
types.workspace = true
url.workspace = true
xshell.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ use std::{
};

use serde::{Deserialize, Serialize, Serializer};
use types::{
BaseToken, ChainId, L1BatchCommitDataGeneratorMode, L1Network, ProverMode, WalletCreation,
};
use xshell::Shell;

use crate::{
configs::{ContractsConfig, GenesisConfig, ReadConfig, SaveConfig, Secrets, WalletsConfig},
consts::{CONTRACTS_FILE, GENESIS_FILE, L1_CONTRACTS_FOUNDRY, SECRETS_FILE, WALLETS_FILE},
types::{BaseToken, ChainId, L1BatchCommitDataGeneratorMode, L1Network, ProverMode},
wallets::{create_localhost_wallets, WalletCreation},
consts::{
CONFIG_NAME, CONTRACTS_FILE, GENESIS_FILE, L1_CONTRACTS_FOUNDRY, SECRETS_FILE, WALLETS_FILE,
},
create_localhost_wallets,
traits::{FileConfigWithDefaultName, ReadConfig, SaveConfig, SaveConfigWithBasePath},
ContractsConfig, GenesisConfig, SecretsConfig, WalletsConfig,
};

/// Chain configuration file. This file is created in the chain
Expand Down Expand Up @@ -82,8 +87,8 @@ impl ChainConfig {
ContractsConfig::read(self.get_shell(), self.configs.join(CONTRACTS_FILE))
}

pub fn get_secrets_config(&self) -> anyhow::Result<Secrets> {
Secrets::read(self.get_shell(), self.configs.join(SECRETS_FILE))
pub fn get_secrets_config(&self) -> anyhow::Result<SecretsConfig> {
SecretsConfig::read(self.get_shell(), self.configs.join(SECRETS_FILE))
}

pub fn path_to_foundry(&self) -> PathBuf {
Expand All @@ -95,6 +100,11 @@ impl ChainConfig {
config.save(shell, path)
}

pub fn save_with_base_path(&self, shell: &Shell, path: impl AsRef<Path>) -> anyhow::Result<()> {
let config = self.get_internal();
config.save_with_base_path(shell, path)
}

fn get_internal(&self) -> ChainConfigInternal {
ChainConfigInternal {
id: self.id,
Expand All @@ -110,5 +120,6 @@ impl ChainConfig {
}
}

impl ReadConfig for ChainConfigInternal {}
impl SaveConfig for ChainConfigInternal {}
impl FileConfigWithDefaultName for ChainConfigInternal {
const FILE_NAME: &'static str = CONFIG_NAME;
}
39 changes: 39 additions & 0 deletions zk_toolbox/crates/config/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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";
/// Name of the secrets config file
pub(crate) const SECRETS_FILE: &str = "secrets.yaml";
/// Name of the general config file
pub(crate) const GENERAL_FILE: &str = "general.yaml";
/// Name of the genesis config file
pub(crate) const GENESIS_FILE: &str = "genesis.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";
/// 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
pub const DOCKER_COMPOSE_FILE: &str = "docker-compose.yml";
/// Path to the config file with mnemonic for localhost wallets
pub(crate) const CONFIGS_PATH: &str = "etc/env/file_based";
pub(crate) const LOCAL_CONFIGS_PATH: &str = "configs/";
pub(crate) const LOCAL_DB_PATH: &str = "db/";

/// Path to ecosystem contacts
pub(crate) const ECOSYSTEM_PATH: &str = "etc/ecosystem";

/// Path to l1 contracts foundry folder inside zksync-era
pub(crate) const L1_CONTRACTS_FOUNDRY: &str = "contracts/l1-contracts-foundry";

pub(crate) const ERA_CHAIN_ID: ChainId = ChainId(270);

pub(crate) const TEST_CONFIG_PATH: &str = "etc/test_config/constant/eth.json";
pub(crate) const BASE_PATH: &str = "m/44'/60'/0'";
Loading
Loading