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
use super::gossipsub::GossipsubStream;
use serde::{Deserialize, Serialize};
use super::swarm::{Connection, SwarmApi};
use crate::error::Error;
use crate::p2p::{MultiaddrWithPeerId, SwarmOptions};
use crate::subscription::SubscriptionFuture;
use ipfs_bitswap::{Bitswap, BitswapEvent};
use libipld::Cid;
use libp2p::autonat;
use libp2p::core::{Multiaddr, PeerId};
use libp2p::dcutr::behaviour::{Behaviour as Dcutr, Event as DcutrEvent};
use libp2p::gossipsub::GossipsubEvent;
use libp2p::identify::{Behaviour as Identify, Config as IdentifyConfig, Event as IdentifyEvent};
use libp2p::kad::record::{
store::{MemoryStore, MemoryStoreConfig},
Record,
};
use libp2p::kad::{Kademlia, KademliaConfig, KademliaEvent};
use libp2p::mdns::{tokio::Behaviour as Mdns, Config as MdnsConfig, Event as MdnsEvent};
use libp2p::ping::{Behaviour as Ping, Event as PingEvent};
use libp2p::relay::v2::client::transport::ClientTransport;
use libp2p::relay::v2::client::{Client as RelayClient, Event as RelayClientEvent};
use libp2p::relay::v2::relay::{rate_limiter, Event as RelayEvent, Relay};
use libp2p::swarm::behaviour::toggle::Toggle;
use libp2p::swarm::keep_alive::Behaviour as KeepAliveBehaviour;
use libp2p::swarm::NetworkBehaviour;
use std::convert::TryFrom;
use std::num::NonZeroU32;
use std::time::Duration;
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourEvent", event_process = false)]
pub struct Behaviour {
pub mdns: Toggle<Mdns>,
pub kademlia: Kademlia<MemoryStore>,
pub bitswap: Bitswap,
pub ping: Ping,
pub identify: Identify,
pub keepalive: Toggle<KeepAliveBehaviour>,
pub pubsub: GossipsubStream,
pub autonat: autonat::Behaviour,
pub relay: Toggle<Relay>,
pub relay_client: Toggle<RelayClient>,
pub dcutr: Toggle<Dcutr>,
pub swarm: SwarmApi,
}
#[derive(Debug)]
pub enum BehaviourEvent {
Mdns(MdnsEvent),
Kad(KademliaEvent),
Bitswap(BitswapEvent),
Ping(PingEvent),
Identify(IdentifyEvent),
Gossipsub(GossipsubEvent),
Autonat(autonat::Event),
Relay(RelayEvent),
RelayClient(RelayClientEvent),
Dcutr(DcutrEvent),
Void(void::Void),
}
impl From<MdnsEvent> for BehaviourEvent {
fn from(event: MdnsEvent) -> Self {
BehaviourEvent::Mdns(event)
}
}
impl From<KademliaEvent> for BehaviourEvent {
fn from(event: KademliaEvent) -> Self {
BehaviourEvent::Kad(event)
}
}
impl From<BitswapEvent> for BehaviourEvent {
fn from(event: BitswapEvent) -> Self {
BehaviourEvent::Bitswap(event)
}
}
impl From<PingEvent> for BehaviourEvent {
fn from(event: PingEvent) -> Self {
BehaviourEvent::Ping(event)
}
}
impl From<IdentifyEvent> for BehaviourEvent {
fn from(event: IdentifyEvent) -> Self {
BehaviourEvent::Identify(event)
}
}
impl From<GossipsubEvent> for BehaviourEvent {
fn from(event: GossipsubEvent) -> Self {
BehaviourEvent::Gossipsub(event)
}
}
impl From<autonat::Event> for BehaviourEvent {
fn from(event: autonat::Event) -> Self {
BehaviourEvent::Autonat(event)
}
}
impl From<RelayEvent> for BehaviourEvent {
fn from(event: RelayEvent) -> Self {
BehaviourEvent::Relay(event)
}
}
impl From<RelayClientEvent> for BehaviourEvent {
fn from(event: RelayClientEvent) -> Self {
BehaviourEvent::RelayClient(event)
}
}
impl From<DcutrEvent> for BehaviourEvent {
fn from(event: DcutrEvent) -> Self {
BehaviourEvent::Dcutr(event)
}
}
impl From<void::Void> for BehaviourEvent {
fn from(event: void::Void) -> Self {
BehaviourEvent::Void(event)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KadResult {
Complete,
Peers(Vec<PeerId>),
Records(Vec<Record>),
Record(Record),
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RelayConfig {
pub max_reservations: usize,
pub max_reservations_per_peer: usize,
pub reservation_duration: std::time::Duration,
pub reservation_rate_limiters: Vec<RateLimit>,
pub max_circuits: usize,
pub max_circuits_per_peer: usize,
pub max_circuit_duration: std::time::Duration,
pub max_circuit_bytes: u64,
pub circuit_src_rate_limiters: Vec<RateLimit>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IdentifyConfiguration {
pub protocol_version: String,
pub agent_version: String,
pub initial_delay: Duration,
pub interval: Duration,
pub push_update: bool,
pub cache: usize,
}
impl Default for IdentifyConfiguration {
fn default() -> Self {
Self {
protocol_version: "/ipfs/0.1.0".into(),
agent_version: "rust-ipfs".into(),
initial_delay: Duration::from_millis(500),
interval: Duration::from_secs(5 * 60),
push_update: false,
cache: 0,
}
}
}
impl IdentifyConfiguration {
pub fn into(self, publuc_key: libp2p::identity::PublicKey) -> IdentifyConfig {
IdentifyConfig::new(self.protocol_version, publuc_key)
.with_agent_version(self.agent_version)
.with_initial_delay(self.initial_delay)
.with_interval(self.interval)
.with_push_listen_addr_updates(self.push_update)
.with_cache_size(self.cache)
}
}
impl From<RelayConfig> for libp2p::relay::v2::relay::Config {
fn from(
RelayConfig {
max_reservations,
max_reservations_per_peer,
reservation_duration,
reservation_rate_limiters,
max_circuits,
max_circuits_per_peer,
max_circuit_duration,
max_circuit_bytes,
circuit_src_rate_limiters,
}: RelayConfig,
) -> Self {
let reservation_rate_limiters = reservation_rate_limiters
.iter()
.map(|rate| match rate {
RateLimit::PerPeer { limit, interval } => {
rate_limiter::new_per_peer(rate_limiter::GenericRateLimiterConfig {
limit: *limit,
interval: *interval,
})
}
RateLimit::PerIp { limit, interval } => {
rate_limiter::new_per_ip(rate_limiter::GenericRateLimiterConfig {
limit: *limit,
interval: *interval,
})
}
})
.collect::<Vec<_>>();
let circuit_src_rate_limiters = circuit_src_rate_limiters
.iter()
.map(|rate| match rate {
RateLimit::PerPeer { limit, interval } => {
rate_limiter::new_per_peer(rate_limiter::GenericRateLimiterConfig {
limit: *limit,
interval: *interval,
})
}
RateLimit::PerIp { limit, interval } => {
rate_limiter::new_per_ip(rate_limiter::GenericRateLimiterConfig {
limit: *limit,
interval: *interval,
})
}
})
.collect::<Vec<_>>();
libp2p::relay::v2::relay::Config {
max_reservations,
max_reservations_per_peer,
reservation_duration,
reservation_rate_limiters,
max_circuits,
max_circuits_per_peer,
max_circuit_duration,
max_circuit_bytes,
circuit_src_rate_limiters,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum RateLimit {
PerPeer {
limit: NonZeroU32,
interval: std::time::Duration,
},
PerIp {
limit: NonZeroU32,
interval: std::time::Duration,
},
}
#[derive(Default, Clone, Debug)]
pub struct KadStoreConfig {
pub memory: Option<MemoryStoreConfig>,
}
impl Behaviour {
pub async fn new(options: SwarmOptions) -> Result<(Self, Option<ClientTransport>), Error> {
let peer_id = options.peer_id;
info!("net: starting with peer id {}", peer_id);
let mdns = if options.mdns {
let config = MdnsConfig {
enable_ipv6: options.mdns_ipv6,
..Default::default()
};
Mdns::new(config).ok()
} else {
None
}
.into();
let store = {
let config = options
.kad_store_config
.unwrap_or_default()
.memory
.unwrap_or_default();
MemoryStore::with_config(peer_id, config)
};
let kad_config = match options.kad_config.clone() {
Some(config) => config,
None => {
let mut kad_config = KademliaConfig::default();
kad_config.disjoint_query_paths(true);
kad_config.set_query_timeout(std::time::Duration::from_secs(300));
kad_config
}
};
let mut kademlia = Kademlia::with_config(peer_id, store, kad_config);
for addr in &options.bootstrap {
let addr = MultiaddrWithPeerId::try_from(addr.clone())?;
kademlia.add_address(&addr.peer_id, addr.multiaddr.as_ref().clone());
}
let autonat = autonat::Behaviour::new(options.peer_id.to_owned(), Default::default());
let bitswap = Bitswap::default();
let keepalive = options.keep_alive.then(KeepAliveBehaviour::default).into();
let ping = Ping::new(options.ping_config.unwrap_or_default());
let identify = Identify::new(
options
.identify_config
.unwrap_or_default()
.into(options.keypair.public()),
);
let pubsub = {
let config = libp2p::gossipsub::GossipsubConfigBuilder::default()
.max_transmit_size(512 * 1024)
.build()
.map_err(|e| anyhow::anyhow!("{}", e))?;
let gossipsub = libp2p::gossipsub::Gossipsub::new(
libp2p::gossipsub::MessageAuthenticity::Signed(options.keypair),
config,
)
.map_err(|e| anyhow::anyhow!("{}", e))?;
GossipsubStream::from(gossipsub)
};
let swarm = SwarmApi::default();
let dcutr = Toggle::from(options.dcutr.then(Dcutr::new));
let relay_config = options
.relay_server_config
.map(|rc| rc.into())
.unwrap_or_default();
let relay = Toggle::from(
options
.relay_server
.then(|| Relay::new(peer_id, relay_config)),
);
let (transport, relay_client) = match options.relay {
true => {
let (transport, client) = RelayClient::new_transport_and_behaviour(peer_id);
(Some(transport), Some(client).into())
}
false => (None, None.into()),
};
Ok((
Behaviour {
mdns,
kademlia,
bitswap,
keepalive,
ping,
identify,
autonat,
pubsub,
swarm,
dcutr,
relay,
relay_client,
},
transport,
))
}
pub fn add_peer(&mut self, peer: PeerId, addr: Option<Multiaddr>) {
if let Some(addr) = addr {
self.kademlia.add_address(&peer, addr);
}
self.swarm.add_peer(peer);
self.pubsub.add_explicit_peer(&peer);
self.bitswap.connect(peer);
}
pub fn remove_peer(&mut self, peer: &PeerId) {
self.swarm.remove_peer(peer);
self.pubsub.remove_explicit_peer(peer);
self.kademlia.remove_peer(peer);
}
pub fn addrs(&mut self) -> Vec<(PeerId, Vec<Multiaddr>)> {
let peers = self
.swarm
.peers()
.map(|(k, _)| k)
.cloned()
.collect::<Vec<_>>();
let mut addrs = Vec::with_capacity(peers.len());
for peer_id in peers.into_iter() {
let peer_addrs = self.addresses_of_peer(&peer_id);
addrs.push((peer_id, peer_addrs));
}
addrs
}
pub fn connections(&self) -> impl Iterator<Item = Connection> + '_ {
self.swarm.connections()
}
pub fn connect(
&mut self,
addr: MultiaddrWithPeerId,
) -> Option<Option<SubscriptionFuture<(), String>>> {
self.swarm.connect(addr)
}
pub fn want_block(&mut self, cid: Cid) {
let key = cid.hash().to_bytes();
self.kademlia.get_providers(key.into());
self.bitswap.want_block(cid, 1);
}
pub fn stop_providing_block(&mut self, cid: &Cid) {
info!("Finished providing block {}", cid.to_string());
let key = cid.hash().to_bytes();
self.kademlia.stop_providing(&key.into());
}
pub fn supported_protocols(&self) -> Vec<String> {
self.swarm.protocols().collect::<Vec<_>>()
}
pub fn pubsub(&mut self) -> &mut GossipsubStream {
&mut self.pubsub
}
pub fn bitswap(&mut self) -> &mut Bitswap {
&mut self.bitswap
}
pub fn kademlia(&mut self) -> &mut Kademlia<MemoryStore> {
&mut self.kademlia
}
}
pub async fn build_behaviour(
options: SwarmOptions,
) -> Result<(Behaviour, Option<ClientTransport>), Error> {
Behaviour::new(options).await
}