libp2p_pubsub_core/
message.rs

1//! The `Message` type represents a bit of data which is published to a pubsub topic, distributed
2//! over a pubsub network and received by any node subscribed to the topic.
3//!
4//! This module contains the public API type of a pubsub message.
5
6use bytes::Bytes;
7use libp2p::identity::PeerId;
8
9use crate::topic::TopicHash;
10
11/// A pubsub message.
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct Message {
14    /// The author of this message.
15    pub from: Option<PeerId>,
16    /// The data of this message.
17    pub data: Vec<u8>,
18    /// The sequence number of this message.
19    pub sequence_number: Option<Bytes>,
20    /// The topic this message is published to.
21    pub topic: TopicHash,
22    /// The signature of this message.
23    pub signature: Option<Vec<u8>>,
24    /// The key of this message.
25    pub key: Option<Vec<u8>>,
26}
27
28impl Message {
29    /// Creates a new message.
30    #[must_use]
31    pub fn new(topic: impl Into<TopicHash>, data: impl Into<Vec<u8>>) -> Self {
32        Self {
33            from: None,
34            data: data.into(),
35            sequence_number: None,
36            topic: topic.into(),
37            signature: None,
38            key: None,
39        }
40    }
41
42    /// Creates a new message with a sequence number.
43    #[must_use]
44    pub fn new_with_sequence_number(
45        topic: impl Into<TopicHash>,
46        data: impl Into<Vec<u8>>,
47        seq_no: impl Into<Vec<u8>>,
48    ) -> Self {
49        Self {
50            from: None,
51            data: data.into(),
52            sequence_number: Some(Bytes::from(seq_no.into())),
53            topic: topic.into(),
54            signature: None,
55            key: None,
56        }
57    }
58}