use crate::{
simplex::{
metrics::TimeoutReason,
types::{Certificate, Proposal},
},
types::View,
};
use commonware_cryptography::{certificate::Scheme, Digest};
use commonware_utils::channel::{fallible::AsyncFallibleExt, mpsc};
pub enum Message<S: Scheme, D: Digest> {
Proposal(Proposal<D>),
Timeout(View, TimeoutReason),
Verified(Certificate<S, D>, bool),
}
#[derive(Clone)]
pub struct Mailbox<S: Scheme, D: Digest> {
sender: mpsc::Sender<Message<S, D>>,
}
impl<S: Scheme, D: Digest> Mailbox<S, D> {
pub const fn new(sender: mpsc::Sender<Message<S, D>>) -> Self {
Self { sender }
}
pub async fn proposal(&mut self, proposal: Proposal<D>) {
self.sender.send_lossy(Message::Proposal(proposal)).await;
}
pub async fn timeout(&mut self, view: View, reason: TimeoutReason) {
self.sender.send_lossy(Message::Timeout(view, reason)).await;
}
pub async fn recovered(&mut self, certificate: Certificate<S, D>) {
self.sender
.send_lossy(Message::Verified(certificate, false))
.await;
}
pub async fn resolved(&mut self, certificate: Certificate<S, D>) {
self.sender
.send_lossy(Message::Verified(certificate, true))
.await;
}
}