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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
use std::{sync::Arc, time::Duration};
use futures_util::{stream::FuturesUnordered, StreamExt};
use log::{error, trace};
use rand::{rngs::OsRng, seq::SliceRandom, RngCore};
use karyon_core::{
async_runtime::{
lock::{Mutex, RwLock},
Executor,
},
async_util::timeout,
crypto::KeyPair,
util::decode,
};
use karyon_net::{Conn, Endpoint};
use crate::{
connector::Connector,
listener::Listener,
message::{
get_msg_payload, FindPeerMsg, NetMsg, NetMsgCmd, PeerMsg, PeersMsg, PingMsg, PongMsg,
ShutdownMsg,
},
monitor::{ConnEvent, DiscoveryEvent, Monitor},
routing_table::RoutingTable,
slots::ConnectionSlots,
version::version_match,
Config, Error, PeerID, Result,
};
/// Maximum number of peers that can be returned in a PeersMsg.
pub const MAX_PEERS_IN_PEERSMSG: usize = 10;
pub struct LookupService {
/// Peer's ID
id: PeerID,
/// Routing Table
table: Arc<Mutex<RoutingTable>>,
/// Listener
listener: Arc<Listener>,
/// Connector
connector: Arc<Connector>,
/// Outbound slots.
outbound_slots: Arc<ConnectionSlots>,
/// Resolved listen endpoint
listen_endpoint: Option<RwLock<Endpoint>>,
/// Holds the configuration for the P2P network.
config: Arc<Config>,
/// Responsible for network and system monitoring.
monitor: Arc<Monitor>,
}
impl LookupService {
/// Creates a new lookup service
pub fn new(
key_pair: &KeyPair,
id: &PeerID,
table: Arc<Mutex<RoutingTable>>,
config: Arc<Config>,
monitor: Arc<Monitor>,
ex: Executor,
) -> Self {
let inbound_slots = Arc::new(ConnectionSlots::new(config.lookup_inbound_slots));
let outbound_slots = Arc::new(ConnectionSlots::new(config.lookup_outbound_slots));
let listener = Listener::new(
key_pair,
inbound_slots.clone(),
config.enable_tls,
monitor.clone(),
ex.clone(),
);
let connector = Connector::new(
key_pair,
config.lookup_connect_retries,
outbound_slots.clone(),
config.enable_tls,
monitor.clone(),
ex,
);
let listen_endpoint = config
.listen_endpoint
.as_ref()
.map(|endpoint| RwLock::new(endpoint.clone()));
Self {
id: id.clone(),
table,
listener,
connector,
outbound_slots,
listen_endpoint,
config,
monitor,
}
}
/// Start the lookup service.
pub async fn start(self: &Arc<Self>) -> Result<()> {
self.start_listener().await?;
Ok(())
}
/// Set the resolved listen endpoint.
pub async fn set_listen_endpoint(&self, resolved_endpoint: &Endpoint) {
if let Some(endpoint) = &self.listen_endpoint {
*endpoint.write().await = resolved_endpoint.clone();
}
}
/// Shuts down the lookup service.
pub async fn shutdown(&self) {
self.connector.shutdown().await;
self.listener.shutdown().await;
}
/// Starts iterative lookup and populate the routing table.
///
/// This method begins by generating a random peer ID and connecting to the
/// provided endpoint. It then sends a FindPeer message containing the
/// randomly generated peer ID. Upon receiving peers from the initial lookup,
/// it starts connecting to these received peers and sends them a FindPeer
/// message that contains our own peer ID.
pub async fn start_lookup(&self, endpoint: &Endpoint, peer_id: Option<PeerID>) -> Result<()> {
trace!("Lookup started {endpoint}");
self.monitor
.notify(DiscoveryEvent::LookupStarted(endpoint.clone()))
.await;
let mut random_peers = vec![];
if let Err(err) = self
.random_lookup(endpoint, peer_id, &mut random_peers)
.await
{
self.monitor
.notify(DiscoveryEvent::LookupFailed(endpoint.clone()))
.await;
return Err(err);
};
let mut peer_buffer = vec![];
self.self_lookup(&random_peers, &mut peer_buffer).await;
while peer_buffer.len() < MAX_PEERS_IN_PEERSMSG {
match random_peers.pop() {
Some(p) => peer_buffer.push(p),
None => break,
}
}
let mut table = self.table.lock().await;
for peer in peer_buffer.iter() {
let result = table.add_entry(peer.clone().into());
trace!("Add entry {:?}", result);
}
drop(table);
self.monitor
.notify(DiscoveryEvent::LookupSucceeded(
endpoint.clone(),
peer_buffer.len(),
))
.await;
Ok(())
}
/// Starts a random lookup
///
/// This will perfom lookup on a random generated PeerID
async fn random_lookup(
&self,
endpoint: &Endpoint,
peer_id: Option<PeerID>,
random_peers: &mut Vec<PeerMsg>,
) -> Result<()> {
for _ in 0..2 {
let random_peer_id = PeerID::random();
let peers = self
.connect(endpoint.clone(), peer_id.clone(), &random_peer_id)
.await?;
let table = self.table.lock().await;
for peer in peers {
if random_peers.contains(&peer)
|| peer.peer_id == self.id
|| table.contains_key(&peer.peer_id.0)
{
continue;
}
random_peers.push(peer);
}
}
Ok(())
}
/// Starts a self lookup
async fn self_lookup(&self, random_peers: &Vec<PeerMsg>, peer_buffer: &mut Vec<PeerMsg>) {
let mut tasks = FuturesUnordered::new();
for peer in random_peers.choose_multiple(&mut OsRng, random_peers.len()) {
let endpoint = Endpoint::Tcp(peer.addr.clone(), peer.discovery_port);
tasks.push(self.connect(endpoint, Some(peer.peer_id.clone()), &self.id))
}
while let Some(result) = tasks.next().await {
match result {
Ok(peers) => peer_buffer.extend(peers),
Err(err) => {
error!("Failed to do self lookup: {err}");
}
}
}
}
/// Connects to the given endpoint and initiates a lookup process for the
/// provided peer ID.
async fn connect(
&self,
endpoint: Endpoint,
peer_id: Option<PeerID>,
target_peer_id: &PeerID,
) -> Result<Vec<PeerMsg>> {
let conn = self.connector.connect(&endpoint, &peer_id).await?;
self.monitor
.notify(ConnEvent::Connected(endpoint.clone()))
.await;
let result = self.handle_outbound(conn, target_peer_id).await;
self.monitor.notify(ConnEvent::Disconnected(endpoint)).await;
self.outbound_slots.remove().await;
result
}
/// Handles outbound connection
async fn handle_outbound(
&self,
conn: Conn<NetMsg>,
target_peer_id: &PeerID,
) -> Result<Vec<PeerMsg>> {
trace!("Send Ping msg");
self.send_ping_msg(&conn).await?;
trace!("Send FindPeer msg");
let peers = self.send_findpeer_msg(&conn, target_peer_id).await?;
if peers.0.len() >= MAX_PEERS_IN_PEERSMSG {
return Err(Error::Lookup("Received too many peers in PeersMsg"));
}
trace!("Send Peer msg");
if let Some(endpoint) = &self.listen_endpoint {
self.send_peer_msg(&conn, endpoint.read().await.clone())
.await?;
}
trace!("Send Shutdown msg");
self.send_shutdown_msg(&conn).await?;
Ok(peers.0)
}
/// Start a listener.
async fn start_listener(self: &Arc<Self>) -> Result<()> {
let addr = match &self.listen_endpoint {
Some(a) => a.read().await.addr()?.clone(),
None => return Ok(()),
};
let endpoint = Endpoint::Tcp(addr, self.config.discovery_port);
let selfc = self.clone();
let callback = |conn: Conn<NetMsg>| async move {
let t = Duration::from_secs(selfc.config.lookup_connection_lifespan);
timeout(t, selfc.handle_inbound(conn)).await??;
Ok(())
};
self.listener.start(endpoint.clone(), callback).await?;
Ok(())
}
/// Handles inbound connection
async fn handle_inbound(self: &Arc<Self>, conn: Conn<NetMsg>) -> Result<()> {
loop {
let msg: NetMsg = conn.recv().await?;
trace!("Receive msg {:?}", msg.header.command);
if let NetMsgCmd::Shutdown = msg.header.command {
return Ok(());
}
match &msg.header.command {
NetMsgCmd::Ping => {
let (ping_msg, _) = decode::<PingMsg>(&msg.payload)?;
if !version_match(&self.config.version.req, &ping_msg.version) {
return Err(Error::IncompatibleVersion("system: {}".into()));
}
self.send_pong_msg(ping_msg.nonce, &conn).await?;
}
NetMsgCmd::FindPeer => {
let (findpeer_msg, _) = decode::<FindPeerMsg>(&msg.payload)?;
let peer_id = findpeer_msg.0;
self.send_peers_msg(&peer_id, &conn).await?;
}
NetMsgCmd::Peer => {
let (peer, _) = decode::<PeerMsg>(&msg.payload)?;
let result = self.table.lock().await.add_entry(peer.clone().into());
trace!("Add entry result: {:?}", result);
}
c => return Err(Error::InvalidMsg(format!("Unexpected msg: {:?}", c))),
}
}
}
/// Sends a Ping msg and wait to receive the Pong message.
async fn send_ping_msg(&self, conn: &Conn<NetMsg>) -> Result<()> {
trace!("Send Pong msg");
let mut nonce: [u8; 32] = [0; 32];
RngCore::fill_bytes(&mut OsRng, &mut nonce);
let ping_msg = PingMsg {
version: self.config.version.v.clone(),
nonce,
};
conn.send(NetMsg::new(NetMsgCmd::Ping, &ping_msg)?).await?;
let t = Duration::from_secs(self.config.lookup_response_timeout);
let recv_msg: NetMsg = timeout(t, conn.recv()).await??;
let payload = get_msg_payload!(Pong, recv_msg);
let (pong_msg, _) = decode::<PongMsg>(&payload)?;
if ping_msg.nonce != pong_msg.0 {
return Err(Error::InvalidPongMsg);
}
Ok(())
}
/// Sends a Pong msg
async fn send_pong_msg(&self, nonce: [u8; 32], conn: &Conn<NetMsg>) -> Result<()> {
trace!("Send Pong msg");
conn.send(NetMsg::new(NetMsgCmd::Pong, &PongMsg(nonce))?)
.await?;
Ok(())
}
/// Sends a FindPeer msg and wait to receivet the Peers msg.
async fn send_findpeer_msg(&self, conn: &Conn<NetMsg>, peer_id: &PeerID) -> Result<PeersMsg> {
trace!("Send FindPeer msg");
conn.send(NetMsg::new(
NetMsgCmd::FindPeer,
&FindPeerMsg(peer_id.clone()),
)?)
.await?;
let t = Duration::from_secs(self.config.lookup_response_timeout);
let recv_msg: NetMsg = timeout(t, conn.recv()).await??;
let payload = get_msg_payload!(Peers, recv_msg);
let (peers, _) = decode(&payload)?;
Ok(peers)
}
/// Sends a Peers msg.
async fn send_peers_msg(&self, peer_id: &PeerID, conn: &Conn<NetMsg>) -> Result<()> {
trace!("Send Peers msg");
let table = self.table.lock().await;
let entries = table.closest_entries(&peer_id.0, MAX_PEERS_IN_PEERSMSG);
drop(table);
let peers: Vec<PeerMsg> = entries.into_iter().map(|e| e.into()).collect();
conn.send(NetMsg::new(NetMsgCmd::Peers, &PeersMsg(peers))?)
.await?;
Ok(())
}
/// Sends a Peer msg.
async fn send_peer_msg(&self, conn: &Conn<NetMsg>, endpoint: Endpoint) -> Result<()> {
trace!("Send Peer msg");
let peer_msg = PeerMsg {
addr: endpoint.addr()?.clone(),
port: *endpoint.port()?,
discovery_port: self.config.discovery_port,
peer_id: self.id.clone(),
};
conn.send(NetMsg::new(NetMsgCmd::Peer, &peer_msg)?).await?;
Ok(())
}
/// Sends a Shutdown msg.
async fn send_shutdown_msg(&self, conn: &Conn<NetMsg>) -> Result<()> {
trace!("Send Shutdown msg");
conn.send(NetMsg::new(NetMsgCmd::Shutdown, &ShutdownMsg(0))?)
.await?;
Ok(())
}
}
|