From 1aa23405da5cbc3b33c02d2b01a1aeee58ef76e8 Mon Sep 17 00:00:00 2001 From: han0110 Date: Mon, 16 Feb 2026 11:42:10 +0000 Subject: [PATCH] refactor: move `block_on` helper to crate `ere-zkvm-interface` behind `tokio` feature gate --- Cargo.lock | 3 +-- crates/dockerized/Cargo.toml | 4 ++-- crates/dockerized/src/zkvm.rs | 22 +++------------------ crates/zkvm-interface/Cargo.toml | 2 ++ crates/zkvm-interface/src/zkvm.rs | 6 ++++++ crates/zkvm-interface/src/zkvm/tokio.rs | 14 +++++++++++++ crates/zkvm/sp1/Cargo.toml | 10 ++++++++-- crates/zkvm/sp1/src/zkvm.rs | 16 ++------------- crates/zkvm/zisk/Cargo.toml | 14 ++++++++----- crates/zkvm/zisk/src/zkvm/cluster_client.rs | 16 +-------------- 10 files changed, 48 insertions(+), 59 deletions(-) create mode 100644 crates/zkvm-interface/src/zkvm/tokio.rs diff --git a/Cargo.lock b/Cargo.lock index 1ad830f..02d082d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4303,7 +4303,6 @@ dependencies = [ "sp1-sdk", "tempfile", "thiserror 2.0.12", - "tokio", "tracing", ] @@ -4355,7 +4354,6 @@ dependencies = [ "strum 0.27.2", "tempfile", "thiserror 2.0.12", - "tokio", "tonic 0.14.3", "tracing", "uuid 1.17.0", @@ -4377,6 +4375,7 @@ dependencies = [ "serde_yaml", "strum 0.27.2", "thiserror 2.0.12", + "tokio", "toml 0.8.23", ] diff --git a/crates/dockerized/Cargo.toml b/crates/dockerized/Cargo.toml index 4a72962..ee655e6 100644 --- a/crates/dockerized/Cargo.toml +++ b/crates/dockerized/Cargo.toml @@ -10,11 +10,11 @@ anyhow.workspace = true serde = { workspace = true, features = ["derive"] } tempfile.workspace = true thiserror.workspace = true -tokio = { workspace = true, features = ["rt-multi-thread"] } +tokio.workspace = true tracing.workspace = true # Local dependencies -ere-zkvm-interface = { workspace = true, features = ["clap"] } +ere-zkvm-interface = { workspace = true, features = ["clap", "tokio"] } ere-common.workspace = true ere-server.workspace = true diff --git a/crates/dockerized/src/zkvm.rs b/crates/dockerized/src/zkvm.rs index 81e1a52..80cea46 100644 --- a/crates/dockerized/src/zkvm.rs +++ b/crates/dockerized/src/zkvm.rs @@ -16,18 +16,14 @@ use ere_server::{ api::twirp::reqwest::Client, client::{self, Url, zkVMClient}, }; -use ere_zkvm_interface::{ - CommonError, - zkvm::{ - Input, ProgramExecutionReport, ProgramProvingReport, Proof, ProofKind, ProverResource, - PublicValues, zkVM, - }, +use ere_zkvm_interface::zkvm::{ + CommonError, Input, ProgramExecutionReport, ProgramProvingReport, Proof, ProofKind, + ProverResource, PublicValues, block_on, zkVM, }; use std::{ future::Future, iter, pin::Pin, - sync::OnceLock, time::{Duration, Instant}, }; use tempfile::TempDir; @@ -464,18 +460,6 @@ async fn wait_until_healthy(endpoint: &Url, http_client: Client) -> Result<(), E } } -fn block_on(future: impl Future) -> T { - match tokio::runtime::Handle::try_current() { - Ok(handle) => tokio::task::block_in_place(|| handle.block_on(future)), - Err(_) => { - static FALLBACK_RT: OnceLock = OnceLock::new(); - FALLBACK_RT - .get_or_init(|| tokio::runtime::Runtime::new().expect("Failed to create runtime")) - .block_on(future) - } - } -} - #[cfg(test)] mod test { use crate::{ diff --git a/crates/zkvm-interface/Cargo.toml b/crates/zkvm-interface/Cargo.toml index 30fa2af..13fb65e 100644 --- a/crates/zkvm-interface/Cargo.toml +++ b/crates/zkvm-interface/Cargo.toml @@ -16,6 +16,7 @@ thiserror.workspace = true # Optional dependencies clap = { workspace = true, features = ["derive"], optional = true } +tokio = { workspace = true, features = ["rt-multi-thread"], optional = true } [dev-dependencies] bincode = { workspace = true, features = ["alloc", "serde"] } @@ -29,3 +30,4 @@ workspace = true [features] default = [] clap = ["dep:clap"] +tokio = ["dep:tokio"] diff --git a/crates/zkvm-interface/src/zkvm.rs b/crates/zkvm-interface/src/zkvm.rs index 8f51cd3..887e797 100644 --- a/crates/zkvm-interface/src/zkvm.rs +++ b/crates/zkvm-interface/src/zkvm.rs @@ -8,11 +8,17 @@ mod proof; mod report; mod resource; +#[cfg(feature = "tokio")] +mod tokio; + pub use error::CommonError; pub use proof::{Proof, ProofKind}; pub use report::{ProgramExecutionReport, ProgramProvingReport}; pub use resource::{ProverResource, ProverResourceKind, RemoteProverConfig}; +#[cfg(feature = "tokio")] +pub use tokio::block_on; + /// Input for the prover to execute/prove a guest program. #[derive(Clone, Debug, Default)] pub struct Input { diff --git a/crates/zkvm-interface/src/zkvm/tokio.rs b/crates/zkvm-interface/src/zkvm/tokio.rs new file mode 100644 index 0000000..576862c --- /dev/null +++ b/crates/zkvm-interface/src/zkvm/tokio.rs @@ -0,0 +1,14 @@ +use std::sync::OnceLock; + +/// Run a future to completion, reusing the current tokio runtime or creating one. +pub fn block_on(future: impl Future) -> T { + match tokio::runtime::Handle::try_current() { + Ok(handle) => tokio::task::block_in_place(|| handle.block_on(future)), + Err(_) => { + static FALLBACK_RT: OnceLock = OnceLock::new(); + FALLBACK_RT + .get_or_init(|| tokio::runtime::Runtime::new().expect("Failed to create runtime")) + .block_on(future) + } + } +} diff --git a/crates/zkvm/sp1/Cargo.toml b/crates/zkvm/sp1/Cargo.toml index 84bb6bc..1f1fed9 100644 --- a/crates/zkvm/sp1/Cargo.toml +++ b/crates/zkvm/sp1/Cargo.toml @@ -11,7 +11,6 @@ bincode = { workspace = true, features = ["alloc", "serde"] } serde.workspace = true tempfile.workspace = true thiserror.workspace = true -tokio = { workspace = true, features = ["rt-multi-thread"], optional = true } tracing.workspace = true # SP1 dependencies @@ -34,7 +33,14 @@ ere-build-utils.workspace = true [features] default = ["compiler", "zkvm"] compiler = ["dep:ere-compile-utils"] -zkvm = ["dep:tokio", "dep:sp1-cuda", "dep:sp1-hypercube", "dep:sp1-p3-field", "dep:sp1-recursion-executor", "dep:sp1-sdk"] +zkvm = [ + "dep:sp1-cuda", + "dep:sp1-hypercube", + "dep:sp1-p3-field", + "dep:sp1-recursion-executor", + "dep:sp1-sdk", + "ere-zkvm-interface/tokio", +] [lints] workspace = true diff --git a/crates/zkvm/sp1/src/zkvm.rs b/crates/zkvm/sp1/src/zkvm.rs index 230bc6d..077cf38 100644 --- a/crates/zkvm/sp1/src/zkvm.rs +++ b/crates/zkvm/sp1/src/zkvm.rs @@ -2,10 +2,10 @@ use crate::{program::SP1Program, zkvm::sdk::SP1Sdk}; use anyhow::bail; use ere_zkvm_interface::zkvm::{ CommonError, Input, ProgramExecutionReport, ProgramProvingReport, Proof, ProofKind, - ProverResource, PublicValues, zkVM, zkVMProgramDigest, + ProverResource, PublicValues, block_on, zkVM, zkVMProgramDigest, }; use sp1_sdk::{SP1ProofMode, SP1ProofWithPublicValues, SP1Stdin, SP1VerifyingKey}; -use std::{future::Future, sync::OnceLock, time::Instant}; +use std::time::Instant; use tracing::info; mod error; @@ -129,18 +129,6 @@ fn input_to_stdin(input: &Input) -> Result { Ok(stdin) } -fn block_on(future: impl Future) -> T { - match tokio::runtime::Handle::try_current() { - Ok(handle) => tokio::task::block_in_place(|| handle.block_on(future)), - Err(_) => { - static FALLBACK_RT: OnceLock = OnceLock::new(); - FALLBACK_RT - .get_or_init(|| tokio::runtime::Runtime::new().expect("Failed to create runtime")) - .block_on(future) - } - } -} - #[cfg(test)] mod tests { use crate::{compiler::RustRv32imaCustomized, program::SP1Program, zkvm::EreSP1}; diff --git a/crates/zkvm/zisk/Cargo.toml b/crates/zkvm/zisk/Cargo.toml index 7dba35b..11d437b 100644 --- a/crates/zkvm/zisk/Cargo.toml +++ b/crates/zkvm/zisk/Cargo.toml @@ -16,8 +16,7 @@ serde.workspace = true strum = { workspace = true, features = ["derive"] } tempfile.workspace = true thiserror.workspace = true -tokio = { workspace = true, features = ["rt-multi-thread"] } -tonic.workspace = true +tonic = { workspace = true, optional = true } tracing.workspace = true uuid = { workspace = true, features = ["v4"] } wait-timeout.workspace = true @@ -27,8 +26,8 @@ ere-compile-utils = { workspace = true, optional = true } ere-zkvm-interface.workspace = true # Zisk dependencies -zisk-distributed-grpc-api.workspace = true -zisk-proofman-verifier.workspace = true +zisk-distributed-grpc-api = { workspace = true, optional = true } +zisk-proofman-verifier = { workspace = true, optional = true } [dev-dependencies] ere-test-utils = { workspace = true, features = ["host"] } @@ -39,7 +38,12 @@ ere-build-utils.workspace = true [features] default = ["compiler", "zkvm"] compiler = ["dep:ere-compile-utils"] -zkvm = [] +zkvm = [ + "dep:tonic", + "dep:zisk-distributed-grpc-api", + "dep:zisk-proofman-verifier", + "ere-zkvm-interface/tokio", +] [lints] workspace = true diff --git a/crates/zkvm/zisk/src/zkvm/cluster_client.rs b/crates/zkvm/zisk/src/zkvm/cluster_client.rs index ba27897..dcc36e7 100644 --- a/crates/zkvm/zisk/src/zkvm/cluster_client.rs +++ b/crates/zkvm/zisk/src/zkvm/cluster_client.rs @@ -1,9 +1,8 @@ //! Remote ZisK cluster proving. use crate::zkvm::Error; -use ere_zkvm_interface::zkvm::RemoteProverConfig; +use ere_zkvm_interface::zkvm::{RemoteProverConfig, block_on}; use futures_util::StreamExt; -use std::sync::OnceLock; use std::time::Duration; use tonic::transport::Channel; use tracing::debug; @@ -154,19 +153,6 @@ async fn connect(endpoint: &str) -> Result, Er Ok(ZiskDistributedApiClient::new(channel)) } -/// Run a future to completion, reusing the current tokio runtime or creating one. -fn block_on(future: impl Future) -> T { - match tokio::runtime::Handle::try_current() { - Ok(handle) => tokio::task::block_in_place(|| handle.block_on(future)), - Err(_) => { - static FALLBACK_RT: OnceLock = OnceLock::new(); - FALLBACK_RT - .get_or_init(|| tokio::runtime::Runtime::new().expect("Failed to create runtime")) - .block_on(future) - } - } -} - /// Returns `Error::ClusterError`. fn cluster_error(s: impl ToString) -> Error { Error::ClusterError(s.to_string())