fedimint_core/net/
peers.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use fedimint_core::PeerId;
5
6#[cfg(not(target_family = "wasm"))]
7pub mod fake;
8
9pub type DynP2PConnections<M> = Arc<dyn IP2PConnections<M>>;
10
11/// Connection manager that tries to keep connections open to all peers
12#[async_trait]
13pub trait IP2PConnections<M>: Send + Sync + 'static {
14    /// Send message to recipient; block if channel is full.
15    async fn send(&self, recipient: Recipient, msg: M);
16
17    /// Try to send message to recipient; drop message if channel is full.
18    fn try_send(&self, recipient: Recipient, msg: M);
19
20    /// Await the next message; return None if we are shutting down.
21    async fn receive(&self) -> Option<(PeerId, M)>;
22
23    /// Await the next message from peer; return None if we are shutting down.
24    async fn receive_from_peer(&self, peer: PeerId) -> Option<M>;
25
26    /// Convert the struct to trait object.
27    fn into_dyn(self) -> DynP2PConnections<M>
28    where
29        Self: Sized,
30    {
31        Arc::new(self)
32    }
33}
34
35/// This enum defines the intended recipient of a p2p message.
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum Recipient {
38    Everyone,
39    Peer(PeerId),
40}