From b59ace47946654145000ada75f6bb2129b40369b Mon Sep 17 00:00:00 2001 From: Mateusz Chudkowski <120587768+chudkowsky@users.noreply.github.com> Date: Tue, 10 Sep 2024 20:48:18 +0200 Subject: [PATCH] Fix/sdk format (#55) * container enginse as variable and returning job id as u64 * fmt --- bin/cairo-prove/src/fetch.rs | 22 +++++++--------------- bin/cairo-prove/src/prove.rs | 2 +- prover-sdk/src/errors.rs | 2 ++ prover-sdk/src/sdk.rs | 27 ++++++++++++++++++++------- prover-sdk/tests/helpers/mod.rs | 14 ++++---------- prover/src/threadpool/run.rs | 1 + scripts/e2e_test.sh | 25 +++++++++++++++---------- 7 files changed, 50 insertions(+), 43 deletions(-) diff --git a/bin/cairo-prove/src/fetch.rs b/bin/cairo-prove/src/fetch.rs index 229173b..fc0883b 100644 --- a/bin/cairo-prove/src/fetch.rs +++ b/bin/cairo-prove/src/fetch.rs @@ -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 { - 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 { + 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) { @@ -35,12 +28,11 @@ pub async fn fetch_job_sse(sdk: ProverSDK, job: String) -> Result Result { - 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 { + 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) { diff --git a/bin/cairo-prove/src/prove.rs b/bin/cairo-prove/src/prove.rs index 98ae7a0..4e0d70f 100644 --- a/bin/cairo-prove/src/prove.rs +++ b/bin/cairo-prove/src/prove.rs @@ -9,7 +9,7 @@ use common::{ use prover_sdk::sdk::ProverSDK; use serde_json::Value; -pub async fn prove(args: Args, sdk: ProverSDK) -> Result { +pub async fn prove(args: Args, sdk: ProverSDK) -> Result { let program = std::fs::read_to_string(&args.program_path)?; let proof = match args.cairo_version { CairoVersion::V0 => { diff --git a/prover-sdk/src/errors.rs b/prover-sdk/src/errors.rs index a1317ae..561529c 100644 --- a/prover-sdk/src/errors.rs +++ b/prover-sdk/src/errors.rs @@ -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, } diff --git a/prover-sdk/src/sdk.rs b/prover-sdk/src/sdk.rs index 9219160..01fcf98 100644 --- a/prover-sdk/src/sdk.rs +++ b/prover-sdk/src/sdk.rs @@ -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. @@ -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 { let url = if !url.as_str().ends_with('/') { @@ -33,21 +39,21 @@ impl ProverSDK { .build() } - pub async fn prove_cairo0(&self, data: T) -> Result + pub async fn prove_cairo0(&self, data: T) -> Result where T: ProverInput + Send + 'static, { self.prove(data, self.prover_cairo0.clone()).await } - pub async fn prove_cairo(&self, data: T) -> Result + pub async fn prove_cairo(&self, data: T) -> Result where T: ProverInput + Send + 'static, { self.prove(data, self.prover_cairo.clone()).await } - async fn prove(&self, data: T, url: Url) -> Result + async fn prove(&self, data: T, url: Url) -> Result where T: ProverInput + Send + 'static, { @@ -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::(&response_data)?; + Ok(job.job_id) } - pub async fn verify(self, proof: String) -> Result { + pub async fn verify(self, proof: String) -> Result { 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::(&response_data)?; + Ok(job.job_id) } pub async fn get_job(&self, job_id: u64) -> Result { let url = format!("{}/{}", self.get_job.clone().as_str(), job_id); diff --git a/prover-sdk/tests/helpers/mod.rs b/prover-sdk/tests/helpers/mod.rs index 1739c5d..0143812 100644 --- a/prover-sdk/tests/helpers/mod.rs +++ b/prover-sdk/tests/helpers/mod.rs @@ -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 diff --git a/prover/src/threadpool/run.rs b/prover/src/threadpool/run.rs index 3c6ed19..73eae8c 100644 --- a/prover/src/threadpool/run.rs +++ b/prover/src/threadpool/run.rs @@ -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 } diff --git a/scripts/e2e_test.sh b/scripts/e2e_test.sh index 71026eb..e2cb536 100755 --- a/scripts/e2e_test.sh +++ b/scripts/e2e_test.sh @@ -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 @@ -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 \ @@ -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