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

chore: move utility functions to common crate #615

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 3 additions & 5 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion trace_decoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ criterion = { workspace = true }
glob = "0.3.1"
libtest-mimic = "0.7.3"
plonky2_maybe_rayon = { workspace = true }
pretty_env_logger = { workspace = true }
prover = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
Expand Down
9 changes: 5 additions & 4 deletions zero_bin/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,41 @@ anyhow = { workspace = true }
async-stream = { workspace = true }
cargo_metadata = { workspace = true }
clap = { workspace = true }
dotenvy = { workspace = true }
futures = { workspace = true }
lru = { workspace = true }
once_cell = { workspace = true }
plonky2 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
vergen = { workspace = true }

directories = "5.0.1"

# Local dependencies
evm_arithmetization = { workspace = true }
proof_gen = { workspace = true }
trace_decoder = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
cargo_metadata = { workspace = true }
vergen = { workspace = true }
anyhow = { workspace = true }


[features]
default = ["eth_mainnet"]
eth_mainnet = [
"evm_arithmetization/eth_mainnet",
"proof_gen/eth_mainnet",
"trace_decoder/eth_mainnet",
]
cdk_erigon = [
"evm_arithmetization/cdk_erigon",
"proof_gen/cdk_erigon",
"trace_decoder/cdk_erigon",
]

[lints]
Expand Down
16 changes: 16 additions & 0 deletions zero_bin/common/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::io;

use dotenvy::dotenv;
use tracing::warn;

/// Attempt to load in the local `.env` if present and set any environment
/// variables specified inside of it.
///
/// To keep things simple, any IO error we will treat as the file not existing
/// and continue moving on without the `env` variables set.
pub fn load_dotenvy_vars_if_present() {
match dotenv() {
Ok(_) | Err(dotenvy::Error::Io(io::Error { .. })) => (),
Err(e) => warn!("Found local `.env` file but was unable to parse it! (err: {e})",),
}
}
15 changes: 15 additions & 0 deletions zero_bin/common/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
use std::fs::File;
use std::path::PathBuf;

use proof_gen::proof_types::GeneratedBlockProof;

pub fn generate_block_proof_file_name(directory: &Option<&str>, block_height: u64) -> PathBuf {
let mut path = PathBuf::from(directory.unwrap_or(""));
path.push(format!("b{}.zkproof", block_height));
path
}

pub fn get_previous_proof(path: Option<PathBuf>) -> anyhow::Result<Option<GeneratedBlockProof>> {
if path.is_none() {
return Ok(None);
}

let path = path.unwrap();
let file = File::open(path)?;
let des = &mut serde_json::Deserializer::from_reader(&file);
let proof: GeneratedBlockProof = serde_path_to_error::deserialize(des)?;
Ok(Some(proof))
}
2 changes: 2 additions & 0 deletions zero_bin/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub mod block_interval;
pub mod debug_utils;
pub mod env;
pub mod fs;
pub mod parsing;
pub mod pre_checks;
pub mod prover_state;
pub mod provider;
pub mod tracing;
pub mod version;

