Experimental discovery (#2577)

# Description

A few changes have been made to discovery. In particular a custom re-write of an LRU cache which previously was read/write O(N) for all our sessions ~5k, to a more reasonable hashmap-style O(1). 

Further there has been reported issues in the current discv5, so added error handling to help identify the issue has been added.
This commit is contained in:
Age Manning 2021-09-16 04:45:05 +00:00
parent c5c7476518
commit 56e0615df8
4 changed files with 221 additions and 243 deletions

411
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,8 @@ authors = ["Sigma Prime <contact@sigmaprime.io>"]
edition = "2018"
[dependencies]
discv5 = { version = "0.1.0-beta.9", features = ["libp2p"] }
#discv5 = { version = "0.1.0-beta.10", features = ["libp2p"] }
discv5 = { git = "https://github.com/sigp/discv5", rev="10247bbd299227fef20233f2f5a8de9780de09ac", features = ["libp2p"] }
unsigned-varint = { version = "0.6.0", features = ["codec"] }
types = { path = "../../consensus/types" }
hashset_delay = { path = "../../common/hashset_delay" }

View File

@ -1,6 +1,6 @@
use beacon_node::{get_data_dir, get_eth2_network_config, set_network_config};
use clap::ArgMatches;
use eth2_libp2p::discv5::{enr::CombinedKey, Enr};
use eth2_libp2p::discv5::{enr::CombinedKey, Discv5Config, Enr};
use eth2_libp2p::{
discovery::{create_enr_builder_from_config, load_enr_from_disk, use_or_load_enr},
load_private_key, CombinedKeyExt, NetworkConfig,
@ -18,8 +18,7 @@ pub struct BootNodeConfig<T: EthSpec> {
pub boot_nodes: Vec<Enr>,
pub local_enr: Enr,
pub local_key: CombinedKey,
pub auto_update: bool,
pub disable_packet_filter: bool,
pub discv5_config: Discv5Config,
phantom: PhantomData<T>,
}
@ -58,19 +57,16 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
let logger = slog_scope::logger();
set_network_config(&mut network_config, matches, &data_dir, &logger, true)?;
// default to the standard port
// Set the enr-udp-port to the default listening port if it was not specified.
if !matches.is_present("enr-udp-port") {
network_config.enr_udp_port = Some(
matches
.value_of("port")
.expect("Value required")
.parse()
.map_err(|_| "Invalid port number")?,
);
network_config.enr_udp_port = Some(network_config.discovery_port);
}
let auto_update = matches.is_present("enable-enr_auto_update");
let disable_packet_filter = matches.is_present("disable-packet-filter");
// By default this is enabled. If it is not set, revert to false.
if !matches.is_present("enable-enr-auto-update") {
network_config.discv5_config.enr_update = false;
}
// the address to listen on
let listen_socket =
@ -129,8 +125,7 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
boot_nodes,
local_enr,
local_key,
auto_update,
disable_packet_filter,
discv5_config: network_config.discv5_config,
phantom: PhantomData,
})
}

View File

@ -2,7 +2,7 @@
use super::BootNodeConfig;
use eth2_libp2p::{
discv5::{enr::NodeId, Discv5, Discv5ConfigBuilder, Discv5Event},
discv5::{enr::NodeId, Discv5, Discv5Event},
EnrExt, Eth2Enr,
};
use slog::info;
@ -26,24 +26,13 @@ pub async fn run<T: EthSpec>(config: BootNodeConfig<T>, log: slog::Logger) {
info!(log, "Contact information"; "enr" => config.local_enr.to_base64());
info!(log, "Contact information"; "multiaddrs" => format!("{:?}", config.local_enr.multiaddr_p2p()));
// Build the discv5 server
// default configuration with packet filtering
let discv5_config = {
let mut builder = Discv5ConfigBuilder::new();
if !config.disable_packet_filter {
builder.enable_packet_filter();
}
if !config.auto_update {
builder.disable_enr_update();
}
builder.build()
};
// construct the discv5 server
let mut discv5 =
Discv5::new(config.local_enr.clone(), config.local_key, discv5_config).unwrap();
let mut discv5 = Discv5::new(
config.local_enr.clone(),
config.local_key,
config.discv5_config,
)
.unwrap();
// If there are any bootnodes add them to the routing table
for enr in config.boot_nodes {