@@ -4,6 +4,7 @@ use crate::{
4
4
Risc0Response ,
5
5
} ;
6
6
use alloy_primitives:: B256 ;
7
+ use bonsai_sdk:: blocking:: { Client , SessionId } ;
7
8
use log:: { debug, error, info, warn} ;
8
9
use raiko_lib:: {
9
10
primitives:: keccak:: keccak,
@@ -19,14 +20,17 @@ use std::{
19
20
fs,
20
21
path:: { Path , PathBuf } ,
21
22
} ;
23
+ use tokio:: time:: { sleep as tokio_async_sleep, Duration } ;
22
24
23
25
use crate :: Risc0Param ;
24
26
27
+ const MAX_REQUEST_RETRY : usize = 8 ;
28
+
25
29
#[ derive( thiserror:: Error , Debug ) ]
26
30
pub enum BonsaiExecutionError {
27
31
// common errors: include sdk error, or some others from non-bonsai code
28
32
#[ error( transparent) ]
29
- SdkFailure ( #[ from] bonsai_sdk:: alpha :: SdkErr ) ,
33
+ SdkFailure ( #[ from] bonsai_sdk:: SdkErr ) ,
30
34
#[ error( "bonsai execution error: {0}" ) ]
31
35
Other ( String ) ,
32
36
// critical error like OOM, which is un-recoverable
@@ -44,12 +48,12 @@ pub async fn verify_bonsai_receipt<O: Eq + Debug + DeserializeOwned>(
44
48
max_retries : usize ,
45
49
) -> Result < ( String , Receipt ) , BonsaiExecutionError > {
46
50
info ! ( "Tracking receipt uuid: {uuid}" ) ;
47
- let session = bonsai_sdk :: alpha :: SessionId { uuid } ;
51
+ let session = SessionId { uuid } ;
48
52
49
53
loop {
50
54
let mut res = None ;
51
55
for attempt in 1 ..=max_retries {
52
- let client = bonsai_sdk :: alpha_async :: get_client_from_env ( risc0_zkvm:: VERSION ) . await ?;
56
+ let client = Client :: from_env ( risc0_zkvm:: VERSION ) ?;
53
57
54
58
match session. status ( & client) {
55
59
Ok ( response) => {
@@ -61,7 +65,7 @@ pub async fn verify_bonsai_receipt<O: Eq + Debug + DeserializeOwned>(
61
65
return Err ( BonsaiExecutionError :: SdkFailure ( err) ) ;
62
66
}
63
67
warn ! ( "Attempt {attempt}/{max_retries} for session status request: {err:?}" ) ;
64
- std :: thread :: sleep ( std :: time :: Duration :: from_secs ( 15 ) ) ;
68
+ tokio_async_sleep ( Duration :: from_secs ( 15 ) ) . await ;
65
69
continue ;
66
70
}
67
71
}
@@ -72,17 +76,18 @@ pub async fn verify_bonsai_receipt<O: Eq + Debug + DeserializeOwned>(
72
76
73
77
if res. status == "RUNNING" {
74
78
info ! (
75
- "Current status: {} - state: {} - continue polling..." ,
79
+ "Current {session:?} status: {} - state: {} - continue polling..." ,
76
80
res. status,
77
81
res. state. unwrap_or_default( )
78
82
) ;
79
- std :: thread :: sleep ( std :: time :: Duration :: from_secs ( 15 ) ) ;
83
+ tokio_async_sleep ( Duration :: from_secs ( 15 ) ) . await ;
80
84
} else if res. status == "SUCCEEDED" {
81
85
// Download the receipt, containing the output
86
+ info ! ( "Prove task {session:?} success." ) ;
82
87
let receipt_url = res
83
88
. receipt_url
84
89
. expect ( "API error, missing receipt on completed session" ) ;
85
- let client = bonsai_sdk :: alpha_async :: get_client_from_env ( risc0_zkvm:: VERSION ) . await ?;
90
+ let client = Client :: from_env ( risc0_zkvm:: VERSION ) ?;
86
91
let receipt_buf = client. download ( & receipt_url) ?;
87
92
let receipt: Receipt = bincode:: deserialize ( & receipt_buf) . map_err ( |e| {
88
93
BonsaiExecutionError :: Other ( format ! ( "Failed to deserialize receipt: {e:?}" ) )
@@ -104,10 +109,10 @@ pub async fn verify_bonsai_receipt<O: Eq + Debug + DeserializeOwned>(
104
109
}
105
110
return Ok ( ( session. uuid , receipt) ) ;
106
111
} else {
107
- let client = bonsai_sdk :: alpha_async :: get_client_from_env ( risc0_zkvm:: VERSION ) . await ?;
112
+ let client = Client :: from_env ( risc0_zkvm:: VERSION ) ?;
108
113
let bonsai_err_log = session. logs ( & client) ;
109
114
return Err ( BonsaiExecutionError :: Fatal ( format ! (
110
- "Workflow exited: {} - | err: {} | log: {bonsai_err_log:?}" ,
115
+ "Workflow {session:?} exited: {} - | err: {} | log: {bonsai_err_log:?}" ,
111
116
res. status,
112
117
res. error_msg. unwrap_or_default( ) ,
113
118
) ) ) ;
@@ -167,11 +172,11 @@ pub async fn maybe_prove<I: Serialize, O: Eq + Debug + Serialize + DeserializeOw
167
172
}
168
173
Err ( BonsaiExecutionError :: SdkFailure ( err) ) => {
169
174
warn ! ( "Bonsai SDK fail: {err:?}, keep tracking..." ) ;
170
- std :: thread :: sleep ( std :: time :: Duration :: from_secs ( 15 ) ) ;
175
+ tokio_async_sleep ( Duration :: from_secs ( 15 ) ) . await ;
171
176
}
172
177
Err ( BonsaiExecutionError :: Other ( err) ) => {
173
178
warn ! ( "Something wrong: {err:?}, keep tracking..." ) ;
174
- std :: thread :: sleep ( std :: time :: Duration :: from_secs ( 15 ) ) ;
179
+ tokio_async_sleep ( Duration :: from_secs ( 15 ) ) . await ;
175
180
}
176
181
Err ( BonsaiExecutionError :: Fatal ( err) ) => {
177
182
error ! ( "Fatal error on Bonsai: {err:?}" ) ;
@@ -228,13 +233,13 @@ pub async fn maybe_prove<I: Serialize, O: Eq + Debug + Serialize + DeserializeOw
228
233
}
229
234
230
235
pub async fn upload_receipt ( receipt : & Receipt ) -> anyhow:: Result < String > {
231
- let client = bonsai_sdk :: alpha_async :: get_client_from_env ( risc0_zkvm:: VERSION ) . await ?;
236
+ let client = Client :: from_env ( risc0_zkvm:: VERSION ) ?;
232
237
Ok ( client. upload_receipt ( bincode:: serialize ( receipt) ?) ?)
233
238
}
234
239
235
240
pub async fn cancel_proof ( uuid : String ) -> anyhow:: Result < ( ) > {
236
- let client = bonsai_sdk :: alpha_async :: get_client_from_env ( risc0_zkvm:: VERSION ) . await ?;
237
- let session = bonsai_sdk :: alpha :: SessionId { uuid } ;
241
+ let client = Client :: from_env ( risc0_zkvm:: VERSION ) ?;
242
+ let session = SessionId { uuid } ;
238
243
session. stop ( & client) ?;
239
244
#[ cfg( feature = "bonsai-auto-scaling" ) ]
240
245
auto_scaling:: shutdown_bonsai ( ) . await ?;
@@ -257,7 +262,7 @@ pub async fn prove_bonsai<O: Eq + Debug + DeserializeOwned>(
257
262
// Prepare input data
258
263
let input_data = bytemuck:: cast_slice ( & encoded_input) . to_vec ( ) ;
259
264
260
- let client = bonsai_sdk :: alpha_async :: get_client_from_env ( risc0_zkvm:: VERSION ) . await ?;
265
+ let client = Client :: from_env ( risc0_zkvm:: VERSION ) ?;
261
266
client. upload_img ( & encoded_image_id, elf. to_vec ( ) ) ?;
262
267
// upload input
263
268
let input_id = client. upload_input ( input_data. clone ( ) ) ?;
@@ -266,6 +271,7 @@ pub async fn prove_bonsai<O: Eq + Debug + DeserializeOwned>(
266
271
encoded_image_id. clone ( ) ,
267
272
input_id. clone ( ) ,
268
273
assumption_uuids. clone ( ) ,
274
+ false ,
269
275
) ?;
270
276
271
277
if let Some ( id_store) = id_store {
@@ -277,7 +283,13 @@ pub async fn prove_bonsai<O: Eq + Debug + DeserializeOwned>(
277
283
} ) ?;
278
284
}
279
285
280
- verify_bonsai_receipt ( image_id, expected_output, session. uuid . clone ( ) , 8 ) . await
286
+ verify_bonsai_receipt (
287
+ image_id,
288
+ expected_output,
289
+ session. uuid . clone ( ) ,
290
+ MAX_REQUEST_RETRY ,
291
+ )
292
+ . await
281
293
}
282
294
283
295
pub async fn bonsai_stark_to_snark (
@@ -286,10 +298,14 @@ pub async fn bonsai_stark_to_snark(
286
298
input : B256 ,
287
299
) -> ProverResult < Risc0Response > {
288
300
let image_id = Digest :: from ( RISC0_GUEST_ID ) ;
289
- let ( snark_uuid, snark_receipt) =
290
- stark2snark ( image_id, stark_uuid. clone ( ) , stark_receipt. clone ( ) )
291
- . await
292
- . map_err ( |err| format ! ( "Failed to convert STARK to SNARK: {err:?}" ) ) ?;
301
+ let ( snark_uuid, snark_receipt) = stark2snark (
302
+ image_id,
303
+ stark_uuid. clone ( ) ,
304
+ stark_receipt. clone ( ) ,
305
+ MAX_REQUEST_RETRY ,
306
+ )
307
+ . await
308
+ . map_err ( |err| format ! ( "Failed to convert STARK to SNARK: {err:?}" ) ) ?;
293
309
294
310
info ! ( "Validating SNARK uuid: {snark_uuid}" ) ;
295
311
@@ -382,8 +398,10 @@ pub fn load_receipt<T: serde::de::DeserializeOwned>(
382
398
383
399
pub fn save_receipt < T : serde:: Serialize > ( receipt_label : & String , receipt_data : & ( String , T ) ) {
384
400
if !is_dev_mode ( ) {
401
+ let cache_path = zkp_cache_path ( receipt_label) ;
402
+ info ! ( "Saving receipt to cache: {cache_path:?}" ) ;
385
403
fs:: write (
386
- zkp_cache_path ( receipt_label ) ,
404
+ cache_path ,
387
405
bincode:: serialize ( receipt_data) . expect ( "Failed to serialize receipt!" ) ,
388
406
)
389
407
. expect ( "Failed to save receipt output file." ) ;
0 commit comments