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: add leader command to flush cache #527

Merged
merged 5 commits into from
Aug 23, 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
48 changes: 42 additions & 6 deletions zero_bin/common/src/prover_state/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::{
fmt::{Debug, Display},
fs::{self, OpenOptions},
io::Write,
path::Path,
path::{Path, PathBuf},
};

use alloy::hex;
use anyhow::anyhow;
use directories::ProjectDirs;
use evm_arithmetization::cpu::kernel::aggregator::KERNEL;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -270,6 +271,30 @@ pub fn persist_all_to_disk(
Ok(())
}

/// Flushes all existing prover state configurations and associated circuits
/// that have been written to disk.
pub fn delete_all() -> anyhow::Result<()> {
let circuit_dir = circuit_dir();
let path = Path::new(&circuit_dir);

if path.is_dir() {
for entry in fs::read_dir(path)? {
let entry = entry?;
let file_path = entry.path();

if file_path.is_file()
&& (file_path.starts_with("prover_state")
|| file_path.starts_with("verifier_state"))
{
// Delete all circuit files.
fs::remove_file(file_path)?;
}
}
}

Ok(())
}

/// Writes the provided [`AllRecursiveCircuits`] to disk.
///
/// In particular, we cover both the monolothic and base prover states, as well
Expand Down Expand Up @@ -307,14 +332,25 @@ fn circuit_dir() -> String {
/// 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) {
let circuit_cache_dir = if let Some(path_str) = std::env::var_os(ZK_EVM_CACHE_DIR_ENV) {
PathBuf::from(&path_str)
} else {
match ProjectDirs::from("", "", ZK_EVM_CACHE_DIR_NAME) {
Some(proj_dir) => proj_dir.cache_dir().to_path_buf(),
None => std::env::current_dir()?,
Nashtare marked this conversation as resolved.
Show resolved Hide resolved
};

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

// Sanity check on naming convention for the circuit cache directory.
if let Some(path_str) = Path::new(&circuit_cache_dir).to_str() {
if !path_str.ends_with("_circuit_cache") {
return Err(anyhow!(
"zkEVM circuit cache directory {:?} does not follow convention of ending with \"_circuit_cache\".", path_str
));
}
}

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

Ok(())
}
2 changes: 2 additions & 0 deletions zero_bin/leader/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub(crate) struct Cli {

#[derive(Subcommand)]
pub(crate) enum Command {
/// Deletes all the previously cached circuits.
Clean,
/// Reads input from stdin and writes output to stdout.
Stdio {
/// The previous proof output.
Expand Down
1 change: 1 addition & 0 deletions zero_bin/leader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async fn main() -> Result<()> {
}

match args.command {
Command::Clean => zero_bin_common::prover_state::persistence::delete_all()?,
Command::Stdio { previous_proof } => {
let previous_proof = get_previous_proof(previous_proof)?;
stdio::stdio_main(runtime, previous_proof, prover_config).await?;
Expand Down
Loading