snarkos_node/prover/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod router;
17
18use crate::traits::NodeInterface;
19use snarkos_account::Account;
20use snarkos_node_bft::ledger_service::ProverLedgerService;
21use snarkos_node_router::{
22    Heartbeat,
23    Inbound,
24    Outbound,
25    Router,
26    Routing,
27    messages::{Message, NodeType, UnconfirmedSolution},
28};
29use snarkos_node_sync::{BlockSync, Ping};
30use snarkos_node_tcp::{
31    P2P,
32    protocols::{Disconnect, Handshake, OnConnect, Reading},
33};
34use snarkvm::{
35    ledger::narwhal::Data,
36    prelude::{
37        Network,
38        block::{Block, Header},
39        puzzle::{Puzzle, Solution},
40        store::ConsensusStorage,
41    },
42    synthesizer::VM,
43};
44
45use anyhow::Result;
46use colored::Colorize;
47use core::{marker::PhantomData, time::Duration};
48#[cfg(feature = "locktick")]
49use locktick::parking_lot::{Mutex, RwLock};
50#[cfg(not(feature = "locktick"))]
51use parking_lot::{Mutex, RwLock};
52use rand::{CryptoRng, Rng, rngs::OsRng};
53use snarkos_node_bft::helpers::fmt_id;
54use std::{
55    net::SocketAddr,
56    sync::{
57        Arc,
58        atomic::{AtomicBool, AtomicU8, Ordering},
59    },
60};
61use tokio::task::JoinHandle;
62
63/// A prover is a light node, capable of producing proofs for consensus.
64#[derive(Clone)]
65pub struct Prover<N: Network, C: ConsensusStorage<N>> {
66    /// The router of the node.
67    router: Router<N>,
68    /// The sync module.
69    sync: Arc<BlockSync<N>>,
70    /// The genesis block.
71    genesis: Block<N>,
72    /// The puzzle.
73    puzzle: Puzzle<N>,
74    /// The latest epoch hash.
75    latest_epoch_hash: Arc<RwLock<Option<N::BlockHash>>>,
76    /// The latest block header.
77    latest_block_header: Arc<RwLock<Option<Header<N>>>>,
78    /// The number of puzzle instances.
79    puzzle_instances: Arc<AtomicU8>,
80    /// The maximum number of puzzle instances.
81    max_puzzle_instances: u8,
82    /// The spawned handles.
83    handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
84    /// The shutdown signal.
85    shutdown: Arc<AtomicBool>,
86    /// Keeps track of sending pings.
87    ping: Arc<Ping<N>>,
88    /// PhantomData.
89    _phantom: PhantomData<C>,
90}
91
92impl<N: Network, C: ConsensusStorage<N>> Prover<N, C> {
93    /// Initializes a new prover node.
94    pub async fn new(
95        node_ip: SocketAddr,
96        account: Account<N>,
97        trusted_peers: &[SocketAddr],
98        genesis: Block<N>,
99        dev: Option<u16>,
100        shutdown: Arc<AtomicBool>,
101    ) -> Result<Self> {
102        // Initialize the signal handler.
103        let signal_node = Self::handle_signals(shutdown.clone());
104
105        // Initialize the ledger service.
106        let ledger_service = Arc::new(ProverLedgerService::new());
107        // Determine if the prover should allow external peers.
108        let allow_external_peers = true;
109        // Determine if the prover should rotate external peers.
110        let rotate_external_peers = false;
111
112        // Initialize the node router.
113        let router = Router::new(
114            node_ip,
115            NodeType::Prover,
116            account,
117            ledger_service.clone(),
118            trusted_peers,
119            Self::MAXIMUM_NUMBER_OF_PEERS as u16,
120            rotate_external_peers,
121            allow_external_peers,
122            dev.is_some(),
123        )
124        .await?;
125
126        // Initialize the sync module.
127        let sync = BlockSync::new(ledger_service.clone());
128
129        // Set up the ping logic.
130        let ping = Arc::new(Ping::new_nosync(router.clone()));
131
132        // Compute the maximum number of puzzle instances.
133        let max_puzzle_instances = num_cpus::get().saturating_sub(2).clamp(1, 6);
134        // Initialize the node.
135        let node = Self {
136            router,
137            sync: Arc::new(sync),
138            genesis,
139            puzzle: VM::<N, C>::new_puzzle()?,
140            latest_epoch_hash: Default::default(),
141            latest_block_header: Default::default(),
142            puzzle_instances: Default::default(),
143            max_puzzle_instances: u8::try_from(max_puzzle_instances)?,
144            handles: Default::default(),
145            ping,
146            shutdown,
147            _phantom: Default::default(),
148        };
149        // Initialize the routing.
150        node.initialize_routing().await;
151        // Initialize the puzzle.
152        node.initialize_puzzle().await;
153        // Initialize the notification message loop.
154        node.handles.lock().push(crate::start_notification_message_loop());
155        // Pass the node to the signal handler.
156        let _ = signal_node.set(node.clone());
157        // Return the node.
158        Ok(node)
159    }
160}
161
162#[async_trait]
163impl<N: Network, C: ConsensusStorage<N>> NodeInterface<N> for Prover<N, C> {
164    /// Shuts down the node.
165    async fn shut_down(&self) {
166        info!("Shutting down...");
167
168        // Shut down the puzzle.
169        debug!("Shutting down the puzzle...");
170        self.shutdown.store(true, Ordering::Release);
171
172        // Abort the tasks.
173        debug!("Shutting down the prover...");
174        self.handles.lock().iter().for_each(|handle| handle.abort());
175
176        // Shut down the router.
177        self.router.shut_down().await;
178
179        info!("Node has shut down.");
180    }
181}
182
183impl<N: Network, C: ConsensusStorage<N>> Prover<N, C> {
184    /// Initialize a new instance of the puzzle.
185    async fn initialize_puzzle(&self) {
186        for _ in 0..self.max_puzzle_instances {
187            let prover = self.clone();
188            self.handles.lock().push(tokio::spawn(async move {
189                prover.puzzle_loop().await;
190            }));
191        }
192    }
193
194    /// Executes an instance of the puzzle.
195    async fn puzzle_loop(&self) {
196        loop {
197            // If the node is not connected to any peers, then skip this iteration.
198            if self.router.number_of_connected_peers() == 0 {
199                debug!("Skipping an iteration of the puzzle (no connected peers)");
200                tokio::time::sleep(Duration::from_secs(N::ANCHOR_TIME as u64)).await;
201                continue;
202            }
203
204            // If the number of instances of the puzzle exceeds the maximum, then skip this iteration.
205            if self.num_puzzle_instances() > self.max_puzzle_instances {
206                // Sleep for a brief period of time.
207                tokio::time::sleep(Duration::from_millis(500)).await;
208                continue;
209            }
210
211            // Read the latest epoch hash.
212            let latest_epoch_hash = *self.latest_epoch_hash.read();
213            // Read the latest state.
214            let latest_state = self
215                .latest_block_header
216                .read()
217                .as_ref()
218                .map(|header| (header.coinbase_target(), header.proof_target()));
219
220            // If the latest epoch hash and latest state exists, then proceed to generate a solution.
221            if let (Some(epoch_hash), Some((coinbase_target, proof_target))) = (latest_epoch_hash, latest_state) {
222                // Execute the puzzle.
223                let prover = self.clone();
224                let result = tokio::task::spawn_blocking(move || {
225                    prover.puzzle_iteration(epoch_hash, coinbase_target, proof_target, &mut OsRng)
226                })
227                .await;
228
229                // If the prover found a solution, then broadcast it.
230                if let Ok(Some((solution_target, solution))) = result {
231                    info!("Found a Solution '{}' (Proof Target {solution_target})", solution.id());
232                    // Broadcast the solution.
233                    self.broadcast_solution(solution);
234                }
235            } else {
236                // Otherwise, sleep for a brief period of time, to await for puzzle state.
237                tokio::time::sleep(Duration::from_secs(1)).await;
238            }
239
240            // If the Ctrl-C handler registered the signal, stop the prover.
241            if self.shutdown.load(Ordering::Acquire) {
242                debug!("Shutting down the puzzle...");
243                break;
244            }
245        }
246    }
247
248    /// Performs one iteration of the puzzle.
249    fn puzzle_iteration<R: Rng + CryptoRng>(
250        &self,
251        epoch_hash: N::BlockHash,
252        coinbase_target: u64,
253        proof_target: u64,
254        rng: &mut R,
255    ) -> Option<(u64, Solution<N>)> {
256        // Increment the puzzle instances.
257        self.increment_puzzle_instances();
258
259        debug!(
260            "Proving 'Puzzle' for Epoch '{}' {}",
261            fmt_id(epoch_hash),
262            format!("(Coinbase Target {coinbase_target}, Proof Target {proof_target})").dimmed()
263        );
264
265        // Compute the solution.
266        let result =
267            self.puzzle.prove(epoch_hash, self.address(), rng.r#gen(), Some(proof_target)).ok().and_then(|solution| {
268                self.puzzle.get_proof_target(&solution).ok().map(|solution_target| (solution_target, solution))
269            });
270
271        // Decrement the puzzle instances.
272        self.decrement_puzzle_instances();
273        // Return the result.
274        result
275    }
276
277    /// Broadcasts the solution to the network.
278    fn broadcast_solution(&self, solution: Solution<N>) {
279        // Prepare the unconfirmed solution message.
280        let message = Message::UnconfirmedSolution(UnconfirmedSolution {
281            solution_id: solution.id(),
282            solution: Data::Object(solution),
283        });
284        // Propagate the "UnconfirmedSolution".
285        self.propagate(message, &[]);
286    }
287
288    /// Returns the current number of puzzle instances.
289    fn num_puzzle_instances(&self) -> u8 {
290        self.puzzle_instances.load(Ordering::Relaxed)
291    }
292
293    /// Increments the number of puzzle instances.
294    fn increment_puzzle_instances(&self) {
295        self.puzzle_instances.fetch_add(1, Ordering::Relaxed);
296        #[cfg(debug_assertions)]
297        trace!("Number of Instances - {}", self.num_puzzle_instances());
298    }
299
300    /// Decrements the number of puzzle instances.
301    fn decrement_puzzle_instances(&self) {
302        self.puzzle_instances.fetch_sub(1, Ordering::Relaxed);
303        #[cfg(debug_assertions)]
304        trace!("Number of Instances - {}", self.num_puzzle_instances());
305    }
306}