snarkos_node/client/
router.rs

1// Copyright 2024 Aleo Network Foundation
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, time::Duration};
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>
66where
67    Self: Outbound<N>,
68{
69    async fn on_connect(&self, peer_addr: SocketAddr) {
70        // Resolve the peer address to the listener address.
71        let Some(peer_ip) = self.router.resolve_to_listener(&peer_addr) else { return };
72        // Promote the peer's status from "connecting" to "connected".
73        self.router().insert_connected_peer(peer_ip);
74        // If it's a bootstrap peer, first request its peers.
75        if self.router.bootstrap_peers().contains(&peer_ip) {
76            Outbound::send(self, peer_ip, Message::PeerRequest(PeerRequest));
77        }
78        // Retrieve the block locators.
79        let block_locators = match self.sync.get_block_locators() {
80            Ok(block_locators) => Some(block_locators),
81            Err(e) => {
82                error!("Failed to get block locators: {e}");
83                return;
84            }
85        };
86        // Send the first `Ping` message to the peer.
87        self.send_ping(peer_ip, block_locators);
88    }
89}
90
91#[async_trait]
92impl<N: Network, C: ConsensusStorage<N>> Disconnect for Client<N, C> {
93    /// Any extra operations to be performed during a disconnect.
94    async fn handle_disconnect(&self, peer_addr: SocketAddr) {
95        if let Some(peer_ip) = self.router.resolve_to_listener(&peer_addr) {
96            self.sync.remove_peer(&peer_ip);
97            self.router.remove_connected_peer(peer_ip);
98        }
99    }
100}
101
102#[async_trait]
103impl<N: Network, C: ConsensusStorage<N>> Writing for Client<N, C> {
104    type Codec = MessageCodec<N>;
105    type Message = Message<N>;
106
107    /// Creates an [`Encoder`] used to write the outbound messages to the target stream.
108    /// The `side` parameter indicates the connection side **from the node's perspective**.
109    fn codec(&self, _addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
110        Default::default()
111    }
112}
113
114#[async_trait]
115impl<N: Network, C: ConsensusStorage<N>> Reading for Client<N, C> {
116    type Codec = MessageCodec<N>;
117    type Message = Message<N>;
118
119    /// Creates a [`Decoder`] used to interpret messages from the network.
120    /// The `side` param indicates the connection side **from the node's perspective**.
121    fn codec(&self, _peer_addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
122        Default::default()
123    }
124
125    /// Processes a message received from the network.
126    async fn process_message(&self, peer_addr: SocketAddr, message: Self::Message) -> io::Result<()> {
127        let clone = self.clone();
128        if matches!(message, Message::BlockRequest(_) | Message::BlockResponse(_)) {
129            // Handle BlockRequest and BlockResponse messages in a separate task to not block the
130            // inbound queue.
131            tokio::spawn(async move {
132                clone.process_message_inner(peer_addr, message).await;
133            });
134        } else {
135            self.process_message_inner(peer_addr, message).await;
136        }
137        Ok(())
138    }
139}
140
141impl<N: Network, C: ConsensusStorage<N>> Client<N, C> {
142    async fn process_message_inner(
143        &self,
144        peer_addr: SocketAddr,
145        message: <Client<N, C> as snarkos_node_tcp::protocols::Reading>::Message,
146    ) {
147        // Process the message. Disconnect if the peer violated the protocol.
148        if let Err(error) = self.inbound(peer_addr, message).await {
149            if let Some(peer_ip) = self.router().resolve_to_listener(&peer_addr) {
150                warn!("Disconnecting from '{peer_ip}' - {error}");
151                Outbound::send(self, peer_ip, Message::Disconnect(DisconnectReason::ProtocolViolation.into()));
152                // Disconnect from this peer.
153                self.router().disconnect(peer_ip);
154            }
155        }
156    }
157}
158
159#[async_trait]
160impl<N: Network, C: ConsensusStorage<N>> CommunicationService for Client<N, C> {
161    /// The message type.
162    type Message = Message<N>;
163
164    /// Prepares a block request to be sent.
165    fn prepare_block_request(start_height: u32, end_height: u32) -> Self::Message {
166        debug_assert!(start_height < end_height, "Invalid block request format");
167        Message::BlockRequest(BlockRequest { start_height, end_height })
168    }
169
170    /// Sends the given message to specified peer.
171    ///
172    /// This function returns as soon as the message is queued to be sent,
173    /// without waiting for the actual delivery; instead, the caller is provided with a [`oneshot::Receiver`]
174    /// which can be used to determine when and whether the message has been delivered.
175    async fn send(
176        &self,
177        peer_ip: SocketAddr,
178        message: Self::Message,
179    ) -> Option<tokio::sync::oneshot::Receiver<io::Result<()>>> {
180        Outbound::send(self, peer_ip, message)
181    }
182}
183
184#[async_trait]
185impl<N: Network, C: ConsensusStorage<N>> Routing<N> for Client<N, C> {}
186
187impl<N: Network, C: ConsensusStorage<N>> Heartbeat<N> for Client<N, C> {}
188
189impl<N: Network, C: ConsensusStorage<N>> Outbound<N> for Client<N, C> {
190    /// Returns a reference to the router.
191    fn router(&self) -> &Router<N> {
192        &self.router
193    }
194
195    /// Returns `true` if the node is synced up to the latest block (within the given tolerance).
196    fn is_block_synced(&self) -> bool {
197        self.sync.is_block_synced()
198    }
199
200    /// Returns the number of blocks this node is behind the greatest peer height.
201    fn num_blocks_behind(&self) -> u32 {
202        self.sync.num_blocks_behind()
203    }
204}
205
206#[async_trait]
207impl<N: Network, C: ConsensusStorage<N>> Inbound<N> for Client<N, C> {
208    /// Handles a `BlockRequest` message.
209    fn block_request(&self, peer_ip: SocketAddr, message: BlockRequest) -> bool {
210        let BlockRequest { start_height, end_height } = &message;
211
212        // Retrieve the blocks within the requested range.
213        let blocks = match self.ledger.get_blocks(*start_height..*end_height) {
214            Ok(blocks) => Data::Object(DataBlocks(blocks)),
215            Err(error) => {
216                error!("Failed to retrieve blocks {start_height} to {end_height} from the ledger - {error}");
217                return false;
218            }
219        };
220        // Send the `BlockResponse` message to the peer.
221        Outbound::send(self, peer_ip, Message::BlockResponse(BlockResponse { request: message, blocks }));
222        true
223    }
224
225    /// Handles a `BlockResponse` message.
226    fn block_response(&self, peer_ip: SocketAddr, blocks: Vec<Block<N>>) -> bool {
227        // Tries to advance with blocks from the sync module.
228        match self.sync.advance_with_sync_blocks(peer_ip, blocks) {
229            Ok(()) => true,
230            Err(error) => {
231                warn!("{error}");
232                false
233            }
234        }
235    }
236
237    /// Processes the block locators and sends back a `Pong` message.
238    fn ping(&self, peer_ip: SocketAddr, message: Ping<N>) -> bool {
239        // Check if the sync module is in router mode.
240        if self.sync.mode().is_router() {
241            // If block locators were provided, then update the peer in the sync pool.
242            if let Some(block_locators) = message.block_locators {
243                // Check the block locators are valid, and update the peer in the sync pool.
244                if let Err(error) = self.sync.update_peer_locators(peer_ip, block_locators) {
245                    warn!("Peer '{peer_ip}' sent invalid block locators: {error}");
246                    return false;
247                }
248            }
249        }
250
251        // Send a `Pong` message to the peer.
252        Outbound::send(self, peer_ip, Message::Pong(Pong { is_fork: Some(false) }));
253        true
254    }
255
256    /// Sleeps for a period and then sends a `Ping` message to the peer.
257    fn pong(&self, peer_ip: SocketAddr, _message: Pong) -> bool {
258        // Spawn an asynchronous task for the `Ping` request.
259        let self_ = self.clone();
260        tokio::spawn(async move {
261            // Sleep for the preset time before sending a `Ping` request.
262            tokio::time::sleep(Duration::from_secs(Self::PING_SLEEP_IN_SECS)).await;
263            // Check that the peer is still connected.
264            if self_.router().is_connected(&peer_ip) {
265                // Retrieve the block locators.
266                match self_.sync.get_block_locators() {
267                    // Send a `Ping` message to the peer.
268                    Ok(block_locators) => self_.send_ping(peer_ip, Some(block_locators)),
269                    Err(e) => error!("Failed to get block locators - {e}"),
270                }
271            }
272        });
273        true
274    }
275
276    /// Retrieves the latest epoch hash and latest block header, and returns the puzzle response to the peer.
277    fn puzzle_request(&self, peer_ip: SocketAddr) -> bool {
278        // Retrieve the latest epoch hash.
279        let epoch_hash = match self.ledger.latest_epoch_hash() {
280            Ok(epoch_hash) => epoch_hash,
281            Err(error) => {
282                error!("Failed to prepare a puzzle request for '{peer_ip}': {error}");
283                return false;
284            }
285        };
286        // Retrieve the latest block header.
287        let block_header = Data::Object(self.ledger.latest_header());
288        // Send the `PuzzleResponse` message to the peer.
289        Outbound::send(self, peer_ip, Message::PuzzleResponse(PuzzleResponse { epoch_hash, block_header }));
290        true
291    }
292
293    /// Saves the latest epoch hash and latest block header in the node.
294    fn puzzle_response(&self, peer_ip: SocketAddr, _epoch_hash: N::BlockHash, _header: Header<N>) -> bool {
295        debug!("Disconnecting '{peer_ip}' for the following reason - {:?}", DisconnectReason::ProtocolViolation);
296        false
297    }
298
299    /// Propagates the unconfirmed solution to all connected validators.
300    async fn unconfirmed_solution(
301        &self,
302        peer_ip: SocketAddr,
303        serialized: UnconfirmedSolution<N>,
304        solution: Solution<N>,
305    ) -> bool {
306        // Try to add the solution to the verification queue, without changing LRU status of known solutions.
307        let mut solution_queue = self.solution_queue.lock();
308        if !solution_queue.contains(&solution.id()) {
309            solution_queue.put(solution.id(), (peer_ip, serialized, solution));
310        }
311
312        true // Maintain the connection
313    }
314
315    /// Handles an `UnconfirmedTransaction` message.
316    async fn unconfirmed_transaction(
317        &self,
318        peer_ip: SocketAddr,
319        serialized: UnconfirmedTransaction<N>,
320        transaction: Transaction<N>,
321    ) -> bool {
322        // Try to add the transaction to a verification queue, without changing LRU status of known transactions.
323        match &transaction {
324            Transaction::<N>::Fee(..) => (), // Fee Transactions are not valid.
325            Transaction::<N>::Deploy(..) => {
326                let mut deploy_queue = self.deploy_queue.lock();
327                if !deploy_queue.contains(&transaction.id()) {
328                    deploy_queue.put(transaction.id(), (peer_ip, serialized, transaction));
329                }
330            }
331            Transaction::<N>::Execute(..) => {
332                let mut execute_queue = self.execute_queue.lock();
333                if !execute_queue.contains(&transaction.id()) {
334                    execute_queue.put(transaction.id(), (peer_ip, serialized, transaction));
335                }
336            }
337        }
338
339        true // Maintain the connection
340    }
341}