Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xander42280 committed Sep 4, 2024
1 parent 15b2097 commit 4b8ca00
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion sdk/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("src/proto/stage.proto")?;
Ok(())
}
}
19 changes: 12 additions & 7 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

pub mod prover;
pub mod local;
pub mod network;
pub mod prover;

use std::env;
use prover::Prover;
use network::prover::NetworkProver;
use prover::Prover;
use std::env;

pub struct ProverClient {
pub prover: Box<dyn Prover>,
Expand All @@ -14,7 +13,11 @@ pub struct ProverClient {
impl ProverClient {
pub fn new() -> Self {
#[allow(unreachable_code)]
match env::var("ZKM_PROVER").unwrap_or("network".to_string()).to_lowercase().as_str() {
match env::var("ZKM_PROVER")
.unwrap_or("network".to_string())
.to_lowercase()
.as_str()
{
// "local" => Self {
// prover: Box::new(CpuProver::new()),
// },
Expand All @@ -32,12 +35,14 @@ impl ProverClient {
// }

pub fn network() -> Self {
Self { prover: Box::new(NetworkProver::default()) }
Self {
prover: Box::new(NetworkProver::default()),
}
}
}

impl Default for ProverClient {
fn default() -> Self {
Self::new()
}
}
}
1 change: 1 addition & 0 deletions sdk/src/local/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 1 addition & 2 deletions sdk/src/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

pub mod prover;
pub mod prover;
37 changes: 23 additions & 14 deletions sdk/src/network/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use stage_service::stage_service_client::StageServiceClient;
use stage_service::{GenerateProofRequest, GetStatusRequest};
use std::env;
use std::time::Instant;
use tonic::transport::{Channel, ClientTlsConfig};
use tonic::transport::Endpoint;
use tonic::transport::{Channel, ClientTlsConfig};

use ethers::signers::{LocalWallet, Signer};
use crate::prover::{Prover, ProverInput, ProverResult};
use tokio::time::Duration;
use ethers::signers::{LocalWallet, Signer};
use tokio::time::sleep;
use tokio::time::Duration;

use async_trait::async_trait;

Expand All @@ -33,15 +33,16 @@ impl NetworkProver {
let ca_cert_path = env::var("CA_CERT_PATH").unwrap_or("".to_string());
let cert_path = env::var("CERT_PATH").unwrap_or("".to_string());
let key_path = env::var("KEY_PATH").unwrap_or("".to_string());
let domain_name = env::var("DOMAIN_NAME").unwrap_or(DEFALUT_PROVER_NETWORK_DOMAIN.to_string());
let domain_name =
env::var("DOMAIN_NAME").unwrap_or(DEFALUT_PROVER_NETWORK_DOMAIN.to_string());
let private_key = env::var("PRIVATE_KEY").unwrap_or("".to_string());

let ssl_config = if ca_cert_path.is_empty() {
None
} else {
Some(Config::new(ca_cert_path, cert_path, key_path).await?)
};

let endpoint = match ssl_config {
Some(config) => {
let mut tls_config = ClientTlsConfig::new().domain_name(domain_name);
Expand All @@ -57,7 +58,10 @@ impl NetworkProver {
};
let stage_client = StageServiceClient::connect(endpoint).await?;
let wallet = private_key.parse::<LocalWallet>().unwrap();
Ok(NetworkProver { stage_client, wallet })
Ok(NetworkProver {
stage_client,
wallet,
})
}

pub async fn sign_ecdsa(&self, request: &mut GenerateProofRequest) {
Expand All @@ -72,7 +76,7 @@ impl NetworkProver {

impl Default for NetworkProver {
fn default() -> Self {
let rt = tokio::runtime::Runtime::new().unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
let result = rt.block_on(Self::new());
result.unwrap()
}
Expand All @@ -97,7 +101,11 @@ impl Prover for NetworkProver {
Ok(response.proof_id)
}

async fn wait_proof<'a>(&self, proof_id: &'a str, timeout: Option<Duration>) -> anyhow::Result<Option<ProverResult> > {
async fn wait_proof<'a>(
&self,
proof_id: &'a str,
timeout: Option<Duration>,
) -> anyhow::Result<Option<ProverResult>> {
let start_time = Instant::now();
let mut client = self.stage_client.clone();
loop {
Expand All @@ -110,10 +118,7 @@ impl Prover for NetworkProver {
let get_status_request = GetStatusRequest {
proof_id: proof_id.to_string(),
};
let get_status_response = client
.get_status(get_status_request)
.await?
.into_inner();
let get_status_response = client.get_status(get_status_request).await?.into_inner();

match Status::from_i32(get_status_response.status as i32) {
Some(Status::Computing) => {
Expand All @@ -138,8 +143,12 @@ impl Prover for NetworkProver {
}
}

async fn prover<'a>(&self, input: &'a ProverInput, timeout: Option<Duration>) -> anyhow::Result<Option<ProverResult> > {
async fn prover<'a>(
&self,
input: &'a ProverInput,
timeout: Option<Duration>,
) -> anyhow::Result<Option<ProverResult>> {
let proof_id = self.request_proof(input).await?;
self.wait_proof(&proof_id, timeout).await
}
}
}
19 changes: 13 additions & 6 deletions sdk/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

use tokio::time::Duration;
use async_trait::async_trait;
use serde::Serialize;
use serde::Deserialize;
use serde::Serialize;
use tokio::time::Duration;

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct ProverInput {
Expand All @@ -23,6 +22,14 @@ pub struct ProverResult {
#[async_trait]
pub trait Prover {
async fn request_proof<'a>(&self, input: &'a ProverInput) -> anyhow::Result<String>;
async fn wait_proof<'a>(&self, proof_id: &'a str, timeout: Option<Duration>) -> anyhow::Result<Option<ProverResult>>;
async fn prover<'a>(&self, input: &'a ProverInput, timeout: Option<Duration>) -> anyhow::Result<Option<ProverResult>>;
}
async fn wait_proof<'a>(
&self,
proof_id: &'a str,
timeout: Option<Duration>,
) -> anyhow::Result<Option<ProverResult>>;
async fn prover<'a>(
&self,
input: &'a ProverInput,
timeout: Option<Duration>,
) -> anyhow::Result<Option<ProverResult>>;
}

0 comments on commit 4b8ca00

Please sign in to comment.