66// accordance with one or both of these licenses.
77
88mod bitcoind_rpc;
9+ mod electrum;
910
1011use crate :: chain:: bitcoind_rpc:: {
1112 BitcoindRpcClient , BoundedHeaderCache , ChainListener , FeeRateEstimationMode ,
1213} ;
14+ use crate :: chain:: electrum:: ElectrumRuntimeClient ;
1315use crate :: config:: {
14- Config , EsploraSyncConfig , BDK_CLIENT_CONCURRENCY , BDK_CLIENT_STOP_GAP ,
16+ Config , ElectrumSyncConfig , EsploraSyncConfig , BDK_CLIENT_CONCURRENCY , BDK_CLIENT_STOP_GAP ,
1517 BDK_WALLET_SYNC_TIMEOUT_SECS , FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS , LDK_WALLET_SYNC_TIMEOUT_SECS ,
1618 RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL , TX_BROADCAST_TIMEOUT_SECS ,
1719 WALLET_SYNC_INTERVAL_MINIMUM_SECS ,
@@ -107,6 +109,45 @@ impl WalletSyncStatus {
107109 }
108110}
109111
112+ pub ( crate ) enum ElectrumRuntimeStatus {
113+ Started ( Arc < ElectrumRuntimeClient > ) ,
114+ Stopped ,
115+ }
116+
117+ impl ElectrumRuntimeStatus {
118+ pub ( crate ) fn new ( ) -> Self {
119+ Self :: Stopped
120+ }
121+
122+ pub ( crate ) fn start (
123+ & mut self , server_url : String , runtime : Arc < tokio:: runtime:: Runtime > , logger : Arc < Logger > ,
124+ ) -> Result < ( ) , Error > {
125+ match self {
126+ Self :: Stopped => {
127+ let client =
128+ Arc :: new ( ElectrumRuntimeClient :: new ( server_url. clone ( ) , runtime, logger) ?) ;
129+
130+ * self = Self :: Started ( client) ;
131+ } ,
132+ Self :: Started ( _) => {
133+ debug_assert ! ( false , "We shouldn't call start if we're already started" )
134+ } ,
135+ }
136+ Ok ( ( ) )
137+ }
138+
139+ pub ( crate ) fn stop ( & mut self ) {
140+ * self = Self :: new ( )
141+ }
142+
143+ pub ( crate ) fn client ( & self ) -> Option < & Arc < ElectrumRuntimeClient > > {
144+ match self {
145+ Self :: Started ( client) => Some ( & client) ,
146+ Self :: Stopped { .. } => None ,
147+ }
148+ }
149+ }
150+
110151pub ( crate ) enum ChainSource {
111152 Esplora {
112153 sync_config : EsploraSyncConfig ,
@@ -122,6 +163,20 @@ pub(crate) enum ChainSource {
122163 logger : Arc < Logger > ,
123164 node_metrics : Arc < RwLock < NodeMetrics > > ,
124165 } ,
166+ Electrum {
167+ server_url : String ,
168+ sync_config : ElectrumSyncConfig ,
169+ electrum_runtime_status : RwLock < ElectrumRuntimeStatus > ,
170+ onchain_wallet : Arc < Wallet > ,
171+ onchain_wallet_sync_status : Mutex < WalletSyncStatus > ,
172+ lightning_wallet_sync_status : Mutex < WalletSyncStatus > ,
173+ fee_estimator : Arc < OnchainFeeEstimator > ,
174+ tx_broadcaster : Arc < Broadcaster > ,
175+ kv_store : Arc < DynStore > ,
176+ config : Arc < Config > ,
177+ logger : Arc < Logger > ,
178+ node_metrics : Arc < RwLock < NodeMetrics > > ,
179+ } ,
125180 BitcoindRpc {
126181 bitcoind_rpc_client : Arc < BitcoindRpcClient > ,
127182 header_cache : tokio:: sync:: Mutex < BoundedHeaderCache > ,
@@ -167,6 +222,31 @@ impl ChainSource {
167222 }
168223 }
169224
225+ pub ( crate ) fn new_electrum (
226+ server_url : String , sync_config : ElectrumSyncConfig , onchain_wallet : Arc < Wallet > ,
227+ fee_estimator : Arc < OnchainFeeEstimator > , tx_broadcaster : Arc < Broadcaster > ,
228+ kv_store : Arc < DynStore > , config : Arc < Config > , logger : Arc < Logger > ,
229+ node_metrics : Arc < RwLock < NodeMetrics > > ,
230+ ) -> Self {
231+ let electrum_runtime_status = RwLock :: new ( ElectrumRuntimeStatus :: new ( ) ) ;
232+ let onchain_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
233+ let lightning_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
234+ Self :: Electrum {
235+ server_url,
236+ sync_config,
237+ electrum_runtime_status,
238+ onchain_wallet,
239+ onchain_wallet_sync_status,
240+ lightning_wallet_sync_status,
241+ fee_estimator,
242+ tx_broadcaster,
243+ kv_store,
244+ config,
245+ logger,
246+ node_metrics,
247+ }
248+ }
249+
170250 pub ( crate ) fn new_bitcoind_rpc (
171251 host : String , port : u16 , rpc_user : String , rpc_password : String ,
172252 onchain_wallet : Arc < Wallet > , fee_estimator : Arc < OnchainFeeEstimator > ,
@@ -193,6 +273,33 @@ impl ChainSource {
193273 }
194274 }
195275
276+ pub ( crate ) fn start ( & self , runtime : Arc < tokio:: runtime:: Runtime > ) -> Result < ( ) , Error > {
277+ match self {
278+ Self :: Electrum { server_url, electrum_runtime_status, logger, .. } => {
279+ electrum_runtime_status. write ( ) . unwrap ( ) . start (
280+ server_url. clone ( ) ,
281+ runtime,
282+ Arc :: clone ( & logger) ,
283+ ) ?;
284+ } ,
285+ _ => {
286+ // Nothing to do for other chain sources.
287+ } ,
288+ }
289+ Ok ( ( ) )
290+ }
291+
292+ pub ( crate ) fn stop ( & self ) {
293+ match self {
294+ Self :: Electrum { electrum_runtime_status, .. } => {
295+ electrum_runtime_status. write ( ) . unwrap ( ) . stop ( ) ;
296+ } ,
297+ _ => {
298+ // Nothing to do for other chain sources.
299+ } ,
300+ }
301+ }
302+
196303 pub ( crate ) fn as_utxo_source ( & self ) -> Option < Arc < dyn UtxoSource > > {
197304 match self {
198305 Self :: BitcoindRpc { bitcoind_rpc_client, .. } => Some ( bitcoind_rpc_client. rpc_client ( ) ) ,
@@ -271,6 +378,7 @@ impl ChainSource {
271378 return ;
272379 }
273380 } ,
381+ Self :: Electrum { .. } => todo ! ( ) ,
274382 Self :: BitcoindRpc {
275383 bitcoind_rpc_client,
276384 header_cache,
@@ -538,6 +646,7 @@ impl ChainSource {
538646
539647 res
540648 } ,
649+ Self :: Electrum { .. } => todo ! ( ) ,
541650 Self :: BitcoindRpc { .. } => {
542651 // In BitcoindRpc mode we sync lightning and onchain wallet in one go by via
543652 // `ChainPoller`. So nothing to do here.
@@ -637,6 +746,7 @@ impl ChainSource {
637746
638747 res
639748 } ,
749+ Self :: Electrum { .. } => todo ! ( ) ,
640750 Self :: BitcoindRpc { .. } => {
641751 // In BitcoindRpc mode we sync lightning and onchain wallet in one go by via
642752 // `ChainPoller`. So nothing to do here.
@@ -655,6 +765,11 @@ impl ChainSource {
655765 // `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
656766 unreachable ! ( "Listeners will be synced via transction-based syncing" )
657767 } ,
768+ Self :: Electrum { .. } => {
769+ // In Electrum mode we sync lightning and onchain wallets via
770+ // `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
771+ unreachable ! ( "Listeners will be synced via transction-based syncing" )
772+ } ,
658773 Self :: BitcoindRpc {
659774 bitcoind_rpc_client,
660775 header_cache,
@@ -875,6 +990,7 @@ impl ChainSource {
875990
876991 Ok ( ( ) )
877992 } ,
993+ Self :: Electrum { .. } => todo ! ( ) ,
878994 Self :: BitcoindRpc {
879995 bitcoind_rpc_client,
880996 fee_estimator,
@@ -1085,6 +1201,7 @@ impl ChainSource {
10851201 }
10861202 }
10871203 } ,
1204+ Self :: Electrum { .. } => todo ! ( ) ,
10881205 Self :: BitcoindRpc { bitcoind_rpc_client, tx_broadcaster, logger, .. } => {
10891206 // While it's a bit unclear when we'd be able to lean on Bitcoin Core >v28
10901207 // features, we should eventually switch to use `submitpackage` via the
@@ -1147,12 +1264,14 @@ impl Filter for ChainSource {
11471264 fn register_tx ( & self , txid : & bitcoin:: Txid , script_pubkey : & bitcoin:: Script ) {
11481265 match self {
11491266 Self :: Esplora { tx_sync, .. } => tx_sync. register_tx ( txid, script_pubkey) ,
1267+ Self :: Electrum { .. } => todo ! ( ) ,
11501268 Self :: BitcoindRpc { .. } => ( ) ,
11511269 }
11521270 }
11531271 fn register_output ( & self , output : lightning:: chain:: WatchedOutput ) {
11541272 match self {
11551273 Self :: Esplora { tx_sync, .. } => tx_sync. register_output ( output) ,
1274+ Self :: Electrum { .. } => todo ! ( ) ,
11561275 Self :: BitcoindRpc { .. } => ( ) ,
11571276 }
11581277 }
0 commit comments