use commonware_consensus::types::Epoch;
use commonware_cryptography::{
bls12381::primitives::{group, sharing::Sharing, variant::Variant},
PublicKey,
};
use commonware_utils::{channel::mpsc, ordered::Set};
use tracing::error;
pub enum Message<V: Variant, P: PublicKey> {
Enter(EpochTransition<V, P>),
Exit(Epoch),
}
pub struct EpochTransition<V: Variant, P: PublicKey> {
pub epoch: Epoch,
pub poly: Option<Sharing<V>>,
pub share: Option<group::Share>,
pub dealers: Set<P>,
}
#[derive(Debug, Clone)]
pub struct Mailbox<V: Variant, P: PublicKey> {
sender: mpsc::Sender<Message<V, P>>,
}
impl<V: Variant, P: PublicKey> Mailbox<V, P> {
pub const fn new(sender: mpsc::Sender<Message<V, P>>) -> Self {
Self { sender }
}
pub async fn enter(&mut self, transition: EpochTransition<V, P>) {
if let Err(err) = self.sender.send(Message::Enter(transition)).await {
error!(?err, "failed to send epoch transition");
}
}
pub async fn exit(&mut self, epoch: Epoch) {
if let Err(err) = self.sender.send(Message::Exit(epoch)).await {
error!(?err, "failed to send epoch exit");
}
}
}