From 3d46b39e4862890588a433932f7691ad2845f656 Mon Sep 17 00:00:00 2001 From: Mateusz Chudkowski Date: Tue, 17 Sep 2024 18:30:34 +0200 Subject: [PATCH] clippy and fmt --- common/src/models.rs | 22 ++++-- prover-sdk/tests/helpers/mod.rs | 6 +- prover-sdk/tests/prove_test.rs | 126 +++++++++++++++----------------- prover-sdk/tests/verify_test.rs | 6 +- prover/src/threadpool/prove.rs | 12 ++- prover/src/utils/job.rs | 21 ++++-- prover/src/utils/mod.rs | 2 +- prover/src/verifier.rs | 10 ++- 8 files changed, 111 insertions(+), 94 deletions(-) diff --git a/common/src/models.rs b/common/src/models.rs index 7d1c229..750f81e 100644 --- a/common/src/models.rs +++ b/common/src/models.rs @@ -11,7 +11,7 @@ pub struct JWTResponse { pub expiration: u64, pub session_key: Option, } -#[derive(Serialize, Deserialize, Clone,Debug)] +#[derive(Serialize, Deserialize, Clone, Debug)] pub enum JobStatus { Pending, Running, @@ -20,17 +20,25 @@ pub enum JobStatus { Unknown, } -#[derive(Clone,Serialize,Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct ProverResult { pub proof: String, pub program_hash: Felt, pub program_output: Vec, pub program_output_hash: Felt, } -#[derive(Serialize,Deserialize)] +#[derive(Serialize, Deserialize)] #[serde(untagged)] pub enum JobResponse { - InProgress { id: u64, status: JobStatus }, - Completed { result: ProverResult, status: JobStatus }, - Failed { error: String }, -} \ No newline at end of file + InProgress { + id: u64, + status: JobStatus, + }, + Completed { + result: ProverResult, + status: JobStatus, + }, + Failed { + error: String, + }, +} diff --git a/prover-sdk/tests/helpers/mod.rs b/prover-sdk/tests/helpers/mod.rs index d9cad2f..5e6a30f 100644 --- a/prover-sdk/tests/helpers/mod.rs +++ b/prover-sdk/tests/helpers/mod.rs @@ -7,9 +7,9 @@ pub async fn fetch_job(sdk: ProverSDK, job: u64) -> Option { let response = sdk.get_job(job).await.unwrap(); let response = response.text().await.unwrap(); let json_response: JobResponse = serde_json::from_str(&response).unwrap(); - + if let JobResponse::Completed { result, .. } = json_response { return Some(result); } - None -} \ No newline at end of file + None +} diff --git a/prover-sdk/tests/prove_test.rs b/prover-sdk/tests/prove_test.rs index fbfb0bd..a97eeeb 100644 --- a/prover-sdk/tests/prove_test.rs +++ b/prover-sdk/tests/prove_test.rs @@ -1,5 +1,3 @@ -use std::fs; - use common::prover_input::*; use helpers::fetch_job; use prover_sdk::{access_key::ProverAccessKey, sdk::ProverSDK}; @@ -42,67 +40,63 @@ async fn test_cairo_prove() { assert_eq!("true", result.unwrap()); } -// #[tokio::test] -// async fn test_cairo0_prove() { -// let private_key = std::env::var("PRIVATE_KEY").unwrap(); -// let url = std::env::var("PROVER_URL").unwrap(); -// let access_key = ProverAccessKey::from_hex_string(&private_key).unwrap(); -// let url = Url::parse(&url).unwrap(); -// let sdk = ProverSDK::new(url, access_key).await.unwrap(); -// let program = std::fs::read_to_string("../examples/cairo0/fibonacci_compiled.json").unwrap(); -// let program: Cairo0CompiledProgram = serde_json::from_str(&program).unwrap(); -// let program_input_string = std::fs::read_to_string("../examples/cairo0/input.json").unwrap(); -// let program_input: Value = serde_json::from_str(&program_input_string).unwrap(); -// let layout = "recursive".to_string(); -// let data = Cairo0ProverInput { -// program, -// layout, -// program_input, -// n_queries: Some(16), -// pow_bits: Some(20), -// }; -// let job = sdk.prove_cairo0(data).await.unwrap(); -// let result = fetch_job(sdk.clone(), job).await; -// let job = sdk.clone().verify(result).await.unwrap(); -// let result = fetch_job(sdk.clone(), job).await; -// assert_eq!("true", result); -// } -// #[tokio::test] -// async fn test_cairo_multi_prove() { -// let private_key = std::env::var("PRIVATE_KEY").unwrap(); -// let url = std::env::var("PROVER_URL").unwrap(); -// let access_key = ProverAccessKey::from_hex_string(&private_key).unwrap(); -// let url = Url::parse(&url).unwrap(); -// let sdk = ProverSDK::new(url, access_key).await.unwrap(); -// let program = std::fs::read_to_string("../examples/cairo/fibonacci_compiled.json").unwrap(); -// let program: CairoCompiledProgram = serde_json::from_str(&program).unwrap(); -// let program_input_string = std::fs::read_to_string("../examples/cairo/input.json").unwrap(); -// let mut program_input: Vec = Vec::new(); -// for part in program_input_string.split(',') { -// let felt = Felt::from_dec_str(part).unwrap(); -// program_input.push(felt); -// } -// let layout = "recursive".to_string(); -// let data = CairoProverInput { -// program, -// layout, -// program_input, -// n_queries: Some(16), -// pow_bits: Some(20), -// }; -// let job1 = sdk.prove_cairo(data.clone()).await.unwrap(); -// let job2 = sdk.prove_cairo(data.clone()).await.unwrap(); -// let job3 = sdk.prove_cairo(data.clone()).await.unwrap(); -// let result = fetch_job(sdk.clone(), job1).await; -// let job = sdk.clone().verify(result).await.unwrap(); -// let result = fetch_job(sdk.clone(), job).await; -// assert_eq!("true", result); -// let result = fetch_job(sdk.clone(), job2).await; -// let job = sdk.clone().verify(result).await.unwrap(); -// let result = fetch_job(sdk.clone(), job).await; -// assert_eq!("true", result); -// let result = fetch_job(sdk.clone(), job3).await; -// let job = sdk.clone().verify(result).await.unwrap(); -// let result = fetch_job(sdk.clone(), job).await; -// assert_eq!("true", result); -// } +#[tokio::test] +async fn test_cairo0_prove() { + let private_key = std::env::var("PRIVATE_KEY").unwrap(); + let url = std::env::var("PROVER_URL").unwrap(); + let access_key = ProverAccessKey::from_hex_string(&private_key).unwrap(); + let url = Url::parse(&url).unwrap(); + let sdk = ProverSDK::new(url, access_key).await.unwrap(); + let program = std::fs::read_to_string("../examples/cairo0/fibonacci_compiled.json").unwrap(); + let program: Cairo0CompiledProgram = serde_json::from_str(&program).unwrap(); + let program_input_string = std::fs::read_to_string("../examples/cairo0/input.json").unwrap(); + let program_input: Value = serde_json::from_str(&program_input_string).unwrap(); + let layout = "recursive".to_string(); + let data = Cairo0ProverInput { + program, + layout, + program_input, + n_queries: Some(16), + pow_bits: Some(20), + }; + let job = sdk.prove_cairo0(data).await.unwrap(); + let result = fetch_job(sdk.clone(), job).await; + let result = sdk.clone().verify(result.unwrap().proof).await.unwrap(); + assert_eq!("true", result); +} +#[tokio::test] +async fn test_cairo_multi_prove() { + let private_key = std::env::var("PRIVATE_KEY").unwrap(); + let url = std::env::var("PROVER_URL").unwrap(); + let access_key = ProverAccessKey::from_hex_string(&private_key).unwrap(); + let url = Url::parse(&url).unwrap(); + let sdk = ProverSDK::new(url, access_key).await.unwrap(); + let program = std::fs::read_to_string("../examples/cairo/fibonacci_compiled.json").unwrap(); + let program: CairoCompiledProgram = serde_json::from_str(&program).unwrap(); + let program_input_string = std::fs::read_to_string("../examples/cairo/input.json").unwrap(); + let mut program_input: Vec = Vec::new(); + for part in program_input_string.split(',') { + let felt = Felt::from_dec_str(part).unwrap(); + program_input.push(felt); + } + let layout = "recursive".to_string(); + let data = CairoProverInput { + program, + layout, + program_input, + n_queries: Some(16), + pow_bits: Some(20), + }; + let job1 = sdk.prove_cairo(data.clone()).await.unwrap(); + let job2 = sdk.prove_cairo(data.clone()).await.unwrap(); + let job3 = sdk.prove_cairo(data.clone()).await.unwrap(); + let result = fetch_job(sdk.clone(), job1).await; + let result = sdk.clone().verify(result.unwrap().proof).await.unwrap(); + assert_eq!("true", result); + let result = fetch_job(sdk.clone(), job2).await; + let result = sdk.clone().verify(result.unwrap().proof).await.unwrap(); + assert_eq!("true", result); + let result = fetch_job(sdk.clone(), job3).await; + let result = sdk.clone().verify(result.unwrap().proof).await.unwrap(); + assert_eq!("true", result); +} diff --git a/prover-sdk/tests/verify_test.rs b/prover-sdk/tests/verify_test.rs index 0a59f38..f0717b6 100644 --- a/prover-sdk/tests/verify_test.rs +++ b/prover-sdk/tests/verify_test.rs @@ -14,8 +14,8 @@ async fn test_verify_invalid_proof() { let url = Url::parse(&url).unwrap(); let sdk = ProverSDK::new(url, access_key).await.unwrap(); let result = sdk.clone().verify("wrong proof".to_string()).await; - assert!(result.is_ok(),"Failed to verify proof"); - assert_eq!("false", result.unwrap()); + assert!(result.is_ok(), "Failed to verify proof"); + assert_eq!("false", result.unwrap()); } #[tokio::test] @@ -46,6 +46,6 @@ async fn test_verify_valid_proof() { assert!(result.is_some()); let result = result.unwrap(); let result = sdk.clone().verify(result.proof).await; - assert!(result.is_ok(),"Failed to verify proof"); + assert!(result.is_ok(), "Failed to verify proof"); assert_eq!("true", result.unwrap()); } diff --git a/prover/src/threadpool/prove.rs b/prover/src/threadpool/prove.rs index a2f56b2..3231bd0 100644 --- a/prover/src/threadpool/prove.rs +++ b/prover/src/threadpool/prove.rs @@ -1,7 +1,9 @@ use super::run::RunPaths; use super::CairoVersionedInput; use crate::errors::ProverError; -use crate::utils::proof_parser::{extract_program_hash, extract_program_output, program_output_hash}; +use crate::utils::proof_parser::{ + extract_program_hash, extract_program_output, program_output_hash, +}; use crate::utils::{config::Template, job::JobStore}; use common::models::{JobStatus, ProverResult}; use serde_json::Value; @@ -48,7 +50,7 @@ pub async fn prove( 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()); - let prover_result = ProverResult{ + let prover_result = ProverResult { proof: final_result, program_hash, program_output, @@ -56,7 +58,11 @@ pub async fn prove( }; job_store - .update_job_status(job_id, JobStatus::Completed,serde_json::to_string_pretty(&prover_result).ok()) + .update_job_status( + job_id, + JobStatus::Completed, + serde_json::to_string_pretty(&prover_result).ok(), + ) .await; if sender.receiver_count() > 0 { sender diff --git a/prover/src/utils/job.rs b/prover/src/utils/job.rs index b80ce61..92d373d 100644 --- a/prover/src/utils/job.rs +++ b/prover/src/utils/job.rs @@ -23,12 +23,20 @@ pub struct Job { pub created: Instant, } -#[derive(Serialize,Deserialize)] +#[derive(Serialize, Deserialize)] #[serde(untagged)] pub enum JobResponse { - InProgress { id: u64, status: JobStatus }, - Completed { result: ProverResult, status: JobStatus }, - Failed { error: String }, + InProgress { + id: u64, + status: JobStatus, + }, + Completed { + result: ProverResult, + status: JobStatus, + }, + Failed { + error: String, + }, } #[derive(Default, Clone)] @@ -113,10 +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()).unwrap(), }), ), JobStatus::Failed => ( diff --git a/prover/src/utils/mod.rs b/prover/src/utils/mod.rs index d3cad6f..3da6868 100644 --- a/prover/src/utils/mod.rs +++ b/prover/src/utils/mod.rs @@ -1,4 +1,4 @@ pub mod config; pub mod job; +pub mod proof_parser; pub mod shutdown; -pub mod proof_parser; \ No newline at end of file diff --git a/prover/src/verifier.rs b/prover/src/verifier.rs index a1d7deb..f66bf26 100644 --- a/prover/src/verifier.rs +++ b/prover/src/verifier.rs @@ -1,9 +1,13 @@ -use crate::{auth::jwt::Claims, errors::ProverError, extractors::workdir::TempDirHandle}; -use axum::{response::IntoResponse, Json}; +use crate::{auth::jwt::Claims, extractors::workdir::TempDirHandle}; +use axum::Json; use std::process::Command; -pub async fn verify_proof(TempDirHandle(dir):TempDirHandle,_claims:Claims,Json(proof): Json) -> Json { +pub async fn verify_proof( + TempDirHandle(dir): TempDirHandle, + _claims: Claims, + Json(proof): Json, +) -> Json { // Define the path for the proof file let file = dir.into_path().join("proof");