Skip to main content

fedimint_server_core/
dashboard_ui.rs

1use std::collections::BTreeMap;
2use std::sync::Arc;
3use std::time::Duration;
4
5use async_trait::async_trait;
6use fedimint_core::admin_client::GuardianConfigBackup;
7use fedimint_core::bitcoin::Network;
8use fedimint_core::core::ModuleKind;
9use fedimint_core::module::ApiAuth;
10use fedimint_core::module::audit::AuditSummary;
11use fedimint_core::net::auth::GuardianAuthToken;
12use fedimint_core::session_outcome::SessionStatusV2;
13use fedimint_core::util::SafeUrl;
14use fedimint_core::{Feerate, PeerId};
15use serde::{Deserialize, Serialize};
16
17use crate::{DynServerModule, ServerModule};
18
19pub type DynDashboardApi = Arc<dyn IDashboardApi + Send + Sync + 'static>;
20
21/// Type of the connection to a peer. Mirrors iroh::endpoint::ConnectionType.
22#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum ConnectionType {
25    /// Direct UDP connectivity
26    Direct,
27    /// Going through an Iroh relay
28    Relay,
29    /// Both relay and direct paths available
30    Mixed,
31}
32
33/// P2P connection status for a peer. None indicates disconnected.
34#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
35pub struct P2PConnectionStatus {
36    /// The type of connection (Direct, Relay, Mixed), None if unknown
37    pub conn_type: Option<ConnectionType>,
38    /// Round-trip time (only available for iroh connections)
39    pub rtt: Option<Duration>,
40}
41
42/// Interface for guardian dashboard API in a running federation
43#[async_trait]
44pub trait IDashboardApi {
45    /// Get the guardian's authentication details
46    async fn auth(&self) -> ApiAuth;
47
48    /// Get the guardian ID
49    async fn guardian_id(&self) -> PeerId;
50
51    /// Get a map of peer IDs to guardian names
52    async fn guardian_names(&self) -> BTreeMap<PeerId, String>;
53
54    /// Get the federation name
55    async fn federation_name(&self) -> String;
56
57    /// Get the current active session count
58    async fn session_count(&self) -> u64;
59
60    /// Get items in a given session
61    async fn get_session_status(&self, session_idx: u64) -> SessionStatusV2;
62
63    /// The time it took to order our last proposal in the current session
64    async fn consensus_ord_latency(&self) -> Option<Duration>;
65
66    /// Returns a map of peer ID to connection status (None = disconnected)
67    async fn p2p_connection_status(&self) -> BTreeMap<PeerId, Option<P2PConnectionStatus>>;
68
69    /// Get the federation invite code to share with users
70    async fn federation_invite_code(&self) -> String;
71
72    /// Get the federation audit summary
73    async fn federation_audit(&self) -> AuditSummary;
74
75    /// Get the url of the bitcoin rpc
76    async fn bitcoin_rpc_url(&self) -> SafeUrl;
77
78    /// Get the status of the bitcoin backend
79    async fn bitcoin_rpc_status(&self) -> Option<ServerBitcoinRpcStatus>;
80
81    /// Download a backup of the guardian's configuration
82    async fn download_guardian_config_backup(
83        &self,
84        password: &str,
85        guardian_auth: &GuardianAuthToken,
86    ) -> GuardianConfigBackup;
87
88    /// Get reference to a server module instance by module kind
89    fn get_module_by_kind(&self, kind: ModuleKind) -> Option<&DynServerModule>;
90
91    /// Get the fedimintd version
92    async fn fedimintd_version(&self) -> String;
93
94    /// Get the git hash of the fedimintd binary, or `None` if it is not
95    /// available (e.g. it was built outside a git checkout).
96    async fn fedimintd_version_hash(&self) -> Option<String>;
97
98    /// Change the guardian password
99    async fn change_password(
100        &self,
101        new_password: &str,
102        current_password: &str,
103        guardian_auth: &GuardianAuthToken,
104    ) -> Result<(), String>;
105
106    /// Create a trait object
107    fn into_dyn(self) -> DynDashboardApi
108    where
109        Self: Sized + Send + Sync + 'static,
110    {
111        Arc::new(self)
112    }
113}
114
115/// Extension trait for IDashboardApi providing type-safe module access
116pub trait DashboardApiModuleExt {
117    /// Get a typed reference to a server module instance by kind
118    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M>;
119}
120
121impl DashboardApiModuleExt for DynDashboardApi {
122    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M> {
123        self.get_module_by_kind(M::module_kind())?
124            .as_any()
125            .downcast_ref::<M>()
126    }
127}
128
129#[derive(Debug, Clone)]
130pub struct ServerBitcoinRpcStatus {
131    pub network: Network,
132    pub block_count: u64,
133    pub fee_rate: Feerate,
134    pub sync_progress: Option<f64>,
135}