polkadot_runtime_parachains/
util.rs1use alloc::{collections::btree_set::BTreeSet, vec::Vec};
21use frame_system::pallet_prelude::BlockNumberFor;
22use polkadot_primitives::{HeadData, Id as ParaId, PersistedValidationData, ValidatorIndex};
23
24use crate::{configuration, hrmp, paras};
25
26pub fn make_persisted_validation_data<T: paras::Config + hrmp::Config>(
31 para_id: ParaId,
32 relay_parent_number: BlockNumberFor<T>,
33 relay_parent_storage_root: T::Hash,
34) -> Option<PersistedValidationData<T::Hash, BlockNumberFor<T>>> {
35 let config = configuration::ActiveConfig::<T>::get();
36
37 Some(PersistedValidationData {
38 parent_head: paras::Heads::<T>::get(¶_id)?,
39 relay_parent_number,
40 relay_parent_storage_root,
41 max_pov_size: config.max_pov_size,
42 })
43}
44
45pub fn make_persisted_validation_data_with_parent<T: configuration::Config>(
48 relay_parent_number: BlockNumberFor<T>,
49 relay_parent_storage_root: T::Hash,
50 parent_head: HeadData,
51) -> PersistedValidationData<T::Hash, BlockNumberFor<T>> {
52 let config = configuration::ActiveConfig::<T>::get();
53
54 PersistedValidationData {
55 parent_head,
56 relay_parent_number,
57 relay_parent_storage_root,
58 max_pov_size: config.max_pov_size,
59 }
60}
61
62pub fn split_active_subset<T: Clone>(active: &[ValidatorIndex], all: &[T]) -> (Vec<T>, Vec<T>) {
71 let active_set: BTreeSet<_> = active.iter().cloned().collect();
72 let active_result = take_active_subset(active, all);
74 let inactive_result = all
76 .iter()
77 .enumerate()
78 .filter(|(i, _)| !active_set.contains(&ValidatorIndex(*i as _)))
79 .map(|(_, v)| v)
80 .cloned()
81 .collect();
82
83 if active_result.len() != active.len() {
84 log::warn!(
85 target: "runtime::parachains",
86 "Took active validators from set with wrong size.",
87 );
88 }
89
90 (active_result, inactive_result)
91}
92
93pub fn take_active_subset_and_inactive<T: Clone>(active: &[ValidatorIndex], all: &[T]) -> Vec<T> {
99 let (mut a, mut i) = split_active_subset(active, all);
100 a.append(&mut i);
101 a
102}
103
104pub fn take_active_subset<T: Clone>(active: &[ValidatorIndex], set: &[T]) -> Vec<T> {
106 let subset: Vec<_> = active.iter().filter_map(|i| set.get(i.0 as usize)).cloned().collect();
107
108 if subset.len() != active.len() {
109 log::warn!(
110 target: "runtime::parachains",
111 "Took active validators from set with wrong size",
112 );
113 }
114
115 subset
116}
117
118#[cfg(test)]
119mod tests {
120
121 use alloc::vec::Vec;
122
123 use crate::util::{split_active_subset, take_active_subset};
124 use polkadot_primitives::ValidatorIndex;
125
126 #[test]
127 fn take_active_subset_is_compatible_with_split_active_subset() {
128 let active: Vec<_> = vec![ValidatorIndex(1), ValidatorIndex(7), ValidatorIndex(3)];
129 let validators = vec![9, 1, 6, 7, 4, 5, 2, 3, 0, 8];
130 let (selected, unselected) = split_active_subset(&active, &validators);
131 let selected2 = take_active_subset(&active, &validators);
132 assert_eq!(selected, selected2);
133 assert_eq!(unselected, vec![9, 6, 4, 5, 2, 0, 8]);
134 assert_eq!(selected, vec![1, 3, 7]);
135 }
136}