fedimint_server_core/
bitcoin_rpc.rs1use std::fmt::Debug;
2use std::sync::Arc;
3use std::time::Duration;
4
5use anyhow::{Context, Result, ensure};
6use fedimint_core::Feerate;
7use fedimint_core::bitcoin::{Block, BlockHash, Network, Transaction};
8use fedimint_core::envs::BitcoinRpcConfig;
9use fedimint_core::task::TaskGroup;
10use fedimint_core::util::SafeUrl;
11use fedimint_logging::LOG_SERVER;
12use tokio::sync::watch;
13use tracing::debug;
14
15use crate::dashboard_ui::ServerBitcoinRpcStatus;
16
17#[derive(Debug, Clone)]
18pub struct ServerBitcoinRpcMonitor {
19 rpc: DynServerBitcoinRpc,
20 status_receiver: watch::Receiver<Option<ServerBitcoinRpcStatus>>,
21}
22
23impl ServerBitcoinRpcMonitor {
24 pub fn new(
25 rpc: DynServerBitcoinRpc,
26 update_interval: Duration,
27 task_group: &TaskGroup,
28 ) -> Self {
29 let (status_sender, status_receiver) = watch::channel(None);
30
31 let rpc_clone = rpc.clone();
32 debug!(
33 target: LOG_SERVER,
34 interval_ms = %update_interval.as_millis(),
35 "Starting bitcoin rpc monitor"
36 );
37
38 task_group.spawn_cancellable("bitcoin-status-update", async move {
39 let mut interval = tokio::time::interval(update_interval);
40 loop {
41 interval.tick().await;
42 match Self::fetch_status(&rpc_clone).await {
43 Ok(new_status) => {
44 status_sender.send_replace(Some(new_status));
45 }
46 Err(..) => {
47 status_sender.send_replace(None);
48 }
49 }
50 }
51 });
52
53 Self {
54 rpc,
55 status_receiver,
56 }
57 }
58
59 async fn fetch_status(rpc: &DynServerBitcoinRpc) -> Result<ServerBitcoinRpcStatus> {
60 let network = rpc.get_network().await?;
61 let block_count = rpc.get_block_count().await?;
62 let sync_progress = rpc.get_sync_progress().await?;
63
64 let fee_rate = if network == Network::Regtest {
65 Feerate { sats_per_kvb: 1000 }
66 } else {
67 rpc.get_feerate().await?.context("Feerate not available")?
68 };
69
70 Ok(ServerBitcoinRpcStatus {
71 network,
72 block_count,
73 fee_rate,
74 sync_progress,
75 })
76 }
77
78 pub fn get_bitcoin_rpc_config(&self) -> BitcoinRpcConfig {
79 self.rpc.get_bitcoin_rpc_config()
80 }
81
82 pub fn url(&self) -> SafeUrl {
83 self.rpc.get_url()
84 }
85
86 pub fn status(&self) -> Option<ServerBitcoinRpcStatus> {
87 self.status_receiver.borrow().clone()
88 }
89
90 pub async fn get_block(&self, hash: &BlockHash) -> Result<Block> {
91 ensure!(
92 self.status_receiver.borrow().is_some(),
93 "Not connected to bitcoin backend"
94 );
95
96 self.rpc.get_block(hash).await
97 }
98
99 pub async fn get_block_hash(&self, height: u64) -> Result<BlockHash> {
100 ensure!(
101 self.status_receiver.borrow().is_some(),
102 "Not connected to bitcoin backend"
103 );
104
105 self.rpc.get_block_hash(height).await
106 }
107
108 pub async fn submit_transaction(&self, tx: Transaction) {
109 if self.status_receiver.borrow().is_some() {
110 self.rpc.submit_transaction(tx).await;
111 }
112 }
113}
114
115pub type DynServerBitcoinRpc = Arc<dyn IServerBitcoinRpc>;
116
117#[async_trait::async_trait]
118pub trait IServerBitcoinRpc: Debug + Send + Sync + 'static {
119 fn get_bitcoin_rpc_config(&self) -> BitcoinRpcConfig;
121
122 fn get_url(&self) -> SafeUrl;
124
125 async fn get_network(&self) -> Result<Network>;
127
128 async fn get_block_count(&self) -> Result<u64>;
130
131 async fn get_block_hash(&self, height: u64) -> Result<BlockHash>;
143
144 async fn get_block(&self, block_hash: &BlockHash) -> Result<Block>;
145
146 async fn get_feerate(&self) -> Result<Option<Feerate>>;
151
152 async fn submit_transaction(&self, transaction: Transaction);
167
168 async fn get_sync_progress(&self) -> Result<Option<f64>>;
171
172 fn into_dyn(self) -> DynServerBitcoinRpc
173 where
174 Self: Sized,
175 {
176 Arc::new(self)
177 }
178}