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