Skip to main content

sp1_sdk/blocking/network/
mod.rs

1//! # SP1 Network Prover (Blocking)
2//!
3//! A blocking wrapper around the async [`crate::network::prover::NetworkProver`] that can generate
4//! proofs on a remote RPC server.
5
6pub mod builder;
7pub mod prove;
8
9use std::time::Duration;
10
11use alloy_primitives::{B256, U256};
12use anyhow::Result;
13use prove::NetworkProveBuilder;
14use sp1_core_executor::StatusCode;
15use sp1_core_machine::io::SP1Stdin;
16use sp1_primitives::Elf;
17use sp1_prover::{worker::SP1NodeCore, SP1VerifyingKey};
18
19use crate::{
20    blocking::prover::Prover,
21    network::{
22        proto::{
23            types::{FulfillmentStrategy, ProofRequest},
24            GetProofRequestParamsResponse, GetProofRequestStatusResponse,
25        },
26        NetworkMode,
27    },
28    SP1ProofMode, SP1ProofWithPublicValues, SP1ProvingKey, SP1VerificationError,
29};
30
31/// A blocking implementation of the network prover that can generate proofs on a remote RPC server.
32#[derive(Clone)]
33pub struct NetworkProver {
34    pub(crate) prover: crate::network::prover::NetworkProver,
35}
36
37impl Prover for NetworkProver {
38    type ProvingKey = SP1ProvingKey;
39    type Error = anyhow::Error;
40    type ProveRequest<'a> = NetworkProveBuilder<'a>;
41
42    fn inner(&self) -> &SP1NodeCore {
43        self.prover.node.inner()
44    }
45
46    fn setup(&self, elf: Elf) -> Result<Self::ProvingKey, Self::Error> {
47        crate::blocking::block_on(async {
48            let vk = self.prover.node.setup(&elf).await?;
49            Ok(SP1ProvingKey { vk, elf })
50        })
51    }
52
53    fn prove<'a>(&'a self, pk: &'a Self::ProvingKey, stdin: SP1Stdin) -> Self::ProveRequest<'a> {
54        let strategy = self.prover.default_fulfillment_strategy();
55
56        NetworkProveBuilder {
57            base: crate::blocking::prover::BaseProveRequest::new(self, pk, stdin),
58            timeout: None,
59            strategy,
60            skip_simulation: false,
61            cycle_limit: None,
62            gas_limit: None,
63            tee_2fa: false,
64            min_auction_period: 0,
65            whitelist: None,
66            auctioneer: None,
67            executor: None,
68            verifier: None,
69            treasury: None,
70            max_price_per_pgu: None,
71            auction_timeout: None,
72            private_stdin: false,
73        }
74    }
75
76    fn verify(
77        &self,
78        proof: &SP1ProofWithPublicValues,
79        vkey: &SP1VerifyingKey,
80        status_code: Option<StatusCode>,
81    ) -> Result<(), SP1VerificationError> {
82        crate::Prover::verify(&self.prover, proof, vkey, status_code)
83    }
84}
85
86impl NetworkProver {
87    /// Gets the network mode of this prover.
88    #[must_use]
89    pub fn network_mode(&self) -> NetworkMode {
90        self.prover.network_mode()
91    }
92
93    /// Gets the default fulfillment strategy for this prover's network mode.
94    #[must_use]
95    pub fn default_fulfillment_strategy(&self) -> FulfillmentStrategy {
96        self.prover.default_fulfillment_strategy()
97    }
98
99    /// Get the credit balance of your account on the prover network.
100    ///
101    /// # Example
102    /// ```rust,no_run
103    /// use sp1_sdk::blocking::{Prover, ProverClient, SP1Stdin};
104    ///
105    /// let client = ProverClient::builder().network().build();
106    /// let balance = client.get_balance().unwrap();
107    /// ```
108    pub fn get_balance(&self) -> Result<U256> {
109        crate::blocking::block_on(self.prover.get_balance())
110    }
111
112    /// Registers a program if it is not already registered.
113    ///
114    /// # Details
115    /// * `vk`: The verifying key to use for the program.
116    /// * `elf`: The elf to use for the program.
117    pub fn register_program(&self, vk: &SP1VerifyingKey, elf: &[u8]) -> Result<B256> {
118        crate::blocking::block_on(self.prover.register_program(vk, elf))
119    }
120
121    /// Gets the proof request parameters from the network.
122    ///
123    /// # Details
124    /// * `mode`: The proof mode to get the parameters for.
125    pub fn get_proof_request_params(
126        &self,
127        mode: SP1ProofMode,
128    ) -> Result<GetProofRequestParamsResponse> {
129        crate::blocking::block_on(self.prover.get_proof_request_params(mode))
130    }
131
132    /// Gets the status of a proof request.
133    ///
134    /// # Details
135    /// * `request_id`: The request ID to get the status of.
136    pub fn get_proof_status(
137        &self,
138        request_id: B256,
139    ) -> Result<(GetProofRequestStatusResponse, Option<SP1ProofWithPublicValues>)> {
140        crate::blocking::block_on(self.prover.get_proof_status(request_id))
141    }
142
143    /// Gets the proof request details, if available.
144    ///
145    /// # Details
146    /// * `request_id`: The request ID to get the details of.
147    pub fn get_proof_request(&self, request_id: B256) -> Result<Option<ProofRequest>> {
148        crate::blocking::block_on(self.prover.get_proof_request(request_id))
149    }
150
151    /// Gets the status of a proof request with handling for timeouts and unfulfillable requests.
152    ///
153    /// # Details
154    /// * `request_id`: The request ID to get the status of.
155    /// * `remaining_timeout`: The remaining timeout for the proof request.
156    pub fn process_proof_status(
157        &self,
158        request_id: B256,
159        remaining_timeout: Option<Duration>,
160    ) -> Result<(Option<SP1ProofWithPublicValues>, crate::network::proto::types::FulfillmentStatus)>
161    {
162        crate::blocking::block_on(self.prover.process_proof_status(request_id, remaining_timeout))
163    }
164
165    /// Cancels a proof request by updating the deadline to the current time.
166    /// Only available in Mainnet mode (auction-based proving).
167    pub fn cancel_request(&self, request_id: B256) -> Result<()> {
168        crate::blocking::block_on(self.prover.cancel_request(request_id))
169    }
170
171    /// Waits for a proof to be generated and returns the proof. If a timeout is supplied, the
172    /// function will return an error if the proof is not generated within the timeout.
173    pub fn wait_proof(
174        &self,
175        request_id: B256,
176        timeout: Option<Duration>,
177        auction_timeout: Option<Duration>,
178    ) -> Result<SP1ProofWithPublicValues> {
179        crate::blocking::block_on(self.prover.wait_proof(request_id, timeout, auction_timeout))
180    }
181}