mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-15 02:51:25 +00:00
feat(libp2p): add SwarmBuilder
Introduce the new `libp2p::SwarmBuilder`. Users should use the new `libp2p::SwarmBuilder` instead of the now deprecated `libp2p::swarm::SwarmBuilder`. See `libp2p::SwarmBuilder` docs on how to use the new builder. Fixes #3657. Fixes #3563. Fixes #3179. Pull-Request: #4120.
This commit is contained in:
@ -10,7 +10,7 @@ async-std = { version = "1.12", features = ["attributes"] }
|
||||
async-trait = "0.1"
|
||||
env_logger = "0.10"
|
||||
futures = "0.3.28"
|
||||
libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "websocket", "yamux"] }
|
||||
libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
@ -22,14 +22,13 @@
|
||||
|
||||
use async_std::io;
|
||||
use futures::{prelude::*, select};
|
||||
use libp2p::core::upgrade::Version;
|
||||
use libp2p::kad;
|
||||
use libp2p::kad::record::store::MemoryStore;
|
||||
use libp2p::kad::Mode;
|
||||
use libp2p::{
|
||||
identity, mdns, noise,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
mdns, noise,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux,
|
||||
};
|
||||
use std::error::Error;
|
||||
|
||||
@ -37,16 +36,6 @@ use std::error::Error;
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
// Create a random key for ourselves.
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
let transport = tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
// We create a custom network behaviour that combines Kademlia and mDNS.
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Behaviour {
|
||||
@ -54,15 +43,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
mdns: mdns::async_io::Behaviour,
|
||||
}
|
||||
|
||||
// Create a swarm to manage peers and events.
|
||||
let mut swarm = {
|
||||
// Create a Kademlia behaviour.
|
||||
let store = MemoryStore::new(local_peer_id);
|
||||
let kademlia = kad::Behaviour::new(local_peer_id, store);
|
||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
|
||||
let behaviour = Behaviour { kademlia, mdns };
|
||||
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
|
||||
};
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_async_std()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| {
|
||||
Ok(Behaviour {
|
||||
kademlia: kad::Behaviour::new(
|
||||
key.public().to_peer_id(),
|
||||
MemoryStore::new(key.public().to_peer_id()),
|
||||
),
|
||||
mdns: mdns::async_io::Behaviour::new(
|
||||
mdns::Config::default(),
|
||||
key.public().to_peer_id(),
|
||||
)?,
|
||||
})
|
||||
})?
|
||||
.build();
|
||||
|
||||
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));
|
||||
|
||||
|
Reference in New Issue
Block a user