Skip to content

Commit 220f84a

Browse files
authored
swarm/: Enable advanced dialing requests (#2317)
Enable advanced dialing requests both on `Swarm` and via `NetworkBehaviourAction`. Users can now trigger a dial with a specific set of addresses, optionally extended via `NetworkBehaviour::addresses_of_peer`. In addition the whole process is now modelled in a type safe way via the builder pattern. Example of a `NetworkBehaviour` requesting a dial to a specific peer with a set of addresses additionally extended through `NetworkBehaviour::addresses_of_peer`: ```rust NetworkBehaviourAction::Dial { opts: DialOpts::peer_id(peer_id) .condition(PeerCondition::Always) .addresses(addresses) .extend_addresses_through_behaviour() .build(), handler, } ``` Example of a user requesting a dial to an unknown peer with a single address via `Swarm`: ```rust swarm1.dial( DialOpts::unknown_peer_id() .address(addr2.clone()) .build() ) ```
1 parent 144dc12 commit 220f84a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+680
-414
lines changed

Cargo.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ getrandom = "0.2.3" # Explicit dependency to be used in `wasm-bindgen` feature
7272
instant = "0.1.11" # Explicit dependency to be used in `wasm-bindgen` feature
7373
lazy_static = "1.2"
7474
libp2p-core = { version = "0.30.0", path = "core", default-features = false }
75-
libp2p-floodsub = { version = "0.31.0", path = "protocols/floodsub", optional = true }
76-
libp2p-gossipsub = { version = "0.33.0", path = "./protocols/gossipsub", optional = true }
77-
libp2p-identify = { version = "0.31.0", path = "protocols/identify", optional = true }
75+
libp2p-floodsub = { version = "0.32.0", path = "protocols/floodsub", optional = true }
76+
libp2p-gossipsub = { version = "0.34.0", path = "./protocols/gossipsub", optional = true }
77+
libp2p-identify = { version = "0.32.0", path = "protocols/identify", optional = true }
7878
libp2p-kad = { version = "0.33.0", path = "protocols/kad", optional = true }
79-
libp2p-metrics = { version = "0.1.0", path = "misc/metrics", optional = true }
79+
libp2p-metrics = { version = "0.2.0", path = "misc/metrics", optional = true }
8080
libp2p-mplex = { version = "0.30.0", path = "muxers/mplex", optional = true }
8181
libp2p-noise = { version = "0.33.0", path = "transports/noise", optional = true }
82-
libp2p-ping = { version = "0.31.0", path = "protocols/ping", optional = true }
82+
libp2p-ping = { version = "0.32.0", path = "protocols/ping", optional = true }
8383
libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true }
8484
libp2p-pnet = { version = "0.22.0", path = "transports/pnet", optional = true }
85-
libp2p-relay = { version = "0.4.0", path = "protocols/relay", optional = true }
86-
libp2p-rendezvous = { version = "0.1.0", path = "protocols/rendezvous", optional = true }
87-
libp2p-request-response = { version = "0.13.0", path = "protocols/request-response", optional = true }
88-
libp2p-swarm = { version = "0.31.0", path = "swarm" }
89-
libp2p-swarm-derive = { version = "0.25.0", path = "swarm-derive" }
85+
libp2p-relay = { version = "0.5.0", path = "protocols/relay", optional = true }
86+
libp2p-rendezvous = { version = "0.2.0", path = "protocols/rendezvous", optional = true }
87+
libp2p-request-response = { version = "0.14.0", path = "protocols/request-response", optional = true }
88+
libp2p-swarm = { version = "0.32.0", path = "swarm" }
89+
libp2p-swarm-derive = { version = "0.26.0", path = "swarm-derive" }
9090
libp2p-uds = { version = "0.30.0", path = "transports/uds", optional = true }
9191
libp2p-wasm-ext = { version = "0.30.0", path = "transports/wasm-ext", default-features = false, optional = true }
9292
libp2p-yamux = { version = "0.34.0", path = "muxers/yamux", optional = true }
@@ -99,7 +99,7 @@ smallvec = "1.6.1"
9999
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
100100
libp2p-deflate = { version = "0.30.0", path = "transports/deflate", optional = true }
101101
libp2p-dns = { version = "0.30.0", path = "transports/dns", optional = true, default-features = false }
102-
libp2p-mdns = { version = "0.32.0", path = "protocols/mdns", optional = true }
102+
libp2p-mdns = { version = "0.33.0", path = "protocols/mdns", optional = true }
103103
libp2p-tcp = { version = "0.30.0", path = "transports/tcp", default-features = false, optional = true }
104104
libp2p-websocket = { version = "0.32.0", path = "transports/websocket", optional = true }
105105

