mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-17 03:42:46 +00:00
upgrade libp2p to v0.53.* (#4935)
* update libp2p and address compiler errors * remove bandwidth logging from transport * use libp2p registry * make clippy happy * use rust 1.73 * correct rpc keep alive * remove comments and obsolte code * remove libp2p prefix * make clippy happy * use quic under facade * remove fast msg id * bubble up close statements * fix wrong comment
This commit is contained in:
@@ -4,7 +4,6 @@ use crate::config::{gossipsub_config, GossipsubConfigParams, NetworkLoad};
|
||||
use crate::discovery::{
|
||||
subnet_predicate, DiscoveredPeers, Discovery, FIND_NODE_QUERY_CLOSEST_PEERS,
|
||||
};
|
||||
use crate::metrics::AggregatedBandwidthSinks;
|
||||
use crate::peer_manager::{
|
||||
config::Config as PeerManagerCfg, peerdb::score::PeerAction, peerdb::score::ReportSource,
|
||||
ConnectionDirection, PeerManager, PeerManagerEvent,
|
||||
@@ -127,8 +126,6 @@ pub struct Network<AppReqId: ReqId, TSpec: EthSpec> {
|
||||
/// The interval for updating gossipsub scores
|
||||
update_gossipsub_scores: tokio::time::Interval,
|
||||
gossip_cache: GossipCache,
|
||||
/// The bandwidth logger for the underlying libp2p transport.
|
||||
pub bandwidth: AggregatedBandwidthSinks,
|
||||
/// This node's PeerId.
|
||||
pub local_peer_id: PeerId,
|
||||
/// Logger for behaviour actions.
|
||||
@@ -139,10 +136,11 @@ pub struct Network<AppReqId: ReqId, TSpec: EthSpec> {
|
||||
impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
pub async fn new(
|
||||
executor: task_executor::TaskExecutor,
|
||||
ctx: ServiceContext<'_>,
|
||||
mut ctx: ServiceContext<'_>,
|
||||
log: &slog::Logger,
|
||||
) -> error::Result<(Self, Arc<NetworkGlobals<TSpec>>)> {
|
||||
let log = log.new(o!("service"=> "libp2p"));
|
||||
|
||||
let mut config = ctx.config.clone();
|
||||
trace!(log, "Libp2p Service starting");
|
||||
// initialise the node's ID
|
||||
@@ -257,10 +255,13 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
gossipsub_config_params,
|
||||
);
|
||||
|
||||
// If metrics are enabled for gossipsub build the configuration
|
||||
let gossipsub_metrics = ctx
|
||||
.gossipsub_registry
|
||||
.map(|registry| (registry, Default::default()));
|
||||
// If metrics are enabled for libp2p build the configuration
|
||||
let gossipsub_metrics = ctx.libp2p_registry.as_mut().map(|registry| {
|
||||
(
|
||||
registry.sub_registry_with_prefix("gossipsub"),
|
||||
Default::default(),
|
||||
)
|
||||
});
|
||||
|
||||
let snappy_transform = SnappyTransform::new(config.gs_config.max_transmit_size());
|
||||
let mut gossipsub = Gossipsub::new_with_subscription_filter_and_transform(
|
||||
@@ -366,9 +367,8 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
};
|
||||
|
||||
// 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))?;
|
||||
let transport = 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);
|
||||
@@ -379,20 +379,41 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
}
|
||||
|
||||
// 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 swarm = {
|
||||
let builder = SwarmBuilder::with_existing_identity(local_keypair)
|
||||
.with_tokio()
|
||||
.with_other_transport(|_key| transport)
|
||||
.expect("infalible");
|
||||
|
||||
// NOTE: adding bandwidth metrics changes the generics of the swarm, so types diverge
|
||||
if let Some(libp2p_registry) = ctx.libp2p_registry {
|
||||
builder
|
||||
.with_bandwidth_metrics(libp2p_registry)
|
||||
.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()
|
||||
} else {
|
||||
builder
|
||||
.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,
|
||||
@@ -403,7 +424,6 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
score_settings,
|
||||
update_gossipsub_scores,
|
||||
gossip_cache,
|
||||
bandwidth,
|
||||
local_peer_id,
|
||||
log,
|
||||
};
|
||||
@@ -1251,7 +1271,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
let handler_id = event.conn_id;
|
||||
// The METADATA and PING RPC responses are handled within the behaviour and not propagated
|
||||
match event.event {
|
||||
Err(handler_err) => {
|
||||
HandlerEvent::Err(handler_err) => {
|
||||
match handler_err {
|
||||
HandlerErr::Inbound {
|
||||
id: _,
|
||||
@@ -1286,7 +1306,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(RPCReceived::Request(id, request)) => {
|
||||
HandlerEvent::Ok(RPCReceived::Request(id, request)) => {
|
||||
let peer_request_id = (handler_id, id);
|
||||
match request {
|
||||
/* Behaviour managed protocols: Ping and Metadata */
|
||||
@@ -1385,7 +1405,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(RPCReceived::Response(id, resp)) => {
|
||||
HandlerEvent::Ok(RPCReceived::Response(id, resp)) => {
|
||||
match resp {
|
||||
/* Behaviour managed protocols */
|
||||
RPCResponse::Pong(ping) => {
|
||||
@@ -1422,7 +1442,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(RPCReceived::EndOfStream(id, termination)) => {
|
||||
HandlerEvent::Ok(RPCReceived::EndOfStream(id, termination)) => {
|
||||
let response = match termination {
|
||||
ResponseTermination::BlocksByRange => Response::BlocksByRange(None),
|
||||
ResponseTermination::BlocksByRoot => Response::BlocksByRoot(None),
|
||||
@@ -1431,6 +1451,11 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
};
|
||||
self.build_response(id, peer_id, response)
|
||||
}
|
||||
HandlerEvent::Close(_) => {
|
||||
let _ = self.swarm.disconnect_peer_id(peer_id);
|
||||
// NOTE: we wait for the swarm to report the connection as actually closed
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1624,7 +1649,11 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
None
|
||||
}
|
||||
}
|
||||
SwarmEvent::Dialing { .. } => None,
|
||||
_ => {
|
||||
// NOTE: SwarmEvent is a non exhaustive enum so updates should be based on
|
||||
// release notes more than compiler feedback
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(ev) = maybe_event {
|
||||
|
||||
Reference in New Issue
Block a user