-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
1023 lines (937 loc) · 36.9 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! # The primary module for interacting with Sindri's API.
use std::{collections::HashMap, fs, fs::File, io::Write, path::Path, time::Duration};
use openapi::{
apis::{
circuit_download, circuit_status,
circuits_api::{
circuit_create, circuit_delete, circuit_detail, proof_create, CircuitDetailError,
},
configuration::Configuration,
proof_status,
proofs_api::{proof_delete, proof_detail, ProofDetailError},
Error,
},
models::{CircuitInfoResponse, CircuitProveInput, JobStatus, ProofInfoResponse},
};
use regex::Regex;
use reqwest::header::{HeaderMap, HeaderValue};
use tracing::{debug, info, warn};
use crate::{
custom_middleware::{retry_client, HeaderDeduplicatorMiddleware, LoggingMiddleware},
types::{CircuitInfo, ProofInput},
utils::compress_directory,
};
#[cfg(any(feature = "record", feature = "replay"))]
use crate::custom_middleware::vcr_middleware;
#[cfg(feature = "rich-terminal")]
use console::style;
#[cfg(feature = "rich-terminal")]
use indicatif::{ProgressBar, ProgressStyle};
/// Configuration options for authenticating with the Sindri API.
///
/// This struct is used to configure authentication when initializing a [`SindriClient`].
/// While these options can be passed directly in code, it is generally recommended to
/// set them using environment variables instead (`SINDRI_API_KEY` and `SINDRI_BASE_URL`).
///
/// # Fields
///
/// * `api_key` - Optional API key for authentication. If not provided, falls back to `SINDRI_API_KEY` environment variable
/// * `base_url` - Optional base URL for API requests. Should be left as `None` except for internal development purposes.
/// If not provided, falls back to `SINDRI_BASE_URL` environment variable, then to the default production URL
///
/// # Examples
///
/// ```
/// use sindri_rs::client::AuthOptions;
///
/// // Explicitly passing API key within code
/// let auth = AuthOptions {
/// api_key: Some("my_api_key".to_string()),
/// base_url: None, // Use default production URL
/// };
/// ```
#[derive(Default, Debug, Clone)]
pub struct AuthOptions {
pub api_key: Option<String>,
pub base_url: Option<String>,
}
/// Configuration options for polling behavior when waiting for long-running operations.
///
/// This struct is used to configure how a [`SindriClient`] polls Sindri's API while
/// waiting for operations like circuit compilation or proof generation to complete.
/// It can be provided during client initialization or modified afterwards through
/// the client's `polling_options` field.
///
/// # Fields
///
/// * `interval` - Duration to wait between API status checks (default: 1 second)
/// * `timeout` - Optional maximum duration to wait for operation completion (default: 10 minutes)
///
/// # Examples
///
/// ```
/// use sindri_rs::client::{SindriClient, PollingOptions};
/// use std::time::Duration;
///
/// // Create client with custom polling options
/// let polling = PollingOptions {
/// interval: Duration::from_secs(5),
/// timeout: Some(Duration::from_secs(1800)), // 30 minutes
/// };
/// let client = SindriClient::new(None, Some(polling));
///
/// // Or modify polling options after creation
/// let mut client = SindriClient::new(None, None);
/// client.polling_options.timeout = Some(Duration::from_secs(1800));
/// ```
///
/// The default values are suitable for most use cases and only need to be adjusted
/// when working with circuits or proofs that are known to require longer processing times.
#[derive(Debug, Clone)]
pub struct PollingOptions {
pub interval: Duration,
pub timeout: Option<Duration>,
}
impl Default for PollingOptions {
fn default() -> Self {
Self {
interval: Duration::from_secs(1),
timeout: Some(Duration::from_secs(60 * 10)),
}
}
}
/// The [`SindriClient`] struct encapsulates all the necessary methods and properties
/// required to communicate effectively with the Sindri API, handling tasks
/// like uploads of circuits or guest code and proof generation.
#[derive(Debug)]
pub struct SindriClient {
config: Configuration,
pub polling_options: PollingOptions,
}
impl Default for SindriClient {
/// Creates a new Sindri API client with default options.
///
/// This is equivalent to calling `SindriClient::new(None, None)`.
/// Authentication will be read from environment variables and default polling options will be used.
fn default() -> Self {
Self::new(None, None)
}
}
impl SindriClient {
/// Creates a new Sindri API client.
///
/// # Arguments
///
/// * `auth_options` - Optional authentication configuration. If not provided, will attempt to read from environment variables
/// * `polling_options` - Optional polling configuration for long-running operations
///
/// # Environment Variables
///
/// * `SINDRI_API_KEY` - API key for authentication (if auth_options not provided)
/// * `SINDRI_BASE_URL` - Base URL for API requests (if auth_options not provided)
///
/// # Examples
///
/// ```
/// use sindri_rs::client::SindriClient;
/// let client = SindriClient::new(None, None); // inferring your API key from `SINDRI_API_KEY`
/// ```
pub fn new(auth_options: Option<AuthOptions>, polling_options: Option<PollingOptions>) -> Self {
let mut headers = HeaderMap::new();
headers.insert(
"Sindri-Client",
HeaderValue::from_str(
format!("{}/v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")).as_str(),
)
.expect("Could not insert default rust client header"),
);
#[allow(unused_mut)] // needed for VCR mutation
let mut client_builder = reqwest_middleware::ClientBuilder::new(
reqwest::Client::builder()
.default_headers(headers)
.build()
.expect("Could not build client"),
)
.with(HeaderDeduplicatorMiddleware)
.with(LoggingMiddleware)
.with(retry_client(None));
#[cfg(any(feature = "record", feature = "replay"))]
{
// Do not apply vcr to unit tests
if !cfg!(test) {
let bundle = std::env::var("VCR_PATH")
.unwrap_or_else(|_| "tests/recordings/replay.vcr.json".to_string());
let bundle_path = std::path::PathBuf::from(&bundle);
#[cfg(feature = "replay")]
if !bundle_path.exists() {
panic!("Recording not found at: {}", bundle_path.display());
}
client_builder = client_builder.with(vcr_middleware(bundle_path));
}
}
let client = client_builder.build();
// First try to read from auth_options, then from environment variables, then use default values
let auth = auth_options.unwrap_or_default();
let base_url = auth
.base_url
.or_else(|| std::env::var("SINDRI_BASE_URL").ok())
.unwrap_or_else(|| "https://sindri.app".to_string());
let api_key = auth
.api_key
.or_else(|| std::env::var("SINDRI_API_KEY").ok());
let config = Configuration {
base_path: base_url,
bearer_access_token: api_key,
client,
..Default::default()
};
Self {
config,
polling_options: polling_options.unwrap_or_default(),
}
}
/// Returns the configured API key
pub fn api_key(&self) -> Option<&str> {
self.config.bearer_access_token.as_deref()
}
/// Returns the configured base URL for API requests
pub fn base_url(&self) -> &str {
&self.config.base_path
}
/// Sets the API key for this client.
///
/// # Examples
///
/// ```
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::default()
/// .with_api_key("my_api_key");
/// ```
pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
self.config.bearer_access_token = Some(api_key.into());
self
}
/// Sets the base URL for this client.
///
/// Should be left as default except for internal development purposes.
///
/// # Examples
///
/// ```
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::default()
/// .with_base_url("https://custom.sindri.app");
/// ```
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.config.base_path = base_url.into();
self
}
/// Sets the polling interval for this client.
///
/// # Examples
///
/// ```
/// use sindri_rs::client::SindriClient;
/// use std::time::Duration;
///
/// let client = SindriClient::default()
/// .with_polling_interval(Duration::from_secs(5));
/// ```
pub fn with_polling_interval(mut self, interval: Duration) -> Self {
self.polling_options.interval = interval;
self
}
/// Sets the polling timeout for this client.
///
/// # Examples
///
/// ```
/// use sindri_rs::client::SindriClient;
/// use std::time::Duration;
///
/// let client = SindriClient::default()
/// .with_timeout(Duration::from_secs(1800)); // 30 minutes
/// ```
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.polling_options.timeout = Some(timeout);
self
}
/// Removes the polling timeout for this client.
///
/// # Examples
///
/// ```
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::default()
/// .with_no_timeout();
/// ```
pub fn with_no_timeout(mut self) -> Self {
self.polling_options.timeout = None;
self
}
/// Creates and deploys a new circuit from a local project.
///
/// In order to generate proofs on Sindri, you must first deploy the zero-knowledge circuit or
/// guest code with this method. Upon deployment, this method continuously polls the service to
/// track the compilation status until the process either completes successfully or fails.
///
/// # Arguments
///
/// * `project` - Path to a local project directory or an archive file (.zip, .tar, .tar.gz, .tgz)
/// * `tags` - Optional list of tags to identify the circuit
/// * `meta` - Optional metadata (key-value pairs) to associate with the circuit
///
/// # Returns
///
/// Returns circuit information on successful compilation, or error if compilation fails or times out.
///
/// # Examples
///
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
/// let circuit = client.create_circuit(
/// "path/to/circuit".to_string(),
/// Some(vec!["a_custom_tag".to_string()]),
/// Some(HashMap::from([("key".to_string(), "value".to_string())]))
/// ).await?;
/// ```
pub async fn create_circuit(
&self,
project: String,
tags: Option<Vec<String>>,
meta: Option<HashMap<String, String>>,
) -> Result<CircuitInfoResponse, Box<dyn std::error::Error>> {
info!("Creating new circuit from project: {}", project);
debug!("Circuit tags: {:?}, metadata: {:?}", tags, meta);
// Validate tags if provided
let tag_rules = Regex::new(r"^[a-zA-Z0-9_.-]+$").unwrap();
if let Some(ref tags) = tags {
for tag in tags {
if !tag_rules.is_match(tag) {
return Err(format!("\"{tag}\" is not a valid tag. Tags may only contain alphanumeric characters, underscores, hyphens, and periods.").into());
}
}
}
#[cfg(feature = "rich-terminal")]
println!("{}", style(format!(" ✓ Valid tags specified: {}", tags.as_ref().map_or(0, |t| t.len()))).cyan());
// Load the project into a byte array whether it is a compressed
// file already or a directory
let project_bytes = match Path::new(&project) {
p if p.is_dir() => {
info!("Compressing directory for upload");
compress_directory(p, None).await?
}
p if p.is_file() => {
let extension_regex = Regex::new(r"(?i)\.(zip|tar|tar\.gz|tgz)$")?;
if !extension_regex.is_match(&project) {
return Err("Project is not a zip file or tarball".into());
}
#[cfg(feature = "rich-terminal")]
println!("{}", style(" ✓ Detected compressed project file").cyan());
fs::read(&project)?
}
_ => return Err("Project is not a file or directory".into()),
};
info!("Uploading circuit to Sindri");
#[cfg(feature = "rich-terminal")]
println!("{}", style("Uploading circuit...").bold());
#[cfg(feature = "rich-terminal")]
let pb = ProgressBar::new_spinner();
#[cfg(feature = "rich-terminal")]
pb.enable_steady_tick(Duration::from_millis(120));
#[cfg(feature = "rich-terminal")]
pb.set_style(
ProgressStyle::with_template("{spinner} {msg:.cyan}")
.unwrap()
.tick_strings(&crate::utils::CLOCK_TICKS),
);
#[cfg(feature = "rich-terminal")]
pb.set_message("Sending files to circuit create endpoint...");
let response = circuit_create(&self.config, project_bytes, meta, tags).await?;
let circuit_id = response.id();
info!("Circuit created with ID: {}", circuit_id);
#[cfg(feature = "rich-terminal")]
let mut current_status = *response.status();
#[cfg(feature = "rich-terminal")]
pb.set_message(format!("Job status: {}", current_status));
let start_time = std::time::Instant::now();
let mut status = circuit_status(&self.config, circuit_id).await?;
debug!("Initial circuit status: {:?}", status.status);
while !matches!(status.status, JobStatus::Ready | JobStatus::Failed) {
if let Some(timeout) = self.polling_options.timeout {
if start_time.elapsed() > timeout {
warn!("Circuit compilation timed out after {:?}", timeout);
return Err(
"Circuit compilation did not complete within timeout duration".into(),
);
}
}
std::thread::sleep(self.polling_options.interval);
status = circuit_status(&self.config, circuit_id).await?;
#[cfg(feature = "rich-terminal")]
if status.status != current_status {
pb.set_message(format!("Job status: {}", status.status));
current_status = status.status;
}
}
match status.status {
JobStatus::Ready => info!(
"Circuit compilation completed successfully after {:?}",
start_time.elapsed()
),
JobStatus::Failed => warn!(
"Circuit compilation failed after {:?}",
start_time.elapsed()
),
_ => unreachable!(),
}
let circuit_info = circuit_detail(&self.config, circuit_id, None).await?;
Ok(circuit_info)
}
/// Blocking version of `create_circuit`.
///
/// This method provides the same functionality as `create_circuit` but can be used
/// in synchronous contexts. It internally creates a runtime to execute the async operation.
pub fn create_circuit_blocking(
&self,
project: String,
tags: Option<Vec<String>>,
meta: Option<HashMap<String, String>>,
) -> Result<CircuitInfoResponse, Box<dyn std::error::Error>> {
let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(self.create_circuit(project, tags, meta))
}
/// Deletes a circuit by ID.
///
/// # Arguments
///
/// * `circuit_id` - ID of the circuit to delete
///
/// # Warning
///
/// Once deleted, the circuit will no longer be viewable on the Sindri dashboard
/// and you will not be able to generate proofs from it. You should only delete a circuit
/// if its existence may cause confusion or misuse.
pub async fn delete_circuit(&self, circuit_id: &str) -> Result<(), Box<dyn std::error::Error>> {
info!("Deleting circuit with ID: {}", circuit_id);
circuit_delete(&self.config, circuit_id).await?;
Ok(())
}
/// Downloads and saves a project's source code.
///
/// This method allows you to retrieve the original source code that was uploaded to Sindri for a given circuit.
/// The code is downloaded as a tarball (.tar.gz) and saved to the specified location. This is useful for:
///
/// - Obtaining code from public circuits to use as examples or templates
/// - Retrieving your own previously deployed circuits
/// - Backing up circuit source code
/// - Collaborating by sharing circuit implementations
///
/// # Arguments
///
/// * `circuit_id` - ID of the circuit to clone
/// * `download_path` - Path where the circuit archive should be saved
/// * `override_max_project_size` - Optional maximum allowed size in bytes (defaults to 2GB)
///
/// # Examples
///
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
///
/// client.clone_circuit(
/// "abc123",
/// "path/to/save/circuit.tar.gz".to_string(),
/// None
/// ).await?;
/// ```
pub async fn clone_circuit(
&self,
circuit_id: &str,
download_path: String,
override_max_project_size: Option<usize>,
) -> Result<(), Box<dyn std::error::Error>> {
info!("Cloning circuit with ID: {}", circuit_id);
debug!("Download path: {}", download_path);
// Ensure circuit exists, is ready, and is not too large
let circuit_info = circuit_detail(&self.config, circuit_id, None).await?;
if *circuit_info.status() != JobStatus::Ready {
warn!("Circuit does not indicate ready status");
return Err("Circuit does not indicate ready status".into());
}
if circuit_info.file_size().unwrap_or(0) as u64
> override_max_project_size.unwrap_or(2 * 1024 * 1024 * 1024) as u64
{
// 2GB
warn!(
"Circuit tarball is larger than {} bytes.",
override_max_project_size.unwrap_or(2 * 1024 * 1024 * 1024)
);
return Err(format!("Circuit tarball is larger than {} bytes. If you still want to clone it, pass a larger size into `override_max_project_size`", override_max_project_size.unwrap_or(2* 1024 * 1024 * 1024)).into());
}
// Then, download the circuit
let download_response = circuit_download(&self.config, circuit_id, None).await?;
debug!("Circuit downloaded successfully");
// Write the circuit to the specified path
let mut file = File::create(download_path.clone())?;
file.write_all(&download_response.bytes().await?)?;
info!("Circuit written to {}", download_path);
Ok(())
}
/// Retrieves the details of a circuit.
///
/// You can use this method to get the status, metadata, and other details of a circuit.
///
/// # Arguments
///
/// * `circuit_id` - ID of the circuit to retrieve
/// * `include_verification_key` - Whether to include the verification key in the response
///
/// # Returns
///
/// A [`CircuitInfoResponse`] object containing the circuit details.
/// The enum variant corresponds to the type of circuit deployed.
///
/// # Examples
///
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
/// let circuit = client.get_circuit("abc123", None).await?;
/// ```
pub async fn get_circuit(
&self,
circuit_id: &str,
include_verification_key: Option<bool>,
) -> Result<CircuitInfoResponse, Error<CircuitDetailError>> {
info!("Getting circuit with ID: {}", circuit_id);
let circuit_info =
circuit_detail(&self.config, circuit_id, include_verification_key).await?;
Ok(circuit_info)
}
/// Creates and generates a proof for a circuit.
///
/// This method initiates proof generation and automatically polls the Sindri API until the proof
/// is either successfully generated or fails. The polling interval and timeout can be configured
/// through the client's `polling_options`.
///
/// # Arguments
///
/// * `circuit_id` - ID of the circuit to prove
/// * `proof_input` - Input values for the proof. Can be provided as a JSON object, &str, or String.
/// The format (JSON, TOML, base64, etc.) should match your circuit's expected input structure.
/// * `meta` - Optional metadata key-value pairs
/// * `verify` - Whether to verify the proof (server-side) after generation. A proof status
/// of `Failed` would be returned if the proof is not valid.
/// * `prover_implementation` - Optional specific prover implementation to use.
/// This field is generally for internal development only.
/// Sindri automatically selects the most performant implementation
/// based on your project's deployment details.
///
/// # Returns
///
/// Returns proof information on successful generation, or error if generation fails or times out.
///
/// # Examples
///
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
/// let proof = client.prove_circuit("abc123", "x=10,y=20", None, None, None).await?;
/// ```
pub async fn prove_circuit(
&self,
circuit_id: &str,
proof_input: impl Into<ProofInput>,
meta: Option<HashMap<String, String>>,
verify: Option<bool>,
prover_implementation: Option<String>,
) -> Result<ProofInfoResponse, Box<dyn std::error::Error>> {
info!("Creating proof for circuit: {}", circuit_id);
debug!(
"Proof metadata: {:?}, verify: {:?}, prover: {:?}",
meta, verify, prover_implementation
);
let circuit_prove_input = CircuitProveInput {
proof_input: Box::new(proof_input.into().0),
perform_verify: verify,
meta,
prover_implementation,
};
let proof_info = proof_create(&self.config, circuit_id, circuit_prove_input).await?;
let proof_id = proof_info.proof_id;
info!("Proof generation started with ID: {}", proof_id);
let mut status = proof_status(&self.config, &proof_id).await?;
debug!("Initial proof status: {:?}", status.status);
let start_time = std::time::Instant::now();
while !matches!(status.status, JobStatus::Ready | JobStatus::Failed) {
if let Some(timeout) = self.polling_options.timeout {
if start_time.elapsed() > timeout {
warn!("Proof generation timed out after {:?}", timeout);
return Err("Proof generation did not complete within timeout duration".into());
}
}
std::thread::sleep(self.polling_options.interval);
status = proof_status(&self.config, &proof_id).await?;
}
match status.status {
JobStatus::Ready => info!(
"Proof generation completed successfully after {:?}",
start_time.elapsed()
),
JobStatus::Failed => warn!("Proof generation failed after {:?}", start_time.elapsed()),
_ => unreachable!(),
}
let proof_info = proof_detail(&self.config, &proof_id, None, None, None, None).await?;
Ok(proof_info)
}
/// Blocking version of `prove_circuit`.
///
/// This method provides the same functionality as `prove_circuit` but can be used
/// in synchronous contexts. It internally creates a runtime to execute the async operation.
pub fn prove_circuit_blocking(
&self,
circuit_id: &str,
proof_input: impl Into<ProofInput>,
meta: Option<HashMap<String, String>>,
verify: Option<bool>,
prover_implementation: Option<String>,
) -> Result<ProofInfoResponse, Box<dyn std::error::Error>> {
let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(self.prove_circuit(
circuit_id,
proof_input,
meta,
verify,
prover_implementation,
))
}
/// Deletes a proof by ID.
///
/// # Arguments
///
/// * `proof_id` - ID of the proof to delete
///
/// # Warning
///
/// Once deleted, the proof will no longer be viewable on the Sindri dashboard.
/// You should only delete a proof if its existence may cause confusion and retrieval
/// of the wrong proof details.
pub async fn delete_proof(&self, proof_id: &str) -> Result<(), Box<dyn std::error::Error>> {
info!("Deleting proof with ID: {}", proof_id);
proof_delete(&self.config, proof_id).await?;
Ok(())
}
/// Gets information about a proof.
///
/// This method allows you to retrieve details about a previously generated proof, including
/// the proof data itself and any public outputs. While `prove_circuit()` returns
/// the same information immediately after generation, this method is particularly useful for:
///
/// - Retrieving details of historical proofs
/// - Fetching public outputs from previous proof generations
/// - Verifying proof status after system interruptions
/// - Downloading proof data for external verification
///
/// # Arguments
///
/// * `proof_id` - ID of the proof to retrieve
/// * `include_proof` - Whether to include the proof data in the response
/// * `include_public` - Whether to include public inputs/outputs in the response
/// * `include_verification_key` - Whether to include the verification key in the response
///
/// # Examples
///
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
///
/// // Get just the proof status
/// let basic_info = client.get_proof("abc123", None, None, None).await?;
///
/// // Get just the public outputs
/// let proof_with_outputs = client.get_proof("abc123", None, Some(true), None).await?;
/// ```
pub async fn get_proof(
&self,
proof_id: &str,
include_proof: Option<bool>,
include_public: Option<bool>,
include_verification_key: Option<bool>,
) -> Result<ProofInfoResponse, Error<ProofDetailError>> {
let proof_info = proof_detail(
&self.config,
proof_id,
include_proof,
include_public,
None,
include_verification_key,
)
.await?;
Ok(proof_info)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::BoojumCircuitInfoResponse;
use tracing_test::traced_test;
use wiremock::{
matchers::{header_exists, method, path},
Mock, MockServer, ResponseTemplate,
};
#[test]
fn test_new_client_with_options() {
let auth_options = AuthOptions {
api_key: Some("test_key".to_string()),
base_url: Some("https://fake.sindri.app".to_string()),
};
let client = SindriClient::new(Some(auth_options), None);
assert_eq!(client.api_key(), Some("test_key"));
assert_eq!(client.base_url(), "https://fake.sindri.app");
}
#[test]
fn test_builder_methods() {
// Test chaining all builder methods
let client = SindriClient::default()
.with_api_key("test_key")
.with_base_url("https://example.com")
.with_polling_interval(Duration::from_secs(5))
.with_timeout(Duration::from_secs(300));
// Verify all settings were applied correctly
assert_eq!(client.api_key(), Some("test_key"));
assert_eq!(client.base_url(), "https://example.com");
assert_eq!(client.polling_options.interval, Duration::from_secs(5));
assert_eq!(client.polling_options.timeout, Some(Duration::from_secs(300)));
}
#[test]
fn test_with_no_timeout() {
// Test that with_no_timeout removes the timeout
let client = SindriClient::default()
.with_timeout(Duration::from_secs(300))
.with_no_timeout();
assert_eq!(client.polling_options.timeout, None);
}
#[test]
fn test_new_client_with_env_vars() {
temp_env::with_vars(
vec![
("SINDRI_API_KEY", Some("env_test_key")),
("SINDRI_BASE_URL", Some("https://example.com")),
],
|| {
let client = SindriClient::new(None, None);
assert_eq!(client.api_key(), Some("env_test_key"));
assert_eq!(client.base_url(), "https://example.com");
},
);
}
#[test]
fn test_auth_options_override_env_vars() {
temp_env::with_vars(
vec![
("SINDRI_API_KEY", Some("env_test_key")),
("SINDRI_BASE_URL", Some("https://example.com")),
],
|| {
let auth_options = AuthOptions {
api_key: Some("test_key".to_string()),
base_url: Some("https://other.example.com".to_string()),
};
let client = SindriClient::new(Some(auth_options), None);
// authoptions should override env vars
assert_eq!(client.api_key(), Some("test_key"));
assert_eq!(client.base_url(), "https://other.example.com");
},
);
}
#[test]
fn test_new_client_auth_defaults() {
temp_env::with_vars(
vec![
("SINDRI_API_KEY", None::<String>),
("SINDRI_BASE_URL", None::<String>),
],
|| {
let client = SindriClient::new(None, None);
assert_eq!(client.api_key(), None);
assert_eq!(client.base_url(), "https://sindri.app");
},
);
}
#[test]
fn test_new_client_config_defaults() {
let client = SindriClient::new(None, None);
let config = client.config;
// Ensure the fields we are not setting have not changed between openapi codegen
assert_eq!(
config.user_agent,
Some("OpenAPI-Generator/v1.15.1/rust".to_owned())
);
assert_eq!(config.basic_auth, None);
assert_eq!(config.oauth_access_token, None);
// the api_key field in the config struct is unused and an unfortunate name overlap
// `bearer_access_token` is the actual config field that handles Sindri API keys
assert!(config.api_key.is_none());
}
#[test]
fn test_polling_options_custom() {
let polling_options = PollingOptions {
interval: Duration::from_secs(5),
timeout: Some(Duration::from_secs(300)), // 5 minutes
};
let client = SindriClient::new(None, Some(polling_options));
assert_eq!(client.polling_options.interval, Duration::from_secs(5));
assert_eq!(
client.polling_options.timeout,
Some(Duration::from_secs(300))
);
}
#[test]
fn test_post_client_init_polling_tweaks() {
let mut client = SindriClient::new(None, None);
assert_eq!(client.polling_options.interval, Duration::from_secs(1));
assert_eq!(
client.polling_options.timeout,
Some(Duration::from_secs(600))
);
client.polling_options.interval = Duration::from_secs(5);
client.polling_options.timeout = Some(Duration::from_secs(300));
assert_eq!(client.polling_options.interval, Duration::from_secs(5));
assert_eq!(
client.polling_options.timeout,
Some(Duration::from_secs(300))
);
}
#[tokio::test]
async fn test_client_default_header() {
let mock_server = MockServer::start().await;
Mock::given(method("GET"))
.and(header_exists("sindri-client"))
.respond_with(ResponseTemplate::new(200))
.mount(&mock_server)
.await;
let outer_client = SindriClient::new(None, None);
let inner_client = &outer_client.config.client;
let request = inner_client.get(mock_server.uri()).build().unwrap();
let response = inner_client.execute(request).await.unwrap();
assert_eq!(response.status(), 200);
}
#[tokio::test]
async fn test_circuit_create_tag_validation() {
let client = SindriClient::new(None, None);
let mut tags = vec!["test_t@g".to_string()];
let mut circuit = client
.create_circuit("fake_path".to_string(), Some(tags), None)
.await;
assert!(circuit.is_err());
assert!(circuit.unwrap_err().to_string().contains("not a valid tag"));
tags = vec![
"test_tag".to_string(),
"1-2-3-4-5-6".to_string(),
"ಠ_ಠ".to_string(),
];
circuit = client
.create_circuit("fake_path".to_string(), Some(tags), None)
.await;
assert!(circuit.is_err());
assert!(circuit.unwrap_err().to_string().contains("ಠ_ಠ"));
}
async fn mock_compile_server() -> MockServer {
// Setup mock server
let mock_server = wiremock::MockServer::start().await;
// Setup mock responses
wiremock::Mock::given(method("POST"))
.and(path("/api/v1/circuit/create"))
.respond_with(
ResponseTemplate::new(200).set_body_json(CircuitInfoResponse::Boojum(Box::new(
BoojumCircuitInfoResponse {
circuit_id: "test_circuit_123".to_string(),
..Default::default()
},
))),
)
.mount(&mock_server)
.await;
wiremock::Mock::given(method("GET"))
.and(path("/api/v1/circuit/test_circuit_123/status"))
.respond_with(
ResponseTemplate::new(200).set_body_json(CircuitInfoResponse::Boojum(Box::new(
BoojumCircuitInfoResponse {
status: JobStatus::Ready,
..Default::default()
},
))),
)
.mount(&mock_server)
.await;
// The circuit detail is returned from the method, but does not influence logging
wiremock::Mock::given(method("GET"))
.and(path("/api/v1/circuit/test_circuit_123/detail"))
.respond_with(ResponseTemplate::new(200))
.mount(&mock_server)
.await;
mock_server
}
#[tokio::test]
#[traced_test]
async fn test_verbose_logging() {
let mock_server = mock_compile_server().await;
// Create client with test configuration
let auth_options = AuthOptions {
api_key: Some("test_key".to_string()),
base_url: Some(mock_server.uri()),
};
let client = SindriClient::new(Some(auth_options), None);
// Create a temporary test directory
let temp_dir = tempfile::tempdir().unwrap();
let test_file = temp_dir.path().join("test.zip");
std::fs::write(&test_file, "test content").unwrap();
// Execute the operation
let _result = client
.create_circuit(test_file.to_str().unwrap().to_string(), None, None)
.await;
// Create method logs (debug + info level)
assert!(logs_contain("Creating new circuit from project"));
assert!(logs_contain("Uploading circuit to Sindri"));
assert!(logs_contain("Circuit created with ID: test_circuit_123"));
assert!(logs_contain("Circuit compilation completed"));
// Logs from the request/response (debug level)
// Ensure we see exactly three (create, status, detail)
logs_assert(|lines: &[&str]| {
match lines
.iter()
.filter(|line| line.contains("Request sent"))
.count()