2020-06-19 06:30:07 +00:00
|
|
|
//! The main bootnode server execution.
|
|
|
|
|
|
|
|
use super::BootNodeConfig;
|
Rename eth2_libp2p to lighthouse_network (#2702)
## Description
The `eth2_libp2p` crate was originally named and designed to incorporate a simple libp2p integration into lighthouse. Since its origins the crates purpose has expanded dramatically. It now houses a lot more sophistication that is specific to lighthouse and no longer just a libp2p integration.
As of this writing it currently houses the following high-level lighthouse-specific logic:
- Lighthouse's implementation of the eth2 RPC protocol and specific encodings/decodings
- Integration and handling of ENRs with respect to libp2p and eth2
- Lighthouse's discovery logic, its integration with discv5 and logic about searching and handling peers.
- Lighthouse's peer manager - This is a large module handling various aspects of Lighthouse's network, such as peer scoring, handling pings and metadata, connection maintenance and recording, etc.
- Lighthouse's peer database - This is a collection of information stored for each individual peer which is specific to lighthouse. We store connection state, sync state, last seen ips and scores etc. The data stored for each peer is designed for various elements of the lighthouse code base such as syncing and the http api.
- Gossipsub scoring - This stores a collection of gossipsub 1.1 scoring mechanisms that are continuously analyssed and updated based on the ethereum 2 networks and how Lighthouse performs on these networks.
- Lighthouse specific types for managing gossipsub topics, sync status and ENR fields
- Lighthouse's network HTTP API metrics - A collection of metrics for lighthouse network monitoring
- Lighthouse's custom configuration of all networking protocols, RPC, gossipsub, discovery, identify and libp2p.
Therefore it makes sense to rename the crate to be more akin to its current purposes, simply that it manages the majority of Lighthouse's network stack. This PR renames this crate to `lighthouse_network`
Co-authored-by: Paul Hauner <paul@paulhauner.com>
2021-10-19 00:30:39 +00:00
|
|
|
use lighthouse_network::{
|
2021-09-16 04:45:05 +00:00
|
|
|
discv5::{enr::NodeId, Discv5, Discv5Event},
|
2020-09-29 08:28:29 +00:00
|
|
|
EnrExt, Eth2Enr,
|
|
|
|
};
|
2020-06-19 06:30:07 +00:00
|
|
|
use slog::info;
|
2020-09-29 08:28:29 +00:00
|
|
|
use types::EthSpec;
|
2020-06-19 06:30:07 +00:00
|
|
|
|
2020-09-29 08:28:29 +00:00
|
|
|
pub async fn run<T: EthSpec>(config: BootNodeConfig<T>, log: slog::Logger) {
|
2022-11-30 03:21:35 +00:00
|
|
|
let BootNodeConfig {
|
|
|
|
listen_socket,
|
|
|
|
boot_nodes,
|
|
|
|
local_enr,
|
|
|
|
local_key,
|
|
|
|
discv5_config,
|
|
|
|
..
|
|
|
|
} = config;
|
|
|
|
|
2020-06-19 06:30:07 +00:00
|
|
|
// Print out useful information about the generated ENR
|
|
|
|
|
2022-11-30 03:21:35 +00:00
|
|
|
let enr_v4_socket = local_enr.udp4_socket();
|
|
|
|
let enr_v6_socket = local_enr.udp6_socket();
|
|
|
|
let eth2_field = local_enr
|
2020-09-29 08:28:29 +00:00
|
|
|
.eth2()
|
|
|
|
.map(|fork_id| hex::encode(fork_id.fork_digest))
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2022-11-30 03:21:35 +00:00
|
|
|
let pretty_v4_socket = enr_v4_socket.as_ref().map(|addr| addr.to_string());
|
|
|
|
let pretty_v6_socket = enr_v6_socket.as_ref().map(|addr| addr.to_string());
|
|
|
|
info!(
|
|
|
|
log, "Configuration parameters";
|
|
|
|
"listening_address" => %listen_socket,
|
|
|
|
"advertised_v4_address" => ?pretty_v4_socket,
|
|
|
|
"advertised_v6_address" => ?pretty_v6_socket,
|
|
|
|
"eth2" => eth2_field
|
|
|
|
);
|
2020-06-19 06:30:07 +00:00
|
|
|
|
2022-11-30 03:21:35 +00:00
|
|
|
info!(log, "Identity established"; "peer_id" => %local_enr.peer_id(), "node_id" => %local_enr.node_id());
|
2020-06-19 06:30:07 +00:00
|
|
|
|
|
|
|
// build the contactable multiaddr list, adding the p2p protocol
|
2022-11-30 03:21:35 +00:00
|
|
|
info!(log, "Contact information"; "enr" => local_enr.to_base64());
|
|
|
|
info!(log, "Contact information"; "multiaddrs" => ?local_enr.multiaddr_p2p());
|
2020-06-19 06:30:07 +00:00
|
|
|
|
|
|
|
// construct the discv5 server
|
2023-03-21 05:14:57 +00:00
|
|
|
let mut discv5: Discv5 = Discv5::new(local_enr.clone(), local_key, discv5_config).unwrap();
|
2020-06-19 06:30:07 +00:00
|
|
|
|
|
|
|
// If there are any bootnodes add them to the routing table
|
2022-11-30 03:21:35 +00:00
|
|
|
for enr in boot_nodes {
|
2021-08-06 00:47:31 +00:00
|
|
|
info!(
|
|
|
|
log,
|
|
|
|
"Adding bootnode";
|
2022-11-30 03:21:35 +00:00
|
|
|
"ipv4_address" => ?enr.udp4_socket(),
|
|
|
|
"ipv6_address" => ?enr.udp6_socket(),
|
|
|
|
"peer_id" => ?enr.peer_id(),
|
|
|
|
"node_id" => ?enr.node_id()
|
2021-08-06 00:47:31 +00:00
|
|
|
);
|
2022-11-30 03:21:35 +00:00
|
|
|
if enr != local_enr {
|
2021-08-06 00:47:31 +00:00
|
|
|
if let Err(e) = discv5.add_enr(enr) {
|
2022-11-30 03:21:35 +00:00
|
|
|
slog::warn!(log, "Failed adding ENR"; "error" => ?e);
|
2021-08-06 00:47:31 +00:00
|
|
|
}
|
2020-06-19 06:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the server
|
2022-11-30 03:21:35 +00:00
|
|
|
if let Err(e) = discv5.start(listen_socket).await {
|
|
|
|
slog::crit!(log, "Could not start discv5 server"; "error" => %e);
|
2020-08-10 05:19:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-19 06:30:07 +00:00
|
|
|
|
|
|
|
// if there are peers in the local routing table, establish a session by running a query
|
|
|
|
if !discv5.table_entries_id().is_empty() {
|
|
|
|
info!(log, "Executing bootstrap query...");
|
2020-09-29 08:28:29 +00:00
|
|
|
let _ = discv5.find_node(NodeId::random()).await;
|
2020-06-19 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// respond with metrics every 10 seconds
|
|
|
|
let mut metric_interval = tokio::time::interval(tokio::time::Duration::from_secs(10));
|
|
|
|
|
|
|
|
// get an event stream
|
|
|
|
let mut event_stream = match discv5.event_stream().await {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(e) => {
|
2022-11-30 03:21:35 +00:00
|
|
|
slog::crit!(log, "Failed to obtain event stream"; "error" => %e);
|
2020-06-19 06:30:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// listen for events
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2021-02-10 23:29:49 +00:00
|
|
|
_ = metric_interval.tick() => {
|
2022-11-30 03:21:35 +00:00
|
|
|
// Get some ipv4/ipv6 stats to add in the metrics.
|
|
|
|
let mut ipv4_only_reachable: usize = 0;
|
|
|
|
let mut ipv6_only_reachable: usize= 0;
|
|
|
|
let mut ipv4_ipv6_reachable: usize = 0;
|
|
|
|
let mut unreachable_nodes: usize = 0;
|
|
|
|
for enr in discv5.kbuckets().iter_ref().filter_map(|entry| entry.status.is_connected().then_some(entry.node.value)) {
|
|
|
|
let declares_ipv4 = enr.udp4_socket().is_some();
|
|
|
|
let declares_ipv6 = enr.udp6_socket().is_some();
|
|
|
|
match (declares_ipv4, declares_ipv6) {
|
|
|
|
(true, true) => ipv4_ipv6_reachable += 1,
|
|
|
|
(true, false) => ipv4_only_reachable += 1,
|
|
|
|
(false, true) => ipv6_only_reachable += 1,
|
|
|
|
(false, false) => unreachable_nodes += 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 06:30:07 +00:00
|
|
|
// display server metrics
|
|
|
|
let metrics = discv5.metrics();
|
2022-11-30 03:21:35 +00:00
|
|
|
info!(
|
|
|
|
log, "Server metrics";
|
|
|
|
"connected_peers" => discv5.connected_peers(),
|
|
|
|
"active_sessions" => metrics.active_sessions,
|
|
|
|
"requests/s" => format_args!("{:.2}", metrics.unsolicited_requests_per_second),
|
|
|
|
"ipv4_nodes" => ipv4_only_reachable,
|
|
|
|
"ipv6_nodes" => ipv6_only_reachable,
|
|
|
|
"ipv6_and_ipv4_nodes" => ipv4_ipv6_reachable,
|
|
|
|
"unreachable_nodes" => unreachable_nodes,
|
|
|
|
);
|
|
|
|
|
2020-06-19 06:30:07 +00:00
|
|
|
}
|
|
|
|
Some(event) = event_stream.recv() => {
|
|
|
|
match event {
|
|
|
|
Discv5Event::Discovered(_enr) => {
|
|
|
|
// An ENR has bee obtained by the server
|
|
|
|
// Ignore these events here
|
|
|
|
}
|
|
|
|
Discv5Event::EnrAdded { .. } => {} // Ignore
|
2022-10-28 05:40:06 +00:00
|
|
|
Discv5Event::TalkRequest(_) => {} // Ignore
|
2020-06-19 06:30:07 +00:00
|
|
|
Discv5Event::NodeInserted { .. } => {} // Ignore
|
|
|
|
Discv5Event::SocketUpdated(socket_addr) => {
|
2022-11-30 03:21:35 +00:00
|
|
|
info!(log, "Advertised socket address updated"; "socket_addr" => %socket_addr);
|
2020-06-19 06:30:07 +00:00
|
|
|
}
|
2022-10-28 05:40:06 +00:00
|
|
|
Discv5Event::SessionEstablished{ .. } => {} // Ignore
|
2020-06-19 06:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|