Skip to content

Commit

Permalink
Fix/sdk format (#55)
Browse files Browse the repository at this point in the history
* container enginse as variable and returning job id as u64

* fmt
  • Loading branch information
chudkowsky authored Sep 10, 2024
1 parent 47b168e commit b59ace4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 43 deletions.
22 changes: 7 additions & 15 deletions bin/cairo-prove/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
use std::time::Duration;

use prover_sdk::sdk::ProverSDK;
use serde::Deserialize;
use serde_json::Value;
use tokio::time::sleep;
use tracing::info;

use crate::errors::ProveErrors;

#[derive(Deserialize)]
pub struct JobId {
pub job_id: u64,
}

pub async fn fetch_job_sse(sdk: ProverSDK, job: String) -> Result<String, ProveErrors> {
let job: JobId = serde_json::from_str(&job)?;
info!("Job ID: {}", job.job_id);
sdk.sse(job.job_id).await?;
pub async fn fetch_job_sse(sdk: ProverSDK, job: u64) -> Result<String, ProveErrors> {
info!("Job ID: {}", job);
sdk.sse(job).await?;
info!("Job completed");
let response = sdk.get_job(job.job_id).await?;
let response = sdk.get_job(job).await?;
let response = response.text().await?;
let json_response: Value = serde_json::from_str(&response)?;
if let Some(status) = json_response.get("status").and_then(Value::as_str) {
Expand All @@ -35,12 +28,11 @@ pub async fn fetch_job_sse(sdk: ProverSDK, job: String) -> Result<String, ProveE
Err(ProveErrors::Custom(json_response.to_string()))
}
}
pub async fn fetch_job_polling(sdk: ProverSDK, job: String) -> Result<String, ProveErrors> {
let job: JobId = serde_json::from_str(&job)?;
info!("Fetching job: {}", job.job_id);
pub async fn fetch_job_polling(sdk: ProverSDK, job: u64) -> Result<String, ProveErrors> {
info!("Fetching job: {}", job);
let mut counter = 0;
loop {
let response = sdk.get_job(job.job_id).await?;
let response = sdk.get_job(job).await?;
let response = response.text().await?;
let json_response: Value = serde_json::from_str(&response)?;
if let Some(status) = json_response.get("status").and_then(Value::as_str) {
Expand Down
2 changes: 1 addition & 1 deletion bin/cairo-prove/src/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use common::{
use prover_sdk::sdk::ProverSDK;
use serde_json::Value;

pub async fn prove(args: Args, sdk: ProverSDK) -> Result<String, ProveErrors> {
pub async fn prove(args: Args, sdk: ProverSDK) -> Result<u64, ProveErrors> {
let program = std::fs::read_to_string(&args.program_path)?;
let proof = match args.cairo_version {
CairoVersion::V0 => {
Expand Down
2 changes: 2 additions & 0 deletions prover-sdk/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum SdkErrors {
RegisterResponseError(String),
#[error("SSE error: {0}")]
SSEError(String),
#[error("Verify response error: {0}")]
VerifyResponseError(String),
#[error("Invalid key")]
InvalidKey,
}
27 changes: 20 additions & 7 deletions prover-sdk/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use common::{requests::AddKeyRequest, ProverInput};
use ed25519_dalek::{ed25519::signature::SignerMut, VerifyingKey};
use futures::StreamExt;
use reqwest::{Client, Response};
use serde::Deserialize;
use url::Url;
#[derive(Debug, Clone)]
/// ProverSDK is a struct representing a client for interacting with the Prover service.
Expand All @@ -17,6 +18,11 @@ pub struct ProverSDK {
pub authority: ProverAccessKey,
}

#[derive(Deserialize)]
pub struct JobId {
pub job_id: u64,
}

impl ProverSDK {
pub async fn new(url: Url, access_key: ProverAccessKey) -> Result<Self, SdkErrors> {
let url = if !url.as_str().ends_with('/') {
Expand All @@ -33,21 +39,21 @@ impl ProverSDK {
.build()
}

pub async fn prove_cairo0<T>(&self, data: T) -> Result<String, SdkErrors>
pub async fn prove_cairo0<T>(&self, data: T) -> Result<u64, SdkErrors>
where
T: ProverInput + Send + 'static,
{
self.prove(data, self.prover_cairo0.clone()).await
}

pub async fn prove_cairo<T>(&self, data: T) -> Result<String, SdkErrors>
pub async fn prove_cairo<T>(&self, data: T) -> Result<u64, SdkErrors>
where
T: ProverInput + Send + 'static,
{
self.prove(data, self.prover_cairo.clone()).await
}

async fn prove<T>(&self, data: T, url: Url) -> Result<String, SdkErrors>
async fn prove<T>(&self, data: T, url: Url) -> Result<u64, SdkErrors>
where
T: ProverInput + Send + 'static,
{
Expand All @@ -64,18 +70,25 @@ impl ProverSDK {
return Err(SdkErrors::ProveResponseError(response_data));
}
let response_data = response.text().await?;

Ok(response_data)
let job = serde_json::from_str::<JobId>(&response_data)?;
Ok(job.job_id)
}
pub async fn verify(self, proof: String) -> Result<String, SdkErrors> {
pub async fn verify(self, proof: String) -> Result<u64, SdkErrors> {
let response = self
.client
.post(self.verify.clone())
.json(&proof)
.send()
.await?;
if !response.status().is_success() {
let response_data: String = response.text().await?;
tracing::error!("{}", response_data);
return Err(SdkErrors::VerifyResponseError(response_data));
}
let response_data = response.text().await?;
Ok(response_data)

let job = serde_json::from_str::<JobId>(&response_data)?;
Ok(job.job_id)
}
pub async fn get_job(&self, job_id: u64) -> Result<Response, SdkErrors> {
let url = format!("{}/{}", self.get_job.clone().as_str(), job_id);
Expand Down
14 changes: 4 additions & 10 deletions prover-sdk/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use prover_sdk::sdk::ProverSDK;
use serde::Deserialize;
use serde_json::Value;

#[derive(Deserialize)]
pub struct JobId {
pub job_id: u64,
}
pub async fn fetch_job(sdk: ProverSDK, job: String) -> String {
let job: JobId = serde_json::from_str(&job).unwrap();
println!("Job ID: {}", job.job_id);
sdk.sse(job.job_id).await.unwrap();
let response = sdk.get_job(job.job_id).await.unwrap();
pub async fn fetch_job(sdk: ProverSDK, job: u64) -> String {
println!("Job ID: {}", job);
sdk.sse(job).await.unwrap();
let response = sdk.get_job(job).await.unwrap();
let response = response.text().await.unwrap();
let json_response: Value = serde_json::from_str(&response).unwrap();
return json_response
Expand Down
1 change: 1 addition & 0 deletions prover/src/threadpool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl CairoVersionedInput {
async fn run(&self, paths: &RunPaths<'_>) -> Result<(), ProverError> {
match self {
CairoVersionedInput::Cairo(input) => {
trace!("Running cairo1-run");
let command = paths.cairo1_run_command(&input.layout);
command_run(command).await
}
Expand Down
25 changes: 15 additions & 10 deletions scripts/e2e_test.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# !/usr/bin/env bash
#!/usr/bin/env bash

set -eux
IMAGE_NAME="http-prover-test"
# Check if the image already exists
CONTAINER_ENGINE="${CONTAINER_ENGINE:-docker}"

if docker images | grep -q "$IMAGE_NAME"; then
# Check if the image already exists
if $CONTAINER_ENGINE images | grep -q "$IMAGE_NAME"; then
echo "Image $IMAGE_NAME already exists. Skipping build step."
else
echo "Image $IMAGE_NAME does not exist. Building the image..."

if [ "${CI:-}" == "true" ]; then
docker buildx build -t $IMAGE_NAME . \
$CONTAINER_ENGINE buildx build -t $IMAGE_NAME . \
--cache-from type=local,src=/tmp/.buildx-cache \
--cache-to type=local,dest=/tmp/.buildx-cache-new,mode=max \
--output=type=docker,dest=image.tar
docker load -i image.tar
--output=type=$CONTAINER_ENGINE,dest=image.tar
$CONTAINER_ENGINE load -i image.tar
else
docker build -t $IMAGE_NAME .
$CONTAINER_ENGINE build -t $IMAGE_NAME .
fi

if [ $? -ne 0 ]; then
Expand All @@ -35,7 +36,11 @@ KEYGEN_OUTPUT=$(cargo run -p keygen)
ADMIN_PUBLIC_KEY=$(echo "$KEYGEN_OUTPUT" | grep "Public key" | awk '{print $3}' | tr -d ',' | tr -d '[:space:]')
ADMIN_PRIVATE_KEY=$(echo "$KEYGEN_OUTPUT" | grep "Private key" | awk '{print $3}' | tr -d ',' | tr -d '[:space:]')

docker run -d --name http_prover_test \
REPLACE_FLAG=""
if [ "$CONTAINER_ENGINE" == "podman" ]; then
REPLACE_FLAG="--replace"
fi
$CONTAINER_ENGINE run -d --name http_prover_test $REPLACE_FLAG \
-p 3040:3000 $IMAGE_NAME \
--jwt-secret-key "secret" \
--message-expiration-time 3600 \
Expand All @@ -45,5 +50,5 @@ docker run -d --name http_prover_test \

PRIVATE_KEY=$PRIVATE_KEY PROVER_URL="http://localhost:3040" ADMIN_PRIVATE_KEY=$ADMIN_PRIVATE_KEY cargo test --no-fail-fast --workspace --verbose

docker stop http_prover_test
docker rm http_prover_test
$CONTAINER_ENGINE stop http_prover_test
$CONTAINER_ENGINE rm http_prover_test

0 comments on commit b59ace4

Please sign in to comment.