use commonware_codec::Encode; use commonware_consensus::{ threshold_simplex::types::View, Supervisor as Su, ThresholdSupervisor as TSu, }; use commonware_cryptography::{ bls12381::{ dkg::ops::evaluate_all, primitives::{ group, poly::{self, Public}, variant::{MinSig, Variant}, }, }, PublicKey, }; use commonware_utils::modulo; use std::collections::HashMap; /// Implementation of `commonware-consensus::Supervisor`. #[derive(Clone)] pub struct Supervisor { identity: ::Public, polynomial: Vec<::Public>, participants: Vec

, participants_map: HashMap, share: group::Share, } impl Supervisor

{ pub fn new(polynomial: Public, mut participants: Vec

, 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); } let identity = *poly::public::(&polynomial); let polynomial = evaluate_all::(&polynomial, participants.len() as u32); // Return supervisor Self { identity, polynomial, participants, participants_map, share, } } } impl Su for Supervisor

{ type Index = View; type PublicKey = P; fn leader(&self, _: Self::Index) -> Option { unimplemented!("only defined in supertrait") } fn participants(&self, _: Self::Index) -> Option<&Vec> { Some(&self.participants) } fn is_participant(&self, _: Self::Index, candidate: &Self::PublicKey) -> Option { self.participants_map.get(candidate).cloned() } } impl TSu for Supervisor

{ type Seed = ::Signature; type Polynomial = Vec<::Public>; type Share = group::Share; type Identity = ::Public; fn leader(&self, _: Self::Index, seed: Self::Seed) -> Option { let seed = seed.encode(); let index = modulo(&seed, self.participants.len() as u64); Some(self.participants[index as usize].clone()) } fn identity(&self) -> &Self::Identity { &self.identity } fn polynomial(&self, _: Self::Index) -> Option<&Self::Polynomial> { Some(&self.polynomial) } fn share(&self, _: Self::Index) -> Option<&Self::Share> { Some(&self.share) } }