feat(discv5): bootstrap cli (#8004)

This commit is contained in:
Emilia Hane
2024-05-02 13:16:22 +02:00
committed by GitHub
parent 10ef202d7c
commit 7428573d7c
7 changed files with 125 additions and 20 deletions

View File

@ -37,7 +37,10 @@ pub mod network_stack_id;
pub use discv5::{self, IpMode};
pub use config::{BootNode, Config, ConfigBuilder};
pub use config::{
BootNode, Config, ConfigBuilder, DEFAULT_COUNT_BOOTSTRAP_LOOKUPS,
DEFAULT_SECONDS_BOOTSTRAP_LOOKUP_INTERVAL, DEFAULT_SECONDS_LOOKUP_INTERVAL,
};
pub use enr::enr_to_discv4_id;
pub use error::Error;
pub use filter::{FilterOutcome, MustNotIncludeKeys};
@ -45,17 +48,6 @@ pub use network_stack_id::NetworkStackId;
use metrics::{DiscoveredPeersMetrics, Discv5Metrics};
/// Default number of times to do pulse lookup queries, at bootstrap (pulse intervals, defaulting
/// to 5 seconds).
///
/// Default is 100 counts.
pub const DEFAULT_COUNT_PULSE_LOOKUPS_AT_BOOTSTRAP: u64 = 100;
/// Default duration of look up interval, for pulse look ups at bootstrap.
///
/// Default is 5 seconds.
pub const DEFAULT_SECONDS_PULSE_LOOKUP_INTERVAL: u64 = 5;
/// Max kbucket index is 255.
///
/// This is the max log2distance for 32 byte [`NodeId`](discv5::enr::NodeId) - 1. See <https://github.com/sigp/discv5/blob/e9e0d4f93ec35591832a9a8d937b4161127da87b/src/kbucket.rs#L586-L587>.
@ -180,7 +172,13 @@ impl Discv5 {
// 2. start discv5
//
let Config {
discv5_config, bootstrap_nodes, lookup_interval, discovered_peer_filter, ..
discv5_config,
bootstrap_nodes,
lookup_interval,
bootstrap_lookup_interval,
bootstrap_lookup_countdown,
discovered_peer_filter,
..
} = discv5_config;
let EnrCombinedKeyWrapper(enr) = enr.into();
@ -206,7 +204,13 @@ impl Discv5 {
//
// 4. start bg kbuckets maintenance
//
Self::spawn_populate_kbuckets_bg(lookup_interval, metrics.clone(), discv5.clone());
Self::spawn_populate_kbuckets_bg(
lookup_interval,
bootstrap_lookup_interval,
bootstrap_lookup_countdown,
metrics.clone(),
discv5.clone(),
);
Ok((
Self { discv5, ip_mode, fork_key, discovered_peer_filter, metrics },
@ -319,6 +323,8 @@ impl Discv5 {
/// Backgrounds regular look up queries, in order to keep kbuckets populated.
fn spawn_populate_kbuckets_bg(
lookup_interval: u64,
bootstrap_lookup_interval: u64,
bootstrap_lookup_countdown: u64,
metrics: Discv5Metrics,
discv5: Arc<discv5::Discv5>,
) {
@ -327,18 +333,18 @@ impl Discv5 {
let lookup_interval = Duration::from_secs(lookup_interval);
let metrics = metrics.discovered_peers;
let mut kbucket_index = MAX_KBUCKET_INDEX;
let pulse_lookup_interval = Duration::from_secs(DEFAULT_SECONDS_PULSE_LOOKUP_INTERVAL);
let pulse_lookup_interval = Duration::from_secs(bootstrap_lookup_interval);
// todo: graceful shutdown
async move {
// make many fast lookup queries at bootstrap, trying to fill kbuckets at furthest
// log2distance from local node
for i in (0..DEFAULT_COUNT_PULSE_LOOKUPS_AT_BOOTSTRAP).rev() {
for i in (0..bootstrap_lookup_countdown).rev() {
let target = discv5::enr::NodeId::random();
trace!(target: "net::discv5",
%target,
bootstrap_boost_runs_count_down=i,
bootstrap_boost_runs_countdown=i,
lookup_interval=format!("{:#?}", pulse_lookup_interval),
"starting bootstrap boost lookup query"
);