66// accordance with one or both of these licenses.
77
88pub ( crate ) mod bitcoind;
9+ mod cbf;
910mod electrum;
1011mod esplora;
1112
@@ -17,11 +18,12 @@ use bitcoin::{Script, Txid};
1718use lightning:: chain:: { BestBlock , Filter } ;
1819
1920use crate :: chain:: bitcoind:: { BitcoindChainSource , UtxoSourceClient } ;
21+ use crate :: chain:: cbf:: CbfChainSource ;
2022use crate :: chain:: electrum:: ElectrumChainSource ;
2123use crate :: chain:: esplora:: EsploraChainSource ;
2224use crate :: config:: {
23- BackgroundSyncConfig , BitcoindRestClientConfig , Config , ElectrumSyncConfig , EsploraSyncConfig ,
24- WALLET_SYNC_INTERVAL_MINIMUM_SECS ,
25+ BackgroundSyncConfig , BitcoindRestClientConfig , CbfSyncConfig , Config , ElectrumSyncConfig ,
26+ EsploraSyncConfig , WALLET_SYNC_INTERVAL_MINIMUM_SECS ,
2527} ;
2628use crate :: fee_estimator:: OnchainFeeEstimator ;
2729use crate :: logger:: { log_debug, log_info, log_trace, LdkLogger , Logger } ;
@@ -93,6 +95,7 @@ enum ChainSourceKind {
9395 Esplora ( EsploraChainSource ) ,
9496 Electrum ( ElectrumChainSource ) ,
9597 Bitcoind ( BitcoindChainSource ) ,
98+ Cbf ( CbfChainSource ) ,
9699}
97100
98101impl ChainSource {
@@ -184,11 +187,33 @@ impl ChainSource {
184187 ( Self { kind, registered_txids, tx_broadcaster, logger } , best_block)
185188 }
186189
190+ pub ( crate ) fn new_cbf (
191+ peers : Vec < String > , sync_config : CbfSyncConfig , fee_estimator : Arc < OnchainFeeEstimator > ,
192+ tx_broadcaster : Arc < Broadcaster > , kv_store : Arc < DynStore > , config : Arc < Config > ,
193+ logger : Arc < Logger > , node_metrics : Arc < RwLock < NodeMetrics > > ,
194+ ) -> ( Self , Option < BestBlock > ) {
195+ let cbf_chain_source = CbfChainSource :: new (
196+ peers,
197+ sync_config,
198+ fee_estimator,
199+ kv_store,
200+ config,
201+ Arc :: clone ( & logger) ,
202+ node_metrics,
203+ ) ;
204+ let kind = ChainSourceKind :: Cbf ( cbf_chain_source) ;
205+ let registered_txids = Mutex :: new ( Vec :: new ( ) ) ;
206+ ( Self { kind, registered_txids, tx_broadcaster, logger } , None )
207+ }
208+
187209 pub ( crate ) fn start ( & self , runtime : Arc < Runtime > ) -> Result < ( ) , Error > {
188210 match & self . kind {
189211 ChainSourceKind :: Electrum ( electrum_chain_source) => {
190212 electrum_chain_source. start ( runtime) ?
191213 } ,
214+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
215+ cbf_chain_source. start ( runtime) ;
216+ } ,
192217 _ => {
193218 // Nothing to do for other chain sources.
194219 } ,
@@ -199,6 +224,9 @@ impl ChainSource {
199224 pub ( crate ) fn stop ( & self ) {
200225 match & self . kind {
201226 ChainSourceKind :: Electrum ( electrum_chain_source) => electrum_chain_source. stop ( ) ,
227+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
228+ cbf_chain_source. stop ( ) ;
229+ } ,
202230 _ => {
203231 // Nothing to do for other chain sources.
204232 } ,
@@ -210,6 +238,7 @@ impl ChainSource {
210238 ChainSourceKind :: Bitcoind ( bitcoind_chain_source) => {
211239 Some ( bitcoind_chain_source. as_utxo_source ( ) )
212240 } ,
241+ ChainSourceKind :: Cbf { .. } => None ,
213242 _ => None ,
214243 }
215244 }
@@ -223,6 +252,7 @@ impl ChainSource {
223252 ChainSourceKind :: Esplora ( _) => true ,
224253 ChainSourceKind :: Electrum { .. } => true ,
225254 ChainSourceKind :: Bitcoind { .. } => false ,
255+ ChainSourceKind :: Cbf { .. } => false ,
226256 }
227257 }
228258
@@ -289,6 +319,28 @@ impl ChainSource {
289319 )
290320 . await
291321 } ,
322+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
323+ if let Some ( background_sync_config) =
324+ cbf_chain_source. sync_config . background_sync_config . as_ref ( )
325+ {
326+ self . start_tx_based_sync_loop (
327+ stop_sync_receiver,
328+ onchain_wallet,
329+ channel_manager,
330+ chain_monitor,
331+ output_sweeper,
332+ background_sync_config,
333+ Arc :: clone ( & self . logger ) ,
334+ )
335+ . await
336+ } else {
337+ log_info ! (
338+ self . logger,
339+ "Background syncing is disabled. Manual syncing required for onchain wallet, lightning wallet, and fee rate updates." ,
340+ ) ;
341+ return ;
342+ }
343+ } ,
292344 }
293345 }
294346
@@ -368,6 +420,9 @@ impl ChainSource {
368420 // `ChainPoller`. So nothing to do here.
369421 unreachable ! ( "Onchain wallet will be synced via chain polling" )
370422 } ,
423+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
424+ cbf_chain_source. sync_onchain_wallet ( onchain_wallet) . await
425+ } ,
371426 }
372427 }
373428
@@ -393,6 +448,11 @@ impl ChainSource {
393448 // `ChainPoller`. So nothing to do here.
394449 unreachable ! ( "Lightning wallet will be synced via chain polling" )
395450 } ,
451+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
452+ cbf_chain_source
453+ . sync_lightning_wallet ( channel_manager, chain_monitor, output_sweeper)
454+ . await
455+ } ,
396456 }
397457 }
398458
@@ -421,6 +481,10 @@ impl ChainSource {
421481 )
422482 . await
423483 } ,
484+ ChainSourceKind :: Cbf { .. } => {
485+ // In CBF mode we sync wallets via compact block filters.
486+ unreachable ! ( "Listeners will be synced via compact block filter syncing" )
487+ } ,
424488 }
425489 }
426490
@@ -435,6 +499,9 @@ impl ChainSource {
435499 ChainSourceKind :: Bitcoind ( bitcoind_chain_source) => {
436500 bitcoind_chain_source. update_fee_rate_estimates ( ) . await
437501 } ,
502+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
503+ cbf_chain_source. update_fee_rate_estimates ( ) . await
504+ } ,
438505 }
439506 }
440507
@@ -463,6 +530,9 @@ impl ChainSource {
463530 ChainSourceKind :: Bitcoind ( bitcoind_chain_source) => {
464531 bitcoind_chain_source. process_broadcast_package( next_package) . await
465532 } ,
533+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
534+ cbf_chain_source. process_broadcast_package( next_package) . await
535+ } ,
466536 }
467537 }
468538 }
@@ -481,6 +551,9 @@ impl Filter for ChainSource {
481551 electrum_chain_source. register_tx ( txid, script_pubkey)
482552 } ,
483553 ChainSourceKind :: Bitcoind { .. } => ( ) ,
554+ ChainSourceKind :: Cbf ( cbf_chain_source) => {
555+ cbf_chain_source. register_tx ( txid, script_pubkey)
556+ } ,
484557 }
485558 }
486559 fn register_output ( & self , output : lightning:: chain:: WatchedOutput ) {
@@ -492,6 +565,7 @@ impl Filter for ChainSource {
492565 electrum_chain_source. register_output ( output)
493566 } ,
494567 ChainSourceKind :: Bitcoind { .. } => ( ) ,
568+ ChainSourceKind :: Cbf ( cbf_chain_source) => cbf_chain_source. register_output ( output) ,
495569 }
496570 }
497571}
0 commit comments