-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathrouter.rs
341 lines (305 loc) · 13.1 KB
/
router.rs
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2024 Aleo Network Foundation
// 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.
use super::*;
use snarkos_node_router::{
Routing,
messages::{
BlockRequest,
BlockResponse,
DataBlocks,
DisconnectReason,
MessageCodec,
PeerRequest,
Ping,
Pong,
PuzzleResponse,
UnconfirmedTransaction,
},
};
use snarkos_node_sync::communication_service::CommunicationService;
use snarkos_node_tcp::{Connection, ConnectionSide, Tcp};
use snarkvm::{
ledger::narwhal::Data,
prelude::{Network, block::Transaction},
};
use std::{io, net::SocketAddr, time::Duration};
impl<N: Network, C: ConsensusStorage<N>> P2P for Client<N, C> {
/// Returns a reference to the TCP instance.
fn tcp(&self) -> &Tcp {
self.router.tcp()
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> Handshake for Client<N, C> {
/// Performs the handshake protocol.
async fn perform_handshake(&self, mut connection: Connection) -> io::Result<Connection> {
// Perform the handshake.
let peer_addr = connection.addr();
let conn_side = connection.side();
let stream = self.borrow_stream(&mut connection);
let genesis_header = *self.genesis.header();
let restrictions_id = self.ledger.vm().restrictions().restrictions_id();
self.router.handshake(peer_addr, stream, conn_side, genesis_header, restrictions_id).await?;
Ok(connection)
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> OnConnect for Client<N, C>
where
Self: Outbound<N>,
{
async fn on_connect(&self, peer_addr: SocketAddr) {
// Resolve the peer address to the listener address.
let Some(peer_ip) = self.router.resolve_to_listener(&peer_addr) else { return };
// Promote the peer's status from "connecting" to "connected".
self.router().insert_connected_peer(peer_ip);
// If it's a bootstrap peer, first request its peers.
if self.router.bootstrap_peers().contains(&peer_ip) {
Outbound::send(self, peer_ip, Message::PeerRequest(PeerRequest));
}
// Retrieve the block locators.
let block_locators = match self.sync.get_block_locators() {
Ok(block_locators) => Some(block_locators),
Err(e) => {
error!("Failed to get block locators: {e}");
return;
}
};
// Send the first `Ping` message to the peer.
self.send_ping(peer_ip, block_locators);
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> Disconnect for Client<N, C> {
/// Any extra operations to be performed during a disconnect.
async fn handle_disconnect(&self, peer_addr: SocketAddr) {
if let Some(peer_ip) = self.router.resolve_to_listener(&peer_addr) {
self.sync.remove_peer(&peer_ip);
self.router.remove_connected_peer(peer_ip);
}
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> Writing for Client<N, C> {
type Codec = MessageCodec<N>;
type Message = Message<N>;
/// Creates an [`Encoder`] used to write the outbound messages to the target stream.
/// The `side` parameter indicates the connection side **from the node's perspective**.
fn codec(&self, _addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
Default::default()
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> Reading for Client<N, C> {
type Codec = MessageCodec<N>;
type Message = Message<N>;
/// Creates a [`Decoder`] used to interpret messages from the network.
/// The `side` param indicates the connection side **from the node's perspective**.
fn codec(&self, _peer_addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
Default::default()
}
/// Processes a message received from the network.
async fn process_message(&self, peer_addr: SocketAddr, message: Self::Message) -> io::Result<()> {
let clone = self.clone();
if matches!(message, Message::BlockRequest(_) | Message::BlockResponse(_)) {
// Handle BlockRequest and BlockResponse messages in a separate task to not block the
// inbound queue.
tokio::spawn(async move {
clone.process_message_inner(peer_addr, message).await;
});
} else {
self.process_message_inner(peer_addr, message).await;
}
Ok(())
}
}
impl<N: Network, C: ConsensusStorage<N>> Client<N, C> {
async fn process_message_inner(
&self,
peer_addr: SocketAddr,
message: <Client<N, C> as snarkos_node_tcp::protocols::Reading>::Message,
) {
// Process the message. Disconnect if the peer violated the protocol.
if let Err(error) = self.inbound(peer_addr, message).await {
if let Some(peer_ip) = self.router().resolve_to_listener(&peer_addr) {
warn!("Disconnecting from '{peer_ip}' - {error}");
Outbound::send(self, peer_ip, Message::Disconnect(DisconnectReason::ProtocolViolation.into()));
// Disconnect from this peer.
self.router().disconnect(peer_ip);
}
}
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> CommunicationService for Client<N, C> {
/// The message type.
type Message = Message<N>;
/// Prepares a block request to be sent.
fn prepare_block_request(start_height: u32, end_height: u32) -> Self::Message {
debug_assert!(start_height < end_height, "Invalid block request format");
Message::BlockRequest(BlockRequest { start_height, end_height })
}
/// Sends the given message to specified peer.
///
/// This function returns as soon as the message is queued to be sent,
/// without waiting for the actual delivery; instead, the caller is provided with a [`oneshot::Receiver`]
/// which can be used to determine when and whether the message has been delivered.
async fn send(
&self,
peer_ip: SocketAddr,
message: Self::Message,
) -> Option<tokio::sync::oneshot::Receiver<io::Result<()>>> {
Outbound::send(self, peer_ip, message)
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> Routing<N> for Client<N, C> {}
impl<N: Network, C: ConsensusStorage<N>> Heartbeat<N> for Client<N, C> {}
impl<N: Network, C: ConsensusStorage<N>> Outbound<N> for Client<N, C> {
/// Returns a reference to the router.
fn router(&self) -> &Router<N> {
&self.router
}
/// Returns `true` if the node is synced up to the latest block (within the given tolerance).
fn is_block_synced(&self) -> bool {
self.sync.is_block_synced()
}
/// Returns the number of blocks this node is behind the greatest peer height.
fn num_blocks_behind(&self) -> u32 {
self.sync.num_blocks_behind()
}
}
#[async_trait]
impl<N: Network, C: ConsensusStorage<N>> Inbound<N> for Client<N, C> {
/// Handles a `BlockRequest` message.
fn block_request(&self, peer_ip: SocketAddr, message: BlockRequest) -> bool {
let BlockRequest { start_height, end_height } = &message;
// Retrieve the blocks within the requested range.
let blocks = match self.ledger.get_blocks(*start_height..*end_height) {
Ok(blocks) => Data::Object(DataBlocks(blocks)),
Err(error) => {
error!("Failed to retrieve blocks {start_height} to {end_height} from the ledger - {error}");
return false;
}
};
// Send the `BlockResponse` message to the peer.
Outbound::send(self, peer_ip, Message::BlockResponse(BlockResponse { request: message, blocks }));
true
}
/// Handles a `BlockResponse` message.
fn block_response(&self, peer_ip: SocketAddr, blocks: Vec<Block<N>>) -> bool {
// Tries to advance with blocks from the sync module.
match self.sync.advance_with_sync_blocks(peer_ip, blocks) {
Ok(()) => true,
Err(error) => {
warn!("{error}");
false
}
}
}
/// Processes the block locators and sends back a `Pong` message.
fn ping(&self, peer_ip: SocketAddr, message: Ping<N>) -> bool {
// Check if the sync module is in router mode.
if self.sync.mode().is_router() {
// If block locators were provided, then update the peer in the sync pool.
if let Some(block_locators) = message.block_locators {
// Check the block locators are valid, and update the peer in the sync pool.
if let Err(error) = self.sync.update_peer_locators(peer_ip, block_locators) {
warn!("Peer '{peer_ip}' sent invalid block locators: {error}");
return false;
}
}
}
// Send a `Pong` message to the peer.
Outbound::send(self, peer_ip, Message::Pong(Pong { is_fork: Some(false) }));
true
}
/// Sleeps for a period and then sends a `Ping` message to the peer.
fn pong(&self, peer_ip: SocketAddr, _message: Pong) -> bool {
// Spawn an asynchronous task for the `Ping` request.
let self_ = self.clone();
tokio::spawn(async move {
// Sleep for the preset time before sending a `Ping` request.
tokio::time::sleep(Duration::from_secs(Self::PING_SLEEP_IN_SECS)).await;
// Check that the peer is still connected.
if self_.router().is_connected(&peer_ip) {
// Retrieve the block locators.
match self_.sync.get_block_locators() {
// Send a `Ping` message to the peer.
Ok(block_locators) => self_.send_ping(peer_ip, Some(block_locators)),
Err(e) => error!("Failed to get block locators - {e}"),
}
}
});
true
}
/// Retrieves the latest epoch hash and latest block header, and returns the puzzle response to the peer.
fn puzzle_request(&self, peer_ip: SocketAddr) -> bool {
// Retrieve the latest epoch hash.
let epoch_hash = match self.ledger.latest_epoch_hash() {
Ok(epoch_hash) => epoch_hash,
Err(error) => {
error!("Failed to prepare a puzzle request for '{peer_ip}': {error}");
return false;
}
};
// Retrieve the latest block header.
let block_header = Data::Object(self.ledger.latest_header());
// Send the `PuzzleResponse` message to the peer.
Outbound::send(self, peer_ip, Message::PuzzleResponse(PuzzleResponse { epoch_hash, block_header }));
true
}
/// Saves the latest epoch hash and latest block header in the node.
fn puzzle_response(&self, peer_ip: SocketAddr, _epoch_hash: N::BlockHash, _header: Header<N>) -> bool {
debug!("Disconnecting '{peer_ip}' for the following reason - {:?}", DisconnectReason::ProtocolViolation);
false
}
/// Propagates the unconfirmed solution to all connected validators.
async fn unconfirmed_solution(
&self,
peer_ip: SocketAddr,
serialized: UnconfirmedSolution<N>,
solution: Solution<N>,
) -> bool {
// Try to add the solution to the verification queue, without changing LRU status of known solutions.
let mut solution_queue = self.solution_queue.lock();
if !solution_queue.contains(&solution.id()) {
solution_queue.put(solution.id(), (peer_ip, serialized, solution));
}
true // Maintain the connection
}
/// Handles an `UnconfirmedTransaction` message.
async fn unconfirmed_transaction(
&self,
peer_ip: SocketAddr,
serialized: UnconfirmedTransaction<N>,
transaction: Transaction<N>,
) -> bool {
// Try to add the transaction to a verification queue, without changing LRU status of known transactions.
match &transaction {
Transaction::<N>::Fee(..) => (), // Fee Transactions are not valid.
Transaction::<N>::Deploy(..) => {
let mut deploy_queue = self.deploy_queue.lock();
if !deploy_queue.contains(&transaction.id()) {
deploy_queue.put(transaction.id(), (peer_ip, serialized, transaction));
}
}
Transaction::<N>::Execute(..) => {
let mut execute_queue = self.execute_queue.lock();
if !execute_queue.contains(&transaction.id()) {
execute_queue.put(transaction.id(), (peer_ip, serialized, transaction));
}
}
}
true // Maintain the connection
}
}