gossip: choose a random subset on send instead of limiting connections (#2776)

* gossip: choose random subset on send

* naming bikeshed
This commit is contained in:
Andronik Ordian
2021-03-30 20:59:53 +02:00
committed by GitHub
parent a3115401c3
commit 9ac35d9f2b
10 changed files with 41 additions and 27 deletions
+13
View File
@@ -52,6 +52,7 @@ use thiserror::Error;
pub mod validator_discovery;
pub use metered_channel as metered;
pub use polkadot_node_network_protocol::MIN_GOSSIP_PEERS;
/// These reexports are required so that external crates can use the `delegated_subsystem` macro properly.
pub mod reexports {
@@ -267,6 +268,18 @@ pub async fn signing_key(validators: &[ValidatorId], keystore: SyncCryptoStorePt
None
}
/// Chooses a random subset of sqrt(v.len()), but at least `min` elements.
pub fn choose_random_sqrt_subset<T>(mut v: Vec<T>, min: usize) -> Vec<T> {
use rand::seq::SliceRandom as _;
let mut rng = rand::thread_rng();
v.shuffle(&mut rng);
let len_sqrt = (v.len() as f64).sqrt() as usize;
let len = std::cmp::max(min, len_sqrt);
v.truncate(len);
v
}
/// Local validator information
///
/// It can be created if the local node is a validator in the context of a particular