mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 22:01:04 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user