polkadot_statement_distribution/v2/
groups.rs1use polkadot_primitives::{
20 effective_minimum_backing_votes, GroupIndex, IndexedVec, ValidatorIndex,
21};
22
23use std::collections::HashMap;
24
25#[derive(Debug, Clone)]
28pub struct Groups {
29 groups: IndexedVec<GroupIndex, Vec<ValidatorIndex>>,
30 by_validator_index: HashMap<ValidatorIndex, GroupIndex>,
31 backing_threshold: u32,
32}
33
34impl Groups {
35 pub fn new(
38 groups: IndexedVec<GroupIndex, Vec<ValidatorIndex>>,
39 backing_threshold: u32,
40 ) -> Self {
41 let mut by_validator_index = HashMap::new();
42
43 for (i, group) in groups.iter().enumerate() {
44 let index = GroupIndex(i as _);
45 for v in group {
46 by_validator_index.insert(*v, index);
47 }
48 }
49
50 Groups { groups, by_validator_index, backing_threshold }
51 }
52
53 pub fn all(&self) -> &IndexedVec<GroupIndex, Vec<ValidatorIndex>> {
55 &self.groups
56 }
57
58 pub fn get(&self, group_index: GroupIndex) -> Option<&[ValidatorIndex]> {
60 self.groups.get(group_index).map(|x| &x[..])
61 }
62
63 pub fn get_size_and_backing_threshold(
65 &self,
66 group_index: GroupIndex,
67 ) -> Option<(usize, usize)> {
68 self.get(group_index)
69 .map(|g| (g.len(), effective_minimum_backing_votes(g.len(), self.backing_threshold)))
70 }
71
72 pub fn by_validator_index(&self, validator_index: ValidatorIndex) -> Option<GroupIndex> {
74 self.by_validator_index.get(&validator_index).map(|x| *x)
75 }
76}