Skip to content

Commit

Permalink
error handling and binary update
Browse files Browse the repository at this point in the history
  • Loading branch information
chudkowsky committed Sep 17, 2024
1 parent 5d02dad commit ddeb119
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 35 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ futures = "0.3.30"
async-stream = "0.3.5"
swiftness_proof_parser = { git = "https://github.com/cartridge-gg/swiftness", rev = "1d46e21"}
starknet-crypto = "0.7.0"
anyhow = "1.0.89"
32 changes: 11 additions & 21 deletions bin/cairo-prove/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
use std::time::Duration;

use prover_sdk::sdk::ProverSDK;
use prover_sdk::{sdk::ProverSDK, JobResponse, ProverResult};
use serde_json::Value;
use tokio::time::sleep;
use tracing::info;

use crate::errors::ProveErrors;

pub async fn fetch_job_sse(sdk: ProverSDK, job: u64) -> Result<String, ProveErrors> {
pub async fn fetch_job_sse(sdk: ProverSDK, job: u64) -> Result<ProverResult, ProveErrors> {
info!("Job ID: {}", job);
sdk.sse(job).await?;
info!("Job completed");
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) {
if status == "Completed" {
return Ok(json_response
.get("result")
.and_then(Value::as_str)
.unwrap_or("No result found")
.to_string());
} else {
Err(ProveErrors::Custom(json_response.to_string()))
}
} else {
Err(ProveErrors::Custom(json_response.to_string()))
let json_response: JobResponse = serde_json::from_str(&response).unwrap();
if let JobResponse::Completed { result, .. } = json_response {
return Ok(result);
}
Err(ProveErrors::Custom("Job failed".to_string()))
}
pub async fn fetch_job_polling(sdk: ProverSDK, job: u64) -> Result<String, ProveErrors> {
pub async fn fetch_job_polling(sdk: ProverSDK, job: u64) -> Result<ProverResult, ProveErrors> {
info!("Fetching job: {}", job);
let mut counter = 0;
loop {
Expand All @@ -38,11 +29,10 @@ pub async fn fetch_job_polling(sdk: ProverSDK, job: u64) -> Result<String, Prove
if let Some(status) = json_response.get("status").and_then(Value::as_str) {
match status {
"Completed" => {
return Ok(json_response
.get("result")
.and_then(Value::as_str)
.unwrap_or("No result found")
.to_string());
let json_response: JobResponse = serde_json::from_str(&response).unwrap();
if let JobResponse::Completed { result, .. } = json_response {
return Ok(result);
}
}
"Pending" | "Running" => {
info!("Job is still in progress. Status: {}", status);
Expand Down
3 changes: 1 addition & 2 deletions bin/cairo-prove/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub async fn main() -> Result<(), ProveErrors> {
fetch_job_polling(sdk, job).await?
};
let path: std::path::PathBuf = args.program_output;
std::fs::write(path, job)?;
std::fs::write(path, serde_json::to_string_pretty(&job)?)?;
}

Ok(())
}
1 change: 1 addition & 0 deletions prover-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod errors;
pub mod sdk;
pub mod sdk_builder;

pub use common::models::{JobResponse, ProverResult};
pub use common::prover_input::*;
3 changes: 2 additions & 1 deletion prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ starknet-types-core.workspace = true
futures.workspace = true
async-stream.workspace = true
swiftness_proof_parser.workspace = true
starknet-crypto.workspace = true
starknet-crypto.workspace = true
anyhow.workspace = true
4 changes: 4 additions & 0 deletions prover/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Error as AnyhowError;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Expand Down Expand Up @@ -34,6 +35,8 @@ pub enum ProverError {
KeyError(#[from] ed25519_dalek::SignatureError),
#[error("Failed to send message via SSE{0}")]
SseError(String),
#[error(transparent)]
ParserError(#[from] AnyhowError),
}
impl<T> From<SendError<T>> for ProverError {
fn from(err: SendError<T>) -> ProverError {
Expand Down Expand Up @@ -81,6 +84,7 @@ impl IntoResponse for ProverError {
ProverError::AddressParse(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
ProverError::KeyError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
ProverError::SseError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
ProverError::ParserError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
};

let body = Json(json!({ "error": error_message }));
Expand Down
4 changes: 2 additions & 2 deletions prover/src/threadpool/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub async fn prove(
let sender = sse_tx.lock().await;

if prove_status.success() {
let proof_json = serde_json::from_str::<json_parser::StarkProof>(&final_result).unwrap();
let stark_proof = stark_proof::StarkProof::try_from(proof_json).unwrap();
let proof_json = serde_json::from_str::<json_parser::StarkProof>(&final_result)?;
let stark_proof = stark_proof::StarkProof::try_from(proof_json)?;
let program_hash = extract_program_hash(stark_proof.clone());
let program_output = extract_program_output(stark_proof.clone());
let program_output_hash = program_output_hash(program_output.clone());
Expand Down
14 changes: 5 additions & 9 deletions prover/src/utils/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
};
use tokio::sync::Mutex;

use crate::{auth::jwt::Claims, server::AppState};
use crate::{auth::jwt::Claims, errors::ProverError, server::AppState};

#[derive(Clone)]
pub struct Job {
Expand Down Expand Up @@ -107,7 +107,7 @@ pub async fn get_job(
Path(id): Path<u64>,
State(app_state): State<AppState>,
_claims: Claims,
) -> impl IntoResponse {
) -> Result<impl IntoResponse, ProverError> {
if let Some(job) = app_state.job_store.get_job(id).await {
let (status, response) = match job.status {
JobStatus::Pending | JobStatus::Running => (
Expand All @@ -121,7 +121,7 @@ pub async fn get_job(
StatusCode::OK,
Json(JobResponse::Completed {
status: job.status.clone(),
result: serde_json::from_str(&job.result.clone().unwrap()).unwrap(),
result: serde_json::from_str(&job.result.clone().unwrap_or_default())?,
}),
),
JobStatus::Failed => (
Expand All @@ -140,12 +140,8 @@ pub async fn get_job(
}),
),
};
(status, response).into_response()
Ok((status, response).into_response())
} else {
(
StatusCode::NOT_FOUND,
Json(format!("Job with id {} not found", id)),
)
.into_response()
Err(ProverError::CustomError("Job not found".to_string()))
}
}

0 comments on commit ddeb119

Please sign in to comment.