From bea8bb182d72233496d187aa41c35982edc5c6b3 Mon Sep 17 00:00:00 2001 From: nhtyy Date: Wed, 20 Nov 2024 14:41:06 -0800 Subject: [PATCH 1/3] feat: more flags in sdk, new dep conflicting macro, rm network builder --- Cargo.lock | 12 +++++ crates/prover/Cargo.toml | 2 + crates/prover/src/types.rs | 2 + crates/sdk/Cargo.toml | 6 ++- crates/sdk/src/install.rs | 23 +++++----- crates/sdk/src/lib.rs | 94 +++++++++++--------------------------- 6 files changed, 59 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 21b1074ae0..a6fbad4c1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1463,6 +1463,17 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "conflicting" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39614d6ae60f980e4d1b0aa93c4b4305304cdf9e74cef76e8895fb08e0c403f3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "console" version = "0.15.8" @@ -5944,6 +5955,7 @@ dependencies = [ "backoff", "bincode", "cfg-if", + "conflicting", "dirs", "futures", "hashbrown 0.14.5", diff --git a/crates/prover/Cargo.toml b/crates/prover/Cargo.toml index 226b902c60..01041a40fa 100644 --- a/crates/prover/Cargo.toml +++ b/crates/prover/Cargo.toml @@ -82,6 +82,8 @@ required-features = ["export-tests"] [features] +cuda = [] +network = [] native-gnark = ["sp1-recursion-gnark-ffi/native"] export-tests = ["dep:test-artifacts"] debug = ["sp1-core-machine/debug"] diff --git a/crates/prover/src/types.rs b/crates/prover/src/types.rs index b4e362e0fc..14f5ef5ff6 100644 --- a/crates/prover/src/types.rs +++ b/crates/prover/src/types.rs @@ -188,7 +188,9 @@ impl SP1Bn254ProofData { pub enum ProverMode { #[default] Cpu, + #[cfg(feature = "cuda")] Cuda, + #[cfg(feature = "network")] Network, #[value(skip)] Mock, diff --git a/crates/sdk/Cargo.toml b/crates/sdk/Cargo.toml index a55b6caf39..fae8210395 100644 --- a/crates/sdk/Cargo.toml +++ b/crates/sdk/Cargo.toml @@ -52,6 +52,7 @@ alloy-signer = { version = "0.5", optional = true } alloy-signer-local = { version = "0.5", optional = true } alloy-primitives = { version = "0.8", optional = true } backoff = { version = "0.4", features = ["tokio"], optional = true } +conflicting = "0.1.0" [dev-dependencies] test-artifacts = { workspace = true } @@ -62,6 +63,7 @@ native-gnark = ["sp1-prover/native-gnark"] # TODO: Once alloy has a 1.* release, we can likely remove this feature flag, as there will be less # dependency resolution issues. network = [ + "sp1-prover/network", "dep:prost", "dep:alloy-sol-types", "dep:tokio", @@ -71,7 +73,9 @@ network = [ "dep:twirp", "dep:reqwest-middleware", ] + network-v2 = [ + "sp1-prover/network", "dep:prost", "dep:alloy-sol-types", "dep:alloy-signer", @@ -85,7 +89,7 @@ network-v2 = [ "dep:tonic", "dep:backoff", ] -cuda = ["sp1-cuda"] +cuda = ["sp1-cuda", "sp1-prover/cuda"] [build-dependencies] vergen = { version = "8", default-features = false, features = [ diff --git a/crates/sdk/src/install.rs b/crates/sdk/src/install.rs index b3207664ba..06bf68ef1d 100644 --- a/crates/sdk/src/install.rs +++ b/crates/sdk/src/install.rs @@ -1,4 +1,3 @@ -use cfg_if::cfg_if; use std::path::PathBuf; #[cfg(any(feature = "network", feature = "network-v2"))] @@ -42,18 +41,18 @@ pub fn try_install_circuit_artifacts(artifacts_type: &str) -> PathBuf { build_dir.display() ); } else { - cfg_if! { - if #[cfg(any(feature = "network", feature = "network-v2"))] { - println!( - "[sp1] {} circuit artifacts for version {} do not exist at {}. downloading...", - artifacts_type, - SP1_CIRCUIT_VERSION, - build_dir.display() - ); - install_circuit_artifacts(build_dir.clone(), artifacts_type); - } - } + #[cfg(any(feature = "network", feature = "network-v2"))] + { + println!( + "[sp1] {} circuit artifacts for version {} do not exist at {}. downloading...", + artifacts_type, + SP1_CIRCUIT_VERSION, + build_dir.display() + ); + install_circuit_artifacts(build_dir.clone(), artifacts_type); + }; } + build_dir } diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 3c40bcb9d6..8abc5a312e 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -48,6 +48,9 @@ pub use sp1_prover::{ SP1VerifyingKey, }; +#[cfg(any(feature = "network", feature = "network-v2"))] +use conflicting::conflicting; + /// A client for interacting with SP1. pub struct ProverClient { /// The underlying prover implementation. @@ -71,6 +74,7 @@ impl ProverClient { /// std::env::set_var("SP1_PROVER", "local"); /// let client = ProverClient::new(); /// ``` + #[deprecated = "Use `ProverClient::builder` instead"] pub fn new() -> Self { #[allow(unreachable_code)] match env::var("SP1_PROVER").unwrap_or("local".to_string()).to_lowercase().as_str() { @@ -185,18 +189,18 @@ impl ProverClient { /// /// let client = ProverClient::network(private_key, rpc_url, skip_simulation); /// ``` + #[cfg(any(feature = "network", feature = "network-v2"))] pub fn network(private_key: String, rpc_url: Option, skip_simulation: bool) -> Self { - cfg_if! { - if #[cfg(feature = "network-v2")] { + conflicting! { + "network" => { Self { - prover: Box::new(NetworkProverV2::new(&private_key, rpc_url, skip_simulation)), + prover: Box::new(NetworkProverV1::new(&private_key, rpc_url, skip_simulation)), } - } else if #[cfg(feature = "network")] { + }, + "network-v2" => { Self { - prover: Box::new(NetworkProverV1::new(&private_key, rpc_url, skip_simulation)), + prover: Box::new(NetworkProverV2::new(&private_key, rpc_url, skip_simulation)), } - } else { - panic!("network feature is not enabled") } } } @@ -315,7 +319,7 @@ impl ProverClient { impl Default for ProverClient { fn default() -> Self { - Self::new() + Self::cpu() } } @@ -323,8 +327,11 @@ impl Default for ProverClient { #[derive(Debug, Default)] pub struct ProverClientBuilder { mode: Option, + #[cfg(any(feature = "network", feature = "network-v2"))] private_key: Option, + #[cfg(any(feature = "network", feature = "network-v2"))] rpc_url: Option, + #[cfg(any(feature = "network", feature = "network-v2"))] skip_simulation: bool, } @@ -336,18 +343,21 @@ impl ProverClientBuilder { } /// Sets the private key. + #[cfg(any(feature = "network", feature = "network-v2"))] pub fn private_key(mut self, private_key: String) -> Self { self.private_key = Some(private_key); self } /// Sets the RPC URL. + #[cfg(any(feature = "network", feature = "network-v2"))] pub fn rpc_url(mut self, rpc_url: String) -> Self { self.rpc_url = Some(rpc_url); self } /// Skips simulation. + #[cfg(any(feature = "network", feature = "network-v2"))] pub fn skip_simulation(mut self) -> Self { self.skip_simulation = true; self @@ -357,29 +367,24 @@ impl ProverClientBuilder { pub fn build(self) -> ProverClient { match self.mode.expect("The prover mode is required") { ProverMode::Cpu => ProverClient::cpu(), + #[cfg(feature = "cuda")] ProverMode::Cuda => { - cfg_if! { - if #[cfg(feature = "cuda")] { - ProverClient::cuda() - } else { - panic!("cuda feature is not enabled") - } - } + ProverClient::cuda() } + #[cfg(any(feature = "network", feature = "network-v2"))] ProverMode::Network => { let private_key = self.private_key.expect("The private key is required"); - cfg_if! { - if #[cfg(feature = "network-v2")] { + conflicting! { + "network" => { ProverClient { - prover: Box::new(NetworkProverV2::new(&private_key, self.rpc_url, self.skip_simulation)), + prover: Box::new(NetworkProverV1::new(&private_key, self.rpc_url, self.skip_simulation)), } - } else if #[cfg(feature = "network")] { + }, + "network-v2" => { ProverClient { - prover: Box::new(NetworkProverV1::new(&private_key, self.rpc_url, self.skip_simulation)), + prover: Box::new(NetworkProverV2::new(&private_key, self.rpc_url, self.skip_simulation)), } - } else { - panic!("network feature is not enabled") } } } @@ -388,51 +393,6 @@ impl ProverClientBuilder { } } -/// Builder type for network prover. -#[cfg(any(feature = "network", feature = "network-v2"))] -#[derive(Debug, Default)] -pub struct NetworkProverBuilder { - private_key: Option, - rpc_url: Option, - skip_simulation: bool, -} - -impl NetworkProverBuilder { - /// Sets the private key. - pub fn private_key(mut self, private_key: String) -> Self { - self.private_key = Some(private_key); - self - } - - /// Sets the RPC URL. - pub fn rpc_url(mut self, rpc_url: String) -> Self { - self.rpc_url = Some(rpc_url); - self - } - - /// Skips simulation. - pub fn skip_simulation(mut self) -> Self { - self.skip_simulation = true; - self - } - - /// Creates a new [NetworkProverV1]. - #[cfg(feature = "network")] - pub fn build(self) -> NetworkProverV1 { - let private_key = self.private_key.expect("The private key is required"); - - NetworkProverV1::new(&private_key, self.rpc_url, self.skip_simulation) - } - - /// Creates a new [NetworkProverV2]. - #[cfg(feature = "network-v2")] - pub fn build_v2(self) -> NetworkProverV2 { - let private_key = self.private_key.expect("The private key is required"); - - NetworkProverV2::new(&private_key, self.rpc_url, self.skip_simulation) - } -} - /// Utility method for blocking on an async function. /// /// If we're already in a tokio runtime, we'll block in place. Otherwise, we'll create a new From f90d28d97294687667ac29fb4e1b49c31eb077e0 Mon Sep 17 00:00:00 2001 From: nhtyy Date: Wed, 20 Nov 2024 15:07:57 -0800 Subject: [PATCH 2/3] fix: remove network builder use --- crates/sdk/src/network-v2/prover.rs | 9 ++------- crates/sdk/src/network/prover.rs | 7 +------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/crates/sdk/src/network-v2/prover.rs b/crates/sdk/src/network-v2/prover.rs index 83a86b55eb..6667bd1c22 100644 --- a/crates/sdk/src/network-v2/prover.rs +++ b/crates/sdk/src/network-v2/prover.rs @@ -3,7 +3,7 @@ use std::time::{Duration, Instant}; use crate::{ network_v2::client::NetworkClient, network_v2::proto::network::{ProofMode, ProofStatus, ProofStrategy}, - NetworkProverBuilder, Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, + Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, SP1ProvingKey, SP1VerifyingKey, }; use anyhow::Result; @@ -40,12 +40,7 @@ impl NetworkProver { let client = NetworkClient::new(private_key, rpc_url); Self { client, local_prover, skip_simulation } } - - /// Creates a new network prover builder. See [`NetworkProverBuilder`] for more details. - pub fn builder() -> NetworkProverBuilder { - NetworkProverBuilder::default() - } - + /// Requests a proof from the prover network, returning the request ID. pub async fn request_proof( &self, diff --git a/crates/sdk/src/network/prover.rs b/crates/sdk/src/network/prover.rs index 70969adf5b..cfce31f49b 100644 --- a/crates/sdk/src/network/prover.rs +++ b/crates/sdk/src/network/prover.rs @@ -5,7 +5,7 @@ use crate::{ client::NetworkClient, proto::network::{ProofMode, ProofStatus}, }, - NetworkProverBuilder, Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, + Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, SP1ProvingKey, SP1VerifyingKey, }; use anyhow::Result; @@ -39,11 +39,6 @@ impl NetworkProver { Self { client: NetworkClient::new(private_key, rpc_url), local_prover, skip_simulation } } - /// Creates a new network prover builder. See [`NetworkProverBuilder`] for more details. - pub fn builder() -> NetworkProverBuilder { - NetworkProverBuilder::default() - } - /// Requests a proof from the prover network, returning the proof ID. pub async fn request_proof( &self, From 6bab3e7b81c37156d4b731b76599c3ef12d95798 Mon Sep 17 00:00:00 2001 From: nhtyy Date: Wed, 20 Nov 2024 15:13:56 -0800 Subject: [PATCH 3/3] fix: fmt --- crates/sdk/src/install.rs | 2 +- crates/sdk/src/lib.rs | 4 +--- crates/sdk/src/network-v2/prover.rs | 5 ++--- crates/sdk/src/network/prover.rs | 3 +-- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/crates/sdk/src/install.rs b/crates/sdk/src/install.rs index 06bf68ef1d..ea21753c04 100644 --- a/crates/sdk/src/install.rs +++ b/crates/sdk/src/install.rs @@ -41,7 +41,7 @@ pub fn try_install_circuit_artifacts(artifacts_type: &str) -> PathBuf { build_dir.display() ); } else { - #[cfg(any(feature = "network", feature = "network-v2"))] + #[cfg(any(feature = "network", feature = "network-v2"))] { println!( "[sp1] {} circuit artifacts for version {} do not exist at {}. downloading...", diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 8abc5a312e..24e1bfac53 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -368,9 +368,7 @@ impl ProverClientBuilder { match self.mode.expect("The prover mode is required") { ProverMode::Cpu => ProverClient::cpu(), #[cfg(feature = "cuda")] - ProverMode::Cuda => { - ProverClient::cuda() - } + ProverMode::Cuda => ProverClient::cuda(), #[cfg(any(feature = "network", feature = "network-v2"))] ProverMode::Network => { let private_key = self.private_key.expect("The private key is required"); diff --git a/crates/sdk/src/network-v2/prover.rs b/crates/sdk/src/network-v2/prover.rs index 6667bd1c22..bf25b1fbaf 100644 --- a/crates/sdk/src/network-v2/prover.rs +++ b/crates/sdk/src/network-v2/prover.rs @@ -3,8 +3,7 @@ use std::time::{Duration, Instant}; use crate::{ network_v2::client::NetworkClient, network_v2::proto::network::{ProofMode, ProofStatus, ProofStrategy}, - Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, - SP1ProvingKey, SP1VerifyingKey, + Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, SP1ProvingKey, SP1VerifyingKey, }; use anyhow::Result; use backoff::{future::retry, ExponentialBackoff}; @@ -40,7 +39,7 @@ impl NetworkProver { let client = NetworkClient::new(private_key, rpc_url); Self { client, local_prover, skip_simulation } } - + /// Requests a proof from the prover network, returning the request ID. pub async fn request_proof( &self, diff --git a/crates/sdk/src/network/prover.rs b/crates/sdk/src/network/prover.rs index cfce31f49b..d88d778be6 100644 --- a/crates/sdk/src/network/prover.rs +++ b/crates/sdk/src/network/prover.rs @@ -5,8 +5,7 @@ use crate::{ client::NetworkClient, proto::network::{ProofMode, ProofStatus}, }, - Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, - SP1ProvingKey, SP1VerifyingKey, + Prover, SP1Context, SP1ProofKind, SP1ProofWithPublicValues, SP1ProvingKey, SP1VerifyingKey, }; use anyhow::Result; use sp1_core_machine::io::SP1Stdin;