From 82fab1a1784cc81e317245b4305a18686478caf5 Mon Sep 17 00:00:00 2001 From: Yijun Zhao Date: Thu, 12 Dec 2024 18:30:06 +0800 Subject: [PATCH] using some std libs instead of thirdparty lib --- prover/Cargo.lock | 3 - prover/Cargo.toml | 3 - prover/src/config.rs | 64 +++++++++++---------- prover/src/version.rs | 6 +- prover/src/zk_circuits_handler/common.rs | 21 +++---- prover/src/zk_circuits_handler/darwin.rs | 6 +- prover/src/zk_circuits_handler/darwin_v2.rs | 6 +- 7 files changed, 49 insertions(+), 60 deletions(-) diff --git a/prover/Cargo.lock b/prover/Cargo.lock index cff651cc37..54d0d8f90f 100644 --- a/prover/Cargo.lock +++ b/prover/Cargo.lock @@ -3285,12 +3285,10 @@ dependencies = [ "eth-keystore", "ethers-core 2.0.7 (git+https://github.com/scroll-tech/ethers-rs.git?branch=v2.0.7)", "ethers-providers 2.0.7 (git+https://github.com/scroll-tech/ethers-rs.git?branch=v2.0.7)", - "futures", "halo2_proofs", "hex", "http 1.1.0", "log", - "once_cell", "prover 0.12.0", "prover 0.13.0", "rand", @@ -3301,7 +3299,6 @@ dependencies = [ "serde", "serde_json", "sled", - "snark-verifier-sdk", "tiny-keccak", "tokio", ] diff --git a/prover/Cargo.toml b/prover/Cargo.toml index 36db6a39b2..05c2b873b8 100644 --- a/prover/Cargo.toml +++ b/prover/Cargo.toml @@ -23,19 +23,16 @@ log = "0.4" env_logger = "0.11.3" serde = { version = "1.0.198", features = ["derive"] } serde_json = "1.0.116" -futures = "0.3.30" ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "v2.0.7" } ethers-providers = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "v2.0.7" } halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" } -snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] } prover_darwin = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.12.2", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] } prover_darwin_v2 = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.13.1", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] } base64 = "0.13.1" reqwest = { version = "0.12.4", features = ["gzip"] } reqwest-middleware = "0.3" reqwest-retry = "0.5" -once_cell = "1.19.0" hex = "0.4.3" tiny-keccak = { version = "2.0.0", features = ["sha3", "keccak"] } rand = "0.8.5" diff --git a/prover/src/config.rs b/prover/src/config.rs index 4e3c1f2ccc..cc42f14692 100644 --- a/prover/src/config.rs +++ b/prover/src/config.rs @@ -1,8 +1,7 @@ +use crate::types::ProverType; use anyhow::{bail, Result}; use serde::{Deserialize, Serialize}; -use std::fs::File; - -use crate::types::ProverType; +use std::{fs::File, sync::OnceLock}; #[derive(Debug, Serialize, Deserialize)] pub struct CircuitConfig { @@ -52,7 +51,7 @@ impl Config { } static SCROLL_PROVER_ASSETS_DIR_ENV_NAME: &str = "SCROLL_PROVER_ASSETS_DIR"; -static mut SCROLL_PROVER_ASSETS_DIRS: Vec = vec![]; +static SCROLL_PROVER_ASSETS_DIRS: OnceLock> = OnceLock::new(); #[derive(Debug)] pub struct AssetsDirEnvConfig {} @@ -64,39 +63,42 @@ impl AssetsDirEnvConfig { if dirs.len() != 2 { bail!("env variable SCROLL_PROVER_ASSETS_DIR value must be 2 parts seperated by comma.") } - unsafe { - SCROLL_PROVER_ASSETS_DIRS = dirs.into_iter().map(|s| s.to_string()).collect(); - log::info!( - "init SCROLL_PROVER_ASSETS_DIRS: {:?}", - SCROLL_PROVER_ASSETS_DIRS - ); - } + + SCROLL_PROVER_ASSETS_DIRS.get_or_init(|| dirs.into_iter().map(|s| s.to_string()).collect()); + log::info!( + "init SCROLL_PROVER_ASSETS_DIRS: {:?}", + SCROLL_PROVER_ASSETS_DIRS + ); Ok(()) } pub fn enable_first() { - unsafe { - log::info!( - "set env {SCROLL_PROVER_ASSETS_DIR_ENV_NAME} to {}", - &SCROLL_PROVER_ASSETS_DIRS[0] - ); - std::env::set_var( - SCROLL_PROVER_ASSETS_DIR_ENV_NAME, - &SCROLL_PROVER_ASSETS_DIRS[0], - ); - } + debug_assert!( + SCROLL_PROVER_ASSETS_DIRS.get().is_some() + && SCROLL_PROVER_ASSETS_DIRS.get().unwrap().len() >= 2 + ); + log::info!( + "set env {SCROLL_PROVER_ASSETS_DIR_ENV_NAME} to {}", + &SCROLL_PROVER_ASSETS_DIRS.get().unwrap()[0] + ); + std::env::set_var( + SCROLL_PROVER_ASSETS_DIR_ENV_NAME, + &SCROLL_PROVER_ASSETS_DIRS.get().unwrap()[0], + ); } pub fn enable_second() { - unsafe { - log::info!( - "set env {SCROLL_PROVER_ASSETS_DIR_ENV_NAME} to {}", - &SCROLL_PROVER_ASSETS_DIRS[1] - ); - std::env::set_var( - SCROLL_PROVER_ASSETS_DIR_ENV_NAME, - &SCROLL_PROVER_ASSETS_DIRS[1], - ); - } + debug_assert!( + SCROLL_PROVER_ASSETS_DIRS.get().is_some() + && SCROLL_PROVER_ASSETS_DIRS.get().unwrap().len() >= 2 + ); + log::info!( + "set env {SCROLL_PROVER_ASSETS_DIR_ENV_NAME} to {}", + &SCROLL_PROVER_ASSETS_DIRS.get().unwrap()[1] + ); + std::env::set_var( + SCROLL_PROVER_ASSETS_DIR_ENV_NAME, + &SCROLL_PROVER_ASSETS_DIRS.get().unwrap()[1], + ); } } diff --git a/prover/src/version.rs b/prover/src/version.rs index 76249adeae..4bc92cb0bf 100644 --- a/prover/src/version.rs +++ b/prover/src/version.rs @@ -1,7 +1,7 @@ -use std::cell::OnceCell; +use std::sync::OnceLock; static DEFAULT_COMMIT: &str = "unknown"; -static mut VERSION: OnceCell = OnceCell::new(); +static VERSION: OnceLock = OnceLock::new(); pub const TAG: &str = "v0.0.0"; pub const DEFAULT_ZK_VERSION: &str = "000000-000000"; @@ -14,5 +14,5 @@ fn init_version() -> String { } pub fn get_version() -> String { - unsafe { VERSION.get_or_init(init_version).clone() } + VERSION.get_or_init(init_version).clone() } diff --git a/prover/src/zk_circuits_handler/common.rs b/prover/src/zk_circuits_handler/common.rs index e88628ad65..24a419ecf6 100644 --- a/prover/src/zk_circuits_handler/common.rs +++ b/prover/src/zk_circuits_handler/common.rs @@ -1,23 +1,20 @@ -use std::{collections::BTreeMap, rc::Rc}; - use crate::types::ProverType; - -use once_cell::sync::OnceCell; - use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG}; +use std::{ + collections::BTreeMap, + sync::{Arc, OnceLock}, +}; -static mut PARAMS_MAP: OnceCell>>> = OnceCell::new(); +static PARAMS_MAP: OnceLock>>> = OnceLock::new(); pub fn get_params_map_instance<'a, F>(load_params_func: F) -> &'a BTreeMap> where F: FnOnce() -> BTreeMap>, { - unsafe { - PARAMS_MAP.get_or_init(|| { - let params_map = load_params_func(); - Rc::new(params_map) - }) - } + PARAMS_MAP.get_or_init(|| { + let params_map = load_params_func(); + Arc::new(params_map) + }) } pub fn get_degrees(prover_types: &std::collections::HashSet, f: F) -> Vec diff --git a/prover/src/zk_circuits_handler/darwin.rs b/prover/src/zk_circuits_handler/darwin.rs index 96618f9508..276632073f 100644 --- a/prover/src/zk_circuits_handler/darwin.rs +++ b/prover/src/zk_circuits_handler/darwin.rs @@ -4,12 +4,9 @@ use crate::{ types::{ProverType, TaskType}, }; use anyhow::{bail, Context, Ok, Result}; -use once_cell::sync::Lazy; use serde::Deserialize; use crate::types::{CommonHash, Task}; -use std::{cell::RefCell, cmp::Ordering, env, rc::Rc}; - use prover_darwin::{ aggregator::Prover as BatchProver, check_chunk_hashes, @@ -19,9 +16,10 @@ use prover_darwin::{ BatchProof, BatchProvingTask, BlockTrace, BundleProof, BundleProvingTask, ChunkInfo, ChunkProof, ChunkProvingTask, }; +use std::{cell::RefCell, cmp::Ordering, env, rc::Rc, sync::LazyLock}; // Only used for debugging. -static OUTPUT_DIR: Lazy> = Lazy::new(|| env::var("PROVER_OUTPUT_DIR").ok()); +static OUTPUT_DIR: LazyLock> = LazyLock::new(|| env::var("PROVER_OUTPUT_DIR").ok()); #[derive(Debug, Clone, Deserialize)] pub struct BatchTaskDetail { diff --git a/prover/src/zk_circuits_handler/darwin_v2.rs b/prover/src/zk_circuits_handler/darwin_v2.rs index fce3871d03..70ae82643a 100644 --- a/prover/src/zk_circuits_handler/darwin_v2.rs +++ b/prover/src/zk_circuits_handler/darwin_v2.rs @@ -4,12 +4,9 @@ use crate::{ types::{ProverType, TaskType}, }; use anyhow::{bail, Context, Ok, Result}; -use once_cell::sync::Lazy; use serde::Deserialize; use crate::types::{CommonHash, Task}; -use std::{cell::RefCell, cmp::Ordering, env, rc::Rc}; - use prover_darwin_v2::{ aggregator::Prover as BatchProver, check_chunk_hashes, @@ -19,9 +16,10 @@ use prover_darwin_v2::{ BatchProof, BatchProvingTask, BlockTrace, BundleProof, BundleProvingTask, ChunkInfo, ChunkProof, ChunkProvingTask, }; +use std::{cell::RefCell, cmp::Ordering, env, rc::Rc, sync::LazyLock}; // Only used for debugging. -static OUTPUT_DIR: Lazy> = Lazy::new(|| env::var("PROVER_OUTPUT_DIR").ok()); +static OUTPUT_DIR: LazyLock> = LazyLock::new(|| env::var("PROVER_OUTPUT_DIR").ok()); #[derive(Debug, Clone, Deserialize)] pub struct BatchTaskDetail {