Skip to content

Commit ce153f7

Browse files
fix: spawn blocking for verification (#697)
Co-authored-by: NicolasRampoldi <58613770+NicolasRampoldi@users.noreply.github.com>
1 parent 83c4254 commit ce153f7

5 files changed

Lines changed: 29 additions & 16 deletions

File tree

batcher/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

batcher/aligned-batcher/Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ bytes = "1.6.0"
1919
hex = "0.4.3"
2020
dotenv = "0.15.0"
2121
anyhow = "1.0.83"
22-
ethers = { tag = "v2.0.15-fix-reconnections", features = ["ws", "rustls"], git = "https://github.com/yetanotherco/ethers-rs.git" }
22+
ethers = { tag = "v2.0.15-fix-reconnections", features = [
23+
"ws",
24+
"rustls",
25+
], git = "https://github.com/yetanotherco/ethers-rs.git" }
2326
lambdaworks-crypto = { version = "0.7.0", features = ["serde"] }
2427
serde_yaml = "0.9.34"
2528
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", rev = "v1.0.1" }
26-
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag="v1.0.1" }
29+
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag = "v1.0.1" }
2730
halo2curves = { version = "0.6.0", default-features = false }
28-
halo2_backend = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", rev = "a3a56819d9183ac0b11c8d0543c7673c4a4c71a6"}
29-
halo2_proofs = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", rev = "a3a56819d9183ac0b11c8d0543c7673c4a4c71a6"}
30-
lazy_static = "1.4.0"
31+
halo2_backend = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", rev = "a3a56819d9183ac0b11c8d0543c7673c4a4c71a6" }
32+
halo2_proofs = { git = "https://github.com/yetanotherco/yet-another-halo2-fork.git", rev = "a3a56819d9183ac0b11c8d0543c7673c4a4c71a6" }
3133
bincode = "1.3.3"
32-
aligned-sdk = { path = "../aligned-sdk"}
34+
aligned-sdk = { path = "../aligned-sdk" }

batcher/aligned-batcher/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ impl Batcher {
239239
serde_json::from_str(message.to_text().expect("Message is not text"))
240240
.expect("Failed to deserialize task");
241241

242+
info!(
243+
"Received message with nonce: {}",
244+
U256::from_big_endian(client_msg.verification_data.nonce.as_slice())
245+
);
246+
242247
info!("Verifying message signature...");
243248
if let Ok(addr) = client_msg.verify_signature() {
244249
info!("Message signature verified");
@@ -270,7 +275,7 @@ impl Batcher {
270275

271276
// When pre-verification is enabled, batcher will verify proofs for faster feedback with clients
272277
if self.pre_verification_is_enabled
273-
&& !zk_utils::verify(&nonced_verification_data.verification_data)
278+
&& !zk_utils::verify(&nonced_verification_data.verification_data).await
274279
{
275280
error!("Invalid proof detected. Verification failed.");
276281
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidProof).await;
@@ -703,7 +708,7 @@ impl Batcher {
703708
if client_msg.verification_data.verification_data.proof.len() <= self.max_proof_size {
704709
// When pre-verification is enabled, batcher will verify proofs for faster feedback with clients
705710
if self.pre_verification_is_enabled
706-
&& !zk_utils::verify(&client_msg.verification_data.verification_data)
711+
&& !zk_utils::verify(&client_msg.verification_data.verification_data).await
707712
{
708713
error!("Invalid proof detected. Verification failed.");
709714
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidProof).await;

batcher/aligned-batcher/src/sp1/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use lazy_static::lazy_static;
21
use log::{debug, warn};
32
use sp1_sdk::ProverClient;
3+
use std::sync::OnceLock;
44

5-
lazy_static! {
6-
static ref SP1_PROVER_CLIENT: ProverClient = ProverClient::new();
7-
}
5+
static SP1_PROVER_CLIENT: OnceLock<ProverClient> = OnceLock::new();
86

97
pub fn verify_sp1_proof(proof: &[u8], elf: &[u8]) -> bool {
108
debug!("Verifying SP1 proof");
11-
let (_pk, vk) = SP1_PROVER_CLIENT.setup(elf);
9+
let prover_client = SP1_PROVER_CLIENT.get_or_init(ProverClient::new);
10+
11+
let (_pk, vk) = prover_client.setup(elf);
1212
if let Ok(proof) = bincode::deserialize(proof) {
13-
let res = SP1_PROVER_CLIENT.verify(&proof, &vk).is_ok();
13+
let res = prover_client.verify(&proof, &vk).is_ok();
1414
debug!("SP1 proof is valid: {}", res);
1515
if res {
1616
return true;

batcher/aligned-batcher/src/zk_utils/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ use crate::sp1::verify_sp1_proof;
66
use aligned_sdk::core::types::{ProvingSystemId, VerificationData};
77
use log::{debug, warn};
88

9-
pub(crate) fn verify(verification_data: &VerificationData) -> bool {
9+
pub(crate) async fn verify(verification_data: &VerificationData) -> bool {
10+
let verification_data = verification_data.clone();
11+
tokio::task::spawn_blocking(move || verify_internal(&verification_data))
12+
.await
13+
.unwrap_or(false)
14+
}
15+
16+
fn verify_internal(verification_data: &VerificationData) -> bool {
1017
match verification_data.proving_system {
1118
ProvingSystemId::SP1 => {
1219
if let Some(elf) = &verification_data.vm_program_code {

0 commit comments

Comments
 (0)