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
use crate::{Dreps, VotingGroup};
use jormungandr_lib::crypto::account::Identifier;
use std::collections::HashSet;

pub const DEFAULT_DIRECT_VOTER_GROUP: &str = "direct";
pub const DEFAULT_REPRESENTATIVE_GROUP: &str = "rep";

pub trait VotingGroupAssigner {
    fn assign(&self, vk: &Identifier) -> VotingGroup;
}

pub struct RepsVotersAssigner {
    direct_voters: VotingGroup,
    reps: VotingGroup,
    dreps: HashSet<Identifier>,
}

impl RepsVotersAssigner {
    pub fn new(direct_voters: VotingGroup, reps: VotingGroup, dreps: Dreps) -> Self {
        Self {
            direct_voters,
            reps,
            dreps: dreps.reps,
        }
    }
}

impl VotingGroupAssigner for RepsVotersAssigner {
    fn assign(&self, vk: &Identifier) -> VotingGroup {
        if self.dreps.contains(vk) {
            self.reps.clone()
        } else {
            self.direct_voters.clone()
        }
    }
}

#[cfg(any(test, feature = "test-api", feature = "proptest"))]
impl<F> VotingGroupAssigner for F
where
    F: Fn(&Identifier) -> VotingGroup,
{
    fn assign(&self, vk: &Identifier) -> VotingGroup {
        self(vk)
    }
}