Skip to content

Commit

Permalink
feat: add leader command to flush cache (#527)
Browse files Browse the repository at this point in the history
* feat: add command to flush cache

* Address concerns

* Tweak

* Tweak squared
  • Loading branch information
Nashtare authored Aug 23, 2024
1 parent af11588 commit 6c455ec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
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()?,
};

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

0 comments on commit 6c455ec

Please sign in to comment.