Skip to content

Commit

Permalink
fix: init multi task type issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yiweichi committed Jan 10, 2025
1 parent fc8856c commit 5ee0d7a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 196 deletions.
2 changes: 1 addition & 1 deletion prover/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.
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"] }
scroll-proving-sdk = { git = "https://github.com/scroll-tech/scroll-proving-sdk.git", rev = "61bbbe1"}
scroll-proving-sdk = { git = "https://github.com/scroll-tech/scroll-proving-sdk.git", rev = "e29b98d"}
base64 = "0.13.1"
reqwest = { version = "0.12.4", features = ["gzip"] }
reqwest-middleware = "0.3"
Expand Down
51 changes: 0 additions & 51 deletions prover/src/config.rs

This file was deleted.

22 changes: 16 additions & 6 deletions prover/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#![feature(lazy_cell)]
#![feature(core_intrinsics)]

mod config;
mod prover;
mod types;
mod utils;
mod version;
mod zk_circuits_handler;

use clap::{ArgAction, Parser};
use prover::LocalProver;
use scroll_proving_sdk::{config::Config, prover::ProverBuilder, utils::init_tracing};
use scroll_proving_sdk::{
config::Config,
prover::ProverBuilder,
utils::{get_version, init_tracing},
};
use utils::get_prover_type;

#[derive(Parser, Debug)]
#[clap(disable_version_flag = true)]
Expand All @@ -35,18 +38,25 @@ async fn main() -> anyhow::Result<()> {
let args = Args::parse();

if args.version {
println!("version is {}", version::get_version());
println!("version is {}", get_version());
std::process::exit(0);
}

utils::log_init(args.log_file);

let cfg: Config = Config::from_file(args.config_file)?;
let mut prover_types = vec![];
cfg.prover.circuit_types.iter().for_each(|circuit_type| {
if let Some(pt) = get_prover_type(*circuit_type) {
if !prover_types.contains(&pt) {
prover_types.push(pt);
}
}
});
let local_prover = LocalProver::new(
cfg.prover
.local
.clone()
.ok_or_else(|| anyhow::anyhow!("Missing local prover configuration"))?,
prover_types,
);
let prover = ProverBuilder::new(cfg)
.with_proving_service(Box::new(local_prover))
Expand Down
90 changes: 27 additions & 63 deletions prover/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
types::ProverType,
utils::get_prover_type,
zk_circuits_handler::{CircuitsHandler, CircuitsHandlerProvider},
};
Expand All @@ -18,13 +19,14 @@ use std::{
sync::{Arc, Mutex},
time::{SystemTime, UNIX_EPOCH},
};
use tokio::{runtime::Runtime, sync::RwLock, task::JoinHandle};
use tokio::sync::RwLock;

pub struct LocalProver {
config: LocalProverConfig,
prover_types: Vec<ProverType>,
circuits_handler_provider: RwLock<CircuitsHandlerProvider>,
current_task: Arc<Mutex<Option<JoinHandle<Result<String>>>>>,
next_task_id: Arc<Mutex<u64>>,
result: Arc<Mutex<Result<String>>>,
}

