alto_chain/
supervisor.rs

1use alto_types::{leader_index, Evaluation, Identity, Signature};
2use commonware_codec::Encode;
3use commonware_consensus::{
4    threshold_simplex::types::View, Supervisor as Su, ThresholdSupervisor as TSu,
5};
6use commonware_cryptography::{
7    bls12381::{
8        dkg::ops::evaluate_all,
9        primitives::{
10            group,
11            poly::{self, Poly},
12            variant::MinSig,
13        },
14    },
15    ed25519,
16};
17use commonware_resolver::p2p;
18use std::collections::HashMap;
19
20/// Implementation of [commonware_consensus::Supervisor].
21#[derive(Clone)]
22pub struct Supervisor {
23    identity: Identity,
24    polynomial: Vec<Evaluation>,
25    participants: Vec<ed25519::PublicKey>,
26    participants_map: HashMap<ed25519::PublicKey, u32>,
27
28    share: group::Share,
29}
30
31impl Supervisor {
32    /// Create a new [Supervisor].
33    pub fn new(
34        polynomial: Poly<Evaluation>,
35        mut participants: Vec<ed25519::PublicKey>,
36        share: group::Share,
37    ) -> Self {
38        // Setup participants
39        participants.sort();
40        let mut participants_map = HashMap::new();
41        for (index, validator) in participants.iter().enumerate() {
42            participants_map.insert(validator.clone(), index as u32);
43        }
44        let identity = *poly::public::<MinSig>(&polynomial);
45        let polynomial = evaluate_all::<MinSig>(&polynomial, participants.len() as u32);
46
47        // Return supervisor
48        Self {
49            identity,
50            polynomial,
51            participants,
52            participants_map,
53            share,
54        }
55    }
56}
57
58impl p2p::Coordinator for Supervisor {
59    type PublicKey = ed25519::PublicKey;
60
61    fn peers(&self) -> &Vec<Self::PublicKey> {
62        &self.participants
63    }
64
65    fn peer_set_id(&self) -> u64 {
66        0
67    }
68}
69
70impl Su for Supervisor {
71    type Index = View;
72    type PublicKey = ed25519::PublicKey;
73
74    fn leader(&self, _: Self::Index) -> Option<Self::PublicKey> {
75        unimplemented!("only defined in supertrait")
76    }
77
78    fn participants(&self, _: Self::Index) -> Option<&Vec<Self::PublicKey>> {
79        Some(&self.participants)
80    }
81
82    fn is_participant(&self, _: Self::Index, candidate: &Self::PublicKey) -> Option<u32> {
83        self.participants_map.get(candidate).cloned()
84    }
85}
86
87impl TSu for Supervisor {
88    type Seed = Signature;
89    type Identity = Identity;
90    type Polynomial = Vec<Evaluation>;
91    type Share = group::Share;
92
93    fn leader(&self, _: Self::Index, seed: Self::Seed) -> Option<Self::PublicKey> {
94        let index = leader_index(seed.encode().as_ref(), self.participants.len());
95        Some(self.participants[index].clone())
96    }
97
98    fn identity(&self) -> &Self::Identity {
99        &self.identity
100    }
101
102    fn polynomial(&self, _: Self::Index) -> Option<&Self::Polynomial> {
103        Some(&self.polynomial)
104    }
105
106    fn share(&self, _: Self::Index) -> Option<&Self::Share> {
107        Some(&self.share)
108    }
109}