Skip to content

Commit

Permalink
feat(swarm): enforce creation of Swarm via SwarmBuilder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vnermolaev authored Mar 13, 2023
1 parent 9d05c61 commit 2ec5402
Show file tree
Hide file tree
Showing 28 changed files with 142 additions and 79 deletions.
7 changes: 5 additions & 2 deletions examples/chat-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
use async_std::io;
use futures::{prelude::*, select};
use libp2p::{
gossipsub, identity, mdns, swarm::NetworkBehaviour, swarm::SwarmEvent, PeerId, Swarm,
gossipsub, identity, mdns,
swarm::NetworkBehaviour,
swarm::{SwarmBuilder, SwarmEvent},
PeerId,
};
use std::collections::hash_map::DefaultHasher;
use std::error::Error;
Expand Down Expand Up @@ -104,7 +107,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut swarm = {
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
let behaviour = MyBehaviour { gossipsub, mdns };
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
};

// Read full lines from stdin
Expand Down
6 changes: 3 additions & 3 deletions examples/distributed-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ use libp2p::kad::{
};
use libp2p::{
development_transport, identity, mdns,
swarm::{NetworkBehaviour, SwarmEvent},
PeerId, Swarm,
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
PeerId,
};
use std::error::Error;

Expand Down Expand Up @@ -98,7 +98,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let kademlia = Kademlia::new(local_peer_id, store);
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
let behaviour = MyBehaviour { kademlia, mdns };
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
};

// Read full lines from stdin
Expand Down
7 changes: 4 additions & 3 deletions examples/file-sharing/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use libp2p::{
},
multiaddr::Protocol,
request_response::{self, ProtocolSupport, RequestId, ResponseChannel},
swarm::{ConnectionHandlerUpgrErr, NetworkBehaviour, Swarm, SwarmEvent},
swarm::{ConnectionHandlerUpgrErr, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent},
PeerId,
};

Expand Down Expand Up @@ -47,7 +47,7 @@ pub async fn new(

// Build the Swarm, connecting the lower layer transport logic with the
// higher layer network behaviour logic.
let swarm = Swarm::with_threadpool_executor(
let swarm = SwarmBuilder::with_async_std_executor(
libp2p::development_transport(id_keys).await?,
ComposedBehaviour {
kademlia: Kademlia::new(peer_id, MemoryStore::new(peer_id)),
Expand All @@ -58,7 +58,8 @@ pub async fn new(
),
},
peer_id,
);
)
.build();

let (command_sender, command_receiver) = mpsc::channel(0);
let (event_sender, event_receiver) = mpsc::channel(0);
Expand Down
5 changes: 3 additions & 2 deletions examples/identify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use futures::prelude::*;
use libp2p::{
core::{multiaddr::Multiaddr, upgrade::Version},
identify, identity, noise,
swarm::{Swarm, SwarmEvent},
swarm::{SwarmBuilder, SwarmEvent},
tcp, yamux, PeerId, Transport,
};
use std::error::Error;
Expand All @@ -63,7 +63,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
local_key.public(),
));

let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
let mut swarm =
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build();

// Tell the swarm to listen on all interfaces and a random, OS-assigned
// port.
Expand Down
4 changes: 2 additions & 2 deletions examples/ipfs-kad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use libp2p::kad::record::store::MemoryStore;
use libp2p::kad::{GetClosestPeersError, Kademlia, KademliaConfig, KademliaEvent, QueryResult};
use libp2p::{
development_transport, identity,
swarm::{Swarm, SwarmEvent},
swarm::{SwarmBuilder, SwarmEvent},
PeerId,
};
use std::{env, error::Error, time::Duration};
Expand Down Expand Up @@ -66,7 +66,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
behaviour.add_address(&peer.parse()?, "/dnsaddr/bootstrap.libp2p.io".parse()?);
}

Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
};

// Order Kademlia to search for a peer.
Expand Down
6 changes: 3 additions & 3 deletions examples/ipfs-private/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ use libp2p::{
multiaddr::Protocol,
noise, ping,
pnet::{PnetConfig, PreSharedKey},
swarm::{NetworkBehaviour, SwarmEvent},
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp,
yamux::YamuxConfig,
Multiaddr, PeerId, Swarm, Transport,
Multiaddr, PeerId, Transport,
};
use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration};

Expand Down Expand Up @@ -199,7 +199,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

println!("Subscribing to {gossipsub_topic:?}");
behaviour.gossipsub.subscribe(&gossipsub_topic).unwrap();
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
};

// Reach out to other nodes if specified
Expand Down
6 changes: 4 additions & 2 deletions examples/ping-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
use futures::prelude::*;
use libp2p::{
identity, ping,
swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent},
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
Multiaddr, PeerId,
};
use std::error::Error;
Expand All @@ -56,7 +56,9 @@ async fn main() -> Result<(), Box<dyn Error>> {

let transport = libp2p::development_transport(local_key).await?;

let mut swarm = Swarm::with_async_std_executor(transport, Behaviour::default(), local_peer_id);
let mut swarm =
SwarmBuilder::with_async_std_executor(transport, Behaviour::default(), local_peer_id)
.build();

// Tell the swarm to listen on all interfaces and a random, OS-assigned
// port.
Expand Down
7 changes: 4 additions & 3 deletions examples/rendezvous/src/bin/rzv-discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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());