#[async_trait]
Expand Down Expand Up @@ -52,26 +54,15 @@ impl ProvingService for LocalProver {
GetVkResponse { vks, error: None }
}
async fn prove(&self, req: ProveRequest) -> ProveResponse {
let prover_type = match get_prover_type(req.circuit_type) {
Some(pt) => pt,
None => {
return build_prove_error_response(
String::new(),
TaskStatus::Failed,
None,
String::from("unsupported prover_type"),
)
}
};
let handler = self
.circuits_handler_provider
.write()
.await
.get_circuits_handler(&req.hard_fork_name, prover_type)
.get_circuits_handler(&req.hard_fork_name, self.prover_types.clone())
.context("failed to get circuit handler")
.unwrap();

match self.do_prove(req.clone(), handler) {
match self.do_prove(req.clone(), handler).await {
Ok(resp) => resp,
Err(e) => build_prove_error_response(
String::new(),
Expand All @@ -83,85 +74,58 @@ impl ProvingService for LocalProver {
}

async fn query_task(&self, req: QueryTaskRequest) -> QueryTaskResponse {
let mut current_task = self.current_task.lock().unwrap();

if let Some(handle) = current_task.take() {
if handle.is_finished() {
let result = Runtime::new().unwrap().block_on(handle).unwrap();
match result {
Ok(proof) => {
return build_query_task_response(
req.task_id,
TaskStatus::Success,
Some(proof),
None,
)
}
Err(e) => {
return build_query_task_response(
req.task_id,
TaskStatus::Failed,
None,
Some(e.to_string()),
)
}
}
} else {
*current_task = Some(handle);
return build_query_task_response(req.task_id, TaskStatus::Proving, None, None);
}
} else {
let task_id = req.task_id.clone();
return build_query_task_response(
let mut result_guard = self.result.lock().unwrap();
let resp = match result_guard.as_ref() {
Ok(proof) => build_query_task_response(
req.task_id,
TaskStatus::Success,
Some(proof.clone()),
None,
),
Err(e) => build_query_task_response(
req.task_id,
TaskStatus::Failed,
None,
Some(String::from(&format!(
"failed to query task, task_id: {}",
task_id
))),
);
}
Some(e.to_string()),
),
};
*result_guard = Err(anyhow::Error::msg("prover not started"));
resp
}
}

impl LocalProver {
pub fn new(config: LocalProverConfig) -> Self {
pub fn new(config: LocalProverConfig, prover_types: Vec<ProverType>) -> Self {
let circuits_handler_provider = CircuitsHandlerProvider::new(config.clone())
.context("failed to create circuits handler provider")
.unwrap();

Self {
config,
prover_types,
circuits_handler_provider: RwLock::new(circuits_handler_provider),
current_task: Arc::new(Mutex::new(None)),
next_task_id: Arc::new(Mutex::new(0)),
result: Arc::new(Mutex::new(Err(anyhow::Error::msg("prover not started")))),
}
}

fn do_prove(
async fn do_prove(
&self,
req: ProveRequest,
handler: Arc<Box<dyn CircuitsHandler>>,
) -> Result<ProveResponse> {
let mut current_task = self.current_task.lock().unwrap();
if current_task.is_some() {
return Err(anyhow::Error::msg("prover working on previous task"));
}

let task_id = {
let mut next_task_id = self.next_task_id.lock().unwrap();
*next_task_id += 1;
*next_task_id
};

let req_clone = req.clone();
let handle = tokio::spawn(async move { handler.get_proof_data(req_clone).await });
*current_task = Some(handle);

let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let created_at = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;

let result = handler.get_proof_data(req.clone()).await;
*self.result.lock().unwrap() = result;

Ok(ProveResponse {
task_id: task_id.to_string(),
circuit_type: req.circuit_type,
Expand Down
24 changes: 0 additions & 24 deletions prover/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
use env_logger::Env;
use std::{fs::OpenOptions, sync::Once};

use crate::types::ProverType;
use scroll_proving_sdk::prover::types::CircuitType;

static LOG_INIT: Once = Once::new();

/// Initialize log
pub fn log_init(log_file: Option<String>) {
LOG_INIT.call_once(|| {
let mut builder = env_logger::Builder::from_env(Env::default().default_filter_or("info"));
if let Some(file_path) = log_file {
let target = Box::new(
OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(file_path)
.expect("Can't create log file"),
);
builder.target(env_logger::Target::Pipe(target));
}
builder.init();
});
}

pub fn get_circuit_types(prover_type: ProverType) -> Vec<CircuitType> {
match prover_type {
ProverType::Chunk => vec![CircuitType::Chunk],
Expand Down
18 changes: 0 additions & 18 deletions prover/src/version.rs

This file was deleted.

Loading

0 comments on commit 5ee0d7a

Please sign in to comment.