-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsupervisor.rs
More file actions
83 lines (71 loc) · 2.15 KB
/
supervisor.rs
File metadata and controls
83 lines (71 loc) · 2.15 KB
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
use alto_types::leader_index;
use commonware_consensus::{
threshold_simplex::types::{Seed, View},
Supervisor as Su, ThresholdSupervisor as TSu,
};
use commonware_cryptography::{
bls12381::primitives::{
group,
poly::{self, Poly},
},
ed25519::PublicKey,
};
use std::collections::HashMap;
/// Implementation of `commonware-consensus::Supervisor`.
#[derive(Clone)]
pub struct Supervisor {
identity: Poly<group::Public>,
participants: Vec<PublicKey>,
participants_map: HashMap<PublicKey, u32>,
share: group::Share,
}
impl Supervisor {
pub fn new(
identity: Poly<group::Public>,
mut participants: Vec<PublicKey>,
share: group::Share,
) -> Self {
// Setup participants
participants.sort();
let mut participants_map = HashMap::new();
for (index, validator) in participants.iter().enumerate() {
participants_map.insert(validator.clone(), index as u32);
}
// Return supervisor
Self {
identity,
participants,
participants_map,
share,
}
}
}
impl Su for Supervisor {
type Index = View;
type PublicKey = PublicKey;
fn leader(&self, _: Self::Index) -> Option<Self::PublicKey> {
unimplemented!("only defined in supertrait")
}
fn participants(&self, _: Self::Index) -> Option<&Vec<Self::PublicKey>> {
Some(&self.participants)
}
fn is_participant(&self, _: Self::Index, candidate: &Self::PublicKey) -> Option<u32> {
self.participants_map.get(candidate).cloned()
}
}
impl TSu for Supervisor {
type Seed = group::Signature;
type Identity = poly::Public;
type Share = group::Share;
fn leader(&self, view: Self::Index, seed: Self::Seed) -> Option<Self::PublicKey> {
let seed = Seed::new(view, seed);
let index = leader_index(&seed, self.participants.len());
Some(self.participants[index].clone())
}
fn identity(&self, _: Self::Index) -> Option<&Self::Identity> {
Some(&self.identity)
}
fn share(&self, _: Self::Index) -> Option<&Self::Share> {
Some(&self.share)
}
}