feat(swarm): enforce creation of Swarm via SwarmBuilder

Mark constructors `Swarm::with_X_executor` as deprecated.
Move the deprecated functionality to `SwarmBuilder::with_X_executor`
Use `SwarmBuilder` throughout.

Resolves #3186.
Resolves #3107.

Pull-Request: #3588.
This commit is contained in:
Victor Ermolaev
2023-03-13 20:53:14 +01:00
committed by GitHub
parent 9d05c619e8
commit 2ec5402474
28 changed files with 142 additions and 79 deletions

View File

@ -24,7 +24,7 @@ use libp2p::{
identity,
multiaddr::Protocol,
noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent},
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::time::Duration;
@ -41,7 +41,7 @@ async fn main() {
.parse()
.unwrap();
let mut swarm = Swarm::with_tokio_executor(
let mut swarm = SwarmBuilder::with_tokio_executor(
tcp::tokio::Transport::default()
.upgrade(Version::V1)
.authenticate(noise::NoiseAuthenticated::xx(&key_pair).unwrap())
@ -53,7 +53,8 @@ async fn main() {
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
);
)
.build();
log::info!("Local peer id: {}", swarm.local_peer_id());

View File

@ -22,7 +22,7 @@ use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identify, identity, noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent},
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::time::Duration;
@ -37,7 +37,7 @@ async fn main() {
.parse()
.unwrap();
let mut swarm = Swarm::with_tokio_executor(
let mut swarm = SwarmBuilder::with_tokio_executor(
tcp::tokio::Transport::default()
.upgrade(Version::V1)
.authenticate(noise::NoiseAuthenticated::xx(&key_pair).unwrap())
@ -53,7 +53,8 @@ async fn main() {
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
);
)
.build();
log::info!("Local peer id: {}", swarm.local_peer_id());

View File

@ -22,7 +22,7 @@ use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identity, noise, ping, rendezvous,
swarm::{keep_alive, AddressScore, NetworkBehaviour, Swarm, SwarmEvent},
swarm::{keep_alive, AddressScore, NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::time::Duration;
@ -37,7 +37,7 @@ async fn main() {
.parse()
.unwrap();
let mut swarm = Swarm::with_tokio_executor(
let mut swarm = SwarmBuilder::with_tokio_executor(
tcp::tokio::Transport::default()
.upgrade(Version::V1)
.authenticate(noise::NoiseAuthenticated::xx(&key_pair).unwrap())
@ -49,7 +49,8 @@ async fn main() {
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
);
)
.build();
// In production the external address should be the publicly facing IP address of the rendezvous point.
// This address is recorded in the registration entry by the rendezvous point.

View File

@ -40,7 +40,7 @@ use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identify, identity, noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent},
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, PeerId, Transport,
};
use std::time::Duration;
@ -51,7 +51,7 @@ async fn main() {
let key_pair = identity::Keypair::generate_ed25519();
let mut swarm = Swarm::with_tokio_executor(
let mut swarm = SwarmBuilder::with_tokio_executor(
tcp::tokio::Transport::default()
.upgrade(Version::V1)
.authenticate(noise::NoiseAuthenticated::xx(&key_pair).unwrap())
@ -67,7 +67,8 @@ async fn main() {
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
);
)
.build();
log::info!("Local peer id: {}", swarm.local_peer_id());