Skip to content

Commit

Permalink
default logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Dec 11, 2024
1 parent b24a336 commit b1f7776
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 37 deletions.
17 changes: 9 additions & 8 deletions crates/sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::{
prover::Prover,
};
use anyhow::Result;
use sp1_core_executor::ExecutionReport;
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;

Expand Down Expand Up @@ -39,15 +40,11 @@ impl ProverClient {
.unwrap_or_else(|_| DEFAULT_PROVER_NETWORK_RPC.to_string());
let private_key = env::var("SP1_PRIVATE_KEY").unwrap_or_default();

let network_prover = NetworkProver::builder()
.with_rpc_url(rpc_url)
.with_private_key(private_key)
.build();

let network_prover = NetworkProver::new(rpc_url, private_key);
ProverClient { inner: Box::new(network_prover) }
}
_ => {
let local_prover = LocalProver::builder().build();
let local_prover = LocalProver::new();
ProverClient { inner: Box::new(local_prover) }
}
}
Expand All @@ -57,7 +54,11 @@ impl ProverClient {
self.inner.setup(elf).await
}

pub async fn execute(&self, elf: &[u8], stdin: SP1Stdin) -> Result<ExecutionReport> {
pub async fn execute(
&self,
elf: &[u8],
stdin: &SP1Stdin,
) -> Result<(SP1PublicValues, ExecutionReport), ExecutionError> {
self.inner.execute(elf, stdin).await
}

Expand Down
4 changes: 1 addition & 3 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod utils {
}

#[cfg(any(feature = "network", feature = "network-v2"))]
use {std::future::Future, tokio::task::block_in_place};
use std::future::Future;

pub use sp1_build::include_elf;
pub use sp1_core_executor::{ExecutionReport, HookEnv, SP1Context, SP1ContextBuilder};
Expand All @@ -50,8 +50,6 @@ pub use sp1_prover::{
pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
// Handle case if we're already in an tokio runtime.

use std::future::Future;

use tokio::task::block_in_place;
if let Ok(handle) = tokio::runtime::Handle::try_current() {
block_in_place(|| handle.block_on(fut))
Expand Down
29 changes: 16 additions & 13 deletions crates/sdk/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ use crate::opts::ProofOpts;
use crate::proof::SP1ProofWithPublicValues;
use crate::prover::Prover;
use crate::provers::SP1VerificationError;
use crate::request::ProofRequest;
use crate::request::{ProofRequest, DEFAULT_TIMEOUT};
use crate::verify;

use anyhow::Result;
use async_trait::async_trait;
use sp1_core_executor::{ExecutionReport, SP1Context};
use futures::executor;
use sp1_core_executor::{ExecutionError, ExecutionReport, SP1Context};
use sp1_core_machine::io::SP1Stdin;
use sp1_primitives::io::SP1PublicValues;
use sp1_prover::components::DefaultProverComponents;
use sp1_prover::{SP1Prover, SP1ProvingKey, SP1VerifyingKey, SP1_CIRCUIT_VERSION};
use std::future::{Future, IntoFuture};
use std::pin::Pin;

pub struct LocalProver {
prover: SP1Prover<DefaultProverComponents>,
pub prover: SP1Prover<DefaultProverComponents>,
}

impl LocalProver {
Expand All @@ -29,13 +31,11 @@ impl LocalProver {
}
}

pub struct LocalProverBuilder {
mode: Mode,
}
pub struct LocalProverBuilder {}

impl LocalProverBuilder {
pub fn new() -> Self {
Self { mode: Mode::default() }
Self {}
}

pub fn build(self) -> LocalProver {
Expand All @@ -53,7 +53,7 @@ pub struct LocalProofRequest<'a> {

impl<'a> LocalProofRequest<'a> {
pub fn new(prover: &'a LocalProver, pk: &'a SP1ProvingKey, stdin: SP1Stdin) -> Self {
Self { prover, pk, stdin, timeout: 0, mode: Mode::default() }
Self { prover, pk, stdin, timeout: DEFAULT_TIMEOUT, mode: Mode::default() }
}

pub fn with_mode(mut self, mode: Mode) -> Self {
Expand Down Expand Up @@ -83,9 +83,12 @@ impl Prover for LocalProver {
self.prover.setup(elf)
}

async fn execute(&self, elf: &[u8], stdin: SP1Stdin) -> Result<ExecutionReport> {
let (_, report) = self.prover.execute(elf, &stdin, SP1Context::default())?;
Ok(report)
async fn execute(
&self,
elf: &[u8],
stdin: &SP1Stdin,
) -> Result<(SP1PublicValues, ExecutionReport), ExecutionError> {
self.prover.execute(elf, stdin, SP1Context::default())
}

async fn prove_with_options(
Expand All @@ -106,7 +109,7 @@ impl Prover for LocalProver {
opts: &ProofOpts,
) -> Result<SP1ProofWithPublicValues> {
let request = LocalProofRequest::new(self, pk, stdin.clone()).with_timeout(opts.timeout);
futures::executor::block_on(request.run())
executor::block_on(request.run())
}

async fn verify(
Expand Down Expand Up @@ -136,6 +139,6 @@ impl<'a> IntoFuture for LocalProofRequest<'a> {
#[cfg(feature = "blocking")]
impl<'a> ProofRequest for LocalProofRequest<'a> {
fn run(self) -> Result<SP1ProofWithPublicValues> {
futures::executor::block_on(self.run())
executor::block_on(self.run())
}
}
14 changes: 9 additions & 5 deletions crates/sdk/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Result;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use sp1_core_executor::{ExecutionReport, SP1Context};
use sp1_core_executor::{ExecutionError, ExecutionReport, SP1Context};
use sp1_core_machine::io::SP1Stdin;
use sp1_primitives::io::SP1PublicValues;
use sp1_prover::components::DefaultProverComponents;
use sp1_prover::{SP1Prover, SP1ProvingKey, SP1VerifyingKey, SP1_CIRCUIT_VERSION};
use std::future::{Future, IntoFuture};
Expand Down Expand Up @@ -159,6 +160,7 @@ impl<'a> NetworkProofRequest<'a> {
prover,
pk,
stdin,
// TODO fill in defaults
version: SP1_CIRCUIT_VERSION.to_owned(),
mode: Mode::default(),
fulfillment_strategy: None,
Expand Down Expand Up @@ -241,10 +243,12 @@ impl Prover for NetworkProver {
async fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) {
self.prover.setup(elf)
}

async fn execute(&self, elf: &[u8], stdin: SP1Stdin) -> Result<ExecutionReport> {
let (_, report) = self.prover.execute(elf, &stdin, SP1Context::default())?;
Ok(report)
async fn execute(
&self,
elf: &[u8],
stdin: &SP1Stdin,
) -> Result<(SP1PublicValues, ExecutionReport), ExecutionError> {
self.prover.execute(elf, stdin, SP1Context::default())
}

async fn prove_with_options(
Expand Down
8 changes: 5 additions & 3 deletions crates/sdk/src/opts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::mode::Mode;
use crate::{
mode::Mode,
request::{DEFAULT_CYCLE_LIMIT, DEFAULT_TIMEOUT},
};

pub struct ProofOpts {
pub mode: Mode,
Expand All @@ -8,7 +11,6 @@ pub struct ProofOpts {

impl Default for ProofOpts {
fn default() -> Self {
// TODO better defaults
Self { mode: Mode::default(), timeout: 10000, cycle_limit: 100000000 }
Self { mode: Mode::default(), timeout: DEFAULT_TIMEOUT, cycle_limit: DEFAULT_CYCLE_LIMIT }
}
}
9 changes: 7 additions & 2 deletions crates/sdk/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use async_trait::async_trait;
use sp1_core_executor::ExecutionReport;
use sp1_core_executor::{ExecutionError, ExecutionReport};
use sp1_core_machine::io::SP1Stdin;
use sp1_primitives::io::SP1PublicValues;
use sp1_prover::{SP1ProvingKey, SP1VerifyingKey};

use crate::{opts::ProofOpts, proof::SP1ProofWithPublicValues, provers::SP1VerificationError};
Expand All @@ -10,7 +11,11 @@ use crate::{opts::ProofOpts, proof::SP1ProofWithPublicValues, provers::SP1Verifi
pub trait Prover: Send + Sync {
async fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey);

async fn execute(&self, elf: &[u8], stdin: SP1Stdin) -> Result<ExecutionReport>;
async fn execute(
&self,
elf: &[u8],
stdin: &SP1Stdin,
) -> Result<(SP1PublicValues, ExecutionReport), ExecutionError>;

async fn prove_with_options(
&self,
Expand Down
6 changes: 6 additions & 0 deletions crates/sdk/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use sp1_prover::SP1ProvingKey;
use std::future::{Future, IntoFuture};
use std::pin::Pin;

/// The default timeout seconds for a proof request to be generated (4 hours).
pub const DEFAULT_TIMEOUT: u64 = 14400;

/// The default cycle limit for a proof request.
pub const DEFAULT_CYCLE_LIMIT: u64 = 100_000_000;

pub trait ProofRequest {
fn run(
self,
Expand Down
6 changes: 3 additions & 3 deletions examples/fibonacci/script/bin/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async fn main() {

// Or use old env var behavior

let client = ProverClient::builder()
.from_env();
// let client = ProverClient::builder()
// .from_env();

// let client = ProverClient::new();
let client = ProverClient::new();

// Generate the proving key and verifying key for the given program.
let (pk, vk) = client.setup(ELF).await;
Expand Down

0 comments on commit b1f7776

Please sign in to comment.