Skip to content

Commit cc9037a

Browse files
committed
feat(host): enable zk_any for batch API
1 parent aa6b71c commit cc9037a

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

host/src/server/api/v3/proof/batch.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::{
44
api::v3::{ProofResponse, Status},
55
handler::prove_many,
66
prove_aggregation,
7-
utils::{is_zk_any_request, to_v3_status},
7+
utils::{
8+
draw_for_zk_any_batch_request, fulfill_sp1_params, is_zk_any_request, to_v3_status,
9+
},
810
},
911
};
1012
use axum::{extract::State, routing::post, Json, Router};
@@ -39,22 +41,34 @@ use utoipa::OpenApi;
3941
/// - risc0 - uses the risc0 prover
4042
async fn batch_handler(
4143
State(actor): State<Actor>,
42-
Json(batch_request_opt): Json<Value>,
44+
Json(mut batch_request_opt): Json<Value>,
4345
) -> HostResult<Status> {
4446
if is_zk_any_request(&batch_request_opt) {
45-
return Ok(Status::Ok {
46-
proof_type: ProofType::Native,
47-
data: ProofResponse::Status {
48-
status: TaskStatus::ZKAnyNotDrawn,
49-
},
50-
});
47+
fulfill_sp1_params(&mut batch_request_opt);
5148
}
5249

5350
let batch_request = {
5451
// Override the existing proof request config from the config file and command line
5552
// options with the request from the client, and convert to a BatchProofRequest.
5653
let mut opts = serde_json::to_value(actor.default_request_config())?;
5754
merge(&mut opts, &batch_request_opt);
55+
56+
// For zk_any request, draw zk proof type based on the block hash.
57+
if is_zk_any_request(&opts) {
58+
tracing::info!("bilibili is_zk_any");
59+
match draw_for_zk_any_batch_request(&actor, &opts).await? {
60+
Some(proof_type) => opts["proof_type"] = serde_json::to_value(proof_type).unwrap(),
61+
None => {
62+
return Ok(Status::Ok {
63+
proof_type: ProofType::Native,
64+
data: ProofResponse::Status {
65+
status: TaskStatus::ZKAnyNotDrawn,
66+
},
67+
});
68+
}
69+
}
70+
}
71+
5872
let batch_request_opt: BatchProofRequestOpt = serde_json::from_value(opts)?;
5973
let batch_request: BatchProofRequest = batch_request_opt.try_into()?;
6074

host/src/server/utils.rs

+27
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ pub async fn draw_for_zk_any_request(
9090
Ok(actor.draw(&blockhash))
9191
}
9292

93+
pub async fn draw_for_zk_any_batch_request(
94+
actor: &Actor,
95+
batch_proof_request_opt: &Value,
96+
) -> HostResult<Option<ProofType>> {
97+
let l1_network =
98+
batch_proof_request_opt["l1_network"]
99+
.as_str()
100+
.ok_or(RaikoError::InvalidRequestConfig(
101+
"Missing network".to_string(),
102+
))?;
103+
let batches =
104+
batch_proof_request_opt["batches"]
105+
.as_array()
106+
.ok_or(RaikoError::InvalidRequestConfig(
107+
"Missing batches".to_string(),
108+
))?;
109+
let first_batch = batches.first().ok_or(RaikoError::InvalidRequestConfig(
110+
"batches is empty".to_string(),
111+
))?;
112+
let l1_inclusion_block_number = first_batch["l1_inclusion_block_number"].as_u64().ok_or(
113+
RaikoError::InvalidRequestConfig("Missing l1_inclusion_block_number".to_string()),
114+
)?;
115+
let (_, blockhash) =
116+
get_task_data(&l1_network, l1_inclusion_block_number, actor.chain_specs()).await?;
117+
Ok(actor.draw(&blockhash))
118+
}
119+
93120
pub fn fulfill_sp1_params(req: &mut Value) {
94121
let zk_any_opts = req["zk_any"].as_object().clone();
95122
let sp1_recursion = match zk_any_opts {

0 commit comments

Comments
 (0)