mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 04:27:57 +00:00
Try to fix out of view statements (#5177)
This issue happens when some peer sends a good but already known Seconded statement and the statement-distribution code does not update the statements_received field in the peer_knowledge structure. Subsequently, a Valid statement causes out-of-view message that is incorrectly emitted and causes reputation lose. This PR also introduces a concept of passing the specific pseudo-random generator to subsystems to make it easier to write deterministic tests. This functionality is not really necessary for the specific issue and unit test but it can be useful for other tests and subsystems.
This commit is contained in:
@@ -55,6 +55,7 @@ use polkadot_primitives::v2::{
|
||||
PersistedValidationData, SessionIndex, SessionInfo, Signed, SigningContext, ValidationCode,
|
||||
ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
|
||||
};
|
||||
pub use rand;
|
||||
use sp_application_crypto::AppKey;
|
||||
use sp_core::{traits::SpawnNamed, ByteArray};
|
||||
use sp_keystore::{CryptoStore, Error as KeystoreError, SyncCryptoStorePtr};
|
||||
@@ -276,33 +277,41 @@ pub fn find_validator_group(
|
||||
|
||||
/// Choose a random subset of `min` elements.
|
||||
/// But always include `is_priority` elements.
|
||||
pub fn choose_random_subset<T, F: FnMut(&T) -> bool>(
|
||||
pub fn choose_random_subset<T, F: FnMut(&T) -> bool>(is_priority: F, v: &mut Vec<T>, min: usize) {
|
||||
choose_random_subset_with_rng(is_priority, v, &mut rand::thread_rng(), min)
|
||||
}
|
||||
|
||||
/// Choose a random subset of `min` elements using a specific Random Generator `Rng`
|
||||
/// But always include `is_priority` elements.
|
||||
pub fn choose_random_subset_with_rng<T, F: FnMut(&T) -> bool, R: rand::Rng>(
|
||||
is_priority: F,
|
||||
mut v: Vec<T>,
|
||||
v: &mut Vec<T>,
|
||||
rng: &mut R,
|
||||
min: usize,
|
||||
) -> Vec<T> {
|
||||
) {
|
||||
use rand::seq::SliceRandom as _;
|
||||
|
||||
// partition the elements into priority first
|
||||
// the returned index is when non_priority elements start
|
||||
let i = itertools::partition(&mut v, is_priority);
|
||||
let i = itertools::partition(v.iter_mut(), is_priority);
|
||||
|
||||
if i >= min || v.len() <= i {
|
||||
v.truncate(i);
|
||||
return v
|
||||
return
|
||||
}
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
v[i..].shuffle(&mut rng);
|
||||
v[i..].shuffle(rng);
|
||||
|
||||
v.truncate(min);
|
||||
v
|
||||
}
|
||||
|
||||
/// Returns a `bool` with a probability of `a / b` of being true.
|
||||
pub fn gen_ratio(a: usize, b: usize) -> bool {
|
||||
use rand::Rng as _;
|
||||
let mut rng = rand::thread_rng();
|
||||
gen_ratio_rng(a, b, &mut rand::thread_rng())
|
||||
}
|
||||
|
||||
/// Returns a `bool` with a probability of `a / b` of being true.
|
||||
pub fn gen_ratio_rng<R: rand::Rng>(a: usize, b: usize, rng: &mut R) -> bool {
|
||||
rng.gen_ratio(a as u32, b as u32)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user