Skip to content

Commit

Permalink
feat: update SDK for network
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Dec 20, 2024
1 parent 445e5e6 commit 9a1a513
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 74 deletions.
4 changes: 2 additions & 2 deletions crates/perf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ fn main() {
let (_, _) = time_operation(|| prover.execute(&elf, &stdin));

let (proof, _) = time_operation(|| {
prover.prove(&pk, stdin.clone()).groth16().skip_simulation(true).run().unwrap()
prover.prove(&pk, &stdin).groth16().skip_simulation(true).run().unwrap()
});

let (_, _) = time_operation(|| prover.verify(&proof, &vk));

let (proof, _) = time_operation(|| {
prover.prove(&pk, stdin).plonk().skip_simulation(true).run().unwrap()
prover.prove(&pk, &stdin).plonk().skip_simulation(true).run().unwrap()
});

let (_, _) = time_operation(|| prover.verify(&proof, &vk));
Expand Down
6 changes: 5 additions & 1 deletion crates/sdk/src/cpu/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl CpuProverBuilder {
/// ```
#[must_use]
pub fn build(self) -> CpuProver {
if self.mock { CpuProver::mock() } else { CpuProver::new() }
if self.mock {
CpuProver::mock()
} else {
CpuProver::new()
}
}
}
60 changes: 52 additions & 8 deletions crates/sdk/src/network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use tonic::{

use super::utils::Signable;
use crate::network::proto::artifact::{
artifact_store_client::ArtifactStoreClient, CreateArtifactRequest,
artifact_store_client::ArtifactStoreClient, ArtifactType, CreateArtifactRequest,
};
use crate::network::proto::network::{
prover_network_client::ProverNetworkClient, CreateProgramRequest, CreateProgramRequestBody,
CreateProgramResponse, FulfillmentStatus, FulfillmentStrategy, GetNonceRequest,
GetProgramRequest, GetProgramResponse, GetProofRequestStatusRequest,
GetProofRequestStatusResponse, MessageFormat, ProofMode, RequestProofRequest,
RequestProofRequestBody, RequestProofResponse,
CreateProgramResponse, FulfillmentStatus, FulfillmentStrategy, GetFilteredProofRequestsRequest,
GetFilteredProofRequestsResponse, GetNonceRequest, GetProgramRequest, GetProgramResponse,
GetProofRequestStatusRequest, GetProofRequestStatusResponse, MessageFormat, ProofMode,
RequestProofRequest, RequestProofRequestBody, RequestProofResponse,
};

/// A client for interacting with the network.
Expand Down Expand Up @@ -105,7 +105,8 @@ impl NetworkClient {
) -> Result<CreateProgramResponse> {
// Create the program artifact.
let mut store = self.artifact_store_client().await?;
let program_uri = self.create_artifact_with_content(&mut store, &elf).await?;
let program_uri =
self.create_artifact_with_content(&mut store, ArtifactType::Program, &elf).await?;

// Serialize the verifying key.
let vk_encoded = bincode::serialize(&vk)?;
Expand All @@ -130,6 +131,44 @@ impl NetworkClient {
.into_inner())
}

/// Get all the proof requests that meet the filter criteria.
#[allow(clippy::too_many_arguments)]
pub async fn get_filtered_proof_requests(
&self,
version: Option<String>,
fulfillment_status: Option<i32>,
execution_status: Option<i32>,
minimum_deadline: Option<u64>,
vk_hash: Option<Vec<u8>>,
requester: Option<Vec<u8>>,
fulfiller: Option<Vec<u8>>,
from: Option<u64>,
to: Option<u64>,
limit: Option<u32>,
page: Option<u32>,
mode: Option<i32>,
) -> Result<GetFilteredProofRequestsResponse> {
let mut rpc = self.prover_network_client().await?;
let res = rpc
.get_filtered_proof_requests(GetFilteredProofRequestsRequest {
version,
fulfillment_status,
execution_status,
minimum_deadline,
vk_hash,
requester,
fulfiller,
from,
to,
limit,
page,
mode,
})
.await?
.into_inner();
Ok(res)
}

/// Get the status of a given proof.
///
/// # Details
Expand Down Expand Up @@ -190,7 +229,8 @@ impl NetworkClient {

// Create the stdin artifact.
let mut store = self.artifact_store_client().await?;
let stdin_uri = self.create_artifact_with_content(&mut store, &stdin).await?;
let stdin_uri =
self.create_artifact_with_content(&mut store, ArtifactType::Stdin, &stdin).await?;

// Send the request.
let mut rpc = self.prover_network_client().await?;
Expand Down Expand Up @@ -248,10 +288,14 @@ impl NetworkClient {
pub(crate) async fn create_artifact_with_content<T: Serialize>(
&self,
store: &mut ArtifactStoreClient<Channel>,
artifact_type: ArtifactType,
item: &T,
) -> Result<String> {
let signature = self.signer.sign_message_sync("create_artifact".as_bytes())?;
let request = CreateArtifactRequest { signature: signature.as_bytes().to_vec() };
let request = CreateArtifactRequest {
artifact_type: artifact_type.into(),
signature: signature.as_bytes().to_vec(),
};
let response = store.create_artifact(request).await?.into_inner();

let presigned_url = response.artifact_presigned_url;
Expand Down
39 changes: 39 additions & 0 deletions crates/sdk/src/network/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use thiserror::Error;
use tonic::Status;

/// An error that can occur when interacting with the prover network.
#[derive(Error, Debug)]
pub enum Error {
/// The program execution failed.
#[error("Program simulation failed")]
SimulationFailed,

/// The proof request is unexecutable.
#[error("Proof request 0x{} is unexecutable", hex::encode(.request_id))]
RequestUnexecutable {
/// The ID of the request that cannot be executed.
request_id: Vec<u8>,
},

/// The proof request is unfulfillable.
#[error("Proof request 0x{} is unfulfillable", hex::encode(.request_id))]
RequestUnfulfillable {
/// The ID of the request that cannot be fulfilled.
request_id: Vec<u8>,
},

/// The proof request timed out.
#[error("Proof request 0x{} timed out", hex::encode(.request_id))]
RequestTimedOut {
/// The ID of the request that timed out.
request_id: Vec<u8>,
},

/// An error occurred while interacting with the RPC server.
#[error("RPC error")]
RpcError(#[from] Status),

/// An unknown error occurred.
#[error("Other error: {0}")]
Other(#[from] anyhow::Error),
}
3 changes: 3 additions & 0 deletions crates/sdk/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ mod sign_message;
#[allow(clippy::too_many_lines)]
pub mod proto;
pub mod builder;
mod error;
pub mod prove;
pub mod utils;

pub use error::*;

pub use crate::network::client::NetworkClient;
pub use crate::network::proto::network::FulfillmentStrategy;

Expand Down
62 changes: 56 additions & 6 deletions crates/sdk/src/network/proto/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub struct CreateArtifactRequest {
/// The signature of the user on a pre-defined message. Used for authentication.
#[prost(bytes = "vec", tag = "1")]
pub signature: ::prost::alloc::vec::Vec<u8>,
/// The type of artifact to create.
#[prost(enumeration = "ArtifactType", tag = "2")]
pub artifact_type: i32,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
pub struct CreateArtifactResponse {
Expand All @@ -14,11 +17,58 @@ pub struct CreateArtifactResponse {
#[prost(string, tag = "2")]
pub artifact_presigned_url: ::prost::alloc::string::String,
}
#[derive(
serde::Serialize,
serde::Deserialize,
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
::prost::Enumeration,
)]
#[repr(i32)]
pub enum ArtifactType {
UnspecifiedArtifactType = 0,
/// A program artifact.
Program = 1,
/// A stdin artifact.
Stdin = 2,
/// A proof artifact.
Proof = 3,
}
impl ArtifactType {
/// String value of the enum field names used in the ProtoBuf definition.

Check failure on line 44 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

item in documentation is missing backticks
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.

Check failure on line 47 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

item in documentation is missing backticks
pub fn as_str_name(&self) -> &'static str {

Check failure on line 48 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

this method could have a `#[must_use]` attribute
match self {
Self::UnspecifiedArtifactType => "UNSPECIFIED_ARTIFACT_TYPE",
Self::Program => "PROGRAM",
Self::Stdin => "STDIN",
Self::Proof => "PROOF",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.

Check failure on line 56 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

item in documentation is missing backticks
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {

Check failure on line 57 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

this method could have a `#[must_use]` attribute
match value {
"UNSPECIFIED_ARTIFACT_TYPE" => Some(Self::UnspecifiedArtifactType),
"PROGRAM" => Some(Self::Program),
"STDIN" => Some(Self::Stdin),
"PROOF" => Some(Self::Proof),
_ => None,
}
}
}
/// Generated client implementations.
pub mod artifact_store_client {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
use tonic::codegen::http::Uri;
use tonic::codegen::{Body, Bytes, CompressionEncoding, GrpcMethod, InterceptedService, StdError, http};
use tonic::codegen::*;

Check failure on line 71 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

usage of wildcard import
#[derive(Debug, Clone)]
pub struct ArtifactStoreClient<T> {
inner: tonic::client::Grpc<T>,
Expand Down Expand Up @@ -57,11 +107,11 @@ pub mod artifact_store_client {
F: tonic::service::Interceptor,
T::ResponseBody: Default,
T: tonic::codegen::Service<
http::Request<tonic::body::BoxBody>,
Response = http::Response<
<T as tonic::client::GrpcService<tonic::body::BoxBody>>::ResponseBody,
>,
http::Request<tonic::body::BoxBody>,
Response = http::Response<
<T as tonic::client::GrpcService<tonic::body::BoxBody>>::ResponseBody,
>,
>,
<T as tonic::codegen::Service<http::Request<tonic::body::BoxBody>>>::Error:
Into<StdError> + std::marker::Send + std::marker::Sync,
{
Expand Down Expand Up @@ -123,7 +173,7 @@ pub mod artifact_store_client {
/// Generated server implementations.
pub mod artifact_store_server {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
use tonic::codegen::{Arc, Body, BoxFuture, CompressionEncoding, Context, EnabledCompressionEncodings, InterceptedService, Poll, StdError, async_trait, empty_body, http};
use tonic::codegen::*;

Check failure on line 176 in crates/sdk/src/network/proto/artifact.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

usage of wildcard import
/// Generated trait containing gRPC methods that should be implemented for use with ArtifactStoreServer.
#[async_trait]
pub trait ArtifactStore: std::marker::Send + std::marker::Sync + 'static {
Expand Down
Loading

0 comments on commit 9a1a513

Please sign in to comment.