Skip to main content

fedimint_hbbft/sender_queue/
message.rs

1use rand::distributions::{Distribution, Standard};
2use rand::{seq::SliceRandom, Rng};
3use serde::{Deserialize, Serialize};
4
5use super::SenderQueueableMessage;
6
7/// A `SenderQueue` message.
8#[derive(Clone, Debug, Deserialize, Serialize)]
9pub enum Message<M: SenderQueueableMessage> {
10    /// The announcement that this node has reached the given epoch.
11    EpochStarted(M::Epoch),
12    /// A message of the wrapped algorithm.
13    Algo(M),
14}
15
16impl<M: SenderQueueableMessage> Distribution<Message<M>> for Standard
17where
18    Standard: Distribution<M> + Distribution<M::Epoch>,
19{
20    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Message<M> {
21        let message_type = *["epoch", "algo"].choose(rng).unwrap();
22
23        match message_type {
24            "epoch" => Message::EpochStarted(rng.gen()),
25            "algo" => Message::Algo(rng.gen()),
26            _ => unreachable!(),
27        }
28    }
29}
30
31impl<M: SenderQueueableMessage> From<M> for Message<M> {
32    fn from(message: M) -> Self {
33        Message::Algo(message)
34    }
35}