-
Notifications
You must be signed in to change notification settings - Fork 1
/
libp2p-example.txt
735 lines (678 loc) · 26.1 KB
/
libp2p-example.txt
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/ Copyright 2021 Protocol Labs.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! # File sharing example
//!
//! Basic file sharing application with peers either providing or locating and
//! getting files by name.
//!
//! While obviously showcasing how to build a basic file sharing application,
//! the actual goal of this example is **to show how to integrate rust-libp2p
//! into a larger application**.
//!
//! ## Sample plot
//!
//! Assuming there are 3 nodes, A, B and C. A and B each provide a file while C
//! retrieves a file.
//!
//! Provider nodes A and B each provide a file, file FA and FB respectively.
//! They do so by advertising themselves as a provider for their file on a DHT
//! via [`libp2p-kad`]. The two, among other nodes of the network, are
//! interconnected via the DHT.
//!
//! Node C can locate the providers for file FA or FB on the DHT via
//! [`libp2p-kad`] without being connected to the specific node providing the
//! file, but any node of the DHT. Node C then connects to the corresponding
//! node and requests the file content of the file via
//! [`libp2p-request-response`].
//!
//! ## Architectural properties
//!
//! - Clean clonable async/await interface ([`Client`]) to interact with the
//! network layer.
//!
//! - Single task driving the network layer, no locks required.
//!
//! ## Usage
//!
//! A two node setup with one node providing the file and one node requesting the file.
//!
//! 1. Run command below in one terminal.
//!
//! ```
//! cargo run --example file-sharing -- \
//! --listen-address /ip4/127.0.0.1/tcp/40837 \
//! --secret-key-seed 1 \
//! provide \
//! --path <path-to-your-file> \
//! --name <name-for-others-to-find-your-file>
//! ```
//!
//! 2. Run command below in another terminal.
//!
//! ```
//! cargo run --example file-sharing -- \
//! --peer /ip4/127.0.0.1/tcp/40837/p2p/12D3KooWPjceQrSwdWXPyLLeABRXmuqt69Rg3sBYbU1Nft9HyQ6X \
//! get \
//! --name <name-for-others-to-find-your-file>
//! ```
//!
//! Note: The client does not need to be directly connected to the providing
//! peer, as long as both are connected to some node on the same DHT.
use async_std::io;
use async_std::task::spawn;
use futures::prelude::*;
use libp2p::core::{Multiaddr, PeerId};
use libp2p::multiaddr::Protocol;
use std::error::Error;
use std::path::PathBuf;
use structopt::StructOpt;
#[macro_use]
extern crate log;
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
let opt = Opt::from_args();
let (mut network_client, mut network_events, network_event_loop) =
network::new(opt.secret_key_seed).await?;
// Spawn the network task for it to run in the background.
spawn(network_event_loop.run());
// In case a listen address was provided use it, otherwise listen on any
// address.
match opt.listen_address {
Some(addr) => network_client
.start_listening(addr)
.await
.expect("Listening not to fail."),
None => network_client
.start_listening("/ip4/0.0.0.0/tcp/0".parse()?)
.await
.expect("Listening not to fail."),
};
//todo try other methods
// In case the user provided an address of a peer on the CLI, dial it.
if let Some(addr) = opt.peer {
let peer_id = match addr.iter().last() {
Some(Protocol::P2p(hash)) => PeerId::from_multihash(hash).expect("Valid hash."),
_ => return Err("Expect peer multiaddr to contain peer ID.".into()),
};
network_client
.dial(peer_id, addr)
.await
.expect("Dial to succeed");
}
match opt.argument {
// Providing a file.
CliArgument::Provide { path, name } => {
// Advertise oneself as a provider of the file on the DHT.
network_client.start_providing(name.clone()).await;
loop {
match network_events.next().await {
// Reply with the content of the file on incoming requests.
Some(network::Event::InboundRequest { request, channel }) => {
if request == name {
let file_content = std::fs::read_to_string(&path)?;
network_client.respond_file(file_content, channel).await;
}
}
e => todo!("{:?}", e),
}
}
}
// Locating and getting a file.
CliArgument::Get { name } => {
// Locate all nodes providing the file.
let providers = network_client.get_providers(name.clone()).await;
if providers.is_empty() {
return Err(format!("Could not find provider for file {}.", name).into());
}
// Request the content of the file from each node.
let requests = providers.into_iter().map(|p| {
let mut network_client = network_client.clone();
let name = name.clone();
async move { network_client.request_file(p, name).await }.boxed()
});
// Await the requests, ignore the remaining once a single one succeeds.
let file = futures::future::select_ok(requests)
.await
.map_err(|_| "None of the providers returned file.")?
.0;
println!("Content of file {}: {}", name, file);
}
}
Ok(())
}
#[derive(Debug, StructOpt)]
#[structopt(name = "libp2p file sharing example")]
struct Opt {
/// Fixed value to generate deterministic peer ID.
#[structopt(long)]
secret_key_seed: Option<u8>,
#[structopt(long)]
peer: Option<Multiaddr>,
#[structopt(long)]
listen_address: Option<Multiaddr>,
#[structopt(subcommand)]
argument: CliArgument,
}
#[derive(Debug, StructOpt)]
enum CliArgument {
Provide {
#[structopt(long)]
path: PathBuf,
#[structopt(long)]
name: String,
},
Get {
#[structopt(long)]
name: String,
},
}
/// The network module, encapsulating all network related logic.
mod network {
use super::*;
use async_trait::async_trait;
use futures::channel::{mpsc, oneshot};
use libp2p::core::either::EitherError;
use libp2p::core::upgrade::{read_length_prefixed, write_length_prefixed, ProtocolName};
use libp2p::identity;
use libp2p::identity::ed25519;
use libp2p::kad::record::store::MemoryStore;
use libp2p::kad::{GetProvidersOk, Kademlia, KademliaEvent, QueryId, QueryResult};
use libp2p::multiaddr::Protocol;
use libp2p::request_response::{
ProtocolSupport, RequestId, RequestResponse, RequestResponseCodec, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p::swarm::{ConnectionHandlerUpgrErr, SwarmBuilder, SwarmEvent};
use libp2p::{NetworkBehaviour, Swarm};
use std::collections::{HashMap, HashSet};
use std::iter;
/// Creates the network components, namely:
///
/// - The network client to interact with the network layer from anywhere
/// within your application.
///
/// - The network event stream, e.g. for incoming requests.
///
/// - The network task driving the network itself.
pub async fn new(
secret_key_seed: Option<u8>,
) -> Result<(Client, impl Stream<Item = Event>, EventLoop), Box<dyn Error>> {
// Create a public/private key pair, either random or based on a seed.
let id_keys = match secret_key_seed {
Some(seed) => {
let mut bytes = [0u8; 32];
bytes[0] = seed;
let secret_key = ed25519::SecretKey::from_bytes(&mut bytes).expect(
"this returns `Err` only if the length is wrong; the length is correct; qed",
);
identity::Keypair::Ed25519(secret_key.into())
}
None => identity::Keypair::generate_ed25519(),
};
let peer_id = id_keys.public().to_peer_id();
// Build the Swarm, connecting the lower layer transport logic with the
// higher layer network behaviour logic.
let swarm = SwarmBuilder::new(
libp2p::development_transport(id_keys).await?,
ComposedBehaviour {
kademlia: Kademlia::new(peer_id, MemoryStore::new(peer_id)),
request_response: RequestResponse::new(
FileExchangeCodec(),
iter::once((FileExchangeProtocol(), ProtocolSupport::Full)),
Default::default(),
),
},
peer_id,
)
.build();
let (command_sender, command_receiver) = mpsc::channel(0);
let (event_sender, event_receiver) = mpsc::channel(0);
Ok((
Client {
sender: command_sender,
},
event_receiver,
EventLoop::new(swarm, command_receiver, event_sender),
))
}
#[derive(Clone)]
pub struct Client {
sender: mpsc::Sender<Command>,
}
impl Client {
/// Listen for incoming connections on the given address.
pub async fn start_listening(
&mut self,
addr: Multiaddr,
) -> Result<(), Box<dyn Error + Send>> {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::StartListening { addr, sender })
.await
.expect("Command receiver not to be dropped.");
receiver.await.expect("Sender not to be dropped.")
}
/// Dial the given peer at the given address.
pub async fn dial(
&mut self,
peer_id: PeerId,
peer_addr: Multiaddr,
) -> Result<(), Box<dyn Error + Send>> {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::Dial {
peer_id,
peer_addr,
sender,
})
.await
.expect("Command receiver not to be dropped.");
receiver.await.expect("Sender not to be dropped.")
}
/// Advertise the local node as the provider of the given file on the DHT.
pub async fn start_providing(&mut self, file_name: String) {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::StartProviding { file_name, sender })
.await
.expect("Command receiver not to be dropped.");
receiver.await.expect("Sender not to be dropped.");
}
/// Find the providers for the given file on the DHT.
pub async fn get_providers(&mut self, file_name: String) -> HashSet<PeerId> {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::GetProviders { file_name, sender })
.await
.expect("Command receiver not to be dropped.");
receiver.await.expect("Sender not to be dropped.")
}
/// Request the content of the given file from the given peer.
pub async fn request_file(
&mut self,
peer: PeerId,
file_name: String,
) -> Result<String, Box<dyn Error + Send>> {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::RequestFile {
file_name,
peer,
sender,
})
.await
.expect("Command receiver not to be dropped.");
receiver.await.expect("Sender not be dropped.")
}
/// Respond with the provided file content to the given request.
pub async fn respond_file(&mut self, file: String, channel: ResponseChannel<FileResponse>) {
self.sender
.send(Command::RespondFile { file, channel })
.await
.expect("Command receiver not to be dropped.");
}
}
pub struct EventLoop {
swarm: Swarm<ComposedBehaviour>,
command_receiver: mpsc::Receiver<Command>,
event_sender: mpsc::Sender<Event>,
pending_dial: HashMap<PeerId, oneshot::Sender<Result<(), Box<dyn Error + Send>>>>,
pending_start_providing: HashMap<QueryId, oneshot::Sender<()>>,
pending_get_providers: HashMap<QueryId, oneshot::Sender<HashSet<PeerId>>>,
pending_request_file:
HashMap<RequestId, oneshot::Sender<Result<String, Box<dyn Error + Send>>>>,
}
impl EventLoop {
fn new(
swarm: Swarm<ComposedBehaviour>,
command_receiver: mpsc::Receiver<Command>,
event_sender: mpsc::Sender<Event>,
) -> Self {
Self {
swarm,
command_receiver,
event_sender,
pending_dial: Default::default(),
pending_start_providing: Default::default(),
pending_get_providers: Default::default(),
pending_request_file: Default::default(),
}
}
pub async fn run(mut self) {
loop {
futures::select! {
event = self.swarm.next() => self.handle_event(event.expect("Swarm stream to be infinite.")).await ,
command = self.command_receiver.next() => match command {
Some(c) => self.handle_command(c).await,
// Command channel closed, thus shutting down the network event loop.
None=> return,
},
}
}
}
async fn handle_event(
&mut self,
event: SwarmEvent<
ComposedEvent,
EitherError<ConnectionHandlerUpgrErr<io::Error>, io::Error>,
>,
) {
match event {
SwarmEvent::Behaviour(ComposedEvent::Kademlia(
KademliaEvent::OutboundQueryCompleted {
id,
result: QueryResult::StartProviding(_),
..
},
)) => {
let sender: oneshot::Sender<()> = self
.pending_start_providing
.remove(&id)
.expect("Completed query to be previously pending.");
let _ = sender.send(());
}
SwarmEvent::Behaviour(ComposedEvent::Kademlia(
KademliaEvent::OutboundQueryCompleted {
id,
result: QueryResult::GetProviders(Ok(GetProvidersOk { providers, .. })),
..
},
)) => {
let _ = self
.pending_get_providers
.remove(&id)
.expect("Completed query to be previously pending.")
.send(providers);
}
SwarmEvent::Behaviour(ComposedEvent::Kademlia(_)) => {}
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
RequestResponseEvent::Message { message, .. },
)) => match message {
RequestResponseMessage::Request {
request, channel, ..
} => {
self.event_sender
.send(Event::InboundRequest {
request: request.0,
channel,
})
.await
.expect("Event receiver not to be dropped.");
}
RequestResponseMessage::Response {
request_id,
response,
} => {
let _ = self
.pending_request_file
.remove(&request_id)
.expect("Request to still be pending.")
.send(Ok(response.0));
}
},
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
RequestResponseEvent::OutboundFailure {
request_id, error, ..
},
)) => {
let _ = self
.pending_request_file
.remove(&request_id)
.expect("Request to still be pending.")
.send(Err(Box::new(error)));
}
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
RequestResponseEvent::ResponseSent { .. },
)) => {}
SwarmEvent::NewListenAddr { address, .. } => {
let local_peer_id = *self.swarm.local_peer_id();
println!(
"Local node is listening on {:?}",
address.with(Protocol::P2p(local_peer_id.into()))
);
}
SwarmEvent::IncomingConnection { .. } => {}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
if endpoint.is_dialer() {
if let Some(sender) = self.pending_dial.remove(&peer_id) {
let _ = sender.send(Ok(()));
}
}
}
SwarmEvent::ConnectionClosed { .. } => {}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
if let Some(peer_id) = peer_id {
if let Some(sender) = self.pending_dial.remove(&peer_id) {
let _ = sender.send(Err(Box::new(error)));
}
}
}
SwarmEvent::IncomingConnectionError { .. } => {}
SwarmEvent::Dialing(peer_id) => println!("Dialing {}", peer_id),
e => panic!("{:?}", e),
}
}
async fn handle_command(&mut self, command: Command) {
match command {
Command::StartListening { addr, sender } => {
let _ = match self.swarm.listen_on(addr) {
Ok(_) => sender.send(Ok(())),
Err(e) => sender.send(Err(Box::new(e))),
};
}
Command::Dial {
peer_id,
peer_addr,
sender,
} => {
if self.pending_dial.contains_key(&peer_id) {
todo!("Already dialing peer.");
} else {
self.swarm
.behaviour_mut()
.kademlia
.add_address(&peer_id, peer_addr.clone());
match self
.swarm
.dial(peer_addr.with(Protocol::P2p(peer_id.into())))
{
Ok(()) => {
self.pending_dial.insert(peer_id, sender);
}
Err(e) => {
let _ = sender.send(Err(Box::new(e)));
}
}
}
}
Command::StartProviding { file_name, sender } => {
let query_id = self
.swarm
.behaviour_mut()
.kademlia
.start_providing(file_name.into_bytes().into())
.expect("No store error.");
self.pending_start_providing.insert(query_id, sender);
}
Command::GetProviders { file_name, sender } => {
let query_id = self
.swarm
.behaviour_mut()
.kademlia
.get_providers(file_name.into_bytes().into());
self.pending_get_providers.insert(query_id, sender);
}
Command::RequestFile {
file_name,
peer,
sender,
} => {
let request_id = self
.swarm
.behaviour_mut()
.request_response
.send_request(&peer, FileRequest(file_name));
self.pending_request_file.insert(request_id, sender);
}
Command::RespondFile { file, channel } => {
self.swarm
.behaviour_mut()
.request_response
.send_response(channel, FileResponse(file))
.expect("Connection to peer to be still open.");
}
}
}
}
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "ComposedEvent")]
struct ComposedBehaviour {
request_response: RequestResponse<FileExchangeCodec>,
kademlia: Kademlia<MemoryStore>,
}
#[derive(Debug)]
enum ComposedEvent {
RequestResponse(RequestResponseEvent<FileRequest, FileResponse>),
Kademlia(KademliaEvent),
}
impl From<RequestResponseEvent<FileRequest, FileResponse>> for ComposedEvent {
fn from(event: RequestResponseEvent<FileRequest, FileResponse>) -> Self {
ComposedEvent::RequestResponse(event)
}
}
impl From<KademliaEvent> for ComposedEvent {
fn from(event: KademliaEvent) -> Self {
ComposedEvent::Kademlia(event)
}
}
#[derive(Debug)]
enum Command {
StartListening {
addr: Multiaddr,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
},
Dial {
peer_id: PeerId,
peer_addr: Multiaddr,
sender: oneshot::Sender<Result<(), Box<dyn Error + Send>>>,
},
StartProviding {
file_name: String,
sender: oneshot::Sender<()>,
},
GetProviders {
file_name: String,
sender: oneshot::Sender<HashSet<PeerId>>,
},
RequestFile {
file_name: String,
peer: PeerId,
sender: oneshot::Sender<Result<String, Box<dyn Error + Send>>>,
},
RespondFile {
file: String,
channel: ResponseChannel<FileResponse>,
},
}
#[derive(Debug)]
pub enum Event {
InboundRequest {
request: String,
channel: ResponseChannel<FileResponse>,
},
}
// Simple file exchange protocol
#[derive(Debug, Clone)]
struct FileExchangeProtocol();
#[derive(Clone)]
struct FileExchangeCodec();
#[derive(Debug, Clone, PartialEq, Eq)]
struct FileRequest(String);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileResponse(String);
impl ProtocolName for FileExchangeProtocol {
fn protocol_name(&self) -> &[u8] {
"/file-exchange/1".as_bytes()
}
}
#[async_trait]
impl RequestResponseCodec for FileExchangeCodec {
type Protocol = FileExchangeProtocol;
type Request = FileRequest;
type Response = FileResponse;
async fn read_request<T>(
&mut self,
_: &FileExchangeProtocol,
io: &mut T,
) -> io::Result<Self::Request>
where
T: AsyncRead + Unpin + Send,
{
let vec = read_length_prefixed(io, 1_000_000).await?;
if vec.is_empty() {
return Err(io::ErrorKind::UnexpectedEof.into());
}
Ok(FileRequest(String::from_utf8(vec).unwrap()))
}
async fn read_response<T>(
&mut self,
_: &FileExchangeProtocol,
io: &mut T,
) -> io::Result<Self::Response>
where
T: AsyncRead + Unpin + Send,
{
let vec = read_length_prefixed(io, 1_000_000).await?;
if vec.is_empty() {
return Err(io::ErrorKind::UnexpectedEof.into());
}
Ok(FileResponse(String::from_utf8(vec).unwrap()))
}
async fn write_request<T>(
&mut self,
_: &FileExchangeProtocol,
io: &mut T,
FileRequest(data): FileRequest,
) -> io::Result<()>
where
T: AsyncWrite + Unpin + Send,
{
write_length_prefixed(io, data).await?;
io.close().await?;
Ok(())
}
async fn write_response<T>(
&mut self,
_: &FileExchangeProtocol,
io: &mut T,
FileResponse(data): FileResponse,
) -> io::Result<()>
where
T: AsyncWrite + Unpin + Send,
{
write_length_prefixed(io, data).await?;
io.close().await?;
Ok(())
}
}
}