This repository has been archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
swarm.rs
605 lines (512 loc) · 21.5 KB
/
swarm.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use crate::p2p::{MultiaddrWithPeerId, MultiaddrWithoutPeerId};
use crate::subscription::{SubscriptionFuture, SubscriptionRegistry};
use core::task::{Context, Poll};
use libp2p::core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
use libp2p::swarm::protocols_handler::{
DummyProtocolsHandler, IntoProtocolsHandler, ProtocolsHandler,
};
use libp2p::swarm::{self, DialPeerCondition, NetworkBehaviour, PollParameters, Swarm};
use std::collections::{hash_map::Entry, HashMap, HashSet, VecDeque};
use std::convert::{TryFrom, TryInto};
use std::time::Duration;
/// A description of currently active connection.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Connection {
/// The connected peer along with its address.
pub addr: MultiaddrWithPeerId,
/// Latest ping report on any of the connections.
pub rtt: Option<Duration>,
}
/// Disconnected will use banning to disconnect a node. Disconnecting a single peer connection is
/// not supported at the moment.
pub struct Disconnector {
peer_id: PeerId,
}
impl Disconnector {
pub fn disconnect<T: NetworkBehaviour>(self, swarm: &mut Swarm<T>) {
Swarm::ban_peer_id(swarm, self.peer_id);
Swarm::unban_peer_id(swarm, self.peer_id);
}
}
// Currently this is swarm::NetworkBehaviourAction<Void, Void>
type NetworkBehaviourAction = swarm::NetworkBehaviourAction<<<<SwarmApi as NetworkBehaviour>::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, <SwarmApi as NetworkBehaviour>::OutEvent>;
#[derive(Debug, Default)]
pub struct SwarmApi {
events: VecDeque<NetworkBehaviourAction>,
// FIXME: anything related to this is probably wrong, and doesn't behave as one would expect
// from the method names
peers: HashSet<PeerId>,
connect_registry: SubscriptionRegistry<(), String>,
connections: HashMap<MultiaddrWithoutPeerId, PeerId>,
roundtrip_times: HashMap<PeerId, Duration>,
connected_peers: HashMap<PeerId, Vec<MultiaddrWithoutPeerId>>,
/// The connections which have been requested, but the swarm/network is yet to ask for
/// addresses; currently filled in the order of adding, with the default size of one.
pending_addresses: HashMap<PeerId, Vec<MultiaddrWithPeerId>>,
/// The connections which have been requested, and the swarm/network has requested the
/// addresses of. Used to keep finishing all of the subscriptions.
pending_connections: HashMap<PeerId, Vec<MultiaddrWithPeerId>>,
pub(crate) bootstrappers: HashSet<MultiaddrWithPeerId>,
}
impl SwarmApi {
pub fn add_peer(&mut self, peer_id: PeerId) {
self.peers.insert(peer_id);
}
pub fn peers(&self) -> impl Iterator<Item = &PeerId> {
self.peers.iter()
}
pub fn remove_peer(&mut self, peer_id: &PeerId) {
self.peers.remove(peer_id);
}
pub fn connections(&self) -> impl Iterator<Item = Connection> + '_ {
self.connected_peers
.iter()
.filter_map(move |(peer, conns)| {
let rtt = self.roundtrip_times.get(peer).cloned();
conns.first().map(|any| Connection {
addr: MultiaddrWithPeerId::from((any.clone(), *peer)),
rtt,
})
})
}
pub fn set_rtt(&mut self, peer_id: &PeerId, rtt: Duration) {
// NOTE: this is for any connection
self.roundtrip_times.insert(*peer_id, rtt);
}
pub fn connect(&mut self, addr: MultiaddrWithPeerId) -> Option<SubscriptionFuture<(), String>> {
let connected_already = self
.connected_peers
.get(&addr.peer_id)
.map(|conns| conns.iter().any(|wo| wo == &addr.multiaddr))
.unwrap_or(false);
if connected_already {
return None;
}
trace!("Connecting to {:?}", addr);
let subscription = self
.connect_registry
.create_subscription(addr.clone().into(), None);
self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id: addr.peer_id,
// rationale: this is sort of explicit command, perhaps the old address is no longer
// valid. Always would be even better but it's bugged at the moment.
condition: DialPeerCondition::NotDialing,
});
self.pending_addresses
.entry(addr.peer_id)
.or_insert_with(|| Vec::with_capacity(1))
.push(addr);
Some(subscription)
}
pub fn disconnect(&mut self, addr: MultiaddrWithPeerId) -> Option<Disconnector> {
trace!("request to disconnect {}", addr);
if let Some(&peer_id) = self.connections.get(&addr.multiaddr) {
Some(Disconnector { peer_id })
} else {
None
}
}
pub fn connections_to(&self, peer_id: &PeerId) -> Vec<Multiaddr> {
self.connected_peers
.get(peer_id)
.cloned()
.map(|addrs| addrs.into_iter().map(From::from).collect())
.unwrap_or_default()
}
}
impl NetworkBehaviour for SwarmApi {
type ProtocolsHandler = DummyProtocolsHandler;
type OutEvent = void::Void;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
Default::default()
}
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
// when libp2p starts dialing, it'll collect these from all of known addresses for the peer
// from the behaviour and dial them all through, ending with calls to inject_connected or
// inject_addr_reach_failure.
let addresses = self.pending_addresses.remove(peer_id).unwrap_or_default();
// store the "given out" addresses as we have created the subscriptions for them
self.pending_connections
.entry(*peer_id)
.or_default()
.extend(addresses.iter().cloned());
addresses.into_iter().map(|a| a.into()).collect()
}
fn inject_connection_established(
&mut self,
peer_id: &PeerId,
_id: &ConnectionId,
cp: &ConnectedPoint,
) {
// TODO: could be that the connection is not yet fully established at this point
trace!("inject_connection_established {} {:?}", peer_id, cp);
let addr = connection_point_addr(cp);
self.peers.insert(*peer_id);
let connections = self.connected_peers.entry(*peer_id).or_default();
connections.push(addr.clone());
let prev = self.connections.insert(addr.clone(), *peer_id);
if let Some(prev) = prev {
error!(
"tracked connection was replaced from {} => {}: {}",
prev, peer_id, addr
);
}
if let ConnectedPoint::Dialer { address } = cp {
// we dialed to the `address`
match self.pending_connections.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let addresses = oe.get_mut();
let address: MultiaddrWithPeerId = address
.clone()
.try_into()
.expect("dialed address contains peerid in libp2p 0.38");
let just_connected = addresses.iter().position(|x| *x == address);
if let Some(just_connected) = just_connected {
addresses.swap_remove(just_connected);
if addresses.is_empty() {
oe.remove();
}
self.connect_registry
.finish_subscription(address.into(), Ok(()));
}
}
Entry::Vacant(_) => {
// we not connecting to this peer through this api, must be libp2p_kad or
// something else.
}
}
}
}
fn inject_connected(&mut self, peer_id: &PeerId) {
// we have at least one fully open connection and handler is running
//
// just finish all of the subscriptions that remain.
trace!("inject connected {}", peer_id);
let all_subs = self
.pending_addresses
.remove(peer_id)
.unwrap_or_default()
.into_iter()
.chain(
self.pending_connections
.remove(peer_id)
.unwrap_or_default()
.into_iter(),
);
for addr in all_subs {
// fail the other than already connected subscriptions in
// inject_connection_established. while the whole swarmapi is quite unclear on the
// actual use cases, assume that connecting one is good enough for all outstanding
// connection requests.
self.connect_registry.finish_subscription(
addr.into(),
Err("finished connecting to another address".into()),
);
}
}
fn inject_connection_closed(
&mut self,
peer_id: &PeerId,
_id: &ConnectionId,
cp: &ConnectedPoint,
) {
trace!("inject_connection_closed {} {:?}", peer_id, cp);
let closed_addr = connection_point_addr(cp);
match self.connected_peers.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let connections = oe.get_mut();
let pos = connections.iter().position(|addr| *addr == closed_addr);
if let Some(pos) = pos {
connections.swap_remove(pos);
}
if connections.is_empty() {
oe.remove();
}
}
Entry::Vacant(_) => {}
}
let removed = self.connections.remove(&closed_addr);
debug_assert!(
removed.is_some(),
"connection was not tracked but it should had been: {}",
closed_addr
);
if let ConnectedPoint::Dialer { .. } = cp {
let addr = MultiaddrWithPeerId::from((closed_addr, peer_id.to_owned()));
match self.pending_connections.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let connections = oe.get_mut();
let pos = connections.iter().position(|x| addr == *x);
if let Some(pos) = pos {
connections.swap_remove(pos);
// this needs to be guarded, so that the connect test case doesn't cause a
// panic following inject_connection_established, inject_connection_closed
// if there's only the DummyProtocolsHandler, which doesn't open a
// substream and closes up immediatedly.
self.connect_registry.finish_subscription(
addr.into(),
Err("Connection reset by peer".to_owned()),
);
}
if connections.is_empty() {
oe.remove();
}
}
Entry::Vacant(_) => {}
}
} else {
// we were not dialing to the peer, thus we cannot have a pending subscription to
// finish.
}
}
fn inject_disconnected(&mut self, peer_id: &PeerId) {
trace!("inject_disconnected: {}", peer_id);
assert!(!self.connected_peers.contains_key(peer_id));
self.roundtrip_times.remove(peer_id);
let failed = self
.pending_addresses
.remove(peer_id)
.unwrap_or_default()
.into_iter()
.chain(
self.pending_connections
.remove(peer_id)
.unwrap_or_default()
.into_iter(),
);
for addr in failed {
self.connect_registry
.finish_subscription(addr.into(), Err("disconnected".into()));
}
}
fn inject_event(&mut self, _peer_id: PeerId, _connection: ConnectionId, _event: void::Void) {}
fn inject_dial_failure(&mut self, peer_id: &PeerId) {
trace!("inject_dial_failure: {}", peer_id);
if self.pending_addresses.contains_key(peer_id) {
// it is possible that these addresses have not been tried yet; they will be asked
// for soon.
self.events
.push_back(swarm::NetworkBehaviourAction::DialPeer {
peer_id: *peer_id,
condition: DialPeerCondition::NotDialing,
});
}
// this should not be executed once, but probably will be in case unsupported addresses or something
// surprising happens.
for failed in self.pending_connections.remove(peer_id).unwrap_or_default() {
self.connect_registry
.finish_subscription(failed.into(), Err("addresses exhausted".into()));
}
}
fn inject_addr_reach_failure(
&mut self,
peer_id: Option<&PeerId>,
addr: &Multiaddr,
error: &dyn std::error::Error,
) {
trace!("inject_addr_reach_failure {} {}", addr, error);
if let Some(peer_id) = peer_id {
match self.pending_connections.entry(*peer_id) {
Entry::Occupied(mut oe) => {
let addresses = oe.get_mut();
let addr = MultiaddrWithPeerId::try_from(addr.clone())
.expect("dialed address contains peerid in libp2p 0.38");
let pos = addresses.iter().position(|a| *a == addr);
if let Some(pos) = pos {
addresses.swap_remove(pos);
self.connect_registry
.finish_subscription(addr.into(), Err(error.to_string()));
}
if addresses.is_empty() {
oe.remove();
}
}
Entry::Vacant(_) => {}
}
}
}
fn poll(
&mut self,
_: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction> {
if let Some(event) = self.events.pop_front() {
Poll::Ready(event)
} else {
Poll::Pending
}
}
}
fn connection_point_addr(cp: &ConnectedPoint) -> MultiaddrWithoutPeerId {
match cp {
ConnectedPoint::Dialer { address } => MultiaddrWithPeerId::try_from(address.to_owned())
.expect("dialed address contains peerid in libp2p 0.38")
.into(),
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr
.to_owned()
.try_into()
.expect("send back address does not contain peerid in libp2p 0.38"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::p2p::transport::build_transport;
use futures::{
stream::{StreamExt, TryStreamExt},
TryFutureExt,
};
use libp2p::identity::Keypair;
use libp2p::swarm::SwarmEvent;
use libp2p::{multiaddr::Protocol, multihash::Multihash, swarm::Swarm, swarm::SwarmBuilder};
use std::convert::TryInto;
#[tokio::test]
async fn swarm_api() {
let (peer1_id, mut swarm1) = build_swarm();
let (peer2_id, mut swarm2) = build_swarm();
Swarm::listen_on(&mut swarm1, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
loop {
if let Some(SwarmEvent::NewListenAddr { .. }) = swarm1.next().await {
break;
}
}
let listeners = Swarm::listeners(&swarm1).cloned().collect::<Vec<_>>();
for mut addr in listeners {
addr.push(Protocol::P2p(
Multihash::from_bytes(&peer1_id.to_bytes()).unwrap(),
));
let mut sub = swarm2
.behaviour_mut()
.connect(addr.try_into().unwrap())
.unwrap();
loop {
tokio::select! {
_ = (&mut swarm1).next() => {},
_ = (&mut swarm2).next() => {},
res = (&mut sub) => {
// this is currently a success even though the connection is never really
// established, the DummyProtocolsHandler doesn't do anything nor want the
// connection to be kept alive and thats it.
//
// it could be argued that this should be `Err("keepalive disconnected")`
// or something and I'd agree, but I also agree this can be an `Ok(())`;
// it's the sort of difficulty with the cli functionality in general: what
// does it mean to connect to a peer? one way to look at it would be to
// make the peer a "pinned peer" or "friend" and to keep the connection
// alive at all costs. perhaps that is something for the next round.
// another aspect would be to fail this future because there was no
// `inject_connected`, only `inject_connection_established`. taking that
// route would be good; it does however leave the special case of adding
// another connection, which does add even more complexity than it exists
// at the present.
res.unwrap();
// just to confirm that there are no connections.
assert_eq!(Vec::<Multiaddr>::new(), swarm1.behaviour().connections_to(&peer2_id));
break;
}
}
}
}
}
#[tokio::test]
async fn wrong_peerid() {
let (_, mut swarm1) = build_swarm();
let (_, mut swarm2) = build_swarm();
let peer3_id = Keypair::generate_ed25519().public().into_peer_id();
Swarm::listen_on(&mut swarm1, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
let addr;
loop {
if let Some(SwarmEvent::NewListenAddr { address, .. }) = swarm1.next().await {
// wonder if there should be a timeout?
addr = address;
break;
}
}
let mut fut = swarm2
.behaviour_mut()
.connect(
MultiaddrWithoutPeerId::try_from(addr)
.unwrap()
.with(peer3_id),
)
.unwrap()
// remove the private type wrapper
.map_err(|e| e.into_inner());
loop {
tokio::select! {
_ = swarm1.next() => {},
_ = swarm2.next() => {},
res = &mut fut => {
assert_eq!(res.unwrap_err(), Some("Pending connection: Invalid peer ID.".into()));
return;
}
}
}
}
#[tokio::test]
async fn racy_connecting_attempts() {
let (peer1_id, mut swarm1) = build_swarm();
let (_, mut swarm2) = build_swarm();
Swarm::listen_on(&mut swarm1, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
Swarm::listen_on(&mut swarm1, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
let mut addr = Vec::with_capacity(2);
while addr.len() < 2 {
if let Some(SwarmEvent::NewListenAddr { address, .. }) = swarm1.next().await {
addr.push(address);
}
}
let targets = (
MultiaddrWithoutPeerId::try_from(addr[0].clone())
.unwrap()
.with(peer1_id),
MultiaddrWithoutPeerId::try_from(addr[1].clone())
.unwrap()
.with(peer1_id),
);
let mut connections = futures::stream::FuturesOrdered::new();
// these two should be attempted in parallel. since we know both of them work, and they are
// given in this order, we know that in libp2p 0.34 only the first should win, however
// both should always be finished.
connections.push(swarm2.behaviour_mut().connect(targets.0).unwrap());
connections.push(swarm2.behaviour_mut().connect(targets.1).unwrap());
let ready = connections
// turn the private error type into Option
.map_err(|e| e.into_inner())
.collect::<Vec<_>>();
tokio::pin!(ready);
loop {
tokio::select! {
_ = swarm1.next() => {}
_ = swarm2.next() => {}
res = &mut ready => {
assert_eq!(
res,
vec![
Ok(()),
Err(Some("finished connecting to another address".into()))
]);
break;
}
}
}
}
fn build_swarm() -> (PeerId, libp2p::swarm::Swarm<SwarmApi>) {
let key = Keypair::generate_ed25519();
let peer_id = key.public().into_peer_id();
let transport = build_transport(key).unwrap();
let swarm = SwarmBuilder::new(transport, SwarmApi::default(), peer_id)
.executor(Box::new(ThreadLocalTokio))
.build();
(peer_id, swarm)
}
use std::future::Future;
use std::pin::Pin;
// can only be used from within tokio context. this is required since otherwise libp2p-tcp will
// use tokio, but from a futures-executor threadpool, which is outside of tokio context.
struct ThreadLocalTokio;
impl libp2p::core::Executor for ThreadLocalTokio {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>) {
tokio::task::spawn(future);
}
}
}