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:
Max Inden
2023-10-10 08:55:14 +02:00
committed by GitHub
parent 3ae72557ab
commit d605255fec
62 changed files with 2970 additions and 940 deletions

View File

@@ -9,11 +9,10 @@ use libp2p::{
multiaddr::Protocol,
noise,
request_response::{self, ProtocolSupport, RequestId, ResponseChannel},
swarm::{NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent},
tcp, yamux, PeerId, Transport,
swarm::{NetworkBehaviour, Swarm, SwarmEvent},
tcp, yamux, PeerId,
};
use libp2p::core::upgrade::Version;
use libp2p::StreamProtocol;
use serde::{Deserialize, Serialize};
use std::collections::{hash_map, HashMap, HashSet};
@@ -41,18 +40,18 @@ pub(crate) async fn new(
};
let peer_id = id_keys.public().to_peer_id();
let transport = tcp::async_io::Transport::default()
.upgrade(Version::V1Lazy)
.authenticate(noise::Config::new(&id_keys)?)
.multiplex(yamux::Config::default())
.boxed();
// Build the Swarm, connecting the lower layer transport logic with the
// higher layer network behaviour logic.
let mut swarm = SwarmBuilder::with_async_std_executor(
transport,
Behaviour {
kademlia: kad::Behaviour::new(peer_id, kad::record::store::MemoryStore::new(peer_id)),
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(id_keys)
.with_async_std()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_behaviour(|key| Behaviour {
kademlia: kad::Behaviour::new(
peer_id,
kad::store::MemoryStore::new(key.public().to_peer_id()),
),
request_response: request_response::cbor::Behaviour::new(
[(
StreamProtocol::new("/file-exchange/1"),
@@ -60,10 +59,8 @@ pub(crate) async fn new(
)],
request_response::Config::default(),
),
},
peer_id,
)
.build();
})?
.build();
swarm
.behaviour_mut()