mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 07:01:03 +00:00
Put all authorities of a session into SessionInfo. (#3813)
* Make SessionInfo include all authorities of the current session. * Add missing import. * Only take subset for assignment keys. * Fix typo. * Make spellcheck happy. * Really. * Fix test. * More clear documentation. * Update comments in `SessionInfo`.
This commit is contained in:
@@ -19,7 +19,10 @@
|
||||
//!
|
||||
//! See https://w3f.github.io/parachain-implementers-guide/runtime/session_info.html.
|
||||
|
||||
use crate::{configuration, paras, scheduler, shared, util::take_active_subset};
|
||||
use crate::{
|
||||
configuration, paras, scheduler, shared,
|
||||
util::{take_active_subset, take_active_subset_and_inactive},
|
||||
};
|
||||
use frame_support::{pallet_prelude::*, traits::OneSessionHandler};
|
||||
use primitives::v1::{AssignmentId, AuthorityDiscoveryId, SessionIndex, SessionInfo};
|
||||
use sp_std::vec::Vec;
|
||||
@@ -120,7 +123,7 @@ impl<T: Config> Pallet<T> {
|
||||
// create a new entry in `Sessions` with information about the current session
|
||||
let new_session_info = SessionInfo {
|
||||
validators, // these are from the notification and are thus already correct.
|
||||
discovery_keys: take_active_subset(&active_set, &discovery_keys),
|
||||
discovery_keys: take_active_subset_and_inactive(&active_set, &discovery_keys),
|
||||
assignment_keys: take_active_subset(&active_set, &assignment_keys),
|
||||
validator_groups,
|
||||
n_cores,
|
||||
@@ -176,6 +179,7 @@ mod tests {
|
||||
new_test_ext, Configuration, MockGenesisConfig, Origin, ParasShared, SessionInfo,
|
||||
System, Test,
|
||||
},
|
||||
util::take_active_subset,
|
||||
};
|
||||
use keyring::Sr25519Keyring;
|
||||
use primitives::v1::{BlockNumber, ValidatorId, ValidatorIndex};
|
||||
@@ -357,7 +361,7 @@ mod tests {
|
||||
assert_eq!(session.validators, validators);
|
||||
assert_eq!(
|
||||
session.discovery_keys,
|
||||
take_active_subset(&active_set, &unscrambled_discovery),
|
||||
take_active_subset_and_inactive(&active_set, &unscrambled_discovery),
|
||||
);
|
||||
assert_eq!(
|
||||
session.assignment_keys,
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
//! on all modules.
|
||||
|
||||
use primitives::v1::{Id as ParaId, PersistedValidationData, ValidatorIndex};
|
||||
use sp_std::vec::Vec;
|
||||
use sp_std::{collections::btree_set::BTreeSet, vec::Vec};
|
||||
|
||||
use crate::{configuration, hrmp, paras};
|
||||
|
||||
@@ -41,15 +41,49 @@ pub fn make_persisted_validation_data<T: paras::Config + hrmp::Config>(
|
||||
})
|
||||
}
|
||||
|
||||
/// Take the active subset of a set containing all validators.
|
||||
pub fn take_active_subset<T: Clone>(active_validators: &[ValidatorIndex], set: &[T]) -> Vec<T> {
|
||||
let subset: Vec<_> = active_validators
|
||||
/// Take an active subset of a set containing all validators.
|
||||
///
|
||||
/// First item in pair will be all items in set have indeces found in the `active` indices set (in
|
||||
/// the order of the `active` vec, the second item will contain the rest, in the original order.
|
||||
///
|
||||
/// ```ignore
|
||||
/// split_active_subset(active.into_iter().collect(), all).0 == take_active_subset(active, all)
|
||||
/// ```
|
||||
pub fn split_active_subset<T: Clone>(active: &[ValidatorIndex], all: &[T]) -> (Vec<T>, Vec<T>) {
|
||||
let active_set: BTreeSet<_> = active.iter().cloned().collect();
|
||||
// active result has ordering of active set.
|
||||
let active_result = take_active_subset(active, all);
|
||||
// inactive result preserves original ordering of `all`.
|
||||
let inactive_result = all
|
||||
.iter()
|
||||
.filter_map(|i| set.get(i.0 as usize))
|
||||
.enumerate()
|
||||
.filter(|(i, _)| !active_set.contains(&ValidatorIndex(*i as _)))
|
||||
.map(|(_, v)| v)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
if subset.len() != active_validators.len() {
|
||||
if active_result.len() != active.len() {
|
||||
log::warn!(
|
||||
target: "runtime::parachains",
|
||||
"Took active validators from set with wrong size.",
|
||||
);
|
||||
}
|
||||
|
||||
(active_result, inactive_result)
|
||||
}
|
||||
|
||||
/// Uses `split_active_subset` and concatenates the inactive to the active vec.
|
||||
pub fn take_active_subset_and_inactive<T: Clone>(active: &[ValidatorIndex], all: &[T]) -> Vec<T> {
|
||||
let (mut a, mut i) = split_active_subset(active, all);
|
||||
a.append(&mut i);
|
||||
a
|
||||
}
|
||||
|
||||
/// Take the active subset of a set containing all validators.
|
||||
pub fn take_active_subset<T: Clone>(active: &[ValidatorIndex], set: &[T]) -> Vec<T> {
|
||||
let subset: Vec<_> = active.iter().filter_map(|i| set.get(i.0 as usize)).cloned().collect();
|
||||
|
||||
if subset.len() != active.len() {
|
||||
log::warn!(
|
||||
target: "runtime::parachains",
|
||||
"Took active validators from set with wrong size",
|
||||
@@ -58,3 +92,23 @@ pub fn take_active_subset<T: Clone>(active_validators: &[ValidatorIndex], set: &
|
||||
|
||||
subset
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
use crate::util::{split_active_subset, take_active_subset};
|
||||
use primitives::v1::ValidatorIndex;
|
||||
|
||||
#[test]
|
||||
fn take_active_subset_is_compatible_with_split_active_subset() {
|
||||
let active: Vec<_> = vec![ValidatorIndex(1), ValidatorIndex(7), ValidatorIndex(3)];
|
||||
let validators = vec![9, 1, 6, 7, 4, 5, 2, 3, 0, 8];
|
||||
let (selected, unselected) = split_active_subset(&active, &validators);
|
||||
let selected2 = take_active_subset(&active, &validators);
|
||||
assert_eq!(selected, selected2);
|
||||
assert_eq!(unselected, vec![9, 6, 4, 5, 2, 0, 8]);
|
||||
assert_eq!(selected, vec![1, 3, 7]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user