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
use core::task::{Context, Poll};
use futures::channel::oneshot;
use futures::StreamExt;
use libp2p::core::{Endpoint, Multiaddr};
use libp2p::swarm::derive_prelude::ConnectionEstablished;
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::{
self, dummy::ConnectionHandler as DummyConnectionHandler, NetworkBehaviour, PollParameters,
};
use libp2p::swarm::{
ConnectionClosed, ConnectionDenied, ConnectionId, ConnectionLimit, DialFailure, FromSwarm,
NetworkBehaviourAction, THandler, THandlerInEvent,
};
use libp2p::PeerId;
use std::collections::hash_map::Entry;
use std::time::Duration;
use wasm_timer::Interval;
use std::collections::{HashMap, HashSet, VecDeque};
use super::PeerInfo;
#[derive(Debug, Clone, Copy, Default)]
pub struct ConnectionLimits {
max_pending_incoming: Option<u32>,
max_pending_outgoing: Option<u32>,
max_established_incoming: Option<u32>,
max_established_outgoing: Option<u32>,
max_established_per_peer: Option<u32>,
max_established_total: Option<u32>,
}
impl ConnectionLimits {
pub fn max_pending_incoming(&self) -> Option<u32> {
self.max_pending_incoming
}
pub fn max_pending_outgoing(&self) -> Option<u32> {
self.max_pending_outgoing
}
pub fn max_established_incoming(&self) -> Option<u32> {
self.max_established_incoming
}
pub fn max_established_outgoing(&self) -> Option<u32> {
self.max_established_outgoing
}
pub fn max_established(&self) -> Option<u32> {
self.max_established_total
}
pub fn max_established_per_peer(&self) -> Option<u32> {
self.max_established_per_peer
}
}
impl ConnectionLimits {
pub fn set_max_pending_incoming(&mut self, limit: Option<u32>) {
self.max_pending_incoming = limit;
}
pub fn set_max_pending_outgoing(&mut self, limit: Option<u32>) {
self.max_pending_outgoing = limit;
}
pub fn set_max_established_incoming(&mut self, limit: Option<u32>) {
self.max_established_incoming = limit;
}
pub fn set_max_established_outgoing(&mut self, limit: Option<u32>) {
self.max_established_outgoing = limit;
}
pub fn set_max_established(&mut self, limit: Option<u32>) {
self.max_established_total = limit;
}
pub fn set_max_established_per_peer(&mut self, limit: Option<u32>) {
self.max_established_per_peer = limit;
}
}
impl ConnectionLimits {
pub fn with_max_pending_incoming(mut self, limit: Option<u32>) -> Self {
self.max_pending_incoming = limit;
self
}
pub fn with_max_pending_outgoing(mut self, limit: Option<u32>) -> Self {
self.max_pending_outgoing = limit;
self
}
pub fn with_max_established_incoming(mut self, limit: Option<u32>) -> Self {
self.max_established_incoming = limit;
self
}
pub fn with_max_established_outgoing(mut self, limit: Option<u32>) -> Self {
self.max_established_outgoing = limit;
self
}
pub fn with_max_established(mut self, limit: Option<u32>) -> Self {
self.max_established_total = limit;
self
}
pub fn with_max_established_per_peer(mut self, limit: Option<u32>) -> Self {
self.max_established_per_peer = limit;
self
}
}
#[derive(Debug)]
#[allow(clippy::type_complexity)]
pub struct Behaviour {
limits: ConnectionLimits,
events: VecDeque<
swarm::NetworkBehaviourAction<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>,
>,
cleanup_interval: Interval,
pending_connections: HashMap<ConnectionId, oneshot::Sender<anyhow::Result<()>>>,
peer_info: HashMap<PeerId, PeerInfo>,
peer_rtt: HashMap<PeerId, [Duration; 3]>,
whitelist: HashSet<PeerId>,
protocols: Vec<Vec<u8>>,
pending_inbound_connections: HashSet<ConnectionId>,
pending_outbound_connections: HashSet<ConnectionId>,
established_inbound_connections: HashSet<ConnectionId>,
established_outbound_connections: HashSet<ConnectionId>,
established_per_peer: HashMap<PeerId, HashSet<ConnectionId>>,
}
impl Default for Behaviour {
fn default() -> Self {
Self {
limits: Default::default(),
events: Default::default(),
cleanup_interval: Interval::new_at(
std::time::Instant::now() + Duration::from_secs(60),
Duration::from_secs(60),
),
pending_connections: Default::default(),
peer_info: Default::default(),
peer_rtt: Default::default(),
whitelist: Default::default(),
protocols: Default::default(),
pending_inbound_connections: Default::default(),
pending_outbound_connections: Default::default(),
established_inbound_connections: Default::default(),
established_outbound_connections: Default::default(),
established_per_peer: Default::default(),
}
}
}
impl Behaviour {
pub fn connect(&mut self, opt: impl Into<DialOpts>) -> oneshot::Receiver<anyhow::Result<()>> {
let opts: DialOpts = opt.into();
let (tx, rx) = oneshot::channel();
let id = opts.connection_id();
self.events.push_back(NetworkBehaviourAction::Dial { opts });
self.pending_connections.insert(id, tx);
rx
}
pub fn protocols(&self) -> impl Iterator<Item = String> + '_ {
self.protocols
.iter()
.map(|protocol| String::from_utf8_lossy(protocol).into_owned())
}
pub fn set_connection_limit(&mut self, limit: ConnectionLimits) {
self.limits = limit;
}
pub fn add(&mut self, peer_id: PeerId) {
self.whitelist.insert(peer_id);
}
pub fn remove(&mut self, peer_id: PeerId) {
self.whitelist.remove(&peer_id);
}
pub fn inject_peer_info<I: Into<PeerInfo>>(&mut self, info: I) {
let info: PeerInfo = info.into();
self.peer_info.insert(info.peer_id, info);
}
pub fn peers(&self) -> impl Iterator<Item = &PeerId> {
self.peer_info.keys()
}
pub fn set_peer_rtt(&mut self, peer_id: PeerId, rtt: Duration) {
if self.peer_info.contains_key(&peer_id) {
self.peer_rtt
.entry(peer_id)
.and_modify(|r| {
r.rotate_left(1);
r[2] = rtt;
})
.or_insert([Duration::from_millis(0), Duration::from_millis(0), rtt]);
}
}
pub fn get_peet_rtt(&self, peer_id: PeerId) -> Option<[Duration; 3]> {
self.peer_rtt.get(&peer_id).copied()
}
pub fn get_peet_latest_rtt(&self, peer_id: PeerId) -> Option<Duration> {
self.get_peet_rtt(peer_id).map(|rtt| rtt[2])
}
pub fn get_peer_info(&self, peer_id: PeerId) -> Option<&PeerInfo> {
self.peer_info.get(&peer_id)
}
pub fn remove_peer_info(&mut self, peer_id: PeerId) {
self.peer_info.remove(&peer_id);
}
fn check_limit(&mut self, limit: Option<u32>, current: usize) -> Result<(), ConnectionDenied> {
let limit = limit.unwrap_or(u32::MAX);
let current = current as u32;
if current >= limit {
return Err(ConnectionDenied::new(ConnectionLimit { limit, current }));
}
Ok(())
}
}
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = DummyConnectionHandler;
type OutEvent = void::Void;
fn handle_pending_inbound_connection(
&mut self,
connection_id: ConnectionId,
_: &Multiaddr,
_: &Multiaddr,
) -> Result<(), ConnectionDenied> {
self.check_limit(
self.limits.max_pending_incoming,
self.pending_inbound_connections.len(),
)?;
self.pending_inbound_connections.insert(connection_id);
Ok(())
}
fn handle_pending_outbound_connection(
&mut self,
connection_id: ConnectionId,
peer_id: Option<PeerId>,
_: &[Multiaddr],
_: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
let mut is_whitelisted = false;
if let Some(peer_id) = peer_id {
is_whitelisted = self.whitelist.contains(&peer_id);
}
if !is_whitelisted {
self.check_limit(
self.limits.max_pending_outgoing,
self.pending_outbound_connections.len(),
)?;
}
self.pending_outbound_connections.insert(connection_id);
Ok(vec![])
}
fn handle_established_inbound_connection(
&mut self,
connection_id: ConnectionId,
peer_id: PeerId,
_: &Multiaddr,
_: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
self.pending_inbound_connections.remove(&connection_id);
if !self.whitelist.contains(&peer_id) {
self.check_limit(
self.limits.max_established_incoming,
self.established_inbound_connections.len(),
)?;
self.check_limit(
self.limits.max_established_per_peer,
self.established_per_peer
.get(&peer_id)
.map(|connections| connections.len())
.unwrap_or(0),
)?;
self.check_limit(
self.limits.max_established_total,
self.established_inbound_connections.len()
+ self.established_outbound_connections.len(),
)?;
}
self.established_inbound_connections.insert(connection_id);
self.established_per_peer
.entry(peer_id)
.or_default()
.insert(connection_id);
Ok(DummyConnectionHandler)
}
fn handle_established_outbound_connection(
&mut self,
connection_id: ConnectionId,
peer_id: PeerId,
_: &Multiaddr,
_: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
self.pending_outbound_connections.remove(&connection_id);
if !self.whitelist.contains(&peer_id) {
self.check_limit(
self.limits.max_established_outgoing,
self.established_outbound_connections.len(),
)?;
self.check_limit(
self.limits.max_established_per_peer,
self.established_per_peer
.get(&peer_id)
.map(|connections| connections.len())
.unwrap_or(0),
)?;
self.check_limit(
self.limits.max_established_total,
self.established_inbound_connections.len()
+ self.established_outbound_connections.len(),
)?;
}
self.established_outbound_connections.insert(connection_id);
self.established_per_peer
.entry(peer_id)
.or_default()
.insert(connection_id);
Ok(DummyConnectionHandler)
}
fn on_connection_handler_event(
&mut self,
_: libp2p::PeerId,
_: swarm::ConnectionId,
_: swarm::THandlerOutEvent<Self>,
) {
}
#[allow(clippy::single_match)]
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished { connection_id, .. }) => {
if let Some(ch) = self.pending_connections.remove(&connection_id) {
let _ = ch.send(Ok(()));
}
}
FromSwarm::DialFailure(DialFailure {
error,
connection_id,
..
}) => {
if let Some(ch) = self.pending_connections.remove(&connection_id) {
let _ = ch.send(Err(anyhow::anyhow!("{error}")));
}
}
FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id,
..
}) => {
self.established_inbound_connections.remove(&connection_id);
self.established_outbound_connections.remove(&connection_id);
if let Entry::Occupied(mut entry) = self.established_per_peer.entry(peer_id) {
entry.get_mut().remove(&connection_id);
if entry.get().is_empty() {
entry.remove();
}
}
if let Some(ch) = self.pending_connections.remove(&connection_id) {
let _ = ch.send(Ok(()));
}
}
_ => {}
}
}
fn poll(
&mut self,
cx: &mut Context,
params: &mut impl PollParameters,
) -> Poll<swarm::NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
let supported_protocols = params.supported_protocols();
if supported_protocols.len() != self.protocols.len() {
self.protocols = supported_protocols.collect();
}
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event);
}
while let Poll::Ready(Some(_)) = self.cleanup_interval.poll_next_unpin(cx) {
let list = self.peer_info.keys().copied().collect::<Vec<_>>();
for peer_id in list {
if !self.established_per_peer.contains_key(&peer_id)
&& !self.whitelist.contains(&peer_id)
{
self.peer_info.remove(&peer_id);
self.peer_rtt.remove(&peer_id);
}
}
}
Poll::Pending
}
}
#[cfg(test)]
mod test {
use std::time::Duration;
use super::Behaviour as PeerBook;
use crate::p2p::{peerbook::ConnectionLimits, transport::build_transport};
use futures::StreamExt;
use libp2p::{
identity::Keypair,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
Multiaddr, PeerId, Swarm,
};
#[derive(NetworkBehaviour)]
struct Behaviour {
peerbook: PeerBook,
keep_alive: keep_alive::Behaviour,
}
#[tokio::test]
async fn connection_limits() {
let (_, addr1, mut swarm1) = build_swarm().await;
let (peer2, _, mut swarm2) = build_swarm().await;
let (peer3, _, mut swarm3) = build_swarm().await;
let (peer4, _, mut swarm4) = build_swarm().await;
swarm1
.behaviour_mut()
.peerbook
.set_connection_limit(ConnectionLimits {
max_established_incoming: Some(1),
..Default::default()
});
swarm2.dial(addr1.clone()).unwrap();
loop {
if let Some(SwarmEvent::ConnectionEstablished { .. }) = swarm1.next().await {
break;
}
}
swarm1.behaviour_mut().peerbook.add(peer3);
swarm3.dial(addr1.clone()).unwrap();
tokio::time::timeout(Duration::from_secs(10), async {
loop {
if let Some(SwarmEvent::ConnectionEstablished { .. }) = swarm1.next().await {
break;
}
}
})
.await
.unwrap();
swarm4.dial(addr1.clone()).unwrap();
tokio::time::timeout(Duration::from_secs(10), async {
loop {
if let Some(SwarmEvent::IncomingConnectionError { .. }) = swarm1.next().await {
break;
}
}
})
.await
.unwrap();
let list = swarm1.connected_peers().copied().collect::<Vec<_>>();
assert!(list.contains(&peer2));
assert!(list.contains(&peer3));
assert!(!list.contains(&peer4));
}
async fn build_swarm() -> (PeerId, Multiaddr, libp2p::swarm::Swarm<Behaviour>) {
let key = Keypair::generate_ed25519();
let peer_id = key.public().to_peer_id();
let transport = build_transport(key, None, Default::default()).unwrap();
let behaviour = Behaviour {
peerbook: PeerBook::default(),
keep_alive: keep_alive::Behaviour,
};
let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, peer_id).build();
Swarm::listen_on(&mut swarm, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
if let Some(SwarmEvent::NewListenAddr { address, .. }) = swarm.next().await {
return (peer_id, address, swarm);
}
panic!("no new addrs")
}
}