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
//! # Network Client
//!
//! This module provides a client for directly interacting with the network prover service.
use std::{
result::Result::Ok as StdOk,
str::FromStr,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use alloy_primitives::{Address, B256, U256};
use anyhow::{Context, Ok, Result};
use async_trait::async_trait;
use reqwest_middleware::ClientWithMiddleware as HttpClientWithMiddleware;
use serde::{de::DeserializeOwned, Serialize};
use sp1_core_machine::io::SP1Stdin;
use sp1_prover::{HashableKey, SP1VerifyingKey};
use tonic::{transport::Channel, Code};
use super::{
grpc,
retry::{self, RetryableRpc, DEFAULT_RETRY_TIMEOUT},
signer::NetworkSigner,
utils::{sign_message, Signable},
NetworkMode, MAINNET_EXPLORER_URL, RESERVED_EXPLORER_URL,
};
use crate::network::proto::{
artifact::{artifact_store_client::ArtifactStoreClient, ArtifactType, CreateArtifactRequest},
// Import the clients for both auction and base.
auction_network::prover_network_client::ProverNetworkClient as AuctionProverNetworkClient,
// Import auction and base specific types for requests.
auction_types::{
CancelRequestRequest as AuctionCancelRequestRequest,
CancelRequestRequestBody as AuctionCancelRequestRequestBody,
GetBalanceRequest as AuctionGetBalanceRequest,
GetFilteredProofRequestsRequest as AuctionGetFilteredProofRequestsRequest,
GetNonceRequest as AuctionGetNonceRequest, GetProgramRequest as AuctionGetProgramRequest,
GetProofRequestParamsRequest as AuctionGetProofRequestParamsRequest,
GetProofRequestStatusRequest as AuctionGetProofRequestStatusRequest,
GetProversByUptimeRequest as AuctionGetProversByUptimeRequest,
MessageFormat as AuctionMessageFormat, RequestProofRequest as AuctionRequestProofRequest,
RequestProofRequestBody as AuctionRequestProofRequestBody,
TransactionVariant as AuctionTransactionVariant,
},
base_network::prover_network_client::ProverNetworkClient as BaseProverNetworkClient,
base_types::{
GetBalanceRequest as BaseGetBalanceRequest,
GetFilteredProofRequestsRequest as BaseGetFilteredProofRequestsRequest,
GetNonceRequest as BaseGetNonceRequest, GetProgramRequest as BaseGetProgramRequest,
GetProofRequestStatusRequest as BaseGetProofRequestStatusRequest,
MessageFormat as BaseMessageFormat, RequestProofRequest as BaseRequestProofRequest,
RequestProofRequestBody as BaseRequestProofRequestBody,
},
// Import standard types (auction by default for backwards compatibility).
types::{
CreateProgramRequest, CreateProgramRequestBody, CreateProgramResponse, FulfillmentStatus,
FulfillmentStrategy, GetProofRequestDetailsRequest, GetProofRequestDetailsResponse,
MessageFormat, ProofMode,
},
CancelRequestResponse,
GetBalanceResponse,
GetFilteredProofRequestsResponse,
// Import unified switchable response types.
GetNonceResponse,
GetProgramResponse,
GetProofRequestParamsResponse,
GetProofRequestStatusResponse,
RequestProofResponse,
};
/// A client for interacting with the network.
#[derive(Clone)]
pub struct NetworkClient {
pub(crate) signer: NetworkSigner,
pub(crate) http: HttpClientWithMiddleware,
pub(crate) rpc_url: String,
pub(crate) network_mode: NetworkMode,
}
#[async_trait]
impl RetryableRpc for NetworkClient {
/// Execute an operation with retries using default timeout.
async fn with_retry<'a, T, F, Fut>(&'a self, operation: F, operation_name: &str) -> Result<T>
where
F: Fn() -> Fut + Send + Sync + 'a,
Fut: std::future::Future<Output = Result<T>> + Send,
T: Send,
{
self.with_retry_timeout(operation, DEFAULT_RETRY_TIMEOUT, operation_name).await
}
/// Execute an operation with retries using the specified timeout.
async fn with_retry_timeout<'a, T, F, Fut>(
&'a self,
operation: F,
timeout: Duration,
operation_name: &str,
) -> Result<T>
where
F: Fn() -> Fut + Send + Sync + 'a,
Fut: std::future::Future<Output = Result<T>> + Send,
T: Send,
{
retry::retry_operation(operation, Some(timeout), operation_name).await
}
}
impl NetworkClient {
/// Creates a new [`NetworkClient`] with the given signer, rpc url, and network mode.
pub fn new(
signer: NetworkSigner,
rpc_url: impl Into<String>,
network_mode: NetworkMode,
) -> Self {
let client = reqwest::Client::builder()
.pool_max_idle_per_host(0)
.pool_idle_timeout(Duration::from_secs(240))
.build()
.unwrap();
Self { signer, http: client.into(), rpc_url: rpc_url.into(), network_mode }
}
/// Get the explorer URL for the current network mode.
#[must_use]
pub fn get_explorer_url(&self) -> &'static str {
match self.network_mode {
NetworkMode::Mainnet => MAINNET_EXPLORER_URL,
NetworkMode::Reserved => RESERVED_EXPLORER_URL,
}
}
/// Get the latest nonce for this account's address.
pub async fn get_nonce(&self) -> Result<u64> {
let response = self.get_nonce_response().await?;
Ok(response.nonce())
}
/// Get the full nonce response (internal helper).
async fn get_nonce_response(&self) -> Result<GetNonceResponse> {
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
let res = rpc
.get_nonce(AuctionGetNonceRequest {
address: self.signer.address().to_vec(),
})
.await?;
Ok(GetNonceResponse::from(res.into_inner()))
},
"getting nonce",
)
.await
}
NetworkMode::Reserved => {
self.with_retry(
|| async {
let mut rpc = self.base_prover_network_client().await?;
let res = rpc
.get_nonce(BaseGetNonceRequest {
address: self.signer.address().to_vec(),
})
.await?;
Ok(GetNonceResponse::from(res.into_inner()))
},
"getting nonce",
)
.await
}
}
}
/// Get the credit balance of your account.
///
/// # Details
/// Uses the key that the client was initialized with.
pub async fn get_balance(&self) -> Result<U256> {
let response = self.get_balance_response().await?;
Ok(U256::from_str(response.balance()).unwrap())
}
/// Get the full balance response (internal helper).
async fn get_balance_response(&self) -> Result<GetBalanceResponse> {
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
let res = rpc
.get_balance(AuctionGetBalanceRequest {
address: self.signer.address().to_vec(),
})
.await?;
Ok(GetBalanceResponse::from(res.into_inner()))
},
"getting balance",
)
.await
}
NetworkMode::Reserved => {
self.with_retry(
|| async {
let mut rpc = self.base_prover_network_client().await?;
let res = rpc
.get_balance(BaseGetBalanceRequest {
address: self.signer.address().to_vec(),
})
.await?;
Ok(GetBalanceResponse::from(res.into_inner()))
},
"getting balance",
)
.await
}
}
}
/// Get the verifying key hash from a verifying key.
///
/// # Details
/// The verifying key hash is used to identify a program.
pub fn get_vk_hash(vk: &SP1VerifyingKey) -> Result<B256> {
let vk_hash = vk.hash_bytes();
Ok(B256::from_slice(&vk_hash))
}
/// Registers a program with the network if it is not already registered.
pub async fn register_program(&self, vk: &SP1VerifyingKey, elf: &[u8]) -> Result<B256> {
let vk_hash = Self::get_vk_hash(vk)?;
// Try to get the existing program.
if (self.get_program(vk_hash).await?).is_some() {
// The program already exists.
Ok(vk_hash)
} else {
// The program doesn't exist, create it.
self.create_program(vk_hash, vk, elf).await?;
tracing::info!("Registered program {:?}", vk_hash);
Ok(vk_hash)
}
}
/// Attempts to get the program on the network.
///
/// # Details
/// Returns `None` if the program does not exist.
pub async fn get_program(&self, vk_hash: B256) -> Result<Option<GetProgramResponse>> {
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
match rpc
.get_program(AuctionGetProgramRequest { vk_hash: vk_hash.to_vec() })
.await
{
StdOk(response) => {
Ok(Some(GetProgramResponse::from(response.into_inner())))
}
Err(status) if status.code() == Code::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
},
"getting program",
)
.await
}
NetworkMode::Reserved => {
self.with_retry(
|| async {
let mut rpc = self.base_prover_network_client().await?;
match rpc
.get_program(BaseGetProgramRequest { vk_hash: vk_hash.to_vec() })
.await
{
StdOk(response) => {
Ok(Some(GetProgramResponse::from(response.into_inner())))
}
Err(status) if status.code() == Code::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
},
"getting program",
)
.await
}
}
}
/// Creates a new program on the network.
pub async fn create_program(
&self,
vk_hash: B256,
vk: &SP1VerifyingKey,
elf: &[u8],
) -> Result<CreateProgramResponse> {
// Create the program artifact.
let mut store = self.artifact_store_client().await?;
let program_uri =
self.create_artifact_with_content(&mut store, ArtifactType::Program, &elf).await?;
// Serialize the verifying key.
let vk_encoded = bincode::serialize(&vk)?;
// Send the request.
self.with_retry(
|| async {
let mut rpc = self.prover_network_client().await?;
let nonce = self.get_nonce().await?;
let request_body = CreateProgramRequestBody {
nonce,
vk_hash: vk_hash.to_vec(),
vk: vk_encoded.clone(),
program_uri: program_uri.clone(),
};
Ok(rpc
.create_program(CreateProgramRequest {
format: MessageFormat::Binary.into(),
signature: request_body.sign(&self.signer).await?,
body: Some(request_body),
})
.await?
.into_inner())
},
"creating program",
)
.await
}
/// Gets the proof request parameters from the network.
/// This is only available in Mainnet (auction) mode.
pub async fn get_proof_request_params(
&self,
mode: ProofMode,
) -> Result<GetProofRequestParamsResponse> {
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
let response = rpc
.get_proof_request_params(AuctionGetProofRequestParamsRequest {
mode: mode.into(),
})
.await?
.into_inner();
Ok(GetProofRequestParamsResponse::from(response))
},
"getting proof request parameters",
)
.await
}
NetworkMode::Reserved => Ok(GetProofRequestParamsResponse::Unsupported),
}
}
/// Get all the proof requests that meet the filter criteria.
#[allow(clippy::too_many_arguments)]
pub async fn get_filtered_proof_requests(
&self,
version: Option<String>,
fulfillment_status: Option<i32>,
execution_status: Option<i32>,
minimum_deadline: Option<u64>,
vk_hash: Option<Vec<u8>>,
requester: Option<Vec<u8>>,
fulfiller: Option<Vec<u8>>,
from: Option<u64>,
to: Option<u64>,
limit: Option<u32>,
page: Option<u32>,
mode: Option<i32>,
not_bid_by: Option<Vec<u8>>,
execute_fail_cause: Option<i32>,
settlement_status: Option<i32>,
error: Option<i32>,
) -> Result<GetFilteredProofRequestsResponse> {
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| {
let version = version.clone();
let vk_hash = vk_hash.clone();
let requester = requester.clone();
let fulfiller = fulfiller.clone();
let not_bid_by = not_bid_by.clone();
async move {
let mut rpc = self.auction_prover_network_client().await?;
let response = rpc
.get_filtered_proof_requests(
AuctionGetFilteredProofRequestsRequest {
version,
fulfillment_status,
execution_status,
minimum_deadline,
vk_hash,
requester,
fulfiller,
from,
to,
limit,
page,
mode,
not_bid_by,
execute_fail_cause,
settlement_status,
error,
},
)
.await?
.into_inner();
Ok(GetFilteredProofRequestsResponse::from(response))
}
},
"getting filtered proof requests",
)
.await
}
NetworkMode::Reserved => {
self.with_retry(
|| {
let version = version.clone();
let vk_hash = vk_hash.clone();
let requester = requester.clone();
let fulfiller = fulfiller.clone();
let not_bid_by = not_bid_by.clone();
async move {
let mut rpc = self.base_prover_network_client().await?;
let response = rpc
.get_filtered_proof_requests(BaseGetFilteredProofRequestsRequest {
version,
fulfillment_status,
execution_status,
minimum_deadline,
vk_hash,
requester,
fulfiller,
from,
to,
limit,
page,
mode,
not_bid_by,
execute_fail_cause,
settlement_status,
error,
})
.await?
.into_inner();
Ok(GetFilteredProofRequestsResponse::from(response))
}
},
"getting filtered proof requests",
)
.await
}
}
}
/// Get the status of a given proof.
///
/// # Details
/// If the status is Fulfilled, the proof is also returned.
pub async fn get_proof_request_status<P: DeserializeOwned>(
&self,
request_id: B256,
timeout: Option<Duration>,
) -> Result<(GetProofRequestStatusResponse, Option<P>)> {
// Get the status.
let res = match self.network_mode {
NetworkMode::Mainnet => {
let auction_response = self
.with_retry_timeout(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
Ok(rpc
.get_proof_request_status(AuctionGetProofRequestStatusRequest {
request_id: request_id.to_vec(),
})
.await?
.into_inner())
},
timeout.unwrap_or(DEFAULT_RETRY_TIMEOUT),
"getting proof request status",
)
.await?;
GetProofRequestStatusResponse::from(auction_response)
}
NetworkMode::Reserved => {
let base_response = self
.with_retry_timeout(
|| async {
let mut rpc = self.base_prover_network_client().await?;
Ok(rpc
.get_proof_request_status(BaseGetProofRequestStatusRequest {
request_id: request_id.to_vec(),
})
.await?
.into_inner())
},
timeout.unwrap_or(DEFAULT_RETRY_TIMEOUT),
"getting proof request status",
)
.await?;
GetProofRequestStatusResponse::from(base_response)
}
};
let status = FulfillmentStatus::try_from(res.fulfillment_status())?;
let proof = match status {
FulfillmentStatus::Fulfilled => {
let proof_uri =
res.proof_uri().ok_or_else(|| anyhow::anyhow!("No proof URI provided"))?;
let proof_bytes = self.download_artifact(proof_uri).await?;
Some(bincode::deserialize(&proof_bytes).context("Failed to deserialize proof")?)
}
_ => None,
};
Ok((res, proof))
}
/// Get the details of a given proof request.
pub async fn get_proof_request_details(
&self,
request_id: B256,
timeout: Option<Duration>,
) -> Result<GetProofRequestDetailsResponse> {
let res = self
.with_retry_timeout(
|| async {
let mut rpc = self.prover_network_client().await?;
Ok(rpc
.get_proof_request_details(GetProofRequestDetailsRequest {
request_id: request_id.to_vec(),
})
.await?
.into_inner())
},
timeout.unwrap_or(DEFAULT_RETRY_TIMEOUT),
"getting proof request details",
)
.await?;
Ok(res)
}
/// Creates a proof request with the given verifying key hash and stdin.
///
/// # Details
/// * `vk_hash`: The verifying key hash of the program to prove. Used to identify the program.
/// * `stdin`: The standard input to provide to the program.
/// * `mode`: The [`ProofMode`] to use.
/// * `version`: The version of the SP1 circuits to use.
/// * `strategy`: The [`FulfillmentStrategy`] to use.
/// * `timeout_secs`: The timeout for the proof request in seconds.
/// * `cycle_limit`: The cycle limit for the proof request.
/// * `gas_limit`: The gas limit for the proof request.
/// * `min_auction_period`: The minimum auction period for the proof request in seconds.
/// * `whitelist`: The auction whitelist for the proof request.
/// * `auctioneer`: The auctioneer for the proof request.
/// * `executor`: The executor for the proof request.
/// * `verifier`: The verifier for the proof request.
/// * `treasury`: The treasury for the proof request.
/// * `public_values_hash`: The hash of the public values to use for the proof.
/// * `base_fee`: The base fee to use for the proof request.
/// * `max_price_per_pgu`: The maximum price per PGU to use for the proof request.
/// * `domain`: The domain bytes to use for the proof request.
#[allow(clippy::too_many_arguments)]
#[allow(unused_variables)]
pub async fn request_proof(
&self,
vk_hash: B256,
stdin: &SP1Stdin,
mode: ProofMode,
version: &str,
strategy: FulfillmentStrategy,
timeout_secs: u64,
cycle_limit: u64,
gas_limit: u64,
min_auction_period: u64,
whitelist: Option<Vec<Address>>,
auctioneer: Address,
executor: Address,
verifier: Address,
treasury: Address,
public_values_hash: Option<Vec<u8>>,
base_fee: u64,
max_price_per_pgu: u64,
domain: Vec<u8>,
) -> Result<RequestProofResponse> {
// Calculate the deadline.
let start = SystemTime::now();
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Invalid start time");
let deadline = since_the_epoch.as_secs() + timeout_secs;
// Create the stdin artifact.
let mut store = self.artifact_store_client().await?;
let stdin_uri =
self.create_artifact_with_content(&mut store, ArtifactType::Stdin, &stdin).await?;
// Send the request.
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
let nonce = self.get_nonce().await?;
let whitelist = if let Some(whitelist) = &whitelist {
whitelist.iter().map(|addr| addr.to_vec()).collect()
} else {
let result = rpc
.get_provers_by_uptime(AuctionGetProversByUptimeRequest {
high_availability_only: false,
})
.await?;
result.into_inner().provers
};
let request_body = AuctionRequestProofRequestBody {
nonce,
version: format!("sp1-{version}"),
vk_hash: vk_hash.to_vec(),
mode: mode.into(),
strategy: strategy.into(),
stdin_uri: stdin_uri.clone(),
deadline,
cycle_limit,
gas_limit,
min_auction_period,
whitelist,
domain: domain.clone(),
auctioneer: auctioneer.to_vec(),
executor: executor.to_vec(),
verifier: verifier.to_vec(),
treasury: treasury.to_vec(),
public_values_hash: public_values_hash.clone(),
base_fee: base_fee.to_string(),
max_price_per_pgu: max_price_per_pgu.to_string(),
variant: AuctionTransactionVariant::RequestVariant.into(),
};
let request_response = rpc
.request_proof(AuctionRequestProofRequest {
format: AuctionMessageFormat::Binary.into(),
signature: request_body.sign(&self.signer).await?,
body: Some(request_body),
})
.await?
.into_inner();
Ok(RequestProofResponse::from(request_response))
},
"requesting proof",
)
.await
}
NetworkMode::Reserved => {
self.with_retry(
|| async {
let mut rpc = self.base_prover_network_client().await?;
let nonce = self.get_nonce().await?;
let request_body = BaseRequestProofRequestBody {
nonce,
version: format!("sp1-{version}"),
vk_hash: vk_hash.to_vec(),
mode: mode.into(),
strategy: strategy.into(),
stdin_uri: stdin_uri.clone(),
deadline,
cycle_limit,
gas_limit,
min_auction_period,
whitelist: whitelist
.clone()
.map(|list| list.into_iter().map(|addr| addr.to_vec()).collect())
.unwrap_or_default(),
};
let request_response = rpc
.request_proof(BaseRequestProofRequest {
format: BaseMessageFormat::Binary.into(),
signature: request_body.sign(&self.signer).await?,
body: Some(request_body),
})
.await?
.into_inner();
Ok(RequestProofResponse::from(request_response))
},
"requesting proof",
)
.await
}
}
}
// NetworkMode-aware generic client for shared operations (create_program,
// get_proof_request_details).
pub(crate) async fn prover_network_client(
&self,
) -> Result<AuctionProverNetworkClient<Channel>> {
// For shared operations, we use the auction client type as it provides the default types.
// The actual network routing is handled by the RPC URL which is correctly set based on
// network_mode.
self.auction_prover_network_client().await
}
// Helper methods for runtime proto type selection.
pub(crate) async fn auction_prover_network_client(
&self,
) -> Result<AuctionProverNetworkClient<Channel>> {
self.with_retry(
|| async {
let channel = grpc::configure_endpoint(&self.rpc_url)?.connect().await?;
Ok(AuctionProverNetworkClient::new(channel))
},
"creating auction network client",
)
.await
}
pub(crate) async fn base_prover_network_client(
&self,
) -> Result<BaseProverNetworkClient<Channel>> {
self.with_retry(
|| async {
let channel = grpc::configure_endpoint(&self.rpc_url)?.connect().await?;
Ok(BaseProverNetworkClient::new(channel))
},
"creating base network client",
)
.await
}
pub(crate) async fn artifact_store_client(&self) -> Result<ArtifactStoreClient<Channel>> {
self.with_retry(
|| async {
let channel = grpc::configure_endpoint(&self.rpc_url)?.connect().await?;
Ok(ArtifactStoreClient::new(channel))
},
"creating artifact client",
)
.await
}
pub(crate) async fn create_artifact_with_content<T: Serialize + Send + Sync>(
&self,
store: &mut ArtifactStoreClient<Channel>,
artifact_type: ArtifactType,
item: &T,
) -> Result<String> {
let signature = sign_message("create_artifact".as_bytes(), &self.signer).await?;
let request = CreateArtifactRequest { artifact_type: artifact_type.into(), signature };
// Create the artifact.
let response = store.create_artifact(request).await?.into_inner();
let presigned_url = response.artifact_presigned_url;
let uri = response.artifact_uri;
// Serialize and compress the content once before retrying uploads.
// Using compression level 3 for a good balance of speed and compression ratio.
let serialized = bincode::serialize::<T>(item)?;
let compressed = zstd::encode_all(&serialized[..], 3)
.map_err(|e| anyhow::anyhow!("Failed to compress artifact: {e}"))?;
// Upload the compressed content.
self.with_retry(
|| async {
let response =
self.http.put(&presigned_url).body(compressed.clone()).send().await?;
if !response.status().is_success() {
return Err(anyhow::anyhow!(
"Failed to upload artifact: HTTP {}",
response.status()
));
}
Ok(())
},
"uploading artifact content",
)
.await?;
Ok(uri)
}
pub(crate) async fn download_artifact(&self, uri: &str) -> Result<Vec<u8>> {
self.with_retry(
|| async {
let response =
self.http.get(uri).send().await.context("Failed to download from URI")?;
if !response.status().is_success() {
return Err(anyhow::anyhow!(
"Failed to download artifact: HTTP {}",
response.status()
));
}
Ok(response.bytes().await.context("Failed to read response body")?.to_vec())
},
"downloading artifact",
)
.await
}
/// Cancel a proof request. This is only available in Mainnet (auction) mode.
pub async fn cancel_request(&self, request_id: B256) -> Result<CancelRequestResponse> {
match self.network_mode {
NetworkMode::Mainnet => {
self.with_retry(
|| async {
let mut rpc = self.auction_prover_network_client().await?;
let nonce = self.get_nonce().await?;
let request_body = AuctionCancelRequestRequestBody {
nonce,
request_id: request_id.to_vec(),
};
let response = rpc
.cancel_request(AuctionCancelRequestRequest {
format: AuctionMessageFormat::Binary.into(),
signature: request_body.sign(&self.signer).await?,
body: Some(request_body),
})
.await?
.into_inner();
Ok(CancelRequestResponse::from(response))
},
"cancelling request",
)
.await
}
NetworkMode::Reserved => Ok(CancelRequestResponse::Unsupported),
}
}
}
#[cfg(test)]
mod test {
use crate::network::{signer::NetworkSigner, NetworkMode, RESERVED_RPC_URL};
#[test]
fn test_can_create_network_client_with_0x_bytes() {
let private_key = hex::encode(alloy_signer_local::PrivateKeySigner::random().to_bytes());
let signer = NetworkSigner::local(&private_key).unwrap();
let _ = super::NetworkClient::new(signer, RESERVED_RPC_URL, NetworkMode::Reserved);
}
}