Skip to content

Commit

Permalink
Added first draft of bonsai-alpha SDK (risc0#606)
Browse files Browse the repository at this point in the history
* Added first draft of bonsai-alpha SDK

* Added license header

* Removed bincode in guest dep of bonsai-alpha

* Made the bonsai-alpha crate a module of bonsai-sdk

This will allow for dual SDKs in the same crate

* recursion: fix compiler warning in guest (risc0#614)

add `#[cfg(not(target_os = "zkvm"))]` to items that are not needed for guest compilation

* Added Debug to SessionId

* Potential stability fix and extra derives

---------

Co-authored-by: Erik Kaneda <[email protected]>
Co-authored-by: Frank Laub <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2023
1 parent 7cbff2a commit d67e377
Show file tree
Hide file tree
Showing 5 changed files with 518 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"bonsai/sdk",
"risc0/bootstrap",
"risc0/bootstrap/poseidon",
"risc0/build",
Expand Down Expand Up @@ -29,6 +30,7 @@ homepage = "https://risczero.com/"
repository = "https://github.com/risc0/risc0/"

[workspace.dependencies]
bonsai-sdk = { version = "0.1.0", default-features = false, path = "bonsai/sdk" }
risc0-build = { version = "0.15.0", default-features = false, path = "risc0/build" }
risc0-build-kernel = { version = "0.15.0", default-features = false, path = "risc0/build_kernel" }
risc0-circuit-rv32im = { version = "0.15.0", default-features = false, path = "risc0/circuit/rv32im" }
Expand Down
18 changes: 18 additions & 0 deletions bonsai/sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "bonsai-sdk"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0"
reqwest = { version = "0.11", features = ["json", "blocking"] }
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
env_logger = "0.9.0"
httpmock = "0.6"
uuid = { version = "1.3", features = ["v4"] }

[lib]
# Disables the doctest from the README because it imports external components and make requests
doctest = false
66 changes: 66 additions & 0 deletions bonsai/sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Bonsai REST SDK

A library to handle HTTP REST requests to the Bonsai-alpha prover interface

## Example Usage

```rust
use std::time::Duration;
use bonsai_alpha_sdk::Client;
use anyhow::{Result, Context};
use risc0_zkvm::serde::to_vec;

// serialize a given input for the guest
let input_data = to_vec(&input_data).unwrap();
let input_data = bytemuck::cast_slice(&input_data).to_vec();

// Construct the bonsai_sdk client from the BONSAI_ENDPOINT env var
let client = Client::from_env()?;

// Upload the ELF file of the guest to the prover
let elf_path = Path::new(METHOD_NAME_PATH);
let img_id = client.upload_img_file(elf_path)?;
// Optionally you can upload a MemoryImage as well, but bincode encoded:
// let program = Program::load_elf(METHOD_NAME_ELF, MEM_SIZE as u32)?;
// let image = MemoryImage::new(&program, PAGE_SIZE as u32)?;
// let image = bincode::serialize(&image).context("Failed to serialize memoryimg")?;
// client.upload_img(image)?

// Upload the serialized input
let input_id = client.upload_input(input_data)?;
// Start the prover session by referencing the img_id and input_id
let session = client.create_session(img_id, input_id)?;

// Monitor the status of the prover session waiting for possible exit status
loop {
// Fetch the session status
let res = session.status(&client)?;
if res.status == "RUNNING" {
// If its still running, continue waiting
println!("Current status: {} - continue polling...", res.status);
std::thread::sleep(Duration::from_secs(POLL_INTERVAL_SEC));
continue;
}
if res.status == "SUCCEEDED" {
// If the session has been successful download the receipt
let receipt_url = res
.receipt_url
.context("API error, missing receipt on completed session")?;
println!("Session completed, downloading: {}", receipt_url);

let receipt_buf = client.download(&receipt_url)?;
// Deserialize the receipt
let receipt: SessionRollupReceipt = bincode::deserialize(&receipt_buf)?;

// verify the the receipt
receipt
.verify(METHOD_NAME_ID)
.context("Receipt verification failed")?;
println!("Receipt verified locally!")
} else {
bail!("Workflow exited: {}", res.status);
}

break;
}
```
Loading

0 comments on commit d67e377

Please sign in to comment.