Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into 0xaatif/typed-back…
Browse files Browse the repository at this point in the history
…end3
  • Loading branch information
0xaatif committed Aug 14, 2024
2 parents 3c94cfe + c6d015b commit de533bc
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 21 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions zero_bin/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
directories = "5.0.1"

thiserror = { workspace = true }
tracing = { workspace = true }
proof_gen = { workspace = true }
Expand All @@ -24,6 +26,7 @@ alloy = { workspace = true }
async-stream = { workspace = true }
cargo_metadata = { workspace = true }
vergen = { workspace = true }
once_cell = { workspace = true }

[build-dependencies]
cargo_metadata = { workspace = true }
Expand Down
55 changes: 37 additions & 18 deletions zero_bin/common/src/prover_state/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
path::Path,
};

use directories::ProjectDirs;
use plonky2::util::serialization::{
Buffer, DefaultGateSerializer, DefaultGeneratorSerializer, IoError,
};
Expand All @@ -17,10 +18,10 @@ use super::{
Config, RecursiveCircuitsForTableSize, SIZE,
};

const CIRCUITS_DIR: &str = "circuits/";
const PROVER_STATE_FILE_PREFIX: &str = "prover_state";
const VERIFIER_STATE_FILE_PREFIX: &str = "verifier_state";
const CARGO_WORKSPACE_DIR_ENV: &str = "CARGO_WORKSPACE_DIR";
const ZK_EVM_CACHE_DIR_NAME: &str = "zk_evm_circuit_cache";
const ZK_EVM_CACHE_DIR_ENV: &str = "ZK_EVM_CACHE_DIR";

fn get_serializers() -> (
DefaultGateSerializer,
Expand Down Expand Up @@ -73,14 +74,15 @@ pub(crate) trait DiskResource {
p: &Self::PathConstrutor,
r: &Self::Resource,
) -> Result<(), DiskResourceError<Self::Error>> {
let circuits_dir = relative_circuit_dir_path();
let circuits_dir = circuit_dir();

// Create the base folder if non-existent.
if std::fs::metadata(&circuits_dir).is_err() {
std::fs::create_dir(&circuits_dir).map_err(|_| {
DiskResourceError::IoError::<Self::Error>(std::io::Error::other(
"Could not create circuits folder",
))
std::fs::create_dir_all(&circuits_dir).map_err(|err| {
DiskResourceError::IoError::<Self::Error>(std::io::Error::other(format!(
"Could not create circuits folder with error \"{:?}\"",
err
)))
})?;
}

Expand All @@ -107,7 +109,7 @@ impl DiskResource for BaseProverResource {
fn path(p: &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_base_{}_{}",
&relative_circuit_dir_path(),
circuit_dir(),
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
p.get_configuration_digest()
Expand Down Expand Up @@ -143,7 +145,7 @@ impl DiskResource for MonolithicProverResource {
fn path(p: &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_monolithic_{}_{}",
&relative_circuit_dir_path(),
circuit_dir(),
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
p.get_configuration_digest()
Expand Down Expand Up @@ -178,7 +180,7 @@ impl DiskResource for RecursiveCircuitResource {
fn path((circuit_type, size): &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_{}_{}_{}",
&relative_circuit_dir_path(),
circuit_dir(),
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
circuit_type.as_short_str(),
Expand Down Expand Up @@ -222,7 +224,7 @@ impl DiskResource for VerifierResource {
fn path(p: &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_{}_{}",
&relative_circuit_dir_path(),
circuit_dir(),
VERIFIER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
p.get_configuration_digest()
Expand Down Expand Up @@ -277,11 +279,28 @@ fn prover_to_disk(
Ok(())
}

/// If we're running in the cargo workspace, then always use the `circuits`
/// directory that lives in `tools/`. Otherwise, just use `circuits` in the
/// current directory.
fn relative_circuit_dir_path() -> String {
env::var(CARGO_WORKSPACE_DIR_ENV)
.map(|p| format!("{}/{}", p, CIRCUITS_DIR))
.unwrap_or_else(|_| CIRCUITS_DIR.to_string())
fn circuit_dir() -> String {
// Guaranteed to be set by the binary if not set by the user.
std::env::var(ZK_EVM_CACHE_DIR_ENV).unwrap_or_else(|_| {
panic!(
"expected the env var \"{}\" to be set",
ZK_EVM_CACHE_DIR_ENV
)
})
}

/// We store serialized circuits inside the cache directory specified by an env
/// variable. If the user does not set this, then we set it base to the OS's
/// standard location for the cache directory.
pub fn set_circuit_cache_dir_env_if_not_set() -> anyhow::Result<()> {
if std::env::var_os(ZK_EVM_CACHE_DIR_ENV).is_none() {
let circuit_cache_dir = match ProjectDirs::from("", "", ZK_EVM_CACHE_DIR_NAME) {
Some(proj_dir) => proj_dir.cache_dir().to_path_buf(),
None => std::env::current_dir()?,
};

std::env::set_var(ZK_EVM_CACHE_DIR_ENV, circuit_cache_dir);
}

Ok(())
}
5 changes: 4 additions & 1 deletion zero_bin/leader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use ops::register;
use paladin::runtime::Runtime;
use proof_gen::proof_types::GeneratedBlockProof;
use tracing::{info, warn};
use zero_bin_common::block_interval::BlockInterval;
use zero_bin_common::version;
use zero_bin_common::{
block_interval::BlockInterval, prover_state::persistence::set_circuit_cache_dir_env_if_not_set,
};

use crate::client::{client_main, ProofParams};

Expand All @@ -38,6 +40,7 @@ fn get_previous_proof(path: Option<PathBuf>) -> Result<Option<GeneratedBlockProo
#[tokio::main]
async fn main() -> Result<()> {
load_dotenvy_vars_if_present();
set_circuit_cache_dir_env_if_not_set()?;
init::tracing();

if env::var_os(EVM_ARITHMETIZATION_PKG_VER).is_none() {
Expand Down
3 changes: 2 additions & 1 deletion zero_bin/verifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use dotenvy::dotenv;
use proof_gen::proof_types::GeneratedBlockProof;
use serde_json::Deserializer;
use tracing::info;
use zero_bin_common::version;
use zero_bin_common::{prover_state::persistence::set_circuit_cache_dir_env_if_not_set, version};

mod cli;
mod init;

fn main() -> Result<()> {
dotenv().ok();
init::tracing();
set_circuit_cache_dir_env_if_not_set()?;

let args: Vec<String> = env::args().collect();
if args.contains(&"--version".to_string()) {
Expand Down
5 changes: 4 additions & 1 deletion zero_bin/worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use clap::Parser;
use dotenvy::dotenv;
use ops::register;
use paladin::runtime::WorkerRuntime;
use zero_bin_common::prover_state::cli::CliProverStateConfig;
use zero_bin_common::prover_state::{
cli::CliProverStateConfig, persistence::set_circuit_cache_dir_env_if_not_set,
};
use zero_bin_common::version;

mod init;
Expand Down Expand Up @@ -38,6 +40,7 @@ async fn main() -> Result<()> {

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

args.prover_state_config
Expand Down

0 comments on commit de533bc

Please sign in to comment.