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    /// Change the guardian password
95    async fn change_password(
96        &self,
97        new_password: &str,
98        current_password: &str,
99        guardian_auth: &GuardianAuthToken,
100    ) -> Result<(), String>;
101
102    /// Create a trait object
103    fn into_dyn(self) -> DynDashboardApi
104    where
105        Self: Sized + Send + Sync + 'static,
106    {
107        Arc::new(self)
108    }
109}
110
111/// Extension trait for IDashboardApi providing type-safe module access
112pub trait DashboardApiModuleExt {
113    /// Get a typed reference to a server module instance by kind
114    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M>;
115}
116
117impl DashboardApiModuleExt for DynDashboardApi {
118    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M> {
119        self.get_module_by_kind(M::module_kind())?
120            .as_any()
121            .downcast_ref::<M>()
122    }
123}
124
125#[derive(Debug, Clone)]
126pub struct ServerBitcoinRpcStatus {
127    pub network: Network,
128    pub block_count: u64,
129    pub fee_rate: Feerate,
130    pub sync_progress: Option<f64>,
131}