fedimint_server/net/
p2p_connector.rs

1//! Provides an abstract network connector interface and multiple
2//! implementations
3
4mod iroh;
5mod tls;
6
7use std::sync::Arc;
8
9use async_trait::async_trait;
10use fedimint_core::PeerId;
11use fedimint_server_core::dashboard_ui::ConnectionType;
12
13pub use self::iroh::*;
14pub use self::tls::*;
15use crate::net::p2p_connection::DynP2PConnection;
16
17pub type DynP2PConnector<M> = Arc<dyn IP2PConnector<M>>;
18
19/// Allows to connect to peers and to listen for incoming connections.
20/// Connections are message based and should be authenticated and encrypted for
21/// production deployments.
22#[async_trait]
23pub trait IP2PConnector<M>: Send + Sync + 'static {
24    fn peers(&self) -> Vec<PeerId>;
25
26    async fn connect(&self, peer: PeerId) -> anyhow::Result<DynP2PConnection<M>>;
27
28    async fn accept(&self) -> anyhow::Result<(PeerId, DynP2PConnection<M>)>;
29
30    /// Get the connection type for a specific peer
31    async fn connection_type(&self, peer: PeerId) -> ConnectionType;
32
33    fn into_dyn(self) -> DynP2PConnector<M>
34    where
35        Self: Sized,
36    {
37        Arc::new(self)
38    }
39}