/// Size of the channel used to send block prover inputs to the per block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tracing_subscriber::{prelude::*, util::SubscriberInitExt, EnvFilter};
pub(crate) fn tracing() {

pub fn init() {
tracing_subscriber::Registry::default()
.with(
tracing_subscriber::fmt::layer()
Expand Down
18 changes: 8 additions & 10 deletions zero_bin/leader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ categories.workspace = true
build = "../common/build.rs"

[dependencies]
paladin-core = { workspace = true }
clap = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
alloy = { workspace = true }
anyhow = { workspace = true }
axum = { workspace = true }
clap = { workspace = true }
futures = { workspace = true }
paladin-core = { workspace = true }
serde = { workspace = true }
dotenvy = { workspace = true }
tokio = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
futures = { workspace = true }
alloy.workspace = true
axum = { workspace = true }
toml = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

# Local dependencies
evm_arithmetization = { workspace = true }
Expand Down
36 changes: 5 additions & 31 deletions zero_bin/leader/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::env;
use std::sync::Arc;
use std::{env, io};
use std::{fs::File, path::PathBuf};

use anyhow::Result;
use clap::Parser;
use cli::Command;
use client::RpcParams;
use dotenvy::dotenv;
use ops::register;
use paladin::runtime::Runtime;
use proof_gen::proof_types::GeneratedBlockProof;
use prover::ProverConfig;
use tracing::{info, warn};
use tracing::info;
use zero_bin_common::env::load_dotenvy_vars_if_present;
use zero_bin_common::fs::get_previous_proof;
use zero_bin_common::{
block_interval::BlockInterval, prover_state::persistence::set_circuit_cache_dir_env_if_not_set,
};
Expand All @@ -22,26 +21,13 @@ use crate::client::{client_main, LeaderConfig};
mod cli;
mod client;
mod http;
mod init;
mod stdio;

fn get_previous_proof(path: Option<PathBuf>) -> Result<Option<GeneratedBlockProof>> {
if path.is_none() {
return Ok(None);
}

let path = path.unwrap();
let file = File::open(path)?;
let des = &mut serde_json::Deserializer::from_reader(&file);
let proof: GeneratedBlockProof = serde_path_to_error::deserialize(des)?;
Ok(Some(proof))
}

#[tokio::main]
async fn main() -> Result<()> {
load_dotenvy_vars_if_present();
set_circuit_cache_dir_env_if_not_set()?;
init::tracing();
zero_bin_common::tracing::init();

let args: Vec<String> = env::args().collect();

Expand Down Expand Up @@ -127,15 +113,3 @@ async fn main() -> Result<()> {

Ok(())
}

/// Attempt to load in the local `.env` if present and set any environment
/// variables specified inside of it.
///
/// To keep things simple, any IO error we will treat as the file not existing
/// and continue moving on without the `env` variables set.
fn load_dotenvy_vars_if_present() {
match dotenv() {
Ok(_) | Err(dotenvy::Error::Io(io::Error { .. })) => (),
Err(e) => warn!("Found local `.env` file but was unable to parse it! (err: {e})",),
}
}
5 changes: 1 addition & 4 deletions zero_bin/ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
keccak-hash = { workspace = true }
paladin-core = { workspace = true }
serde = { workspace = true }
tracing = { workspace = true }
keccak-hash = { workspace = true }

# Local dependencies
evm_arithmetization = { workspace = true }
proof_gen = { workspace = true }
trace_decoder = { workspace = true }
zero_bin_common = { workspace = true }

[features]
default = ["eth_mainnet"]
eth_mainnet = [
"evm_arithmetization/eth_mainnet",
"proof_gen/eth_mainnet",
"trace_decoder/eth_mainnet",
"zero_bin_common/eth_mainnet",
]
cdk_erigon = [
"evm_arithmetization/cdk_erigon",
"proof_gen/cdk_erigon",
"trace_decoder/cdk_erigon",
"zero_bin_common/cdk_erigon",
]
4 changes: 2 additions & 2 deletions zero_bin/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ anyhow = { workspace = true }
clap = { workspace = true }
futures = { workspace = true }
hex = { workspace = true }
itertools = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tower = { workspace = true, features = ["retry"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
url = { workspace = true }
itertools = { workspace = true }

# Local dependencies
compat = { workspace = true }
Expand All @@ -34,9 +34,9 @@ trace_decoder = { workspace = true }
zero_bin_common = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
cargo_metadata = { workspace = true }
vergen = { workspace = true }
anyhow = { workspace = true }

[features]
default = ["eth_mainnet"]
Expand Down
8 changes: 4 additions & 4 deletions zero_bin/verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ edition = "2021"
build = "../common/build.rs"

[dependencies]
anyhow = { workspace = true }
clap = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
dotenvy = { workspace = true }
anyhow = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

# Local dependencies
proof_gen = { workspace = true }
zero_bin_common = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
cargo_metadata = { workspace = true }
vergen = { workspace = true }
anyhow = { workspace = true }


[features]
Expand Down
11 changes: 0 additions & 11 deletions zero_bin/worker/src/init.rs

This file was deleted.

6 changes: 2 additions & 4 deletions zero_bin/worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use zero_bin_common::prover_state::{
cli::CliProverStateConfig,
persistence::{set_circuit_cache_dir_env_if_not_set, CIRCUIT_VERSION},
};
use zero_bin_common::version;

mod init;
use zero_bin_common::{tracing, version};

// TODO: https://github.com/0xPolygonZero/zk_evm/issues/302
// this should probably be removed.
Expand Down Expand Up @@ -40,7 +38,7 @@ async fn main() -> Result<()> {
}

dotenv().ok();
init::tracing();
tracing::init();
set_circuit_cache_dir_env_if_not_set()?;
let args = Cli::parse();

Expand Down
Loading