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