update libp2p (#4864)

## Issue Addressed

updates libp2p to the latest version and uses the new `SwarmBuilder`. Superseeds https://github.com/sigp/lighthouse/pull/4695/
CC @mxinden I don't think we can use both `bandwidth_loggers` with the new syntax right?
This commit is contained in:
João Oliveira 2023-10-19 21:22:55 +00:00
parent 8c28d175b8
commit c6583bb5fa
3 changed files with 290 additions and 252 deletions

485
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -29,10 +29,10 @@ use libp2p::gossipsub::{
self, IdentTopic as Topic, MessageAcceptance, MessageAuthenticity, MessageId, PublishError,
TopicScoreParams,
};
use libp2p::identify;
use libp2p::multiaddr::{Multiaddr, Protocol as MProtocol};
use libp2p::swarm::{Swarm, SwarmBuilder, SwarmEvent};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::PeerId;
use libp2p::{identify, SwarmBuilder};
use slog::{crit, debug, info, o, trace, warn};
use std::path::PathBuf;
use std::pin::Pin;
@ -365,35 +365,34 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
}
};
let (swarm, bandwidth) = {
// Set up the transport - tcp/ws with noise and mplex
let (transport, bandwidth) =
build_transport(local_keypair.clone(), !config.disable_quic_support)
.map_err(|e| format!("Failed to build transport: {:?}", e))?;
// Set up the transport - tcp/quic with noise and mplex
let (transport, bandwidth) =
build_transport(local_keypair.clone(), !config.disable_quic_support)
.map_err(|e| format!("Failed to build transport: {:?}", e))?;
// use the executor for libp2p
struct Executor(task_executor::TaskExecutor);
impl libp2p::swarm::Executor for Executor {
fn exec(&self, f: Pin<Box<dyn futures::Future<Output = ()> + Send>>) {
self.0.spawn(f, "libp2p");
}
// use the executor for libp2p
struct Executor(task_executor::TaskExecutor);
impl libp2p::swarm::Executor for Executor {
fn exec(&self, f: Pin<Box<dyn futures::Future<Output = ()> + Send>>) {
self.0.spawn(f, "libp2p");
}
}
// sets up the libp2p connection limits
(
SwarmBuilder::with_executor(
transport,
behaviour,
local_peer_id,
Executor(executor),
)
.notify_handler_buffer_size(std::num::NonZeroUsize::new(7).expect("Not zero"))
.per_connection_event_buffer_size(4)
.build(),
bandwidth,
)
};
// sets up the libp2p swarm.
let swarm = SwarmBuilder::with_existing_identity(local_keypair)
.with_tokio()
.with_other_transport(|_key| transport)
.expect("infalible")
.with_behaviour(|_| behaviour)
.expect("infalible")
.with_swarm_config(|_| {
libp2p::swarm::Config::with_executor(Executor(executor))
.with_notify_handler_buffer_size(
std::num::NonZeroUsize::new(7).expect("Not zero"),
)
.with_per_connection_event_buffer_size(4)
})
.build();
let mut network = Network {
swarm,

View File

@ -88,7 +88,7 @@ pub fn build_transport(
};
// Enables DNS over the transport.
let transport = libp2p::dns::TokioDnsConfig::system(transport)?.boxed();
let transport = libp2p::dns::tokio::Transport::system(transport)?.boxed();
Ok((transport, bandwidth))
}