distribution: handle sqrt peer view updates (#2871)

* distribution: handle sqrt peer view updates

* someone please put rustc into my brain

* guide updates
This commit is contained in:
Andronik Ordian
2021-04-10 00:45:25 +02:00
committed by GitHub
parent 23cfd0edb9
commit e62939ad7a
4 changed files with 45 additions and 12 deletions
+16 -2
View File
@@ -216,12 +216,26 @@ pub fn choose_random_sqrt_subset<T>(mut v: Vec<T>, min: usize) -> Vec<T> {
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);
let len = max_of_min_and_sqrt_len(v.len(), min);
v.truncate(len);
v
}
/// Returns bool with a probability of `max(len.sqrt(), min) / len`
/// being true.
pub fn gen_ratio_sqrt_subset(len: usize, min: usize) -> bool {
use rand::Rng as _;
let mut rng = rand::thread_rng();
let threshold = max_of_min_and_sqrt_len(len, min);
let n = rng.gen_range(0..len);
n < threshold
}
fn max_of_min_and_sqrt_len(len: usize, min: usize) -> usize {
let len_sqrt = (len as f64).sqrt() as usize;
std::cmp::max(min, len_sqrt)
}
/// Local validator information
///
/// It can be created if the local node is a validator in the context of a particular