snarkos_node/client/
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::*;
17use snarkos_node_router::{
18    Routing,
19    messages::{
20        BlockRequest,
21        BlockResponse,
22        DataBlocks,
23        DisconnectReason,
24        MessageCodec,
25        PeerRequest,
26        Ping,
27        Pong,
28        PuzzleResponse,
29        UnconfirmedTransaction,
30    },
31};
32use snarkos_node_sync::communication_service::CommunicationService;
33use snarkos_node_tcp::{Connection, ConnectionSide, Tcp};
34use snarkvm::{
35    ledger::narwhal::Data,
36    prelude::{Network, block::Transaction},
37};
38
39use std::{io, net::SocketAddr};
40
41impl<N: Network, C: ConsensusStorage<N>> P2P for Client<N, C> {
42    /// Returns a reference to the TCP instance.
43    fn tcp(&self) -> &Tcp {
44        self.router.tcp()
45    }
46}
47
48#[async_trait]
49impl<N: Network, C: ConsensusStorage<N>> Handshake for Client<N, C> {
50    /// Performs the handshake protocol.
51    async fn perform_handshake(&self, mut connection: Connection) -> io::Result<Connection> {
52        // Perform the handshake.
53        let peer_addr = connection.addr();
54        let conn_side = connection.side();
55        let stream = self.borrow_stream(&mut connection);
56        let genesis_header = *self.genesis.header();
57        let restrictions_id = self.ledger.vm().restrictions().restrictions_id();
58        self.router.handshake(peer_addr, stream, conn_side, genesis_header, restrictions_id).await?;
59
60        Ok(connection)
61    }
62}
63
64#[async_trait]
65impl<N: Network, C: ConsensusStorage<N>> OnConnect for Client<N, C> {
66    async fn on_connect(&self, peer_addr: SocketAddr) {
67        // Resolve the peer address to the listener address.
68        let Some(peer_ip) = self.router.resolve_to_listener(&peer_addr) else { return };
69        // Promote the peer's status from "connecting" to "connected".
70        self.router().insert_connected_peer(peer_ip);
71        // If it's a bootstrap peer, first request its peers.
72        if self.router.bootstrap_peers().contains(&peer_ip) {
73            self.router().send(peer_ip, Message::PeerRequest(PeerRequest));
74        }
75        // Send the first `Ping` message to the peer.
76        self.ping.on_peer_connected(peer_ip);
77    }
78}
79
80#[async_trait]
81impl<N: Network, C: ConsensusStorage<N>> Disconnect for Client<N, C> {
82    /// Any extra operations to be performed during a disconnect.
83    async fn handle_disconnect(&self, peer_addr: SocketAddr) {
84        if let Some(peer_ip) = self.router.resolve_to_listener(&peer_addr) {
85            self.sync.remove_peer(&peer_ip);
86            self.router.remove_connected_peer(peer_ip);
87        }
88    }
89}
90
91#[async_trait]
92impl<N: Network, C: ConsensusStorage<N>> Reading for Client<N, C> {
93    type Codec = MessageCodec<N>;
94    type Message = Message<N>;
95
96    /// Creates a [`Decoder`] used to interpret messages from the network.
97    /// The `side` param indicates the connection side **from the node's perspective**.
98    fn codec(&self, _peer_addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
99        Default::default()
100    }
101
102    /// Processes a message received from the network.
103    async fn process_message(&self, peer_addr: SocketAddr, message: Self::Message) -> io::Result<()> {
104        let clone = self.clone();
105        if matches!(message, Message::BlockRequest(_) | Message::BlockResponse(_)) {
106            // Handle BlockRequest and BlockResponse messages in a separate task to not block the
107            // inbound queue.
108            tokio::spawn(async move {
109                clone.process_message_inner(peer_addr, message).await;
110            });
111        } else {
112            self.process_message_inner(peer_addr, message).await;
113        }
114        Ok(())
115    }
116}
117
118impl<N: Network, C: ConsensusStorage<N>> Client<N, C> {
119    async fn process_message_inner(
120        &self,
121        peer_addr: SocketAddr,
122        message: <Client<N, C> as snarkos_node_tcp::protocols::Reading>::Message,
123    ) {
124        // Process the message. Disconnect if the peer violated the protocol.
125        if let Err(error) = self.inbound(peer_addr, message).await {
126            warn!("Failed to process inbound message from '{peer_addr}' - {error}");
127            if let Some(peer_ip) = self.router().resolve_to_listener(&peer_addr) {
128                warn!("Disconnecting from '{peer_ip}' for protocol violation");
129                self.router().send(peer_ip, Message::Disconnect(DisconnectReason::ProtocolViolation.into()));
130                // Disconnect from this peer.
131                self.router().disconnect(peer_ip);
132            }
133        }
134    }
135}
136
137#[async_trait]
138impl<N: Network, C: ConsensusStorage<N>> CommunicationService for Client<N, C> {
139    /// The message type.
140    type Message = Message<N>;
141
142    /// Prepares a block request to be sent.
143    fn prepare_block_request(start_height: u32, end_height: u32) -> Self::Message {
144        debug_assert!(start_height < end_height, "Invalid block request format");
145        Message::BlockRequest(BlockRequest { start_height, end_height })
146    }
147
148    /// Sends the given message to specified peer.
149    ///
150    /// This function returns as soon as the message is queued to be sent,
151    /// without waiting for the actual delivery; instead, the caller is provided with a [`oneshot::Receiver`]
152    /// which can be used to determine when and whether the message has been delivered.
153    async fn send(
154        &self,
155        peer_ip: SocketAddr,
156        message: Self::Message,
157    ) -> Option<tokio::sync::oneshot::Receiver<io::Result<()>>> {
158        self.router().send(peer_ip, message)
159    }
160
161    fn ban_peer(&self, peer_ip: SocketAddr) {
162        debug!("Banning peer {peer_ip} for timing out on block requests");
163
164        let tcp = self.router.tcp().clone();
165        tcp.banned_peers().update_ip_ban(peer_ip.ip());
166
167        tokio::spawn(async move {
168            tcp.disconnect(peer_ip).await;
169        });
170    }
171}
172
173#[async_trait]
174impl<N: Network, C: ConsensusStorage<N>> Routing<N> for Client<N, C> {}
175
176impl<N: Network, C: ConsensusStorage<N>> Heartbeat<N> for Client<N, C> {}
177
178impl<N: Network, C: ConsensusStorage<N>> Outbound<N> for Client<N, C> {
179    /// Returns a reference to the router.
180    fn router(&self) -> &Router<N> {
181        &self.router
182    }
183
184    /// Returns `true` if the node is synced up to the latest block (within the given tolerance).
185    fn is_block_synced(&self) -> bool {
186        self.sync.is_block_synced()
187    }
188
189    /// Returns the number of blocks this node is behind the greatest peer height,
190    /// or `None` if not connected to peers yet.
191    fn num_blocks_behind(&self) -> Option<u32> {
192        self.sync.num_blocks_behind()
193    }
194}
195
196#[async_trait]
197impl<N: Network, C: ConsensusStorage<N>> Inbound<N> for Client<N, C> {
198    /// Returns `true` if the message version is valid.
199    fn is_valid_message_version(&self, message_version: u32) -> bool {
200        self.router().is_valid_message_version(message_version)
201    }
202
203    /// Handles a `BlockRequest` message.
204    fn block_request(&self, peer_ip: SocketAddr, message: BlockRequest) -> bool {
205        let BlockRequest { start_height, end_height } = &message;
206
207        // Retrieve the blocks within the requested range.
208        let blocks = match self.ledger.get_blocks(*start_height..*end_height) {
209            Ok(blocks) => Data::Object(DataBlocks(blocks)),
210            Err(error) => {
211                error!("Failed to retrieve blocks {start_height} to {end_height} from the ledger - {error}");
212                return false;
213            }
214        };
215        // Send the `BlockResponse` message to the peer.
216        self.router().send(peer_ip, Message::BlockResponse(BlockResponse { request: message, blocks }));
217        true
218    }
219
220    /// Handles a `BlockResponse` message.
221    fn block_response(&self, peer_ip: SocketAddr, blocks: Vec<Block<N>>) -> bool {
222        // We do not need to explicitly sync here because insert_block_response, will wake up the sync task.
223        if let Err(err) = self.sync.insert_block_responses(peer_ip, blocks) {
224            warn!("Failed to insert block response: {err}");
225            false
226        } else {
227            true
228        }
229    }
230
231    /// Processes the block locators and sends back a `Pong` message.
232    fn ping(&self, peer_ip: SocketAddr, message: Ping<N>) -> bool {
233        // If block locators were provided, then update the peer in the sync pool.
234        if let Some(block_locators) = message.block_locators {
235            // Check the block locators are valid, and update the peer in the sync pool.
236            if let Err(error) = self.sync.update_peer_locators(peer_ip, block_locators) {
237                warn!("Peer '{peer_ip}' sent invalid block locators: {error}");
238                return false;
239            }
240        }
241
242        // Send a `Pong` message to the peer.
243        self.router().send(peer_ip, Message::Pong(Pong { is_fork: Some(false) }));
244        true
245    }
246
247    /// Sleeps for a period and then sends a `Ping` message to the peer.
248    fn pong(&self, peer_ip: SocketAddr, _message: Pong) -> bool {
249        self.ping.on_pong_received(peer_ip);
250        true
251    }
252
253    /// Retrieves the latest epoch hash and latest block header, and returns the puzzle response to the peer.
254    fn puzzle_request(&self, peer_ip: SocketAddr) -> bool {
255        // Retrieve the latest epoch hash.
256        let epoch_hash = match self.ledger.latest_epoch_hash() {
257            Ok(epoch_hash) => epoch_hash,
258            Err(error) => {
259                error!("Failed to prepare a puzzle request for '{peer_ip}': {error}");
260                return false;
261            }
262        };
263        // Retrieve the latest block header.
264        let block_header = Data::Object(self.ledger.latest_header());
265        // Send the `PuzzleResponse` message to the peer.
266        self.router().send(peer_ip, Message::PuzzleResponse(PuzzleResponse { epoch_hash, block_header }));
267        true
268    }
269
270    /// Saves the latest epoch hash and latest block header in the node.
271    fn puzzle_response(&self, peer_ip: SocketAddr, _epoch_hash: N::BlockHash, _header: Header<N>) -> bool {
272        debug!("Disconnecting '{peer_ip}' for the following reason - {:?}", DisconnectReason::ProtocolViolation);
273        false
274    }
275
276    /// Propagates the unconfirmed solution to all connected validators.
277    async fn unconfirmed_solution(
278        &self,
279        peer_ip: SocketAddr,
280        serialized: UnconfirmedSolution<N>,
281        solution: Solution<N>,
282    ) -> bool {
283        // Try to add the solution to the verification queue, without changing LRU status of known solutions.
284        let mut solution_queue = self.solution_queue.lock();
285        if !solution_queue.contains(&solution.id()) {
286            solution_queue.put(solution.id(), (peer_ip, serialized, solution));
287        }
288
289        true // Maintain the connection
290    }
291
292    /// Handles an `UnconfirmedTransaction` message.
293    async fn unconfirmed_transaction(
294        &self,
295        peer_ip: SocketAddr,
296        serialized: UnconfirmedTransaction<N>,
297        transaction: Transaction<N>,
298    ) -> bool {
299        // Try to add the transaction to a verification queue, without changing LRU status of known transactions.
300        match &transaction {
301            Transaction::<N>::Fee(..) => (), // Fee Transactions are not valid.
302            Transaction::<N>::Deploy(..) => {
303                let mut deploy_queue = self.deploy_queue.lock();
304                if !deploy_queue.contains(&transaction.id()) {
305                    deploy_queue.put(transaction.id(), (peer_ip, serialized, transaction));
306                }
307            }
308            Transaction::<N>::Execute(..) => {
309                let mut execute_queue = self.execute_queue.lock();
310                if !execute_queue.contains(&transaction.id()) {
311                    execute_queue.put(transaction.id(), (peer_ip, serialized, transaction));
312                }
313            }
314        }
315
316        true // Maintain the connection
317    }
318}