Skip to content

Commit

Permalink
chore: structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Dec 12, 2024
1 parent 800b3e1 commit 2873a73
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 169 deletions.
77 changes: 18 additions & 59 deletions crates/sdk/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
use crate::{
local::{LocalProver, LocalProverBuilder},
opts::ProofOpts,
local::LocalProver,
proof::SP1ProofWithPublicValues,
prover::Prover,
request::DynProofRequest,
SP1VerificationError,
};

#[cfg(feature = "network-v2")]
use crate::network_v2::{NetworkProver, NetworkProverBuilder, DEFAULT_PROVER_NETWORK_RPC};
use crate::network_v2::{NetworkProver, DEFAULT_PROVER_NETWORK_RPC};

use anyhow::Result;
use sp1_core_executor::{ExecutionError, ExecutionReport};
use sp1_core_machine::io::SP1Stdin;
use sp1_primitives::io::SP1PublicValues;
use sp1_prover::{SP1ProvingKey, SP1VerifyingKey};
use std::{env, sync::Arc};
use crate::ProofOpts;

pub struct None;
mod request;
use request::DynProofRequest;
mod builder;
use builder::{ProverClientBuilder, None};

pub struct ProverClient {
inner: Box<dyn Prover>,
}

pub struct ProverClientBuilder<T> {
inner_builder: T,
impl Default for ProverClient {
fn default() -> Self {
Self::new()

Check warning on line 30 in crates/sdk/src/client.rs

View workflow job for this annotation

GitHub Actions / Test (ARM)

use of deprecated associated function `client::ProverClient::new`: Use ProverClient::builder() instead

Check warning on line 30 in crates/sdk/src/client.rs

View workflow job for this annotation

GitHub Actions / Cargo Check

use of deprecated associated function `client::ProverClient::new`: Use ProverClient::builder() instead

Check warning on line 30 in crates/sdk/src/client.rs

View workflow job for this annotation

GitHub Actions / Test (x86-64)

use of deprecated associated function `client::ProverClient::new`: Use ProverClient::builder() instead
}
}

#[allow(clippy::new_without_default)]
impl ProverClient {
pub fn builder() -> ProverClientBuilder<None> {
ProverClientBuilder { inner_builder: None }
ProverClientBuilder::new()
}

#[deprecated(note = "Use ProverClient::builder() instead")]
Expand All @@ -50,14 +53,14 @@ impl ProverClient {
ProverClient { inner: Box::new(network_prover) }
}
_ => {
let local_prover = LocalProver::new();
let local_prover = LocalProver::default();
ProverClient { inner: Box::new(local_prover) }
}
}

#[cfg(not(feature = "network-v2"))]
{
let local_prover = LocalProver::new();
let local_prover = LocalProver::default();
ProverClient { inner: Box::new(local_prover) }
}
}
Expand Down Expand Up @@ -87,53 +90,9 @@ impl ProverClient {
}
}

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() }
}

pub fn from_env(self) -> ProverClient {
ProverClient::create_from_env()
}
}

impl<T: BuildableProver> ProverClientBuilder<T> {
pub fn build(self) -> ProverClient {
ProverClient { inner: self.inner_builder.build_prover() }
}
}
/// The default timeout seconds for a proof request to be generated (4 hours).
pub const DEFAULT_TIMEOUT: u64 = 14400;

#[cfg(feature = "network-v2")]
impl ProverClientBuilder<NetworkProverBuilder> {
pub fn rpc_url(mut self, url: String) -> Self {
self.inner_builder = self.inner_builder.rpc_url(url);
self
}
/// The default cycle limit for a proof request.
pub const DEFAULT_CYCLE_LIMIT: u64 = 100_000_000;

pub fn private_key(mut self, key: String) -> Self {
self.inner_builder = self.inner_builder.private_key(key);
self
}
}

pub trait BuildableProver {
fn build_prover(self) -> Box<dyn Prover>;
}

impl BuildableProver for LocalProverBuilder {
fn build_prover(self) -> Box<dyn Prover> {
Box::new(self.build())
}
}

#[cfg(feature = "network-v2")]
impl BuildableProver for NetworkProverBuilder {
fn build_prover(self) -> Box<dyn Prover> {
Box::new(self.build())
}
}
107 changes: 107 additions & 0 deletions crates/sdk/src/client/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::{
local::{LocalProver, LocalProverBuilder},
prover::Prover,
};

#[cfg(feature = "network-v2")]
use crate::network_v2::{NetworkProver, NetworkProverBuilder};

use super::ProverClient;

pub struct None;

pub struct ProverClientBuilder<T> {
inner_builder: T,
}


impl Default for ProverClientBuilder<None> {
fn default() -> Self {
Self::new()
}
}

impl ProverClientBuilder<None> {
pub fn new() -> Self {
ProverClientBuilder { inner_builder: 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() }
}

pub fn from_env(self) -> ProverClient {
ProverClient::create_from_env()
}
}

impl<T: BuildableProver> ProverClientBuilder<T> {
pub fn build(self) -> ProverClient {
ProverClient { inner: self.inner_builder.build_prover() }
}

pub fn with_default_timeout(mut self, timeout: u64) -> Self {
self.inner_builder = self.inner_builder.with_default_timeout(timeout);
self
}

pub fn with_default_cycle_limit(mut self, cycle_limit: u64) -> Self {
self.inner_builder = self.inner_builder.with_default_cycle_limit(cycle_limit);
self
}
}

