Skip to content

Commit

Permalink
chore: apply review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Feb 17, 2025
1 parent dbac6a0 commit 6d7e1de
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions host/src/server/api/v2/proof/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::{extract::State, routing::post, Json, Router};
use raiko_core::interfaces::RaikoError;
use raiko_core::interfaces::{ProofRequestOpt, RaikoError};
use raiko_core::{interfaces::ProofRequest, provider::get_task_data};
use raiko_lib::proof_type::ProofType;
use raiko_reqpool::{SingleProofRequestEntity, SingleProofRequestKey};
Expand Down Expand Up @@ -45,21 +45,8 @@ async fn proof_handler(State(actor): State<Actor>, Json(req): Json<Value>) -> Ho
config.merge(&req)?;

// For zk_any request, draw zk proof type based on the block hash.
//
// A zk_any request looks like: { "proof_type": "zk_any", "zk_any": { "aggregation": <bool> } }
let is_zk_any = config.proof_type == Some("zk_any".to_string());
if is_zk_any {
let network = config
.network
.as_ref()
.ok_or(RaikoError::InvalidRequestConfig(
"Missing network".to_string(),
))?;
let block_number = config.block_number.ok_or(RaikoError::InvalidRequestConfig(
"Missing block number".to_string(),
))?;
let (_, blockhash) = get_task_data(&network, block_number, actor.chain_specs()).await?;
match actor.draw(&blockhash) {
if is_zk_any_request(&config) {
match draw_for_zk_any_request(&actor, &config).await? {
Some(proof_type) => config.proof_type = Some(proof_type.to_string()),
None => {
return Ok(Status::Ok {
Expand All @@ -70,21 +57,13 @@ async fn proof_handler(State(actor): State<Actor>, Json(req): Json<Value>) -> Ho
});
}
}
}
if is_zk_any && config.proof_type == Some(ProofType::Sp1.to_string()) {
// Parse req, extract the aggregation field
// { "proof_type": "zk_any", "zk_any": { "aggregation": <bool> } }
let aggregation = req["zk_any"]["aggregation"].as_bool().unwrap_or(false);
let mut sp1_opts = config
.prover_args
.sp1
.expect("config.merge() should have set sp1");
if aggregation {
sp1_opts["recursion"] = serde_json::Value::String("compressed".to_string());
} else {
sp1_opts["recursion"] = serde_json::Value::String("plonk".to_string());
// Specially process zk_any requests with sp1 parameters.
if config.proof_type == Some(ProofType::Sp1.to_string()) {
// Parse req, extract the aggregation field
// { "proof_type": "zk_any", "zk_any": { "aggregation": <bool> } }
let sp1_opts = sp1_params_for_zk_any_request(&req, &config);
config.prover_args.sp1 = Some(sp1_opts);
}
config.prover_args.sp1 = Some(sp1_opts);
}

// Construct the actual proof request from the available configs.
Expand Down Expand Up @@ -151,3 +130,43 @@ pub fn create_router() -> Router<Actor> {
.nest("/list", list::create_router())
.nest("/prune", prune::create_router())
}

// A zk_any request looks like: { "proof_type": "zk_any", "zk_any": { "aggregation": <bool> } }
fn is_zk_any_request(proof_request_opt: &ProofRequestOpt) -> bool {
proof_request_opt.proof_type == Some("zk_any".to_string())
}

async fn draw_for_zk_any_request(
actor: &Actor,
proof_request_opt: &ProofRequestOpt,
) -> HostResult<Option<ProofType>> {
let network = proof_request_opt
.network
.as_ref()
.ok_or(RaikoError::InvalidRequestConfig(
"Missing network".to_string(),
))?;
let block_number = proof_request_opt
.block_number
.ok_or(RaikoError::InvalidRequestConfig(
"Missing block number".to_string(),
))?;
let (_, blockhash) = get_task_data(&network, block_number, actor.chain_specs()).await?;
Ok(actor.draw(&blockhash))
}

fn sp1_params_for_zk_any_request(req: &Value, proof_request_opt: &ProofRequestOpt) -> Value {
let aggregation = req["zk_any"]["aggregation"].as_bool().unwrap_or(false);
let mut sp1_opts = proof_request_opt
.prover_args
.sp1
.as_ref()
.expect("config.merge() should have set sp1")
.to_owned();
if aggregation {
sp1_opts["recursion"] = serde_json::Value::String("compressed".to_string());
} else {
sp1_opts["recursion"] = serde_json::Value::String("plonk".to_string());
}
sp1_opts
}

0 comments on commit 6d7e1de

Please # to comment.