From 2ffec90ec9d2b6f4f354aeff8ee83ed184c8e54b Mon Sep 17 00:00:00 2001 From: mattstam Date: Wed, 11 Dec 2024 18:37:11 -0800 Subject: [PATCH] small stuff --- crates/sdk/src/artifacts.rs | 1 - crates/sdk/src/client/builder.rs | 14 +++--- crates/sdk/src/local/prover.rs | 28 ++++++------ crates/sdk/src/network-v2/error.rs | 1 + crates/sdk/src/network-v2/prover.rs | 68 ++++++++++++++++------------- crates/sdk/src/verify.rs | 1 + 6 files changed, 62 insertions(+), 51 deletions(-) diff --git a/crates/sdk/src/artifacts.rs b/crates/sdk/src/artifacts.rs index 0b80b9129..ea614d679 100644 --- a/crates/sdk/src/artifacts.rs +++ b/crates/sdk/src/artifacts.rs @@ -72,7 +72,6 @@ pub fn export_solidity_groth16_bn254_verifier(output_dir: impl Into) -> Ok(()) } -#[cfg(feature = "network-v2")] pub async fn download_file( client: &Client, url: &str, diff --git a/crates/sdk/src/client/builder.rs b/crates/sdk/src/client/builder.rs index 3ea55a01a..e1619874e 100644 --- a/crates/sdk/src/client/builder.rs +++ b/crates/sdk/src/client/builder.rs @@ -28,7 +28,7 @@ impl ProverClientBuilder { pub fn local(self) -> ProverClientBuilder { ProverClientBuilder { inner_builder: LocalProver::builder() } } - + #[cfg(feature = "network-v2")] pub fn network(self) -> ProverClientBuilder { ProverClientBuilder { inner_builder: NetworkProver::builder() } @@ -46,7 +46,7 @@ impl ProverClientBuilder { pub fn with_default_timeout(mut self, timeout: u64) -> Self { self.inner_builder = self.inner_builder.with_default_timeout(timeout); - self + self } pub fn with_default_cycle_limit(mut self, cycle_limit: u64) -> Self { @@ -80,7 +80,7 @@ impl BuildableProver for LocalProverBuilder { fn build_prover(self) -> Box { Box::new(self.build()) } - + fn with_default_timeout(self, timeout: u64) -> Self { self.with_timeout(timeout) } @@ -96,11 +96,11 @@ impl BuildableProver for NetworkProverBuilder { Box::new(self.build()) } - fn with_default_cycle_limit(self, cycle_limit: u64) -> Self { - self.with_cycle_limit(cycle_limit) + fn with_default_timeout(self, timeout: u64) -> Self { + self.with_timeout(timeout) } - fn with_default_timeout(self, timeout: u64) -> Self { - self.with_timeout(timeout) + fn with_default_cycle_limit(self, cycle_limit: u64) -> Self { + self.with_cycle_limit(cycle_limit) } } diff --git a/crates/sdk/src/local/prover.rs b/crates/sdk/src/local/prover.rs index 49fc7ba01..16680b291 100644 --- a/crates/sdk/src/local/prover.rs +++ b/crates/sdk/src/local/prover.rs @@ -1,10 +1,10 @@ use crate::install::try_install_circuit_artifacts; -use crate::Mode; -use crate::ProofOpts; use crate::proof::{SP1Proof, SP1ProofWithPublicValues}; use crate::prover::Prover; use crate::{DEFAULT_CYCLE_LIMIT, DEFAULT_TIMEOUT}; use crate::verify; +use crate::Mode; +use crate::ProofOpts; use crate::SP1VerificationError; use anyhow::Result; @@ -29,15 +29,23 @@ pub struct LocalProver { impl Default for LocalProver { fn default() -> Self { + LocalProver::new() + } +} + +impl LocalProver { + /// Creates a new `LocalProver`. + /// + /// Uses default timeout and cycle limit. + pub fn new() -> Self { Self { prover: Arc::new(SP1Prover::new()), timeout: DEFAULT_TIMEOUT, cycle_limit: DEFAULT_CYCLE_LIMIT, } } -} -impl LocalProver { + /// Creates a new network prover builder. See [`LocalProverBuilder`] for more details. pub fn builder() -> LocalProverBuilder { LocalProverBuilder::new() } @@ -152,9 +160,7 @@ impl<'a> LocalProofRequest<'a> { version: String, sp1_prover_opts: SP1ProverOpts, ) -> Result { - let context = SP1Context::builder() - .max_cycles(cycle_limit) - .build(); + let context = SP1Context::builder().max_cycles(cycle_limit).build(); // Generate the core proof let proof: sp1_prover::SP1ProofWithMetadata = @@ -174,8 +180,7 @@ impl<'a> LocalProofRequest<'a> { let public_values = proof.public_values.clone(); // Generate the compressed proof - let reduce_proof = - prover.compress(&pk.vk, proof, deferred_proofs, sp1_prover_opts)?; + let reduce_proof = prover.compress(&pk.vk, proof, deferred_proofs, sp1_prover_opts)?; if mode == Mode::Compressed { return Ok(SP1ProofWithPublicValues { @@ -371,10 +376,7 @@ impl Default for LocalProverBuilder { impl LocalProverBuilder { pub fn new() -> Self { - Self { - timeout: None, - cycle_limit: None, - } + Self { timeout: None, cycle_limit: None } } pub fn with_timeout(mut self, timeout: u64) -> Self { diff --git a/crates/sdk/src/network-v2/error.rs b/crates/sdk/src/network-v2/error.rs index 9572656f4..21376a5c0 100644 --- a/crates/sdk/src/network-v2/error.rs +++ b/crates/sdk/src/network-v2/error.rs @@ -3,6 +3,7 @@ use tonic::Status; use crate::network_v2::types::RequestId; +/// An error that can occur when interacting with the prover network. #[derive(Error, Debug)] pub enum Error { #[error("Program simulation failed")] diff --git a/crates/sdk/src/network-v2/prover.rs b/crates/sdk/src/network-v2/prover.rs index 80e7ac5c5..1b5854ebd 100644 --- a/crates/sdk/src/network-v2/prover.rs +++ b/crates/sdk/src/network-v2/prover.rs @@ -15,7 +15,6 @@ use tokio::runtime::Runtime; use tokio::task; use tokio::time::sleep; -use crate::Mode; use crate::network_v2::retry::{self, with_retry}; use crate::network_v2::{ client::{NetworkClient, DEFAULT_PROVER_NETWORK_RPC}, @@ -23,21 +22,20 @@ use crate::network_v2::{ types::{HashType, RequestId, VerifyingKeyHash}, Error, }; -use crate::ProofOpts; use crate::proof::SP1ProofWithPublicValues; use crate::prover::Prover; -use crate::{DEFAULT_CYCLE_LIMIT, DEFAULT_TIMEOUT}; -use crate::verify; use crate::SP1VerificationError; +use crate::{verify, ProofOpts}; +use crate::{DEFAULT_CYCLE_LIMIT, DEFAULT_TIMEOUT}; /// The default fulfillment strategy to use for proof requests. pub const DEFAULT_FULFILLMENT_STRATEGY: FulfillmentStrategy = FulfillmentStrategy::Hosted; /// The minimum allowed timeout for a proof request to be fulfilled (10 seconds). -pub const MIN_TIMEOUT_SECS: u64 = 10; +pub const MIN_TIMEOUT: u64 = 10; /// The maximum allowed timeout for a proof request to be fulfilled (24 hours). -pub const MAX_TIMEOUT_SECS: u64 = 86400; +pub const MAX_TIMEOUT: u64 = 86400; /// The number of seconds to wait between checking the status of a proof request. pub const STATUS_CHECK_INTERVAL_SECS: u64 = 2; @@ -47,26 +45,25 @@ pub struct NetworkProver { prover: Arc>, network_client: NetworkClient, timeout: u64, - max_cycle_limit: Option, - + cycles_limit: u64, } pub struct NetworkProverBuilder { rpc_url: Option, private_key: Option, timeout: Option, - max_cycle_limit: Option, + cycle_limit: Option, } impl NetworkProver { - /// Creates a new `NetworkProver` with the given private private_key. + /// Creates a new [`NetworkProver`] with the given private private_key. /// This function uses default timeout and cycle limit. pub fn new(rpc_url: String, private_key: String) -> Self { Self { prover: Arc::new(SP1Prover::new()), network_client: NetworkClient::new(&private_key).rpc_url(rpc_url), - timeout: DEFAULT_TIMEOUT, - max_cycle_limit: None, + timeout: DEFAULT_TIMEOUT, + cycles_limit: DEFAULT_CYCLE_LIMIT, } } @@ -75,22 +72,25 @@ impl NetworkProver { NetworkProverBuilder::new() } - /// Get the underlying SP1 prover. + /// Get the underlying [`SP1Prover`]. pub fn sp1_prover(&self) -> &SP1Prover { &self.prover } /// Create a new proof request. - pub fn prove<'a>( - &'a self, - pk: &'a SP1ProvingKey, - stdin: SP1Stdin, - ) -> NetworkProofRequest<'a> { + pub fn prove<'a>(&'a self, pk: &'a SP1ProvingKey, stdin: SP1Stdin) -> NetworkProofRequest<'a> { NetworkProofRequest::new(self, pk, stdin) } } impl NetworkProver { + /// Get the timeout to used for a proof request. + /// + /// Clamps the given timeout to the minimum [`MIN_TIMEOUT`] and maximum [`MAX_TIMEOUT`] allowed values. + pub fn get_timeout(&self) -> u64 { + self.timeout.clamp(MIN_TIMEOUT, MAX_TIMEOUT) + } + /// Get the cycle limit to used for a proof request. /// /// The cycle limit is determined according to the following priority: @@ -153,7 +153,6 @@ impl NetworkProver { .await?; log::info!("Created request {} in transaction {}", request_id, tx_hash); - if self.network_client.get_rpc_url() == DEFAULT_PROVER_NETWORK_RPC { log::info!("View in explorer: {}", request_id.explorer_url()); } @@ -165,9 +164,9 @@ impl NetworkProver { /// submitted. /// /// Additionally, different failure modes are checked: - /// - if the deadline is exceeded, throws a `RequestDeadlineExceeded` error - /// - if the request is unexecutable, throws a `RequestUnexecutable` error - /// - if the request is unfulfillable, throws a `RequestUnfulfillable` error + /// - if the deadline is exceeded, throws [`Error::RequestDeadlineExceeded`] + /// - if the request is unexecutable, throws [`Error::RequestUnexecutable`] + /// - if the request is unfulfillable, throws [`Error::RequestUnfulfillable`] async fn wait_proof( &self, request_id: &RequestId, @@ -222,7 +221,7 @@ impl NetworkProver { impl NetworkProverBuilder { /// Creates a new network prover builder. pub fn new() -> Self { - Self { rpc_url: None, private_key: None, timeout: None, max_cycle_limit: None } + Self { rpc_url: None, private_key: None, timeout: None, cycle_limit: None } } /// Sets the RPC URL for the prover network. @@ -242,23 +241,32 @@ impl NetworkProverBuilder { self } + /// Sets the timeout for proof requests. + /// + /// This is the maximum amount of time to wait for the request to be fulfilled. pub fn with_timeout(mut self, timeout: u64) -> Self { self.timeout = Some(timeout); self } - pub fn with_cycle_limit(mut self, max_cycle_limit: u64) -> Self { - self.max_cycle_limit = Some(max_cycle_limit); + /// Sets the cycle limit for proof requests. + /// + /// This is the maximum number of cycles to allow for the execution of the request. + pub fn with_cycle_limit(mut self, cycle_limit: u64) -> Self { + self.cycle_limit = Some(cycle_limit); self } /// Builds the prover with the given configuration. pub fn build(self) -> NetworkProver { - NetworkProver { + NetworkProver { prover: Arc::new(SP1Prover::new()), - network_client: NetworkClient::new(&self.private_key.expect("A private key set on the builder")).rpc_url(self.rpc_url.expect("An RPC URL set on the builder")), - timeout: self.timeout.unwrap_or(DEFAULT_TIMEOUT), - max_cycle_limit: self.max_cycle_limit, + network_client: NetworkClient::new( + &self.private_key.expect("A private key set on the builder"), + ) + .rpc_url(self.rpc_url.unwrap_or(DEFAULT_PROVER_NETWORK_RPC.to_string())), + timeout: self.timeout.unwrap_or(DEFAULT_TIMEOUT), + cycles_limit: self.cycle_limit.unwrap_or(DEFAULT_CYCLE_LIMIT), } } } @@ -284,7 +292,7 @@ impl<'a> NetworkProofRequest<'a> { mode: Mode::default().into(), version: SP1_CIRCUIT_VERSION.to_string(), timeout: prover.timeout, - cycle_limit: prover.max_cycle_limit, + cycle_limit: Some(prover.cycles_limit), skip_simulation: false, strategy: DEFAULT_FULFILLMENT_STRATEGY, } diff --git a/crates/sdk/src/verify.rs b/crates/sdk/src/verify.rs index a7ad8544a..cb9937174 100644 --- a/crates/sdk/src/verify.rs +++ b/crates/sdk/src/verify.rs @@ -12,6 +12,7 @@ use crate::SP1VerificationError; use crate::{proof::SP1Proof, proof::SP1ProofWithPublicValues}; /// Verify that an SP1 proof is valid given its vkey and metadata. +/// /// For Plonk proofs, verifies that the public inputs of the PlonkBn254 proof match /// the hash of the VK and the committed public values of the SP1ProofWithPublicValues. pub fn verify(