use commonware_actor::mailbox::{Policy, Sender};
use commonware_consensus::{
marshal::core::{Mailbox as MarshalMailbox, Variant},
simplex::types::Finalization,
};
use commonware_cryptography::certificate::Scheme;
use commonware_utils::channel::oneshot;
use std::collections::VecDeque;
pub(crate) enum Message<S, V>
where
S: Scheme,
V: Variant,
{
Subscribe {
response: oneshot::Sender<Finalization<S, V::Commitment>>,
},
Attach {
marshal: MarshalMailbox<S, V>,
},
}
impl<S, V> Policy for Message<S, V>
where
S: Scheme,
V: Variant,
{
type Overflow = VecDeque<Self>;
fn handle(overflow: &mut Self::Overflow, message: Self) {
overflow.push_back(message);
}
}
#[derive(Clone)]
pub struct Mailbox<S, V>
where
S: Scheme,
V: Variant,
{
sender: Sender<Message<S, V>>,
}
impl<S, V> Mailbox<S, V>
where
S: Scheme,
V: Variant,
{
pub(crate) const fn new(sender: Sender<Message<S, V>>) -> Self {
Self { sender }
}
pub fn subscribe(&self) -> oneshot::Receiver<Finalization<S, V::Commitment>> {
let (tx, rx) = oneshot::channel();
let _ = self.sender.enqueue(Message::Subscribe { response: tx });
rx
}
pub fn attach(&self, marshal: MarshalMailbox<S, V>) {
let _ = self.sender.enqueue(Message::Attach { marshal });
}
}