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::session_outcome::SessionStatusV2;
12use fedimint_core::util::SafeUrl;
13use fedimint_core::{Feerate, PeerId};
14use serde::{Deserialize, Serialize};
15
16use crate::net::GuardianAuthToken;
17use crate::{DynServerModule, ServerModule};
18
19pub type DynDashboardApi = Arc<dyn IDashboardApi + Send + Sync + 'static>;
20
21/// Type of the connection to a peer
22#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum ConnectionType {
25    /// Direct connectivity
26    Direct,
27    /// Going through an Iroh relay
28    Relay,
29    /// Unknown connection type
30    Unknown,
31}
32
33/// Interface for guardian dashboard API in a running federation
34#[async_trait]
35pub trait IDashboardApi {
36    /// Get the guardian's authentication details
37    async fn auth(&self) -> ApiAuth;
38
39    /// Get the guardian ID
40    async fn guardian_id(&self) -> PeerId;
41
42    /// Get a map of peer IDs to guardian names
43    async fn guardian_names(&self) -> BTreeMap<PeerId, String>;
44
45    /// Get the federation name
46    async fn federation_name(&self) -> String;
47
48    /// Get the current active session count
49    async fn session_count(&self) -> u64;
50
51    /// Get items in a given session
52    async fn get_session_status(&self, session_idx: u64) -> SessionStatusV2;
53
54    /// The time it took to order our last proposal in the current session
55    async fn consensus_ord_latency(&self) -> Option<Duration>;
56
57    /// Returns a map of peer ID to estimated round trip time
58    async fn p2p_connection_status(&self) -> BTreeMap<PeerId, Option<Duration>>;
59
60    /// Returns a map of peer ID to connection type (Direct or Relay)
61    async fn p2p_connection_type_status(&self) -> BTreeMap<PeerId, ConnectionType>;
62
63    /// Get the federation invite code to share with users
64    async fn federation_invite_code(&self) -> String;
65
66    /// Get the federation audit summary
67    async fn federation_audit(&self) -> AuditSummary;
68
69    /// Get the url of the bitcoin rpc
70    async fn bitcoin_rpc_url(&self) -> SafeUrl;
71
72    /// Get the status of the bitcoin backend
73    async fn bitcoin_rpc_status(&self) -> Option<ServerBitcoinRpcStatus>;
74
75    /// Download a backup of the guardian's configuration
76    async fn download_guardian_config_backup(
77        &self,
78        password: &str,
79        guardian_auth: &GuardianAuthToken,
80    ) -> GuardianConfigBackup;
81
82    /// Get reference to a server module instance by module kind
83    fn get_module_by_kind(&self, kind: ModuleKind) -> Option<&DynServerModule>;
84
85    /// Get the fedimintd version
86    async fn fedimintd_version(&self) -> String;
87
88    /// Create a trait object
89    fn into_dyn(self) -> DynDashboardApi
90    where
91        Self: Sized + Send + Sync + 'static,
92    {
93        Arc::new(self)
94    }
95}
96
97/// Extension trait for IDashboardApi providing type-safe module access
98pub trait DashboardApiModuleExt {
99    /// Get a typed reference to a server module instance by kind
100    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M>;
101}
102
103impl DashboardApiModuleExt for DynDashboardApi {
104    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M> {
105        self.get_module_by_kind(M::module_kind())?
106            .as_any()
107            .downcast_ref::<M>()
108    }
109}
110
111#[derive(Debug, Clone)]
112pub struct ServerBitcoinRpcStatus {
113    pub network: Network,
114    pub block_count: u64,
115    pub fee_rate: Feerate,
116    pub sync_progress: Option<f64>,
117}