mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Merge unstable 20230925 into deneb-free-blobs.
This commit is contained in:
@@ -160,8 +160,6 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
let meta_data = utils::load_or_build_metadata(&config.network_dir, &log);
|
||||
let globals = NetworkGlobals::new(
|
||||
enr,
|
||||
config.listen_addrs().v4().map(|v4_addr| v4_addr.tcp_port),
|
||||
config.listen_addrs().v6().map(|v6_addr| v6_addr.tcp_port),
|
||||
meta_data,
|
||||
config
|
||||
.trusted_peers
|
||||
@@ -371,8 +369,9 @@ 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())
|
||||
.map_err(|e| format!("Failed to build transport: {:?}", e))?;
|
||||
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);
|
||||
@@ -427,9 +426,16 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
async fn start(&mut self, config: &crate::NetworkConfig) -> error::Result<()> {
|
||||
let enr = self.network_globals.local_enr();
|
||||
info!(self.log, "Libp2p Starting"; "peer_id" => %enr.peer_id(), "bandwidth_config" => format!("{}-{}", config.network_load, NetworkLoad::from(config.network_load).name));
|
||||
debug!(self.log, "Attempting to open listening ports"; config.listen_addrs(), "discovery_enabled" => !config.disable_discovery);
|
||||
debug!(self.log, "Attempting to open listening ports"; config.listen_addrs(), "discovery_enabled" => !config.disable_discovery, "quic_enabled" => !config.disable_quic_support);
|
||||
|
||||
for listen_multiaddr in config.listen_addrs().libp2p_addresses() {
|
||||
// If QUIC is disabled, ignore listening on QUIC ports
|
||||
if config.disable_quic_support
|
||||
&& listen_multiaddr.iter().any(|v| v == MProtocol::QuicV1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for listen_multiaddr in config.listen_addrs().tcp_addresses() {
|
||||
match self.swarm.listen_on(listen_multiaddr.clone()) {
|
||||
Ok(_) => {
|
||||
let mut log_address = listen_multiaddr;
|
||||
@@ -470,6 +476,20 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
boot_nodes.dedup();
|
||||
|
||||
for bootnode_enr in boot_nodes {
|
||||
// If QUIC is enabled, attempt QUIC connections first
|
||||
if !config.disable_quic_support {
|
||||
for quic_multiaddr in &bootnode_enr.multiaddr_quic() {
|
||||
if !self
|
||||
.network_globals
|
||||
.peers
|
||||
.read()
|
||||
.is_connected_or_dialing(&bootnode_enr.peer_id())
|
||||
{
|
||||
dial(quic_multiaddr.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for multiaddr in &bootnode_enr.multiaddr() {
|
||||
// ignore udp multiaddr if it exists
|
||||
let components = multiaddr.iter().collect::<Vec<_>>();
|
||||
@@ -1058,30 +1078,27 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Dial cached enrs in discovery service that are in the given `subnet_id` and aren't
|
||||
/// Dial cached Enrs in discovery service that are in the given `subnet_id` and aren't
|
||||
/// in Connected, Dialing or Banned state.
|
||||
fn dial_cached_enrs_in_subnet(&mut self, subnet: Subnet) {
|
||||
let predicate = subnet_predicate::<TSpec>(vec![subnet], &self.log);
|
||||
let peers_to_dial: Vec<PeerId> = self
|
||||
let peers_to_dial: Vec<Enr> = self
|
||||
.discovery()
|
||||
.cached_enrs()
|
||||
.filter_map(|(peer_id, enr)| {
|
||||
let peers = self.network_globals.peers.read();
|
||||
if predicate(enr) && peers.should_dial(peer_id) {
|
||||
Some(*peer_id)
|
||||
.filter_map(|(_peer_id, enr)| {
|
||||
if predicate(enr) {
|
||||
Some(enr.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
for peer_id in peers_to_dial {
|
||||
debug!(self.log, "Dialing cached ENR peer"; "peer_id" => %peer_id);
|
||||
// Remove the ENR from the cache to prevent continual re-dialing on disconnects
|
||||
|
||||
self.discovery_mut().remove_cached_enr(&peer_id);
|
||||
// For any dial event, inform the peer manager
|
||||
let enr = self.discovery_mut().enr_of_peer(&peer_id);
|
||||
self.peer_manager_mut().dial_peer(&peer_id, enr);
|
||||
// Remove the ENR from the cache to prevent continual re-dialing on disconnects
|
||||
for enr in peers_to_dial {
|
||||
debug!(self.log, "Dialing cached ENR peer"; "peer_id" => %enr.peer_id());
|
||||
self.discovery_mut().remove_cached_enr(&enr.peer_id());
|
||||
self.peer_manager_mut().dial_peer(enr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1388,22 +1405,6 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a discovery event.
|
||||
fn inject_discovery_event(
|
||||
&mut self,
|
||||
event: DiscoveredPeers,
|
||||
) -> Option<NetworkEvent<AppReqId, TSpec>> {
|
||||
let DiscoveredPeers { peers } = event;
|
||||
let to_dial_peers = self.peer_manager_mut().peers_discovered(peers);
|
||||
for peer_id in to_dial_peers {
|
||||
debug!(self.log, "Dialing discovered peer"; "peer_id" => %peer_id);
|
||||
// For any dial event, inform the peer manager
|
||||
let enr = self.discovery_mut().enr_of_peer(&peer_id);
|
||||
self.peer_manager_mut().dial_peer(&peer_id, enr);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Handle an identify event.
|
||||
fn inject_identify_event(
|
||||
&mut self,
|
||||
@@ -1504,7 +1505,14 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
BehaviourEvent::BannedPeers(void) => void::unreachable(void),
|
||||
BehaviourEvent::Gossipsub(ge) => self.inject_gs_event(ge),
|
||||
BehaviourEvent::Eth2Rpc(re) => self.inject_rpc_event(re),
|
||||
BehaviourEvent::Discovery(de) => self.inject_discovery_event(de),
|
||||
// Inform the peer manager about discovered peers.
|
||||
//
|
||||
// The peer manager will subsequently decide which peers need to be dialed and then dial
|
||||
// them.
|
||||
BehaviourEvent::Discovery(DiscoveredPeers { peers }) => {
|
||||
self.peer_manager_mut().peers_discovered(peers);
|
||||
None
|
||||
}
|
||||
BehaviourEvent::Identify(ie) => self.inject_identify_event(ie),
|
||||
BehaviourEvent::PeerManager(pe) => self.inject_pm_event(pe),
|
||||
BehaviourEvent::ConnectionLimits(le) => void::unreachable(le),
|
||||
@@ -1536,7 +1544,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
format!("Dialing local peer id {endpoint:?}")
|
||||
}
|
||||
libp2p::swarm::ListenError::Denied { cause } => {
|
||||
format!("Connection was denied with cause {cause}")
|
||||
format!("Connection was denied with cause: {cause:?}")
|
||||
}
|
||||
libp2p::swarm::ListenError::Transport(t) => match t {
|
||||
libp2p::TransportError::MultiaddrNotSupported(m) => {
|
||||
@@ -1586,13 +1594,7 @@ impl<AppReqId: ReqId, TSpec: EthSpec> Network<AppReqId, TSpec> {
|
||||
None
|
||||
}
|
||||
}
|
||||
SwarmEvent::Dialing {
|
||||
peer_id,
|
||||
connection_id: _,
|
||||
} => {
|
||||
debug!(self.log, "Swarm Dialing"; "peer_id" => ?peer_id);
|
||||
None
|
||||
}
|
||||
SwarmEvent::Dialing { .. } => None,
|
||||
};
|
||||
|
||||
if let Some(ev) = maybe_event {
|
||||
|
||||
@@ -4,11 +4,13 @@ use crate::types::{
|
||||
error, EnrAttestationBitfield, EnrSyncCommitteeBitfield, GossipEncoding, GossipKind,
|
||||
};
|
||||
use crate::{GossipTopic, NetworkConfig};
|
||||
use futures::future::Either;
|
||||
use libp2p::bandwidth::BandwidthSinks;
|
||||
use libp2p::core::{multiaddr::Multiaddr, muxing::StreamMuxerBox, transport::Boxed};
|
||||
use libp2p::gossipsub;
|
||||
use libp2p::identity::{secp256k1, Keypair};
|
||||
use libp2p::{core, noise, yamux, PeerId, Transport, TransportExt};
|
||||
use libp2p_quic;
|
||||
use prometheus_client::registry::Registry;
|
||||
use slog::{debug, warn};
|
||||
use ssz::Decode;
|
||||
@@ -37,19 +39,12 @@ pub struct Context<'a> {
|
||||
|
||||
type BoxedTransport = Boxed<(PeerId, StreamMuxerBox)>;
|
||||
|
||||
/// The implementation supports TCP/IP, WebSockets over TCP/IP, noise as the encryption layer, and
|
||||
/// mplex as the multiplexing layer.
|
||||
/// The implementation supports TCP/IP, QUIC (experimental) over UDP, noise as the encryption layer, and
|
||||
/// mplex/yamux as the multiplexing layer (when using TCP).
|
||||
pub fn build_transport(
|
||||
local_private_key: Keypair,
|
||||
quic_support: bool,
|
||||
) -> std::io::Result<(BoxedTransport, Arc<BandwidthSinks>)> {
|
||||
let tcp = libp2p::tcp::tokio::Transport::new(libp2p::tcp::Config::default().nodelay(true));
|
||||
let transport = libp2p::dns::TokioDnsConfig::system(tcp)?;
|
||||
#[cfg(feature = "libp2p-websocket")]
|
||||
let transport = {
|
||||
let trans_clone = transport.clone();
|
||||
transport.or_transport(libp2p::websocket::WsConfig::new(trans_clone))
|
||||
};
|
||||
|
||||
// mplex config
|
||||
let mut mplex_config = libp2p_mplex::MplexConfig::new();
|
||||
mplex_config.set_max_buffer_size(256);
|
||||
@@ -58,18 +53,34 @@ pub fn build_transport(
|
||||
// yamux config
|
||||
let mut yamux_config = yamux::Config::default();
|
||||
yamux_config.set_window_update_mode(yamux::WindowUpdateMode::on_read());
|
||||
let (transport, bandwidth) = transport
|
||||
|
||||
// Creates the TCP transport layer
|
||||
let tcp = libp2p::tcp::tokio::Transport::new(libp2p::tcp::Config::default().nodelay(true))
|
||||
.upgrade(core::upgrade::Version::V1)
|
||||
.authenticate(generate_noise_config(&local_private_key))
|
||||
.multiplex(core::upgrade::SelectUpgrade::new(
|
||||
yamux_config,
|
||||
mplex_config,
|
||||
))
|
||||
.timeout(Duration::from_secs(10))
|
||||
.boxed()
|
||||
.with_bandwidth_logging();
|
||||
.timeout(Duration::from_secs(10));
|
||||
|
||||
let (transport, bandwidth) = if quic_support {
|
||||
// Enables Quic
|
||||
// The default quic configuration suits us for now.
|
||||
let quic_config = libp2p_quic::Config::new(&local_private_key);
|
||||
tcp.or_transport(libp2p_quic::tokio::Transport::new(quic_config))
|
||||
.map(|either_output, _| match either_output {
|
||||
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
})
|
||||
.with_bandwidth_logging()
|
||||
} else {
|
||||
tcp.with_bandwidth_logging()
|
||||
};
|
||||
|
||||
// // Enables DNS over the transport.
|
||||
let transport = libp2p::dns::TokioDnsConfig::system(transport)?.boxed();
|
||||
|
||||
// Authentication
|
||||
Ok((transport, bandwidth))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user