Skip to main content

commonware_glue/dkg/orchestrator/
mailbox.rs

1//! Mailbox for the [`Actor`].
2//!
3//! [`Actor`]: super::Actor
4
5use crate::dkg::ReshareBlock;
6use commonware_actor::{
7    Feedback,
8    mailbox::{Policy, Sender},
9};
10use commonware_consensus::{Reporter, marshal::Update};
11use commonware_utils::{Acknowledgement, acknowledgement::Exact};
12use std::{collections::VecDeque, sync::Arc};
13
14/// Messages that can be sent to the orchestrator.
15pub enum Message<B, A = Exact>
16where
17    B: ReshareBlock,
18    A: Acknowledgement,
19{
20    Finalized { block: Arc<B>, acknowledgement: A },
21}
22
23impl<B, A> Policy for Message<B, A>
24where
25    B: ReshareBlock,
26    A: Acknowledgement,
27{
28    type Overflow = VecDeque<Self>;
29
30    fn handle(overflow: &mut VecDeque<Self>, message: Self) {
31        // Ensure delivery
32        overflow.push_back(message);
33    }
34}
35
36/// Inbound communication channel for epoch transitions.
37#[derive(Debug, Clone)]
38pub struct Mailbox<B, A = Exact>
39where
40    B: ReshareBlock,
41    A: Acknowledgement,
42{
43    sender: Sender<Message<B, A>>,
44}
45
46impl<B, A> Mailbox<B, A>
47where
48    B: ReshareBlock,
49    A: Acknowledgement,
50{
51    /// Create a new [Mailbox].
52    pub const fn new(sender: Sender<Message<B, A>>) -> Self {
53        Self { sender }
54    }
55}
56
57impl<B, A> Reporter for Mailbox<B, A>
58where
59    B: ReshareBlock,
60    A: Acknowledgement,
61{
62    type Activity = Update<B, A>;
63
64    fn report(&mut self, activity: Self::Activity) -> Feedback {
65        let Update::Block(block, acknowledgement) = activity else {
66            return Feedback::Ok;
67        };
68        self.sender.enqueue(Message::Finalized {
69            block,
70            acknowledgement,
71        })
72    }
73}