mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 17:41:22 +00:00
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:
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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)]
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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"
|
||||
|
@ -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()
|
||||
|
@ -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>(
|
||||
|
Reference in New Issue
Block a user