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

View File

@ -88,7 +88,7 @@ pub fn build_transport(
}; };
// Enables DNS over the 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)) Ok((transport, bandwidth))
} }