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_utils::Array;
10use std::error::Error as StdError;
11use std::fmt::Debug;
12use std::future::Future;
13
14pub mod authenticated;
15pub mod simulated;
16pub mod utils;
17
18/// Tuple representing a message received from a given public key.
19///
20/// This message is guaranteed to adhere to the configuration of the channel and
21/// will already be decrypted and authenticated.
22pub type Message<P> = (P, Bytes);
23
24/// Alias for identifying communication channels.
25pub type Channel = u32;
26
27/// Enum indicating the set of recipients to send a message to.
28#[derive(Clone)]
29pub enum Recipients<P: Array> {
30    All,
31    Some(Vec<P>),
32    One(P),
33}
34
35/// Interface for sending messages to a set of recipients.
36pub trait Sender: Clone + Debug + Send + 'static {
37    /// Error that can occur when sending a message.
38    type Error: Debug + StdError + Send;
39
40    /// Public key type used to identify recipients.
41    type PublicKey: Array;
42
43    /// Send a message to a set of recipients.
44    fn send(
45        &mut self,
46        recipients: Recipients<Self::PublicKey>,
47        message: Bytes,
48        priority: bool,
49    ) -> impl Future<Output = Result<Vec<Self::PublicKey>, Self::Error>> + Send;
50}
51
52/// Interface for receiving messages from arbitrary recipients.
53pub trait Receiver: Debug + Send + 'static {
54    /// Error that can occur when receiving a message.
55    type Error: Debug + StdError + Send;
56
57    /// Public key type used to identify recipients.
58    type PublicKey: Array;
59
60    /// Receive a message from an arbitrary recipient.
61    fn recv(
62        &mut self,
63    ) -> impl Future<Output = Result<Message<Self::PublicKey>, Self::Error>> + Send;
64}