From 962c5401301764193bd28728ad2b9ff650185322 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Tue, 2 Jul 2024 11:40:07 +0200 Subject: [PATCH 1/3] Add static KEYSTORE --- .../src/commitment_utils.rs | 18 ++++++++++++++---- .../src/keystore.rs | 9 +++++++++ prover/witness_generator/src/main.rs | 11 ++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs b/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs index 935d06460181..760fe8257608 100644 --- a/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs +++ b/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{str::FromStr, sync::Mutex}; use anyhow::Context as _; use hex::ToHex; @@ -22,9 +22,14 @@ use crate::{ VkCommitments, }; +static KEYSTORE: Lazy>> = Lazy::new(|| Mutex::new(None)); + lazy_static! { // TODO: do not initialize a static const with data read in runtime. - static ref COMMITMENTS: Lazy = Lazy::new(|| { circuit_commitments(&Keystore::default()).unwrap() }); + static ref COMMITMENTS: Lazy = Lazy::new(|| { + let keystore = KEYSTORE.lock().unwrap().clone().unwrap_or_else(Keystore::default); + circuit_commitments(&keystore).unwrap() + }); } fn circuit_commitments(keystore: &Keystore) -> anyhow::Result { @@ -97,14 +102,19 @@ pub fn generate_commitments(keystore: &Keystore) -> anyhow::Result L1VerifierConfig { +pub fn get_cached_commitments(setup_data_generator: Option) -> L1VerifierConfig { + if let Some(setup_data_generator) = setup_data_generator { + let keystore = Keystore::new_with_setup_data_path(setup_data_generator); + let mut keystore_lock = KEYSTORE.lock().unwrap(); + *keystore_lock = Some(keystore); + } tracing::info!("Using cached commitments {:?}", **COMMITMENTS); **COMMITMENTS } #[test] fn test_get_cached_commitments() { - let commitments = get_cached_commitments(); + let commitments = get_cached_commitments(None); assert_eq!( H256::zero(), commitments.params.recursion_circuits_set_vks_hash diff --git a/prover/vk_setup_data_generator_server_fri/src/keystore.rs b/prover/vk_setup_data_generator_server_fri/src/keystore.rs index 25aedeb089ff..70aaff9fc4a4 100644 --- a/prover/vk_setup_data_generator_server_fri/src/keystore.rs +++ b/prover/vk_setup_data_generator_server_fri/src/keystore.rs @@ -36,6 +36,7 @@ pub enum ProverServiceDataType { /// There are 2 types: /// - small verification, finalization keys (used only during verification) /// - large setup keys, used during proving. +#[derive(Clone)] pub struct Keystore { /// Directory to store all the small keys. basedir: PathBuf, @@ -80,6 +81,7 @@ impl Keystore { setup_data_path: Some(setup_data_path), } } + pub fn new_with_optional_setup_path(basedir: PathBuf, setup_data_path: Option) -> Self { Keystore { basedir, @@ -87,6 +89,13 @@ impl Keystore { } } + pub fn new_with_setup_data_path(setup_data_path: String) -> Self { + Keystore { + basedir: get_base_path(), + setup_data_path: Some(setup_data_path), + } + } + pub fn get_base_path(&self) -> &PathBuf { &self.basedir } diff --git a/prover/witness_generator/src/main.rs b/prover/witness_generator/src/main.rs index 8208c62c6277..661965b75061 100644 --- a/prover/witness_generator/src/main.rs +++ b/prover/witness_generator/src/main.rs @@ -111,12 +111,12 @@ async fn main() -> anyhow::Result<()> { let started_at = Instant::now(); let use_push_gateway = opt.batch_size.is_some(); + let prover_config = general_config.prover_config.context("prover config")?; let object_store_config = ProverObjectStoreConfig( - general_config - .prover_config - .context("prover config")? + prover_config .prover_object_store - .context("object store")?, + .context("object store")? + .clone(), ); let store_factory = ObjectStoreFactory::new(object_store_config.0); let config = general_config @@ -202,7 +202,8 @@ async fn main() -> anyhow::Result<()> { let witness_generator_task = match round { AggregationRound::BasicCircuits => { - let vk_commitments = get_cached_commitments(); + let setup_data_path = prover_config.setup_data_path.clone(); + let vk_commitments = get_cached_commitments(Some(setup_data_path)); assert_eq!( vk_commitments, vk_commitments_in_db, From 774f932db8d1caf164aaeef9b6280af7d75111e8 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Tue, 2 Jul 2024 12:04:17 +0200 Subject: [PATCH 2/3] Update variable name --- .../src/commitment_utils.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs b/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs index 760fe8257608..8a55930124e2 100644 --- a/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs +++ b/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs @@ -102,9 +102,9 @@ pub fn generate_commitments(keystore: &Keystore) -> anyhow::Result) -> L1VerifierConfig { - if let Some(setup_data_generator) = setup_data_generator { - let keystore = Keystore::new_with_setup_data_path(setup_data_generator); +pub fn get_cached_commitments(setup_data_path: Option) -> L1VerifierConfig { + if let Some(setup_data_path) = setup_data_path { + let keystore = Keystore::new_with_setup_data_path(setup_data_path); let mut keystore_lock = KEYSTORE.lock().unwrap(); *keystore_lock = Some(keystore); } From df5907fb50e7b66c0861e7bdd6606fcab3befdd1 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Tue, 2 Jul 2024 12:21:32 +0200 Subject: [PATCH 3/3] lint --- .../vk_setup_data_generator_server_fri/src/commitment_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs b/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs index 8a55930124e2..58fd36ab4a59 100644 --- a/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs +++ b/prover/vk_setup_data_generator_server_fri/src/commitment_utils.rs @@ -27,7 +27,7 @@ static KEYSTORE: Lazy>> = Lazy::new(|| Mutex::new(None)); lazy_static! { // TODO: do not initialize a static const with data read in runtime. static ref COMMITMENTS: Lazy = Lazy::new(|| { - let keystore = KEYSTORE.lock().unwrap().clone().unwrap_or_else(Keystore::default); + let keystore = KEYSTORE.lock().unwrap().clone().unwrap_or_default(); circuit_commitments(&keystore).unwrap() }); }