Skip to content

Commit

Permalink
small stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Dec 12, 2024
1 parent 953e5a0 commit 2ffec90
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 51 deletions.
1 change: 0 additions & 1 deletion crates/sdk/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub fn export_solidity_groth16_bn254_verifier(output_dir: impl Into<PathBuf>) ->
Ok(())
}

#[cfg(feature = "network-v2")]
pub async fn download_file(
client: &Client,
url: &str,
Expand Down
14 changes: 7 additions & 7 deletions crates/sdk/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ProverClientBuilder<None> {
pub fn local(self) -> ProverClientBuilder<LocalProverBuilder> {
ProverClientBuilder { inner_builder: LocalProver::builder() }
}

#[cfg(feature = "network-v2")]
pub fn network(self) -> ProverClientBuilder<NetworkProverBuilder> {
ProverClientBuilder { inner_builder: NetworkProver::builder() }
Expand All @@ -46,7 +46,7 @@ impl<T: BuildableProver> ProverClientBuilder<T> {

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 {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl BuildableProver for LocalProverBuilder {
fn build_prover(self) -> Box<dyn Prover> {
Box::new(self.build())
}

fn with_default_timeout(self, timeout: u64) -> Self {
self.with_timeout(timeout)
}
Expand All @@ -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)
}
}
28 changes: 15 additions & 13 deletions crates/sdk/src/local/prover.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
}
Expand Down Expand Up @@ -152,9 +160,7 @@ impl<'a> LocalProofRequest<'a> {
version: String,
sp1_prover_opts: SP1ProverOpts,
) -> Result<SP1ProofWithPublicValues> {
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<sp1_prover::SP1CoreProofData> =
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/src/network-v2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
68 changes: 38 additions & 30 deletions crates/sdk/src/network-v2/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,27 @@ 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},
proto::network::{ExecutionStatus, FulfillmentStatus, FulfillmentStrategy, ProofMode},
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;
Expand All @@ -47,26 +45,25 @@ pub struct NetworkProver {
prover: Arc<SP1Prover<DefaultProverComponents>>,
network_client: NetworkClient,
timeout: u64,
max_cycle_limit: Option<u64>,

cycles_limit: u64,
}

pub struct NetworkProverBuilder {
rpc_url: Option<String>,
private_key: Option<String>,
timeout: Option<u64>,
max_cycle_limit: Option<u64>,
cycle_limit: Option<u64>,
}

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,
}
}

Expand All @@ -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:
Expand Down Expand Up @@ -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());
}
Expand All @@ -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<P: DeserializeOwned>(
&self,
request_id: &RequestId,
Expand Down Expand Up @@ -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.
Expand All @@ -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),
}
}
}
Expand All @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2ffec90

Please sign in to comment.