From 51723da4453f6f51bb7270823b85af5a72bade7b Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Wed, 21 Aug 2024 18:43:41 -0400 Subject: [PATCH 1/4] feat: add command to flush cache --- zero_bin/common/src/prover_state/persistence.rs | 15 +++++++++++++++ zero_bin/leader/src/cli.rs | 2 ++ zero_bin/leader/src/main.rs | 1 + 3 files changed, 18 insertions(+) diff --git a/zero_bin/common/src/prover_state/persistence.rs b/zero_bin/common/src/prover_state/persistence.rs index c40299bce..419256eae 100644 --- a/zero_bin/common/src/prover_state/persistence.rs +++ b/zero_bin/common/src/prover_state/persistence.rs @@ -270,6 +270,21 @@ 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() { + // We will delete the entire directory and recreate it after. + fs::remove_dir_all(path)?; + fs::create_dir(path)?; + } + + Ok(()) +} + /// Writes the provided [`AllRecursiveCircuits`] to disk. /// /// In particular, we cover both the monolothic and base prover states, as well diff --git a/zero_bin/leader/src/cli.rs b/zero_bin/leader/src/cli.rs index eb3ea08f8..8b2d44201 100644 --- a/zero_bin/leader/src/cli.rs +++ b/zero_bin/leader/src/cli.rs @@ -22,6 +22,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. diff --git a/zero_bin/leader/src/main.rs b/zero_bin/leader/src/main.rs index eb250d199..451425762 100644 --- a/zero_bin/leader/src/main.rs +++ b/zero_bin/leader/src/main.rs @@ -62,6 +62,7 @@ async fn main() -> Result<()> { } match args.command { + Command::Clean => zero_bin_common::prover_state::persistence::delete_all()?, Command::Stdio { previous_proof, save_inputs_on_error, From 4d66052f3d239e44e432a4955bf3edcd214a7e9b Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Thu, 22 Aug 2024 12:23:16 -0400 Subject: [PATCH 2/4] Address concerns --- .../common/src/prover_state/persistence.rs | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/zero_bin/common/src/prover_state/persistence.rs b/zero_bin/common/src/prover_state/persistence.rs index 419256eae..59b41eb39 100644 --- a/zero_bin/common/src/prover_state/persistence.rs +++ b/zero_bin/common/src/prover_state/persistence.rs @@ -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; @@ -277,9 +278,18 @@ pub fn delete_all() -> anyhow::Result<()> { let path = Path::new(&circuit_dir); if path.is_dir() { - // We will delete the entire directory and recreate it after. - fs::remove_dir_all(path)?; - fs::create_dir(path)?; + 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(()) @@ -322,14 +332,24 @@ 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. + let path = Path::new(&circuit_cache_dir); + if !path.ends_with("_circuit") { + return Err(anyhow!( + "zkEVM circuit cache directory {:?} does not follow convention of ending with \"_cache\".", path + )); } + std::env::set_var(ZK_EVM_CACHE_DIR_ENV, circuit_cache_dir); + Ok(()) } From 73e4a2306337ddde5de78c1c73a13504da48bd58 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Thu, 22 Aug 2024 13:59:19 -0400 Subject: [PATCH 3/4] Tweak --- zero_bin/common/src/prover_state/persistence.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zero_bin/common/src/prover_state/persistence.rs b/zero_bin/common/src/prover_state/persistence.rs index 59b41eb39..d67637e98 100644 --- a/zero_bin/common/src/prover_state/persistence.rs +++ b/zero_bin/common/src/prover_state/persistence.rs @@ -343,9 +343,9 @@ pub fn set_circuit_cache_dir_env_if_not_set() -> anyhow::Result<()> { // Sanity check on naming convention for the circuit cache directory. let path = Path::new(&circuit_cache_dir); - if !path.ends_with("_circuit") { + if !path.ends_with("_circuit_cache") { return Err(anyhow!( - "zkEVM circuit cache directory {:?} does not follow convention of ending with \"_cache\".", path + "zkEVM circuit cache directory {:?} does not follow convention of ending with \"_circuit_cache\".", path )); } From dca38d5bd12d336b3821724176be873e5b5adfad Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Thu, 22 Aug 2024 14:11:36 -0400 Subject: [PATCH 4/4] Tweak squared --- zero_bin/common/src/prover_state/persistence.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zero_bin/common/src/prover_state/persistence.rs b/zero_bin/common/src/prover_state/persistence.rs index d67637e98..fd45238b9 100644 --- a/zero_bin/common/src/prover_state/persistence.rs +++ b/zero_bin/common/src/prover_state/persistence.rs @@ -342,11 +342,12 @@ pub fn set_circuit_cache_dir_env_if_not_set() -> anyhow::Result<()> { }; // Sanity check on naming convention for the circuit cache directory. - let path = Path::new(&circuit_cache_dir); - if !path.ends_with("_circuit_cache") { - return Err(anyhow!( - "zkEVM circuit cache directory {:?} does not follow convention of ending with \"_circuit_cache\".", path + 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);