blueprint_networking/
types.rs

1use libp2p::{PeerId, gossipsub::IdentTopic};
2use serde::{Deserialize, Serialize};
3use std::fmt::Display;
4
5/// Maximum allowed size for a message payload
6pub const MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;
7
8/// Type of message delivery mechanism
9#[derive(Debug, Clone)]
10pub enum MessageDelivery {
11    /// Broadcast to all peers via gossipsub
12    Broadcast {
13        /// The topic to broadcast on
14        topic: IdentTopic,
15    },
16    /// Direct P2P message to a specific peer
17    Direct {
18        /// The target peer ID
19        peer_id: PeerId,
20    },
21}
22
23/// Message routing information
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct MessageRouting {
26    /// Unique identifier for this message
27    pub message_id: u64,
28    /// The round/sequence number this message belongs to
29    pub round_id: u16,
30    /// The sender's information
31    pub sender: PeerId,
32    /// Optional recipient information for direct messages
33    pub recipient: Option<PeerId>,
34}
35
36/// A protocol message that can be sent over the network
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct ProtocolMessage {
39    /// The protocol name
40    pub protocol: String,
41    /// Routing information for the message
42    pub routing: MessageRouting,
43    /// The actual message payload
44    pub payload: Vec<u8>,
45}
46
47impl Display for MessageRouting {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        write!(
50            f,
51            "msg={} round={} from={} to={:?}",
52            self.message_id, self.round_id, self.sender, self.recipient
53        )
54    }
55}
56
57impl Display for ProtocolMessage {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(
60            f,
61            "[{}] {} payload_size={}",
62            self.protocol,
63            self.routing,
64            self.payload.len()
65        )
66    }
67}