Always limit the number of initial peers in the Config

This way, we can never get the unused peers out.
This commit is contained in:
teor 2021-10-22 08:29:16 +10:00
parent 085930a47c
commit 81ede597c8
2 changed files with 24 additions and 28 deletions

View File

@ -5,6 +5,7 @@ use std::{
time::Duration,
};
use rand::seq::SliceRandom;
use serde::{de, Deserialize, Deserializer};
use zebra_chain::{parameters::Network, serialization::canonical_socket_addr};
@ -116,11 +117,31 @@ impl Config {
}
/// Get the initial seed peers based on the configured network.
/// Limits the number of initial peer addresses to the configured
/// `peerset_initial_target_size`.
///
/// The result is randomly chosen entries from the provided set of addresses.
pub async fn initial_peers(&self) -> HashSet<SocketAddr> {
match self.network {
let initial_peers = match self.network {
Network::Mainnet => Config::resolve_peers(&self.initial_mainnet_peers).await,
Network::Testnet => Config::resolve_peers(&self.initial_testnet_peers).await,
};
let initial_peer_count = initial_peers.len();
// Limit the number of initial peers to `peerset_initial_target_size`
if initial_peer_count > self.peerset_initial_target_size {
info!(
"Limiting the initial peers list from {} to {}",
initial_peer_count, self.peerset_initial_target_size
);
}
let initial_peers_vect: Vec<SocketAddr> = initial_peers.iter().copied().collect();
initial_peers_vect
.choose_multiple(&mut rand::thread_rng(), self.peerset_initial_target_size)
.copied()
.collect()
}
/// Resolves `host` into zero or more IP addresses, retrying up to

View File

@ -3,8 +3,7 @@
// Portions of this submodule were adapted from tower-balance,
// which is (c) 2019 Tower Contributors (MIT licensed).
use rand::seq::SliceRandom;
use std::{collections::HashSet, net::SocketAddr, sync::Arc};
use std::{net::SocketAddr, sync::Arc};
use futures::{
channel::mpsc,
@ -205,7 +204,7 @@ where
> + Clone,
S::Future: Send + 'static,
{
let initial_peers = limit_initial_peers(config).await;
let initial_peers = config.initial_peers().await;
let mut handshake_success_total: usize = 0;
let mut handshake_error_total: usize = 0;
@ -284,30 +283,6 @@ where
Ok(active_outbound_connections)
}
/// Limit the number of `initial_peers` addresses entries to the configured
/// `peerset_initial_target_size`.
///
/// The result is randomly chosen entries from the provided set of addresses.
async fn limit_initial_peers(config: &Config) -> HashSet<SocketAddr> {
let initial_peers = config.initial_peers().await;
let initial_peer_count = initial_peers.len();
// Limit the number of initial peers to `config.peerset_initial_target_size`
if initial_peer_count > config.peerset_initial_target_size {
info!(
"Limiting the initial peers list from {} to {}",
initial_peer_count, config.peerset_initial_target_size
);
}
let initial_peers_vect: Vec<SocketAddr> = initial_peers.iter().copied().collect();
initial_peers_vect
.choose_multiple(&mut rand::thread_rng(), config.peerset_initial_target_size)
.copied()
.collect()
}
/// Open a peer connection listener on `config.listen_addr`,
/// returning the opened [`TcpListener`], and the address it is bound to.
///