fedimint_hbbft/subset/
message.rs

1use rand::distributions::{Distribution, Standard};
2use rand::Rng;
3use rand_derive::Rand;
4use serde::{Deserialize, Serialize};
5
6use crate::binary_agreement;
7use crate::broadcast;
8
9/// Message from Subset to remote nodes.
10#[derive(Serialize, Deserialize, Clone, Debug)]
11pub struct Message<N> {
12    /// The proposer whose contribution this message is about.
13    pub proposer_id: N,
14    /// The wrapped broadcast or agreement message.
15    pub content: MessageContent,
16}
17
18impl<N> Distribution<Message<N>> for Standard
19where
20    Standard: Distribution<N>,
21{
22    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Message<N> {
23        Message {
24            proposer_id: rng.gen::<N>(),
25            content: rng.gen::<MessageContent>(),
26        }
27    }
28}
29
30/// A message about a particular proposer's contribution.
31#[derive(Serialize, Deserialize, Clone, Debug, Rand)]
32pub enum MessageContent {
33    /// A wrapped message for the broadcast instance, to deliver the proposed value.
34    Broadcast(broadcast::Message),
35    /// A wrapped message for the agreement instance, to decide on whether to accept the value.
36    Agreement(binary_agreement::Message),
37}
38
39impl MessageContent {
40    /// Returns a `Message` with this content and the specified proposer ID.
41    pub(super) fn with<N>(self, proposer_id: N) -> Message<N> {
42        Message {
43            proposer_id,
44            content: self,
45        }
46    }
47}