Skip to content

Commit

Permalink
enhanced error handling (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
chudkowsky authored Sep 3, 2024
1 parent ecc2926 commit 5b44d60
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 77 deletions.
62 changes: 0 additions & 62 deletions Makefile.toml

This file was deleted.

4 changes: 2 additions & 2 deletions bin/register/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ This project provides a command-line tool for registering new keys with the Prov
## Installation

```bash
cargo install --git https://github.com/chudkowsky/http_prover_refactored.git register
cargo install --git https://github.com/cartridge-gg/http-prover.git register
```
or alternatively
```bash
git clone https://github.com/chudkowsky/http_prover_refactored.git
git clone https://github.com/cartridge-gg/http-prover.git
cargo build
cargo run -p register
```
Expand Down
14 changes: 8 additions & 6 deletions bin/register/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{arg, Parser};
use ed25519_dalek::VerifyingKey;
use prover_sdk::{access_key::ProverAccessKey, sdk::ProverSDK};
use prover_sdk::{access_key::ProverAccessKey, errors::SdkErrors, sdk::ProverSDK};
use url::Url;

/// Command line arguments for the server
Expand All @@ -18,17 +18,19 @@ pub struct Args {
}

#[tokio::main]
async fn main() {
async fn main() -> Result<(), SdkErrors> {
let args = Args::parse();

let key = ProverAccessKey::from_hex_string(&args.private_key).unwrap();
let key =
ProverAccessKey::from_hex_string(&args.private_key).map_err(|_| SdkErrors::InvalidKey)?;

let mut sdk = ProverSDK::new(args.url, key)
.await
.expect("Failed to create SDK instance");

let bytes: [u8; 32] = prefix_hex::decode(&args.added_key).unwrap();
let added_key = VerifyingKey::from_bytes(&bytes).unwrap();
let bytes: [u8; 32] =
prefix_hex::decode(&args.added_key).map_err(|e| SdkErrors::PrefixError(e.to_string()))?;
let added_key = VerifyingKey::from_bytes(&bytes).map_err(|_| SdkErrors::InvalidKey)?;

sdk.register(added_key).await.unwrap();
sdk.register(added_key).await
}
1 change: 1 addition & 0 deletions common/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pub enum JobStatus {
Running,
Completed,
Failed,
Unknown,
}
2 changes: 1 addition & 1 deletion prover-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Authenticate with the Prover service using your private key and the authenticati
async fn main() -> Result<(), ProverSdkErrors> {
let private_key_hex : String= env::var("PRIVATE_KEY")?;
let url_auth = "http://localhost:3000/auth";
let url_prover = "http://localhost:3000/prove/cairo1";
let url_prover = "http://localhost:3000/prove/cairo";

let result = ProverSDK::new(url_auth, url_prover)
.auth(&private_key_hex)
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,4 +30,6 @@ pub enum SdkErrors {
RegisterResponseError(String),
#[error("SSE error: {0}")]
SSEError(String),
#[error("Invalid key")]
InvalidKey,
}
2 changes: 1 addition & 1 deletion prover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Cairo Proving Server is the core component responsible for managing and veri
Here is an example of running the server with custom settings:

```sh
cairo-prover-server --host 127.0.0.1 --port 8080 --message-expiration-time 7200 --session-expiration-time 14400 --jwt-secret-key "my_super_secret_key" --authorized-keys-path /path/to/authorized_keys.json --authorized-keys "key1,key2,key3" --num-workers 8 --admin-key "admin_super_secret_key"
cairo-prover-server --host 127.0.0.1 --port 8080 --message-expiration-time 7200 --session-expiration-time 14400 --jwt-secret-key "my_super_secret_key" --authorized-keys-path /path/to/authorized_keys.json --authorized-keys key1,key2,key3 --num-workers 8 --admin-key "admin_super_secret_key"
```
## Command-Line Options

Expand Down
8 changes: 8 additions & 0 deletions prover/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ pub enum ProverError {
AddressParse(#[from] AddrParseError),
#[error(transparent)]
KeyError(#[from] ed25519_dalek::SignatureError),
#[error("Failed to send message via SSE{0}")]
SseError(String),
}
impl<T> From<SendError<T>> for ProverError {
fn from(err: SendError<T>) -> ProverError {
ProverError::SendError(err.to_string())
}
}
impl From<Vec<u8>> for ProverError {
fn from(err: Vec<u8>) -> Self {
ProverError::Authorizer(AuthorizerError::DataError(err))
}
}
impl IntoResponse for ProverError {
fn into_response(self) -> Response {
let (status, error_message) = match &self {
Expand Down Expand Up @@ -73,6 +80,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()),
};

let body = Json(json!({ "error": error_message }));
Expand Down
4 changes: 2 additions & 2 deletions prover/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ pub async fn start(args: Args) -> Result<(), ProverError> {

let admin_key_bytes = prefix_hex::decode::<Vec<u8>>(args.admin_key)
.map_err(|e| AuthorizerError::PrefixHexConversionError(e.to_string()))?;
let admin_key = VerifyingKey::from_bytes(&admin_key_bytes.try_into().unwrap())?;
let admin_key = VerifyingKey::from_bytes(&admin_key_bytes.try_into()?)?;

authorizer.authorize(admin_key).await?;
for key in args.authorized_keys.iter() {
let verifying_key_bytes = prefix_hex::decode::<Vec<u8>>(key)
.map_err(|e| AuthorizerError::PrefixHexConversionError(e.to_string()))?;
let verifying_key = VerifyingKey::from_bytes(&verifying_key_bytes.try_into().unwrap())?;
let verifying_key = VerifyingKey::from_bytes(&verifying_key_bytes.try_into()?)?;
authorizer.authorize(verifying_key).await?;
}
let (sse_tx, _) = broadcast::channel(100);
Expand Down
5 changes: 3 additions & 2 deletions prover/src/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ pub async fn sse_handler(
jobs.iter()
.find(|job| job.id == job_id)
.map(|job| job.status.clone())
.unwrap_or(JobStatus::Unknown)
};

let stream = stream! {
if job_status.is_some() && matches!(job_status.clone().unwrap(), JobStatus::Completed | JobStatus::Failed) {
yield Ok(axum::response::sse::Event::default().data(serde_json::to_string(&(job_status.unwrap(), job_id)).unwrap()));
if matches!(job_status.clone(), JobStatus::Completed | JobStatus::Failed) {
yield Ok(axum::response::sse::Event::default().data(serde_json::to_string(&(job_status, job_id)).unwrap()));
return;
}
while let Ok(message) = rx.recv().await {
Expand Down
6 changes: 6 additions & 0 deletions prover/src/utils/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub async fn get_job(
.unwrap_or_else(|| "Unknown error".to_string()),
}),
),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(JobResponse::Failed {
error: "Unknown error".to_string(),
}),
),
};
(status, response).into_response()
} else {
Expand Down
2 changes: 1 addition & 1 deletion prover/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn verify_proof(
if sender.receiver_count() > 0 {
sender
.send(serde_json::to_string(&(JobStatus::Completed, job_id))?)
.unwrap();
.map_err(|e| ProverError::SseError(e.to_string()))?;
}
Ok(())
}

0 comments on commit 5b44d60

Please sign in to comment.