Skip to content

Commit

Permalink
Added state and error_msg to bonsai-sdk (#843)
Browse files Browse the repository at this point in the history
This commit adds the `error_msg` and `state` members of the
`SessionStatusRes` from the bonsai REST API.

These members provide insight into workflow failures and proving
progress.
  • Loading branch information
mothran committed Sep 8, 2023
1 parent 22a00ef commit 27b5d75
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
3 changes: 3 additions & 0 deletions bonsai/ethereum-relay/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub(crate) mod tests {
let status_response = SessionStatusRes {
status: "SUCCEEDED".to_string(),
receipt_url: Some(format!("{}/fake/receipt/path", server.uri())),
error_msg: None,
state: None,
};

let receipt_data_response = Receipt {
Expand Down Expand Up @@ -66,6 +68,7 @@ pub(crate) mod tests {
let snark_status_res = SnarkStatusRes {
status: "SUCCEEDED".to_string(),
output: dummy_snark,
error_msg: None,
};

Mock::given(method("POST"))
Expand Down
5 changes: 5 additions & 0 deletions bonsai/rest-api-mock/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ pub(crate) async fn session_status(
Some(_) => Ok(Json(SessionStatusRes {
status,
receipt_url: Some(format!("{}/receipts/{}", storage.local_url, session_id)),
error_msg: None,
state: None,
})),
None => Ok(Json(SessionStatusRes {
status,
receipt_url: None,
error_msg: None,
state: None,
})),
}
}
Expand All @@ -132,6 +136,7 @@ pub(crate) async fn snark_status(
c: vec![],
public: vec![],
}),
error_msg: None,
}))
}

Expand Down
43 changes: 42 additions & 1 deletion bonsai/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ fn run_bonsai(input_data: Vec<u8>) -> Result<()> {
loop {
let res = session.status(&client)?;
if res.status == "RUNNING" {
tracing::debug!(
"Current status: {} - state: {} - continue polling...",
res.status,
res.state.unwrap_or_default()
);
std::thread::sleep(Duration::from_secs(15));
continue;
}
Expand All @@ -51,7 +56,7 @@ fn run_bonsai(input_data: Vec<u8>) -> Result<()> {
.verify(METHOD_NAME_ID)
.expect("Receipt verification failed");
} else {
panic!("Workflow exited: {}", res.status);
panic!("Workflow exited: {} - | err: {}", res.status, res.error_msg.unwrap_or_default());
}

break;
Expand All @@ -60,3 +65,39 @@ fn run_bonsai(input_data: Vec<u8>) -> Result<()> {
Ok(())
}
```

## STARK to SNARK

After a STARK proof is generated, it is possible to convert the proof to SNARK.

### Example

```rust
fn run_stark2snark(session_id: String) -> Result<()> {
let client = bonsai_sdk::Client::from_env()?;

let snark_session = client.create_snark(session_id)?;
tracing::info!("Created snark session: {}", snark_session.uuid);
loop {
let res = snark_session.status(&client)?;
match res.status.as_str() {
"RUNNING" => {
tracing::debug!("Current status: {} - continue polling...", res.status,);
std::thread::sleep(Duration::from_secs(15));
continue;
}
"SUCCEEDED" => {
let snark_proof = res.output;
tracing::info!("Snark proof!: {snark_proof:?}");
break;
}
_ => {
panic!("Workflow exited: {} err: {}", res.status, res.error_msg.unwrap_or_default());
}
}
}
Ok(())
}

run_stark2snark(session_id)?;
```
34 changes: 29 additions & 5 deletions bonsai/sdk/src/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,31 @@ pub mod responses {
pub struct SessionStatusRes {
/// Current status
///
/// values: [RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED |
/// SUCCEEDED]
/// values: `[ RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED ]`
pub status: String,
/// Final receipt download URL
///
/// If the status == 'SUCCEEDED' then this should be present
/// If the status == `SUCCEEDED` then this should be present
pub receipt_url: Option<String>,
/// Session Error message
///
/// If the session is not `RUNNING` or `SUCCEEDED`, this is the error raised from within bonsai,
/// otherwise it is [None].
pub error_msg: Option<String>,
/// Session Proving State
///
/// If the status is `RUNNING`, this is a indication of where in the
/// proving pipeline the session currently is, otherwise it is [None].
/// Possible states in order, include:
/// * `Setup`
/// * `Executor`
/// * `ProveSegments`
/// * `Planner`
/// * `Recursion`
/// * `RecursionJoin`
/// * `Finalize`
/// * `InProgress`
pub state: Option<String>,
}

/// Snark proof request object
Expand Down Expand Up @@ -122,14 +140,17 @@ pub mod responses {
pub struct SnarkStatusRes {
/// Current status
///
/// values: [RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED |
/// SUCCEEDED]
/// values: `[ RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED ]`
pub status: String,
/// SNARK proof output
///
/// Generated snark proof, following the snarkjs calldata format:
/// <https://github.com/iden3/snarkjs#26-simulate-a-verification-call>
pub output: Option<SnarkProof>,
/// Snark Error message
///
/// If the SNARK status is not `RUNNING` or `SUCCEEDED`, this is the error raised from within bonsai.
pub error_msg: Option<String>,
}
}

Expand Down Expand Up @@ -572,6 +593,8 @@ mod tests {
let response = SessionStatusRes {
status: "RUNNING".to_string(),
receipt_url: None,
error_msg: None,
state: None,
};

let create_mock = server.mock(|when, then| {
Expand Down Expand Up @@ -633,6 +656,7 @@ mod tests {
let response = SnarkStatusRes {
status: "RUNNING".to_string(),
output: None,
error_msg: None,
};

let create_mock = server.mock(|when, then| {
Expand Down

0 comments on commit 27b5d75

Please sign in to comment.