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
use crate::{
executor::{Executor, JoinHandle},
net::behaviour::{GetChannel, NetworkBackendBehaviour, SyncChannel},
};
use futures::stream::{Stream, StreamExt};
use futures::task::AtomicWaker;
use futures::{channel::mpsc, future, pin_mut};
use libipld::error::BlockNotFound;
use libipld::store::StoreParams;
use libipld::{Cid, Result};
use libp2p::core::either::EitherTransport;
use libp2p::core::transport::Transport;
use libp2p::core::upgrade::{SelectUpgrade, Version};
#[cfg(feature = "async_global")]
use libp2p::dns::DnsConfig as Dns;
#[cfg(all(feature = "tokio", not(feature = "async_global")))]
use libp2p::dns::TokioDnsConfig as Dns;
use libp2p::kad::kbucket::Key as BucketKey;
use libp2p::mplex::MplexConfig;
use libp2p::noise::{self, NoiseConfig, X25519Spec};
use libp2p::pnet::{PnetConfig, PreSharedKey};
use libp2p::swarm::{AddressScore, Swarm, SwarmBuilder};
#[cfg(feature = "async_global")]
use libp2p::tcp::TcpConfig;
#[cfg(all(feature = "tokio", not(feature = "async_global")))]
use libp2p::tcp::TokioTcpConfig as TcpConfig;
use libp2p::yamux::YamuxConfig;
use parking_lot::Mutex;
use prometheus::Registry;
use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
mod behaviour;
mod config;
mod p2p_wrapper;
mod peers;
pub use crate::net::behaviour::{QueryId, SyncEvent};
pub use crate::net::config::*;
pub use crate::net::peers::{AddressSource, Event, PeerInfo};
pub use libp2p::core::connection::ListenerId;
pub use libp2p::kad::record::{Key, Record};
pub use libp2p::kad::{PeerRecord, Quorum};
pub use libp2p::swarm::AddressRecord;
pub use libp2p::{Multiaddr, PeerId, TransportError};
pub use libp2p_bitswap::BitswapStore;
pub use libp2p_quic::{generate_keypair, ToLibp2p};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ListenerEvent {
NewListenAddr(Multiaddr),
ExpiredListenAddr(Multiaddr),
}
#[derive(Clone)]
pub struct NetworkService<P: StoreParams> {
executor: Executor,
swarm: Arc<Mutex<Swarm<NetworkBackendBehaviour<P>>>>,
waker: Arc<AtomicWaker>,
_swarm_task: Arc<JoinHandle<()>>,
}
impl<P: StoreParams> NetworkService<P> {
pub async fn new<S: BitswapStore<Params = P>>(
mut config: NetworkConfig,
store: S,
executor: Executor,
) -> Result<Self> {
let peer_id = config.node_key.to_peer_id();
let behaviour = NetworkBackendBehaviour::<P>::new(&mut config, store).await?;
let tcp = {
let transport = TcpConfig::new().nodelay(true).port_reuse(true);
let transport = if let Some(psk) = config.psk {
let psk = PreSharedKey::new(psk);
EitherTransport::Left(
transport.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)),
)
} else {
EitherTransport::Right(transport)
};
let dh_key = noise::Keypair::<X25519Spec>::new()
.into_authentic(&config.node_key.to_keypair())
.unwrap();
let transport = transport
.upgrade(Version::V1)
.authenticate(NoiseConfig::xx(dh_key).into_authenticated())
.multiplex(SelectUpgrade::new(
YamuxConfig::default(),
MplexConfig::new(),
))
.timeout(Duration::from_secs(5))
.boxed();
p2p_wrapper::P2pWrapper(transport)
};
let quic_or_tcp = tcp;
#[cfg(feature = "async_global")]
let transport = if let Some(config) = config.dns {
Dns::custom(quic_or_tcp, config.config, config.opts)
.await?
.boxed()
} else {
Dns::system(quic_or_tcp).await?.boxed()
};
#[cfg(all(feature = "tokio", not(feature = "async_global")))]
let transport = if let Some(config) = config.dns {
Dns::custom(quic_or_tcp, config.config, config.opts)?.boxed()
} else {
Dns::system(quic_or_tcp)?.boxed()
};
let exec = executor.clone();
let swarm = SwarmBuilder::new(transport, behaviour, peer_id)
.executor(Box::new(move |fut| {
exec.spawn(fut).detach();
}))
.build();
let swarm = Arc::new(Mutex::new(swarm));
let swarm2 = swarm.clone();
let waker = Arc::new(AtomicWaker::new());
let waker2 = waker.clone();
let swarm_task = executor.spawn(future::poll_fn(move |cx| {
waker.register(cx.waker());
let mut guard = swarm.lock();
while {
let swarm = &mut *guard;
pin_mut!(swarm);
swarm.poll_next(cx).is_ready()
} {}
Poll::Pending
}));
Ok(Self {
executor,
swarm: swarm2,
waker: waker2,
_swarm_task: Arc::new(swarm_task),
})
}
pub fn local_peer_id(&self) -> PeerId {
let swarm = self.swarm.lock();
*swarm.local_peer_id()
}
pub fn local_node_name(&self) -> String {
let swarm = self.swarm.lock();
swarm.behaviour().local_node_name().to_string()
}
pub fn listen_on(&self, addr: Multiaddr) -> Result<impl Stream<Item = ListenerEvent>> {
let mut swarm = self.swarm.lock();
let stream = swarm.behaviour_mut().event_stream();
let listener = swarm.listen_on(addr)?;
self.waker.wake();
Ok(stream
.take_while(move |event| match event {
Event::ListenerClosed(id) if *id == listener => future::ready(false),
_ => future::ready(true),
})
.filter_map(move |event| match event {
Event::NewListenAddr(id, addr) if id == listener => {
future::ready(Some(ListenerEvent::NewListenAddr(addr)))
}
Event::ExpiredListenAddr(id, addr) if id == listener => {
future::ready(Some(ListenerEvent::ExpiredListenAddr(addr)))
}
_ => future::ready(None),
}))
}
pub fn listeners(&self) -> Vec<Multiaddr> {
let swarm = self.swarm.lock();
swarm.listeners().cloned().collect()
}
pub fn add_external_address(&self, mut addr: Multiaddr) {
crate::net::peers::normalize_addr(&mut addr, &self.local_peer_id());
let mut swarm = self.swarm.lock();
swarm.add_external_address(addr, AddressScore::Infinite);
self.waker.wake();
}
pub fn external_addresses(&self) -> Vec<AddressRecord> {
let swarm = self.swarm.lock();
swarm.external_addresses().cloned().collect()
}
pub fn add_address(&self, peer: &PeerId, addr: Multiaddr) {
let mut swarm = self.swarm.lock();
swarm
.behaviour_mut()
.add_address(peer, addr, AddressSource::User);
self.waker.wake();
}
pub fn remove_address(&self, peer: &PeerId, addr: &Multiaddr) {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().remove_address(peer, addr);
self.waker.wake();
}
pub fn dial(&self, peer: &PeerId) {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().dial(peer);
self.waker.wake();
}
pub fn ban(&self, peer: PeerId) {
let mut swarm = self.swarm.lock();
swarm.ban_peer_id(peer);
self.waker.wake();
}
pub fn unban(&self, peer: PeerId) {
let mut swarm = self.swarm.lock();
swarm.unban_peer_id(peer);
self.waker.wake();
}
pub fn peers(&self) -> Vec<PeerId> {
let swarm = self.swarm.lock();
swarm.behaviour().peers().copied().collect()
}
pub fn connections(&self) -> Vec<(PeerId, Multiaddr)> {
let swarm = self.swarm.lock();
swarm
.behaviour()
.connections()
.map(|(peer_id, addr)| (*peer_id, addr.clone()))
.collect()
}
pub fn is_connected(&self, peer: &PeerId) -> bool {
let swarm = self.swarm.lock();
swarm.behaviour().is_connected(peer)
}
pub fn peer_info(&self, peer: &PeerId) -> Option<PeerInfo> {
let swarm = self.swarm.lock();
swarm.behaviour().info(peer).cloned()
}
pub async fn bootstrap(&self, peers: &[(PeerId, Multiaddr)]) -> Result<()> {
for (peer, addr) in peers {
self.add_address(peer, addr.clone());
self.dial(peer);
}
let rx = {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().bootstrap()
};
tracing::trace!("started bootstrap");
rx.await??;
tracing::trace!("boostrap complete");
Ok(())
}
pub async fn get_closest_peers<K>(&self, key: K) -> Result<Vec<PeerId>>
where
K: Into<BucketKey<K>> + Into<Vec<u8>> + Clone,
{
let rx = {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().get_closest_peers(key)
};
Ok(rx.await??)
}
pub async fn providers(&self, key: Key) -> Result<HashSet<PeerId>> {
let rx = {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().providers(key)
};
Ok(rx.await??)
}
pub async fn provide(&self, key: Key) -> Result<()> {
let rx = {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().provide(key)
};
Ok(rx.await??)
}
pub fn unprovide(&self, key: &Key) {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().unprovide(key);
self.waker.wake();
}
pub async fn get_record(&self, key: &Key, quorum: Quorum) -> Result<Vec<PeerRecord>> {
let rx = {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().get_record(key, quorum)
};
Ok(rx.await??)
}
pub async fn put_record(&self, record: Record, quorum: Quorum) -> Result<()> {
let rx = {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().put_record(record, quorum)
};
rx.await??;
Ok(())
}
pub fn remove_record(&self, key: &Key) {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().remove_record(key);
self.waker.wake();
}
pub fn subscribe(&self, topic: &str) -> Result<impl Stream<Item = Vec<u8>>> {
let mut swarm = self.swarm.lock();
let stream = swarm.behaviour_mut().subscribe(topic)?;
self.waker.wake();
Ok(stream)
}
pub fn publish(&self, topic: &str, msg: Vec<u8>) -> Result<()> {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().publish(topic, msg)?;
self.waker.wake();
Ok(())
}
pub fn broadcast(&self, topic: &str, msg: Vec<u8>) -> Result<()> {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().broadcast(topic, msg)?;
self.waker.wake();
Ok(())
}
pub fn get(&self, cid: Cid, providers: impl Iterator<Item = PeerId>) -> GetQuery<P> {
let mut swarm = self.swarm.lock();
let (rx, id) = swarm.behaviour_mut().get(cid, providers);
self.waker.wake();
GetQuery {
swarm: Some(self.swarm.clone()),
id,
rx,
}
}
pub fn sync(&self, cid: Cid, providers: Vec<PeerId>, missing: Vec<Cid>) -> SyncQuery<P> {
if missing.is_empty() {
return SyncQuery::ready(Ok(()));
}
if providers.is_empty() {
return SyncQuery::ready(Err(BlockNotFound(missing[0]).into()));
}
let mut swarm = self.swarm.lock();
let (rx, id) = swarm
.behaviour_mut()
.sync(cid, providers, missing.into_iter());
self.waker.wake();
SyncQuery {
swarm: Some(self.swarm.clone()),
id: Some(id),
rx,
}
}
pub fn register_metrics(&self, registry: &Registry) -> Result<()> {
let swarm = self.swarm.lock();
swarm.behaviour().register_metrics(registry)
}
pub fn event_stream(&self) -> impl Stream<Item = Event> {
let mut swarm = self.swarm.lock();
swarm.behaviour_mut().event_stream()
}
}
pub struct GetQuery<P: StoreParams> {
swarm: Option<Arc<Mutex<Swarm<NetworkBackendBehaviour<P>>>>>,
id: QueryId,
rx: GetChannel,
}
impl<P: StoreParams> Future for GetQuery<P> {
type Output = Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match Pin::new(&mut self.rx).poll(cx) {
Poll::Ready(Ok(result)) => Poll::Ready(result),
Poll::Ready(Err(err)) => Poll::Ready(Err(err.into())),
Poll::Pending => Poll::Pending,
}
}
}
impl<P: StoreParams> Drop for GetQuery<P> {
fn drop(&mut self) {
let swarm = self.swarm.take().unwrap();
let mut swarm = swarm.lock();
swarm.behaviour_mut().cancel(self.id);
}
}
pub struct SyncQuery<P: StoreParams> {
swarm: Option<Arc<Mutex<Swarm<NetworkBackendBehaviour<P>>>>>,
id: Option<QueryId>,
rx: SyncChannel,
}
impl<P: StoreParams> SyncQuery<P> {
fn ready(res: Result<()>) -> Self {
let (tx, rx) = mpsc::unbounded();
tx.unbounded_send(SyncEvent::Complete(res)).unwrap();
Self {
swarm: None,
id: None,
rx,
}
}
}
impl<P: StoreParams> Future for SyncQuery<P> {
type Output = Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
loop {
match Pin::new(&mut self.rx).poll_next(cx) {
Poll::Ready(Some(SyncEvent::Complete(result))) => return Poll::Ready(result),
Poll::Ready(_) => continue,
Poll::Pending => return Poll::Pending,
}
}
}
}
impl<P: StoreParams> Stream for SyncQuery<P> {
type Item = SyncEvent;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.rx).poll_next(cx)
}
}
impl<P: StoreParams> Drop for SyncQuery<P> {
fn drop(&mut self) {
if let Some(id) = self.id.take() {
let swarm = self.swarm.take().unwrap();
let mut swarm = swarm.lock();
swarm.behaviour_mut().cancel(id);
}
}
}