use commonware_consensus::{ simplex::types::{Activity, View}, Reporter, Supervisor as Su, Viewable, }; use commonware_cryptography::{Digest, PublicKey, Signature}; use std::{collections::HashMap, marker::PhantomData}; use tracing::info; /// Implementation of `commonware-consensus::Supervisor`. // TODO(#755): Use `commonware-cryptography::Specification`. #[derive(Clone)] pub struct Supervisor { participants: Vec

, participants_map: HashMap, _phantom_s: PhantomData, _phantom_d: PhantomData, } impl Supervisor { pub fn new(mut participants: Vec

) -> 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 { participants, participants_map, _phantom_s: PhantomData, _phantom_d: PhantomData, } } } impl Su for Supervisor { type Index = View; type PublicKey = P; fn leader(&self, index: Self::Index) -> Option { Some(self.participants[index as usize % self.participants.len()].clone()) } 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 Reporter for Supervisor { type Activity = Activity; async fn report(&mut self, activity: Self::Activity) { let view = activity.view(); match activity { Activity::Notarization(notarization) => { info!(view, payload = ?notarization.proposal.payload, "notarized"); } Activity::Finalization(finalization) => { info!(view, payload = ?finalization.proposal.payload, "finalized"); } Activity::Nullification(_) => { info!(view, "nullified"); } _ => {} } } }