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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkOS library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod router;
use crate::traits::NodeInterface;
use snarkos_account::Account;
use snarkos_node_messages::{Data, Message, NodeType, UnconfirmedSolution};
use snarkos_node_router::{Heartbeat, Inbound, Outbound, Router, Routing};
use snarkos_node_tcp::{
protocols::{Disconnect, Handshake, OnConnect, Reading, Writing},
P2P,
};
use snarkvm::prelude::{Block, CoinbasePuzzle, ConsensusStorage, EpochChallenge, Header, Network, ProverSolution};
use anyhow::Result;
use colored::Colorize;
use core::{marker::PhantomData, time::Duration};
use parking_lot::{Mutex, RwLock};
use rand::{rngs::OsRng, CryptoRng, Rng};
use std::{
net::SocketAddr,
sync::{
atomic::{AtomicBool, AtomicU8, Ordering},
Arc,
},
};
use tokio::task::JoinHandle;
/// A prover is a full node, capable of producing proofs for consensus.
#[derive(Clone)]
pub struct Prover<N: Network, C: ConsensusStorage<N>> {
/// The router of the node.
router: Router<N>,
/// The genesis block.
genesis: Block<N>,
/// The coinbase puzzle.
coinbase_puzzle: CoinbasePuzzle<N>,
/// The latest epoch challenge.
latest_epoch_challenge: Arc<RwLock<Option<Arc<EpochChallenge<N>>>>>,
/// The latest block header.
latest_block_header: Arc<RwLock<Option<Header<N>>>>,
/// The number of puzzle instances.
puzzle_instances: Arc<AtomicU8>,
/// The maximum number of puzzle instances.
max_puzzle_instances: u8,
/// The spawned handles.
handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
/// The shutdown signal.
shutdown: Arc<AtomicBool>,
/// PhantomData.
_phantom: PhantomData<C>,
}
impl<N: Network, C: ConsensusStorage<N>> Prover<N, C> {
/// Initializes a new prover node.
pub async fn new(
node_ip: SocketAddr,
account: Account<N>,
trusted_peers: &[SocketAddr],
genesis: Block<N>,
dev: Option<u16>,
) -> Result<Self> {
// Initialize the node router.
let router = Router::new(
node_ip,
NodeType::Prover,
account,
trusted_peers,
Self::MAXIMUM_NUMBER_OF_PEERS as u16,
dev.is_some(),
)
.await?;
// Load the coinbase puzzle.
let coinbase_puzzle = CoinbasePuzzle::<N>::load()?;
// Compute the maximum number of puzzle instances.
let max_puzzle_instances = num_cpus::get().saturating_sub(2).clamp(1, 6);
// Initialize the node.
let node = Self {
router,
genesis,
coinbase_puzzle,
latest_epoch_challenge: Default::default(),
latest_block_header: Default::default(),
puzzle_instances: Default::default(),
max_puzzle_instances: u8::try_from(max_puzzle_instances)?,
handles: Default::default(),
shutdown: Default::default(),
_phantom: Default::default(),
};
// Initialize the routing.
node.initialize_routing().await;
// Initialize the coinbase puzzle.
node.initialize_coinbase_puzzle().await;
// Initialize the signal handler.
node.handle_signals();
// Return the node.
Ok(node)
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> NodeInterface<N> for Prover<N, C> {
/// Shuts down the node.
async fn shut_down(&self) {
info!("Shutting down...");
// Shut down the coinbase puzzle.
trace!("Shutting down the coinbase puzzle...");
self.shutdown.store(true, Ordering::Relaxed);
// Abort the tasks.
trace!("Shutting down the prover...");
self.handles.lock().iter().for_each(|handle| handle.abort());
// Shut down the router.
self.router.shut_down().await;
info!("Node has shut down.");
}
}
impl<N: Network, C: ConsensusStorage<N>> Prover<N, C> {
/// Initialize a new instance of the coinbase puzzle.
async fn initialize_coinbase_puzzle(&self) {
for _ in 0..self.max_puzzle_instances {
let prover = self.clone();
self.handles.lock().push(tokio::spawn(async move {
prover.coinbase_puzzle_loop().await;
}));
}
}
/// Executes an instance of the coinbase puzzle.
async fn coinbase_puzzle_loop(&self) {
loop {
// If the node is not connected to any peers, then skip this iteration.
if self.router.number_of_connected_peers() == 0 {
trace!("Skipping an iteration of the coinbase puzzle (no connected peers)");
tokio::time::sleep(Duration::from_secs(N::ANCHOR_TIME as u64)).await;
continue;
}
// If the number of instances of the coinbase puzzle exceeds the maximum, then skip this iteration.
if self.num_puzzle_instances() > self.max_puzzle_instances {
// Sleep for a brief period of time.
tokio::time::sleep(Duration::from_millis(500)).await;
continue;
}
// Read the latest epoch challenge.
let latest_epoch_challenge = self.latest_epoch_challenge.read().clone();
// Read the latest state.
let latest_state = self
.latest_block_header
.read()
.as_ref()
.map(|header| (header.coinbase_target(), header.proof_target()));
// If the latest epoch challenge and latest state exists, then proceed to generate a prover solution.
if let (Some(challenge), Some((coinbase_target, proof_target))) = (latest_epoch_challenge, latest_state) {
// Execute the coinbase puzzle.
let prover = self.clone();
let result = tokio::task::spawn_blocking(move || {
prover.coinbase_puzzle_iteration(&challenge, coinbase_target, proof_target, &mut OsRng)
})
.await;
// If the prover found a solution, then broadcast it.
if let Ok(Some((solution_target, solution))) = result {
info!("Found a Solution '{}' (Proof Target {solution_target})", solution.commitment());
// Broadcast the prover solution.
self.broadcast_prover_solution(solution);
}
} else {
// Otherwise, sleep for a brief period of time, to await for puzzle state.
tokio::time::sleep(Duration::from_secs(1)).await;
}
// If the Ctrl-C handler registered the signal, stop the prover.
if self.shutdown.load(Ordering::Relaxed) {
trace!("Shutting down the coinbase puzzle...");
break;
}
}
}
/// Performs one iteration of the coinbase puzzle.
fn coinbase_puzzle_iteration<R: Rng + CryptoRng>(
&self,
epoch_challenge: &EpochChallenge<N>,
coinbase_target: u64,
proof_target: u64,
rng: &mut R,
) -> Option<(u64, ProverSolution<N>)> {
// Increment the puzzle instances.
self.increment_puzzle_instances();
trace!(
"Proving 'CoinbasePuzzle' {}",
format!(
"(Epoch {}, Coinbase Target {coinbase_target}, Proof Target {proof_target})",
epoch_challenge.epoch_number(),
)
.dimmed()
);
// Compute the prover solution.
let result = self
.coinbase_puzzle
.prove(epoch_challenge, self.address(), rng.gen(), Some(proof_target))
.ok()
.and_then(|solution| solution.to_target().ok().map(|solution_target| (solution_target, solution)));
// Decrement the puzzle instances.
self.decrement_puzzle_instances();
// Return the result.
result
}
/// Broadcasts the prover solution to the network.
fn broadcast_prover_solution(&self, prover_solution: ProverSolution<N>) {
// Prepare the unconfirmed solution message.
let message = Message::UnconfirmedSolution(UnconfirmedSolution {
puzzle_commitment: prover_solution.commitment(),
solution: Data::Object(prover_solution),
});
// Propagate the "UnconfirmedSolution" to the connected validators.
self.propagate_to_validators(message, &[]);
}
/// Returns the current number of puzzle instances.
fn num_puzzle_instances(&self) -> u8 {
self.puzzle_instances.load(Ordering::Relaxed)
}
/// Increments the number of puzzle instances.
fn increment_puzzle_instances(&self) {
self.puzzle_instances.fetch_add(1, Ordering::Relaxed);
#[cfg(debug_assertions)]
trace!("Number of Instances - {}", self.num_puzzle_instances());
}
/// Decrements the number of puzzle instances.
fn decrement_puzzle_instances(&self) {
self.puzzle_instances.fetch_sub(1, Ordering::Relaxed);
#[cfg(debug_assertions)]
trace!("Number of Instances - {}", self.num_puzzle_instances());
}
}