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

@ -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;
@ -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

View File

@ -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;
@ -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

View File

@ -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,
};
@ -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)),
@ -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);

View File

@ -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;
@ -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.

View File

@ -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};
@ -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.

View File

@ -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};
@ -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

View File

@ -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;
@ -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.

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());

View File

@ -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;
@ -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?;

View File

@ -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;
@ -79,7 +76,7 @@ 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)?)
@ -87,7 +84,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.boxed(),
Behaviour::new(local_pub_key),
local_peer_id,
);
)
.build();
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

View File

@ -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;
@ -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();

View File

@ -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;
@ -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))

View File

@ -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;
@ -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))

View File

@ -26,7 +26,7 @@ use libp2p_identity as identity;
use libp2p_identity::PeerId;
use libp2p_plaintext::PlainText2Config;
use libp2p_relay as relay;
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent};
use libp2p_swarm_test::SwarmExt as _;
use std::time::Duration;
@ -124,7 +124,7 @@ fn build_client() -> Swarm<Client> {
.multiplex(libp2p_yamux::YamuxConfig::default())
.boxed();
Swarm::without_executor(
SwarmBuilder::without_executor(
transport,
Client {
relay: behaviour,
@ -132,6 +132,7 @@ fn build_client() -> Swarm<Client> {
},
local_peer_id,
)
.build()
}
#[derive(NetworkBehaviour)]

View File

@ -566,7 +566,7 @@ mod tests {
use libp2p_identity::PeerId;
use libp2p_mplex::MplexConfig;
use libp2p_noise as noise;
use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_swarm::{Swarm, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use std::time::Duration;
@ -594,7 +594,9 @@ mod tests {
let protocol = Behaviour::new(
Config::new("a".to_string(), pubkey.clone()).with_agent_version("b".to_string()),
);
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
let swarm =
SwarmBuilder::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
.build();
(swarm, pubkey)
};
@ -603,7 +605,9 @@ mod tests {
let protocol = Behaviour::new(
Config::new("c".to_string(), pubkey.clone()).with_agent_version("d".to_string()),
);
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
let swarm =
SwarmBuilder::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
.build();
(swarm, pubkey)
};
@ -671,7 +675,9 @@ mod tests {
let (mut swarm1, pubkey1) = {
let (pubkey, transport) = transport();
let protocol = Behaviour::new(Config::new("a".to_string(), pubkey.clone()));
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
let swarm =
SwarmBuilder::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
.build();
(swarm, pubkey)
};
@ -680,7 +686,9 @@ mod tests {
let protocol = Behaviour::new(
Config::new("a".to_string(), pubkey.clone()).with_agent_version("b".to_string()),
);
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
let swarm =
SwarmBuilder::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
.build();
(swarm, pubkey)
};
@ -752,7 +760,7 @@ mod tests {
.with_initial_delay(Duration::from_secs(10)),
);
Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
SwarmBuilder::with_async_std_executor(transport, protocol, pubkey.to_peer_id()).build()
};
let mut swarm2 = {
@ -761,7 +769,7 @@ mod tests {
Config::new("a".to_string(), pubkey.clone()).with_agent_version("b".to_string()),
);
Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
SwarmBuilder::with_async_std_executor(transport, protocol, pubkey.to_peer_id()).build()
};
let swarm1_peer_id = *swarm1.local_peer_id();

View File

@ -37,7 +37,7 @@ use libp2p_core::{
use libp2p_identity as identity;
use libp2p_identity::PeerId;
use libp2p_noise as noise;
use libp2p_swarm::{ConnectionId, Swarm, SwarmEvent};
use libp2p_swarm::{ConnectionId, Swarm, SwarmBuilder, SwarmEvent};
use libp2p_yamux as yamux;
use quickcheck::*;
use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng};
@ -67,7 +67,7 @@ fn build_node_with_config(cfg: KademliaConfig) -> (Multiaddr, TestSwarm) {
let store = MemoryStore::new(local_id);
let behaviour = Kademlia::with_config(local_id, store, cfg);
let mut swarm = Swarm::without_executor(transport, behaviour, local_id);
let mut swarm = SwarmBuilder::without_executor(transport, behaviour, local_id).build();
let address: Multiaddr = Protocol::Memory(random::<u64>()).into();
swarm.listen_on(address.clone()).unwrap();

View File

@ -18,7 +18,7 @@ futures = "0.3.27"
futures-timer = "3"
instant = "0.1.11"
libp2p-core = { version = "0.39.0", path = "../../core" }
libp2p-swarm = { version = "0.42.0", path = "../../swarm" }
libp2p-swarm = { version = "0.42.0", path = "../../swarm", features = ["async-std"] }
libp2p-identity = { version = "0.1.0", path = "../../identity" }
log = "0.4"
quick-protobuf = "0.8"

View File

@ -31,7 +31,7 @@ use libp2p_identity::PeerId;
use libp2p_noise as noise;
use libp2p_ping as ping;
use libp2p_relay as relay;
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use std::error::Error;
use std::net::{Ipv4Addr, Ipv6Addr};
@ -67,7 +67,7 @@ fn main() -> Result<(), Box<dyn Error>> {
)),
};
let mut swarm = Swarm::without_executor(transport, behaviour, local_peer_id);
let mut swarm = SwarmBuilder::without_executor(transport, behaviour, local_peer_id).build();
// Listen on all interfaces
let listen_addr = Multiaddr::empty()

View File

@ -34,7 +34,7 @@ use libp2p_identity::PublicKey;
use libp2p_ping as ping;
use libp2p_plaintext::PlainText2Config;
use libp2p_relay as relay;
use libp2p_swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent};
use std::time::Duration;
#[test]
@ -292,7 +292,7 @@ fn build_relay() -> Swarm<Relay> {
let transport = upgrade_transport(MemoryTransport::default().boxed(), local_public_key);
Swarm::with_threadpool_executor(
SwarmBuilder::with_async_std_executor(
transport,
Relay {
ping: ping::Behaviour::new(ping::Config::new()),
@ -306,6 +306,7 @@ fn build_relay() -> Swarm<Relay> {
},
local_peer_id,
)
.build()
}
fn build_client() -> Swarm<Client> {
@ -319,7 +320,7 @@ fn build_client() -> Swarm<Client> {
local_public_key,
);
Swarm::with_threadpool_executor(
SwarmBuilder::with_async_std_executor(
transport,
Client {
ping: ping::Behaviour::new(ping::Config::new()),
@ -327,6 +328,7 @@ fn build_client() -> Swarm<Client> {
},
local_peer_id,
)
.build()
}
fn upgrade_transport<StreamSink>(

View File

@ -28,7 +28,8 @@ use libp2p_core::{
use libp2p_identity::PeerId;
use libp2p_plaintext::PlainText2Config;
use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, NetworkBehaviour, Swarm, SwarmEvent, THandlerErr,
dial_opts::DialOpts, AddressScore, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent,
THandlerErr,
};
use libp2p_yamux::YamuxConfig;
use std::fmt::Debug;
@ -218,7 +219,7 @@ where
.timeout(Duration::from_secs(20))
.boxed();
Swarm::without_executor(transport, behaviour_fn(identity), peer_id)
SwarmBuilder::without_executor(transport, behaviour_fn(identity), peer_id).build()
}
async fn connect<T>(&mut self, other: &mut Swarm<T>)

View File

@ -87,6 +87,9 @@
- Remove `ConnectionId::new`. Manually creating `ConnectionId`s is now unsupported. See [PR 3327].
- Deprecate methods `Swarm::with_executor`, `Swarm::with_*_executor`, `Swarm::without_executor`.
Introduce similar methods in `SwarmBuilder`. See [PR 3588].
[PR 3364]: https://github.com/libp2p/rust-libp2p/pull/3364
[PR 3170]: https://github.com/libp2p/rust-libp2p/pull/3170
[PR 3134]: https://github.com/libp2p/rust-libp2p/pull/3134
@ -102,6 +105,7 @@
[PR 3375]: https://github.com/libp2p/rust-libp2p/pull/3375
[PR 3254]: https://github.com/libp2p/rust-libp2p/pull/3254
[PR 3497]: https://github.com/libp2p/rust-libp2p/pull/3497
[PR 3588]: https://github.com/libp2p/rust-libp2p/pull/3588
# 0.41.1

View File

@ -350,11 +350,13 @@ where
impl<TBehaviour> Unpin for Swarm<TBehaviour> where TBehaviour: NetworkBehaviour {}
#[allow(deprecated)]
impl<TBehaviour> Swarm<TBehaviour>
where
TBehaviour: NetworkBehaviour,
{
/// Builds a new `Swarm` with a provided executor.
#[deprecated(note = "Use `SwarmBuilder::with_executor` instead.")]
pub fn with_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
@ -369,6 +371,7 @@ where
feature = "tokio",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[deprecated(note = "Use `SwarmBuilder::with_tokio_executor` instead.")]
pub fn with_tokio_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
@ -387,6 +390,7 @@ where
feature = "async-std",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[deprecated(note = "Use `SwarmBuilder::with_async_std_executor` instead.")]
pub fn with_async_std_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
@ -401,6 +405,9 @@ where
}
/// Builds a new `Swarm` with a threadpool executor.
#[deprecated(
note = "The `futures::executor::ThreadPool` executor is deprecated. See https://github.com/libp2p/rust-libp2p/issues/3107."
)]
pub fn with_threadpool_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
@ -429,6 +436,7 @@ where
/// }
/// ```
#[cfg(feature = "wasm-bindgen")]
#[deprecated(note = "Use `SwarmBuilder::with_wasm_executor` instead.")]
pub fn with_wasm_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
@ -448,6 +456,7 @@ where
/// All connections will be polled on the current task, thus quite bad performance
/// characteristics should be expected. Whenever possible use an executor and
/// [`Swarm::with_executor`].
#[deprecated(note = "Use `SwarmBuilder::without_executor` instead.")]
pub fn without_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
@ -493,17 +502,17 @@ where
/// See also [`DialOpts`].
///
/// ```
/// # use libp2p_swarm::Swarm;
/// # use libp2p_swarm::SwarmBuilder;
/// # use libp2p_swarm::dial_opts::{DialOpts, PeerCondition};
/// # use libp2p_core::{Multiaddr, PeerId, Transport};
/// # use libp2p_core::transport::dummy::DummyTransport;
/// # use libp2p_swarm::dummy;
/// #
/// let mut swarm = Swarm::without_executor(
/// let mut swarm = SwarmBuilder::without_executor(
/// DummyTransport::new().boxed(),
/// dummy::Behaviour,
/// PeerId::random(),
/// );
/// ).build();
///
/// // Dial a known peer.
/// swarm.dial(PeerId::random());
@ -1527,6 +1536,29 @@ where
}
}
/// Sets executor to the `wasm` executor.
/// Background tasks will be executed by the browser on the next micro-tick.
///
/// Spawning a task is similar too:
/// ```typescript
/// function spawn(task: () => Promise<void>) {
/// task()
/// }
/// ```
#[cfg(feature = "wasm-bindgen")]
pub fn with_wasm_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId,
) -> Self {
Self::with_executor(
transport,
behaviour,
local_peer_id,
crate::executor::WasmBindgenExecutor,
)
}
/// Builds a new [`SwarmBuilder`] from the given transport, behaviour, local peer ID and a
/// `tokio` executor.
#[cfg(all(

View File

@ -6,7 +6,7 @@ use libp2p_core::upgrade::Version;
use libp2p_core::Transport;
use libp2p_core::{multiaddr::Protocol, Multiaddr};
use libp2p_pnet::{PnetConfig, PreSharedKey};
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent};
const TIMEOUT: Duration = Duration::from_secs(5);
@ -113,11 +113,12 @@ where
.authenticate(libp2p_noise::NoiseAuthenticated::xx(&identity).unwrap())
.multiplex(libp2p_yamux::YamuxConfig::default())
.boxed();
Swarm::with_tokio_executor(
SwarmBuilder::with_tokio_executor(
transport,
keep_alive::Behaviour,
identity.public().to_peer_id(),
)
.build()
}
async fn listen_on<B: NetworkBehaviour>(swarm: &mut Swarm<B>, addr: Multiaddr) -> Multiaddr {

View File

@ -3,7 +3,7 @@ use libp2p_core::multiaddr::Protocol;
use libp2p_core::transport::MemoryTransport;
use libp2p_core::upgrade::Version;
use libp2p_core::Transport;
use libp2p_swarm::{keep_alive, Swarm, SwarmEvent};
use libp2p_swarm::{keep_alive, Swarm, SwarmBuilder, SwarmEvent};
#[tokio::test]
async fn can_establish_connection() {
@ -64,9 +64,10 @@ fn make_swarm() -> Swarm<keep_alive::Behaviour> {
.multiplex(libp2p_yamux::YamuxConfig::default())
.boxed();
Swarm::without_executor(
SwarmBuilder::without_executor(
transport,
keep_alive::Behaviour,
identity.public().to_peer_id(),
)
.build()
}

View File

@ -4,7 +4,7 @@ use libp2p_core::muxing::StreamMuxerBox;
use libp2p_core::Transport;
use libp2p_identity as identity;
use libp2p_ping as ping;
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm};
use libp2p_swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmBuilder};
use rand::thread_rng;
use void::Void;
@ -33,11 +33,7 @@ fn create_swarm() -> Result<Swarm<Behaviour>> {
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
.boxed();
Ok(Swarm::with_tokio_executor(
transport,
Behaviour::default(),
peer_id,
))
Ok(SwarmBuilder::with_tokio_executor(transport, Behaviour::default(), peer_id).build())
}
#[derive(NetworkBehaviour, Default)]