examples/chat-tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use libp2p::{
4444
mdns::{Mdns, MdnsEvent},
4545
mplex,
4646
noise,
47-
swarm::{NetworkBehaviourEventProcess, SwarmBuilder, SwarmEvent},
47+
swarm::{dial_opts::DialOpts, NetworkBehaviourEventProcess, SwarmBuilder, SwarmEvent},
4848
// `TokioTcpConfig` is available through the `tcp-tokio` feature.
4949
tcp::TokioTcpConfig,
5050
Multiaddr,
@@ -148,7 +148,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
148148
// Reach out to another node if specified
149149
if let Some(to_dial) = std::env::args().nth(1) {
150150
let addr: Multiaddr = to_dial.parse()?;
151-
swarm.dial_addr(addr)?;
151+
swarm.dial(addr)?;
152152
println!("Dialed {:?}", to_dial)
153153
}
154154

examples/chat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
128128
// Reach out to another node if specified
129129
if let Some(to_dial) = std::env::args().nth(1) {
130130
let addr: Multiaddr = to_dial.parse()?;
131-
swarm.dial_addr(addr)?;
131+
swarm.dial(addr)?;
132132
println!("Dialed {:?}", to_dial)
133133
}
134134

examples/file-sharing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ mod network {
525525
.add_address(&peer_id, peer_addr.clone());
526526
match self
527527
.swarm
528-
.dial_addr(peer_addr.with(Protocol::P2p(peer_id.into())))
528+
.dial(peer_addr.with(Protocol::P2p(peer_id.into())))
529529
{
530530
Ok(()) => {
531531
self.pending_dial.insert(peer_id, sender);

examples/gossipsub-chat.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use libp2p::gossipsub::MessageId;
5353
use libp2p::gossipsub::{
5454
GossipsubEvent, GossipsubMessage, IdentTopic as Topic, MessageAuthenticity, ValidationMode,
5555
};
56-
use libp2p::{gossipsub, identity, swarm::SwarmEvent, PeerId};
56+
use libp2p::{gossipsub, identity, swarm::SwarmEvent, Multiaddr, PeerId};
5757
use std::collections::hash_map::DefaultHasher;
5858
use std::hash::{Hash, Hasher};
5959
use std::time::Duration;
@@ -122,14 +122,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
122122

123123
// Reach out to another node if specified
124124
if let Some(to_dial) = std::env::args().nth(1) {
125-
let dialing = to_dial.clone();
126-
match to_dial.parse() {
127-
Ok(to_dial) => match swarm.dial_addr(to_dial) {
128-
Ok(_) => println!("Dialed {:?}", dialing),
129-
Err(e) => println!("Dial {:?} failed: {:?}", dialing, e),
130-
},
131-
Err(err) => println!("Failed to parse address to dial: {:?}", err),
132-
}
125+
let address: Multiaddr = to_dial.parse().expect("User to provide valid address.");
126+
match swarm.dial(address.clone()) {
127+
Ok(_) => println!("Dialed {:?}", address),
128+
Err(e) => println!("Dial {:?} failed: {:?}", address, e),
129+
};
133130
}
134131

135132
// Read full lines from stdin

examples/ipfs-private.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn main() -> Result<(), Box<dyn Error>> {
265265
// Reach out to other nodes if specified
266266
for to_dial in std::env::args().skip(1) {
267267
let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?;
268-
swarm.dial_addr(addr)?;
268+
swarm.dial(addr)?;
269269
println!("Dialed {:?}", to_dial)
270270
}
271271

examples/ping.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
use futures::executor::block_on;
4444
use futures::prelude::*;
4545
use libp2p::swarm::{Swarm, SwarmEvent};
46-
use libp2p::{identity, ping, PeerId};
46+
use libp2p::{identity, ping, Multiaddr, PeerId};
4747
use std::error::Error;
4848
use std::task::Poll;
4949

@@ -70,8 +70,8 @@ fn main() -> Result<(), Box<dyn Error>> {
7070
// Dial the peer identified by the multi-address given as the second
7171
// command-line argument, if any.
7272
if let Some(addr) = std::env::args().nth(1) {
73-
let remote = addr.parse()?;
74-
swarm.dial_addr(remote)?;
73+
let remote: Multiaddr = addr.parse()?;
74+
swarm.dial(remote)?;
7575
println!("Dialed {}", addr)
7676
}
7777

misc/metrics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 0.2.0 [unreleased]
2+
3+
- Update dependencies.
4+
15
## Version 0.1.0 [2021-11-01]
26

37
- Add initial version.

misc/metrics/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "libp2p-metrics"
33
edition = "2018"
44
description = "Metrics for libp2p"
5-
version = "0.1.0"
5+
version = "0.2.0"
66
authors = ["Max Inden <mail@max-inden.de>"]
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"
@@ -15,11 +15,11 @@ kad = ["libp2p-kad"]
1515
ping = ["libp2p-ping"]
1616

1717
[dependencies]
18-
libp2p-core= { version = "0.30.0", path = "../../core" }
19-
libp2p-identify = { version = "0.31.0", path = "../../protocols/identify", optional = true }
20-
libp2p-kad = { version = "0.33.0", path = "../../protocols/kad", optional = true }
21-
libp2p-ping = { version = "0.31.0", path = "../../protocols/ping", optional = true }
22-
libp2p-swarm = { version = "0.31.0", path = "../../swarm" }
18+
libp2p-core = { version = "0.30.0", path = "../../core" }
19+
libp2p-identify = { version = "0.32.0", path = "../../protocols/identify", optional = true }
20+
libp2p-kad = { version = "0.33.0", path = "../../protocols/kad", optional = true }
21+
libp2p-ping = { version = "0.32.0", path = "../../protocols/ping", optional = true }
22+
libp2p-swarm = { version = "0.32.0", path = "../../swarm" }
2323
open-metrics-client = "0.12.0"
2424

2525
[dev-dependencies]

misc/metrics/examples/metrics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
5151
use futures::executor::block_on;
5252
use futures::stream::StreamExt;
53+
use libp2p::core::Multiaddr;
5354
use libp2p::metrics::{Metrics, Recorder};
5455
use libp2p::ping::{Ping, PingConfig};
5556
use libp2p::swarm::SwarmEvent;
@@ -74,8 +75,8 @@ fn main() -> Result<(), Box<dyn Error>> {
7475
);
7576
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
7677
if let Some(addr) = std::env::args().nth(1) {
77-
let remote = addr.parse()?;
78-
swarm.dial_addr(remote)?;
78+
let remote: Multiaddr = addr.parse()?;
79+
swarm.dial(remote)?;
7980
tide::log::info!("Dialed {}", addr)
8081
}
8182

0 commit comments

Comments
 (0)