#[cfg(feature = "network-v2")]
impl ProverClientBuilder<NetworkProverBuilder> {
pub fn rpc_url(mut self, url: String) -> Self {
self.inner_builder = self.inner_builder.rpc_url(url);
self
}

pub fn private_key(mut self, key: String) -> Self {
self.inner_builder = self.inner_builder.private_key(key);
self
}
}

pub trait BuildableProver: Sized {
fn build_prover(self) -> Box<dyn Prover>;

fn with_default_timeout(self, timeout: u64) -> Self;

fn with_default_cycle_limit(self, cycle_limit: u64) -> Self;
}

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

fn with_default_cycle_limit(self, cycle_limit: u64) -> Self {
self.with_cycle_limit(cycle_limit)
}
}

#[cfg(feature = "network-v2")]
impl BuildableProver for NetworkProverBuilder {
fn build_prover(self) -> Box<dyn Prover> {
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)
}
}
77 changes: 77 additions & 0 deletions crates/sdk/src/client/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::{
proof::SP1ProofWithPublicValues,
prover::Prover,
};


use anyhow::Result;
use sp1_core_machine::io::SP1Stdin;
use sp1_prover::SP1ProvingKey;
use std::sync::Arc;
use crate::Mode;
use crate::ProofOpts;
use std::future::{Future, IntoFuture};
use std::pin::Pin;

pub struct DynProofRequest<'a> {
prover: &'a dyn Prover,
pk: &'a Arc<SP1ProvingKey>,
stdin: SP1Stdin,
opts: ProofOpts,
}

impl<'a> DynProofRequest<'a> {
pub fn new(
prover: &'a dyn Prover,
pk: &'a Arc<SP1ProvingKey>,
stdin: SP1Stdin,
opts: ProofOpts,
) -> Self {
Self { prover, pk, stdin, opts }
}

pub fn core(mut self) -> Self {
self.opts.mode = Mode::Core;
self
}

pub fn compressed(mut self) -> Self {
self.opts.mode = Mode::Compressed;
self
}

pub fn plonk(mut self) -> Self {
self.opts.mode = Mode::Plonk;
self
}

pub fn groth16(mut self) -> Self {
self.opts.mode = Mode::Groth16;
self
}

pub fn timeout(mut self, timeout: u64) -> Self {
self.opts.timeout = timeout;
self
}

pub fn cycle_limit(mut self, cycle_limit: u64) -> Self {
self.opts.cycle_limit = cycle_limit;
self
}

#[cfg(feature = "blocking")]
fn run(self) -> Result<SP1ProofWithPublicValues> {
self.prover.prove_with_options_sync(&self.pk, self.stdin, self.opts)
}
}

impl<'a> IntoFuture for DynProofRequest<'a> {
type Output = Result<SP1ProofWithPublicValues>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;

fn into_future(self) -> Self::IntoFuture {
self.prover.prove_with_options(self.pk, self.stdin, self.opts)
}
}

45 changes: 41 additions & 4 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//! in the official SP1 documentation for a quick start guide.
mod client;
mod mode;
mod opts;
mod prover;
mod request;
mod verify;
Expand All @@ -33,8 +31,6 @@ pub mod utils {
pub use client::*;
pub use proof::*;

// pub use local::{LocalProver, MockProver, Prover};

pub use sp1_build::include_elf;
pub use sp1_core_executor::{ExecutionReport, HookEnv, SP1Context, SP1ContextBuilder};
pub use sp1_core_machine::{io::SP1Stdin, riscv::cost::CostEstimator, SP1_CIRCUIT_VERSION};
Expand All @@ -47,6 +43,47 @@ pub use sp1_prover::{
use sp1_stark::MachineVerificationError;
use thiserror::Error;

pub struct ProofOpts {
pub mode: Mode,
pub timeout: u64,
pub cycle_limit: u64,
}

impl Default for ProofOpts {
fn default() -> Self {
Self { mode: Mode::default(), timeout: DEFAULT_TIMEOUT, cycle_limit: DEFAULT_CYCLE_LIMIT }
}
}

#[cfg(feature = "network-v2")]
use crate::network_v2::ProofMode;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Core,
Compressed,
Plonk,
Groth16,
}

impl Default for Mode {
fn default() -> Self {
Self::Groth16
}
}

#[cfg(feature = "network-v2")]
impl From<Mode> for ProofMode {
fn from(value: Mode) -> Self {
match value {
Mode::Core => Self::Core,
Mode::Compressed => Self::Compressed,
Mode::Plonk => Self::Plonk,
Mode::Groth16 => Self::Groth16,
}
}
}

#[derive(Error, Debug)]
pub enum SP1VerificationError {
#[error("Invalid public values")]
Expand Down
1 change: 0 additions & 1 deletion crates/sdk/src/local/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(unused_variables)]

use sp1_prover::SP1Prover;

// /// An implementation of [crate::ProverClient] that can generate mock proofs.
// pub struct MockProver {
Expand Down
Loading

0 comments on commit 2873a73

Please sign in to comment.