Expand Down
7 changes: 4 additions & 3 deletions examples/rendezvous/src/bin/rzv-identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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());

Expand Down
7 changes: 4 additions & 3 deletions examples/rendezvous/src/bin/rzv-register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions examples/rendezvous/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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());

Expand Down
9 changes: 5 additions & 4 deletions interop-tests/src/bin/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmEvent};
use libp2p::tls::TlsStream;
use libp2p::websocket::WsConfig;
use libp2p::{
identity, mplex, noise, ping, quic, tcp, tls, webrtc, yamux, InboundUpgradeExt, Multiaddr,
OutboundUpgradeExt, PeerId, Swarm, Transport as _,
identity, mplex, noise, ping, quic, swarm::SwarmBuilder, tcp, tls, webrtc, yamux,
InboundUpgradeExt, Multiaddr, OutboundUpgradeExt, PeerId, Transport as _,
};
use redis::AsyncCommands;

Expand Down Expand Up @@ -78,14 +78,15 @@ async fn main() -> Result<()> {
),
};

let mut swarm = Swarm::with_tokio_executor(
let mut swarm = SwarmBuilder::with_tokio_executor(
boxed_transport,
Behaviour {
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
},
local_peer_id,
);
)
.build();

let mut conn = client.get_async_connection().await?;

Expand Down
10 changes: 4 additions & 6 deletions misc/metrics/examples/metrics/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ use libp2p_identity::PeerId;
use libp2p_metrics::{Metrics, Recorder};
use libp2p_noise as noise;
use libp2p_ping as ping;
use libp2p_swarm::keep_alive;
use libp2p_swarm::NetworkBehaviour;
use libp2p_swarm::Swarm;
use libp2p_swarm::SwarmEvent;
use libp2p_swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use libp2p_yamux as yamux;
use log::info;
Expand All @@ -79,15 +76,16 @@ fn main() -> Result<(), Box<dyn Error>> {
let local_pub_key = local_key.public();
info!("Local peer id: {local_peer_id:?}");

let mut swarm = Swarm::without_executor(
let mut swarm = SwarmBuilder::without_executor(
tcp::async_io::Transport::default()
.upgrade(Version::V1)
.authenticate(noise::NoiseAuthenticated::xx(&local_key)?)
.multiplex(yamux::YamuxConfig::default())
.boxed(),
Behaviour::new(local_pub_key),
local_peer_id,
);
)
.build();

swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

Expand Down
13 changes: 9 additions & 4 deletions misc/multistream-select/tests/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use libp2p_identity as identity;
use libp2p_identity::PeerId;
use libp2p_mplex::MplexConfig;
use libp2p_plaintext::PlainText2Config;
use libp2p_swarm::{dummy, Swarm, SwarmEvent};
use libp2p_swarm::{dummy, SwarmBuilder, SwarmEvent};
use rand::random;
use std::task::Poll;

Expand Down Expand Up @@ -63,9 +63,14 @@ fn transport_upgrade() {
let listen_addr = Multiaddr::from(Protocol::Memory(random::<u64>()));

let mut dialer =
Swarm::with_async_std_executor(dialer_transport, dummy::Behaviour, dialer_id);
let mut listener =
Swarm::with_async_std_executor(listener_transport, dummy::Behaviour, listener_id);
SwarmBuilder::with_async_std_executor(dialer_transport, dummy::Behaviour, dialer_id)
.build();
let mut listener = SwarmBuilder::with_async_std_executor(
listener_transport,
dummy::Behaviour,
listener_id,
)
.build();

listener.listen_on(listen_addr).unwrap();
let (addr_sender, addr_receiver) = oneshot::channel();
Expand Down
5 changes: 3 additions & 2 deletions protocols/autonat/examples/autonat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use libp2p_identify as identify;
use libp2p_identity as identity;
use libp2p_identity::PeerId;
use libp2p_noise as noise;
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use libp2p_yamux as yamux;
use std::error::Error;
Expand Down Expand Up @@ -76,7 +76,8 @@ async fn main() -> Result<(), Box<dyn Error>> {

let behaviour = Behaviour::new(local_key.public());

let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
let mut swarm =
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build();
swarm.listen_on(
Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
Expand Down
5 changes: 3 additions & 2 deletions protocols/autonat/examples/autonat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use libp2p_identify as identify;
use libp2p_identity as identity;
use libp2p_identity::PeerId;
use libp2p_noise as noise;
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use libp2p_yamux as yamux;
use std::error::Error;
Expand Down Expand Up @@ -65,7 +65,8 @@ async fn main() -> Result<(), Box<dyn Error>> {

let behaviour = Behaviour::new(local_key.public());

let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
let mut swarm =
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build();
swarm.listen_on(
Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
Expand Down
Loading

0 comments on commit 2ec5402

Please sign in to comment.