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::peer_availability::PeerAvailability;
13use fedimint_core::session_outcome::SessionStatusV2;
14use fedimint_core::util::SafeUrl;
15use fedimint_core::{Feerate, PeerId};
16use serde::{Deserialize, Serialize};
17
18use crate::{DynServerModule, ServerModule};
19
20pub type DynDashboardApi = Arc<dyn IDashboardApi + Send + Sync + 'static>;
21
22/// Stable dashboard abstraction for the transport paths used to reach a peer.
23#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum ConnectionType {
26    /// Direct UDP connectivity
27    Direct,
28    /// Going through an Iroh relay
29    Relay,
30    /// Both relay and direct paths selected for use
31    Mixed,
32}
33
34/// P2P connection status for a peer. None indicates disconnected.
35#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
36pub struct P2PConnectionStatus {
37    /// The type of connection (Direct, Relay, Mixed), None if unknown
38    pub conn_type: Option<ConnectionType>,
39    /// Round-trip time (only available for iroh connections)
40    pub rtt: Option<Duration>,
41}
42
43/// Interface for guardian dashboard API in a running federation
44#[async_trait]
45pub trait IDashboardApi {
46    /// Password protecting the dashboard UI login form. `None` means the UI
47    /// is served without a login form.
48    fn auth_ui(&self) -> Option<ApiAuth>;
49
50    /// Get the guardian ID
51    async fn guardian_id(&self) -> PeerId;
52
53    /// Get a map of peer IDs to guardian names
54    async fn guardian_names(&self) -> BTreeMap<PeerId, String>;
55
56    /// Read the legacy name from the immutable configuration, if present.
57    async fn federation_name(&self) -> Option<String>;
58
59    /// Get the current active session count
60    async fn session_count(&self) -> u64;
61
62    /// Get items in a given session
63    async fn get_session_status(&self, session_idx: u64) -> SessionStatusV2;
64
65    /// The time it took to order our last proposal in the current session
66    async fn consensus_ord_latency(&self) -> Option<Duration>;
67
68    /// Returns a map of peer ID to connection status (None = disconnected)
69    async fn p2p_connection_status(&self) -> BTreeMap<PeerId, Option<P2PConnectionStatus>>;
70
71    /// This guardian's persisted observations, not a federation-wide estimate.
72    async fn peer_availability(&self) -> Option<DashboardPeerAvailability>;
73
74    /// Get the federation invite code to share with users
75    async fn federation_invite_code(&self) -> String;
76
77    /// Get the federation audit summary
78    async fn federation_audit(&self) -> AuditSummary;
79
80    /// Get the url of the bitcoin rpc
81    async fn bitcoin_rpc_url(&self) -> SafeUrl;
82
83    /// Get the status of the bitcoin backend
84    async fn bitcoin_rpc_status(&self) -> Option<ServerBitcoinRpcStatus>;
85
86    /// Download a backup of the guardian's configuration
87    async fn download_guardian_config_backup(
88        &self,
89        guardian_auth: &GuardianAuthToken,
90    ) -> GuardianConfigBackup;
91
92    /// Get reference to a server module instance by module kind
93    fn get_module_by_kind(&self, kind: ModuleKind) -> Option<&DynServerModule>;
94
95    /// Get the fedimintd version
96    async fn fedimintd_version(&self) -> String;
97
98    /// Get the git hash of the fedimintd binary, or `None` if it is not
99    /// available (e.g. it was built outside a git checkout).
100    async fn fedimintd_version_hash(&self) -> Option<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/// One persisted local observation, including its freshness timestamp.
112#[derive(Debug, Clone)]
113pub struct DashboardPeerAvailability {
114    /// Unix timestamp of the last persistence pass.
115    pub updated_at_secs: u64,
116    /// This guardian's report; the guardian does not measure itself.
117    pub report: PeerAvailability,
118}
119
120/// Extension trait for IDashboardApi providing type-safe module access
121pub trait DashboardApiModuleExt {
122    /// Get a typed reference to a server module instance by kind
123    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M>;
124}
125
126impl DashboardApiModuleExt for DynDashboardApi {
127    fn get_module<M: ServerModule + 'static>(&self) -> Option<&M> {
128        self.get_module_by_kind(M::module_kind())?
129            .as_any()
130            .downcast_ref::<M>()
131    }
132}
133
134#[derive(Debug, Clone)]
135pub struct ServerBitcoinRpcStatus {
136    pub network: Network,
137    pub block_count: u64,
138    /// `None` while the backend cannot estimate fees yet — a node in initial
139    /// block download has no fee data until it reaches the tip, and consensus
140    /// does not start before then.
141    pub fee_rate: Option<Feerate>,
142    pub sync_progress: Option<f64>,
143}