Skip to main content

snarkos_node/prover/
router.rs

1// Copyright (c) 2019-2026 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
16use super::*;
17
18use snarkos_node_network::harden_socket;
19use snarkos_node_router::messages::{
20    BlockRequest,
21    DisconnectReason,
22    Message,
23    MessageCodec,
24    Ping,
25    Pong,
26    PuzzleRequest,
27    UnconfirmedTransaction,
28};
29use snarkos_node_tcp::{ConnectError, Connection, ConnectionSide, Tcp};
30use snarkvm::{
31    console::network::{ConsensusVersion, Network},
32    ledger::block::Transaction,
33    prelude::{Field, Zero},
34    utilities::into_io_error,
35};
36
37use std::{io, net::SocketAddr};
38
39impl<N: Network, C: ConsensusStorage<N>> P2P for Prover<N, C> {
40    /// Returns a reference to the TCP instance.
41    fn tcp(&self) -> &Tcp {
42        self.router.tcp()
43    }
44}
45
46#[async_trait]
47impl<N: Network, C: ConsensusStorage<N>> Handshake for Prover<N, C> {
48    /// Performs the handshake protocol.
49    async fn perform_handshake(&self, mut connection: Connection) -> Result<Connection, ConnectError> {
50        // Perform the handshake.
51        let peer_addr = connection.addr();
52        let conn_side = connection.side();
53        let stream = self.borrow_stream(&mut connection);
54        // Make the socket more robust.
55        harden_socket(stream)?;
56        let genesis_header = *self.genesis.header();
57        let restrictions_id = Field::zero(); // Provers may bypass restrictions, since they do not validate transactions.
58
59        self.router
60            .handshake(peer_addr, stream, conn_side, genesis_header, restrictions_id)
61            .await
62            .map_err(into_io_error)?;
63
64        Ok(connection)
65    }
66}
67
68#[async_trait]
69impl<N: Network, C: ConsensusStorage<N>> OnConnect for Prover<N, C>
70where
71    Self: Outbound<N>,
72{
73    async fn on_connect(&self, peer_addr: SocketAddr) {
74        // Resolve the peer address to the listener address.
75        if let Some(listener_addr) = self.router().resolve_to_listener(peer_addr)
76            && let Some(peer) = self.router().get_connected_peer(listener_addr)
77            && peer.node_type != NodeType::BootstrapClient
78        {
79            // Send the first `Ping` message to the peer.
80            self.ping.on_peer_connected(listener_addr);
81        }
82    }
83}
84
85#[async_trait]
86impl<N: Network, C: ConsensusStorage<N>> Disconnect for Prover<N, C> {
87    /// Any extra operations to be performed during a disconnect.
88    async fn handle_disconnect(&self, peer_addr: SocketAddr) {
89        if let Some(peer_ip) = self.router.resolve_to_listener(peer_addr) {
90            self.sync.remove_peer(&peer_ip);
91            self.router.downgrade_peer_to_candidate(peer_ip);
92            // Clear cached entries applicable to the peer.
93            self.router.cache().clear_peer_entries(peer_ip);
94            #[cfg(feature = "metrics")]
95            self.router.update_metrics();
96        }
97    }
98}
99
100#[async_trait]
101impl<N: Network, C: ConsensusStorage<N>> Reading for Prover<N, C> {
102    type Codec = MessageCodec<N>;
103    type Message = Message<N>;
104
105    /// Creates a [`Decoder`] used to interpret messages from the network.
106    /// The `side` param indicates the connection side **from the node's perspective**.
107    fn codec(&self, _peer_addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
108        Default::default()
109    }
110
111    /// Processes a message received from the network.
112    async fn process_message(&self, peer_addr: SocketAddr, message: Self::Message) -> io::Result<()> {
113        // Process the message. Disconnect if the peer violated the protocol.
114        if let Err(error) = self.inbound(peer_addr, message).await
115            && let Some(peer_ip) = self.router().resolve_to_listener(peer_addr)
116        {
117            warn!("Disconnecting from '{peer_addr}' - {error}");
118            self.router().send(peer_ip, Message::Disconnect(DisconnectReason::ProtocolViolation.into()));
119            // Disconnect from this peer.
120            self.router().disconnect(peer_ip);
121        }
122        Ok(())
123    }
124}
125
126#[async_trait]
127impl<N: Network, C: ConsensusStorage<N>> Routing<N> for Prover<N, C> {}
128
129impl<N: Network, C: ConsensusStorage<N>> Heartbeat<N> for Prover<N, C> {
130    /// This function updates the puzzle if network has updated.
131    fn handle_puzzle_request(&self) {
132        // Find the sync peers.
133        if let Some((sync_peers, _)) = self.sync.find_sync_peers() {
134            // Choose the peer with the highest block height.
135            if let Some((peer_ip, _)) = sync_peers.into_iter().max_by_key(|(_, height)| *height) {
136                // Request the puzzle from the peer.
137                self.router().send(peer_ip, Message::PuzzleRequest(PuzzleRequest));
138            }
139        }
140    }
141}
142
143impl<N: Network, C: ConsensusStorage<N>> Outbound<N> for Prover<N, C> {
144    /// Returns a reference to the router.
145    fn router(&self) -> &Router<N> {
146        &self.router
147    }
148
149    /// Returns `true` if the node is synced up to the latest block (within the given tolerance).
150    fn is_block_synced(&self) -> bool {
151        true
152    }
153
154    /// Returns the number of blocks this node is behind the greatest peer height,
155    /// or `None` if not connected to peers yet.
156    fn num_blocks_behind(&self) -> Option<u32> {
157        //TODO(kaimast): should this return None instead?
158        Some(0)
159    }
160
161    /// Returns the current sync speed in blocks per second.
162    fn get_sync_speed(&self) -> f64 {
163        0.0
164    }
165}
166
167#[async_trait]
168impl<N: Network, C: ConsensusStorage<N>> Inbound<N> for Prover<N, C> {
169    /// Returns `true` if the message version is valid.
170    fn is_valid_message_version(&self, message_version: u32) -> bool {
171        self.router().is_valid_message_version(message_version)
172    }
173
174    /// Handles a `BlockRequest` message.
175    fn block_request(&self, peer_ip: SocketAddr, _message: BlockRequest) -> bool {
176        debug!("Disconnecting '{peer_ip}' for the following reason - {}", DisconnectReason::ProtocolViolation);
177        false
178    }
179
180    /// Handles a `BlockResponse` message.
181    fn block_response(
182        &self,
183        peer_ip: SocketAddr,
184        _blocks: Vec<Block<N>>,
185        _latest_consensus_version: Option<ConsensusVersion>,
186    ) -> bool {
187        debug!("Disconnecting '{peer_ip}' for the following reason - {}", DisconnectReason::ProtocolViolation);
188        false
189    }
190
191    /// Processes the block locators and sends back a `Pong` message.
192    fn ping(&self, peer_ip: SocketAddr, message: Ping<N>) -> bool {
193        // If block locators were provided, then update the peer in the sync pool.
194        if let Some(block_locators) = message.block_locators {
195            // Check the block locators are valid, and update the peer in the sync pool.
196            if let Err(error) = self.sync.update_peer_locators(peer_ip, &block_locators) {
197                warn!("Peer '{peer_ip}' sent invalid block locators: {error}");
198                return false;
199            }
200        }
201
202        // Send a `Pong` message to the peer.
203        self.router().send(peer_ip, Message::Pong(Pong { is_fork: Some(false) }));
204        true
205    }
206
207    /// Sleeps for a period and then sends a `Ping` message to the peer.
208    fn pong(&self, peer_ip: SocketAddr, _message: Pong) -> bool {
209        self.ping.on_pong_received(peer_ip);
210        true
211    }
212
213    /// Disconnects on receipt of a `PuzzleRequest` message.
214    fn puzzle_request(&self, peer_ip: SocketAddr) -> bool {
215        debug!("Disconnecting '{peer_ip}' for the following reason - {}", DisconnectReason::ProtocolViolation);
216        false
217    }
218
219    /// Saves the latest epoch hash and latest block header in the node.
220    fn puzzle_response(&self, peer_ip: SocketAddr, epoch_hash: N::BlockHash, header: Header<N>) -> bool {
221        // Retrieve the block height.
222        let block_height = header.height();
223
224        info!(
225            "Puzzle (Block {block_height}, Coinbase Target {}, Proof Target {})",
226            header.coinbase_target(),
227            header.proof_target()
228        );
229
230        // Save the latest epoch hash in the node.
231        self.latest_epoch_hash.write().replace(epoch_hash);
232        // Save the latest block header in the node.
233        self.latest_block_header.write().replace(header);
234
235        trace!("Received 'PuzzleResponse' from '{peer_ip}' (Block {block_height})");
236        true
237    }
238
239    /// Propagates the unconfirmed solution to all connected validators.
240    async fn unconfirmed_solution(
241        &self,
242        peer_ip: SocketAddr,
243        serialized: UnconfirmedSolution<N>,
244        solution: Solution<N>,
245    ) -> bool {
246        // Retrieve the latest epoch hash.
247        let epoch_hash = *self.latest_epoch_hash.read();
248        // Retrieve the latest proof target.
249        let proof_target = self.latest_block_header.read().as_ref().map(|header| header.proof_target());
250
251        if let (Some(epoch_hash), Some(proof_target)) = (epoch_hash, proof_target) {
252            // Ensure that the solution is valid for the given epoch.
253            let puzzle = self.puzzle.clone();
254            let is_valid =
255                tokio::task::spawn_blocking(move || puzzle.check_solution(&solution, epoch_hash, proof_target)).await;
256
257            match is_valid {
258                // If the solution is valid, propagate the `UnconfirmedSolution`.
259                Ok(Ok(())) => {
260                    let message = Message::UnconfirmedSolution(serialized);
261                    // Propagate the "UnconfirmedSolution".
262                    self.propagate(message, &[peer_ip]);
263                }
264                Ok(Err(_)) => {
265                    trace!("Invalid solution '{}' for the proof target.", solution.id())
266                }
267                // If error occurs after the first 10 blocks of the epoch, log it as a warning, otherwise ignore.
268                Err(error) => {
269                    if let Some(height) = self.latest_block_header.read().as_ref().map(|header| header.height())
270                        && height % N::NUM_BLOCKS_PER_EPOCH > 10
271                    {
272                        warn!("Failed to verify the solution - {error}")
273                    }
274                }
275            }
276        }
277        true
278    }
279
280    /// Handles an `UnconfirmedTransaction` message.
281    async fn unconfirmed_transaction(
282        &self,
283        _peer_ip: SocketAddr,
284        _serialized: UnconfirmedTransaction<N>,
285        _transaction: Transaction<N>,
286    ) -> bool {
287        true
288    }
289}