commonware_p2p/
lib.rs

1//! Communicate with authenticated peers over encrypted connections.
2//!
3//! # Status
4//!
5//! `commonware-p2p` is **ALPHA** software and is not yet recommended for production use. Developers should
6//! expect breaking changes and occasional instability.
7
8use bytes::Bytes;
9use commonware_cryptography::PublicKey;
10use std::{error::Error as StdError, fmt::Debug, future::Future};
11
12pub mod authenticated;
13pub mod simulated;
14pub mod utils;
15
16/// Tuple representing a message received from a given public key.
17///
18/// This message is guaranteed to adhere to the configuration of the channel and
19/// will already be decrypted and authenticated.
20pub type Message<P> = (P, Bytes);
21
22/// Alias for identifying communication channels.
23pub type Channel = u32;
24
25/// Enum indicating the set of recipients to send a message to.
26#[derive(Clone)]
27pub enum Recipients<P: PublicKey> {
28    All,
29    Some(Vec<P>),
30    One(P),
31}
32
33/// Interface for sending messages to a set of recipients.
34pub trait Sender: Clone + Debug + Send + 'static {
35    /// Error that can occur when sending a message.
36    type Error: Debug + StdError + Send;
37
38    /// Public key type used to identify recipients.
39    type PublicKey: PublicKey;
40
41    /// Send a message to a set of recipients.
42    fn send(
43        &mut self,
44        recipients: Recipients<Self::PublicKey>,
45        message: Bytes,
46        priority: bool,
47    ) -> impl Future<Output = Result<Vec<Self::PublicKey>, Self::Error>> + Send;
48}
49
50/// Interface for receiving messages from arbitrary recipients.
51pub trait Receiver: Debug + Send + 'static {
52    /// Error that can occur when receiving a message.
53    type Error: Debug + StdError + Send;
54
55    /// Public key type used to identify recipients.
56    type PublicKey: PublicKey;
57
58    /// Receive a message from an arbitrary recipient.
59    fn recv(
60        &mut self,
61    ) -> impl Future<Output = Result<Message<Self::PublicKey>, Self::Error>> + Send;
62}
63
64/// Interface for blocking other peers.
65pub trait Blocker: Clone + Send + 'static {
66    /// Public key type used to identify peers.
67    type PublicKey: PublicKey;
68
69    /// Block a peer, disconnecting them if currently connected and preventing future connections.
70    fn block(&mut self, peer: Self::PublicKey) -> impl Future<Output = ()> + Send;
71}