//! Inbound communication channel for epoch transitions. use commonware_actor::mailbox::{Policy, Sender}; use commonware_consensus::types::Epoch; use commonware_cryptography::{ bls12381::primitives::{group, sharing::Sharing, variant::Variant}, PublicKey, }; use commonware_utils::ordered::Set; use std::collections::VecDeque; use tracing::error; /// Messages that can be sent to the orchestrator. pub enum Message { Enter(EpochTransition), Exit(Epoch), } impl Policy for Message { type Overflow = VecDeque; fn handle(overflow: &mut VecDeque, message: Self) { match message { Self::Enter(transition) => { let epoch = transition.epoch; if let Some(index) = overflow .iter() .position(|pending| matches!(pending, Self::Exit(pending) if *pending == epoch)) { overflow.remove(index); } else { overflow.push_back(Self::Enter(transition)); } } Self::Exit(epoch) => { if let Some(index) = overflow.iter().position( |pending| matches!(pending, Self::Enter(pending) if pending.epoch == epoch), ) { overflow.remove(index); } else { overflow.push_back(Self::Exit(epoch)); } } } } } /// A notification of an epoch transition. pub struct EpochTransition { /// The epoch to transition to. pub epoch: Epoch, /// The public polynomial for the epoch. pub poly: Option>, /// The share for the local participant for the epoch, if participating. pub share: Option, /// The dealers for the epoch. pub dealers: Set

, } /// Inbound communication channel for epoch transitions. #[derive(Debug, Clone)] pub struct Mailbox { sender: Sender>, } impl Mailbox { /// Create a new [Mailbox]. pub const fn new(sender: Sender>) -> Self { Self { sender } } pub fn enter(&mut self, transition: EpochTransition) { if !self.sender.enqueue(Message::Enter(transition)).accepted() { error!("failed to send epoch transition"); } } pub fn exit(&mut self, epoch: Epoch) { if !self.sender.enqueue(Message::Exit(epoch)).accepted() { error!("failed to send epoch exit"); } } }