1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use primitives::v2::{Id as ParaId, PersistedValidationData, ValidatorIndex};
use sp_std::{collections::btree_set::BTreeSet, vec::Vec};
use crate::{configuration, hrmp, paras};
pub fn make_persisted_validation_data<T: paras::Config + hrmp::Config>(
para_id: ParaId,
relay_parent_number: T::BlockNumber,
relay_parent_storage_root: T::Hash,
) -> Option<PersistedValidationData<T::Hash, T::BlockNumber>> {
let config = <configuration::Pallet<T>>::config();
Some(PersistedValidationData {
parent_head: <paras::Pallet<T>>::para_head(¶_id)?,
relay_parent_number,
relay_parent_storage_root,
max_pov_size: config.max_pov_size,
})
}
pub fn split_active_subset<T: Clone>(active: &[ValidatorIndex], all: &[T]) -> (Vec<T>, Vec<T>) {
let active_set: BTreeSet<_> = active.iter().cloned().collect();
let active_result = take_active_subset(active, all);
let inactive_result = all
.iter()
.enumerate()
.filter(|(i, _)| !active_set.contains(&ValidatorIndex(*i as _)))
.map(|(_, v)| v)
.cloned()
.collect();
if active_result.len() != active.len() {
log::warn!(
target: "runtime::parachains",
"Took active validators from set with wrong size.",
);
}
(active_result, inactive_result)
}
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
}
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",
);
}
subset
}
#[cfg(test)]
mod tests {
use sp_std::vec::Vec;
use crate::util::{split_active_subset, take_active_subset};
use primitives::v2::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]);
}
}