@@ -187,7 +187,11 @@ impl ElectrumRuntimeStatus {
187
187
}
188
188
}
189
189
190
- pub ( crate ) enum ChainSource {
190
+ pub ( crate ) struct ChainSource {
191
+ kind : ChainSourceKind ,
192
+ }
193
+
194
+ enum ChainSourceKind {
191
195
Esplora {
192
196
sync_config : EsploraSyncConfig ,
193
197
esplora_client : EsploraAsyncClient ,
@@ -262,7 +266,7 @@ impl ChainSource {
262
266
263
267
let onchain_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
264
268
let lightning_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
265
- Self :: Esplora {
269
+ let kind = ChainSourceKind :: Esplora {
266
270
sync_config,
267
271
esplora_client,
268
272
onchain_wallet,
@@ -275,7 +279,9 @@ impl ChainSource {
275
279
config,
276
280
logger,
277
281
node_metrics,
278
- }
282
+ } ;
283
+
284
+ Self { kind }
279
285
}
280
286
281
287
pub ( crate ) fn new_electrum (
@@ -287,7 +293,7 @@ impl ChainSource {
287
293
let electrum_runtime_status = RwLock :: new ( ElectrumRuntimeStatus :: new ( ) ) ;
288
294
let onchain_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
289
295
let lightning_wallet_sync_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
290
- Self :: Electrum {
296
+ let kind = ChainSourceKind :: Electrum {
291
297
server_url,
292
298
sync_config,
293
299
electrum_runtime_status,
@@ -300,7 +306,8 @@ impl ChainSource {
300
306
config,
301
307
logger,
302
308
node_metrics,
303
- }
309
+ } ;
310
+ Self { kind }
304
311
}
305
312
306
313
pub ( crate ) fn new_bitcoind_rpc (
@@ -319,7 +326,7 @@ impl ChainSource {
319
326
let header_cache = tokio:: sync:: Mutex :: new ( BoundedHeaderCache :: new ( ) ) ;
320
327
let latest_chain_tip = RwLock :: new ( None ) ;
321
328
let wallet_polling_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
322
- Self :: Bitcoind {
329
+ let kind = ChainSourceKind :: Bitcoind {
323
330
api_client,
324
331
header_cache,
325
332
latest_chain_tip,
@@ -331,7 +338,8 @@ impl ChainSource {
331
338
config,
332
339
logger,
333
340
node_metrics,
334
- }
341
+ } ;
342
+ Self { kind }
335
343
}
336
344
337
345
pub ( crate ) fn new_bitcoind_rest (
@@ -354,7 +362,7 @@ impl ChainSource {
354
362
let latest_chain_tip = RwLock :: new ( None ) ;
355
363
let wallet_polling_status = Mutex :: new ( WalletSyncStatus :: Completed ) ;
356
364
357
- Self :: Bitcoind {
365
+ let kind = ChainSourceKind :: Bitcoind {
358
366
api_client,
359
367
header_cache,
360
368
latest_chain_tip,
@@ -366,12 +374,19 @@ impl ChainSource {
366
374
config,
367
375
logger,
368
376
node_metrics,
369
- }
377
+ } ;
378
+ Self { kind }
370
379
}
371
380
372
381
pub ( crate ) fn start ( & self , runtime : Arc < tokio:: runtime:: Runtime > ) -> Result < ( ) , Error > {
373
- match self {
374
- Self :: Electrum { server_url, electrum_runtime_status, config, logger, .. } => {
382
+ match & self . kind {
383
+ ChainSourceKind :: Electrum {
384
+ server_url,
385
+ electrum_runtime_status,
386
+ config,
387
+ logger,
388
+ ..
389
+ } => {
375
390
electrum_runtime_status. write ( ) . unwrap ( ) . start (
376
391
server_url. clone ( ) ,
377
392
Arc :: clone ( & runtime) ,
@@ -387,8 +402,8 @@ impl ChainSource {
387
402
}
388
403
389
404
pub ( crate ) fn stop ( & self ) {
390
- match self {
391
- Self :: Electrum { electrum_runtime_status, .. } => {
405
+ match & self . kind {
406
+ ChainSourceKind :: Electrum { electrum_runtime_status, .. } => {
392
407
electrum_runtime_status. write ( ) . unwrap ( ) . stop ( ) ;
393
408
} ,
394
409
_ => {
@@ -398,19 +413,27 @@ impl ChainSource {
398
413
}
399
414
400
415
pub ( crate ) fn as_utxo_source ( & self ) -> Option < Arc < dyn UtxoSource > > {
401
- match self {
402
- Self :: Bitcoind { api_client, .. } => Some ( api_client. utxo_source ( ) ) ,
416
+ match & self . kind {
417
+ ChainSourceKind :: Bitcoind { api_client, .. } => Some ( api_client. utxo_source ( ) ) ,
403
418
_ => None ,
404
419
}
405
420
}
406
421
422
+ pub ( crate ) fn is_transaction_based ( & self ) -> bool {
423
+ match & self . kind {
424
+ ChainSourceKind :: Esplora { .. } => true ,
425
+ ChainSourceKind :: Electrum { .. } => true ,
426
+ ChainSourceKind :: Bitcoind { .. } => false ,
427
+ }
428
+ }
429
+
407
430
pub ( crate ) async fn continuously_sync_wallets (
408
431
& self , mut stop_sync_receiver : tokio:: sync:: watch:: Receiver < ( ) > ,
409
432
channel_manager : Arc < ChannelManager > , chain_monitor : Arc < ChainMonitor > ,
410
433
output_sweeper : Arc < Sweeper > ,
411
434
) {
412
- match self {
413
- Self :: Esplora { sync_config, logger, .. } => {
435
+ match & self . kind {
436
+ ChainSourceKind :: Esplora { sync_config, logger, .. } => {
414
437
if let Some ( background_sync_config) = sync_config. background_sync_config . as_ref ( ) {
415
438
self . start_tx_based_sync_loop (
416
439
stop_sync_receiver,
@@ -430,7 +453,7 @@ impl ChainSource {
430
453
return ;
431
454
}
432
455
} ,
433
- Self :: Electrum { sync_config, logger, .. } => {
456
+ ChainSourceKind :: Electrum { sync_config, logger, .. } => {
434
457
if let Some ( background_sync_config) = sync_config. background_sync_config . as_ref ( ) {
435
458
self . start_tx_based_sync_loop (
436
459
stop_sync_receiver,
@@ -450,7 +473,7 @@ impl ChainSource {
450
473
return ;
451
474
}
452
475
} ,
453
- Self :: Bitcoind {
476
+ ChainSourceKind :: Bitcoind {
454
477
api_client,
455
478
header_cache,
456
479
latest_chain_tip,
@@ -681,8 +704,8 @@ impl ChainSource {
681
704
// Synchronize the onchain wallet via transaction-based protocols (i.e., Esplora, Electrum,
682
705
// etc.)
683
706
pub ( crate ) async fn sync_onchain_wallet ( & self ) -> Result < ( ) , Error > {
684
- match self {
685
- Self :: Esplora {
707
+ match & self . kind {
708
+ ChainSourceKind :: Esplora {
686
709
esplora_client,
687
710
onchain_wallet,
688
711
onchain_wallet_sync_status,
@@ -795,7 +818,7 @@ impl ChainSource {
795
818
796
819
res
797
820
} ,
798
- Self :: Electrum {
821
+ ChainSourceKind :: Electrum {
799
822
electrum_runtime_status,
800
823
onchain_wallet,
801
824
onchain_wallet_sync_status,
@@ -887,7 +910,7 @@ impl ChainSource {
887
910
888
911
res
889
912
} ,
890
- Self :: Bitcoind { .. } => {
913
+ ChainSourceKind :: Bitcoind { .. } => {
891
914
// In BitcoindRpc mode we sync lightning and onchain wallet in one go via
892
915
// `ChainPoller`. So nothing to do here.
893
916
unreachable ! ( "Onchain wallet will be synced via chain polling" )
@@ -901,8 +924,8 @@ impl ChainSource {
901
924
& self , channel_manager : Arc < ChannelManager > , chain_monitor : Arc < ChainMonitor > ,
902
925
output_sweeper : Arc < Sweeper > ,
903
926
) -> Result < ( ) , Error > {
904
- match self {
905
- Self :: Esplora {
927
+ match & self . kind {
928
+ ChainSourceKind :: Esplora {
906
929
tx_sync,
907
930
lightning_wallet_sync_status,
908
931
kv_store,
@@ -986,7 +1009,7 @@ impl ChainSource {
986
1009
987
1010
res
988
1011
} ,
989
- Self :: Electrum {
1012
+ ChainSourceKind :: Electrum {
990
1013
electrum_runtime_status,
991
1014
lightning_wallet_sync_status,
992
1015
kv_store,
@@ -1057,7 +1080,7 @@ impl ChainSource {
1057
1080
1058
1081
res
1059
1082
} ,
1060
- Self :: Bitcoind { .. } => {
1083
+ ChainSourceKind :: Bitcoind { .. } => {
1061
1084
// In BitcoindRpc mode we sync lightning and onchain wallet in one go via
1062
1085
// `ChainPoller`. So nothing to do here.
1063
1086
unreachable ! ( "Lightning wallet will be synced via chain polling" )
@@ -1069,18 +1092,18 @@ impl ChainSource {
1069
1092
& self , channel_manager : Arc < ChannelManager > , chain_monitor : Arc < ChainMonitor > ,
1070
1093
output_sweeper : Arc < Sweeper > ,
1071
1094
) -> Result < ( ) , Error > {
1072
- match self {
1073
- Self :: Esplora { .. } => {
1095
+ match & self . kind {
1096
+ ChainSourceKind :: Esplora { .. } => {
1074
1097
// In Esplora mode we sync lightning and onchain wallets via
1075
1098
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
1076
1099
unreachable ! ( "Listeners will be synced via transction-based syncing" )
1077
1100
} ,
1078
- Self :: Electrum { .. } => {
1101
+ ChainSourceKind :: Electrum { .. } => {
1079
1102
// In Electrum mode we sync lightning and onchain wallets via
1080
1103
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
1081
1104
unreachable ! ( "Listeners will be synced via transction-based syncing" )
1082
1105
} ,
1083
- Self :: Bitcoind {
1106
+ ChainSourceKind :: Bitcoind {
1084
1107
api_client,
1085
1108
header_cache,
1086
1109
latest_chain_tip,
@@ -1220,8 +1243,8 @@ impl ChainSource {
1220
1243
}
1221
1244
1222
1245
pub ( crate ) async fn update_fee_rate_estimates ( & self ) -> Result < ( ) , Error > {
1223
- match self {
1224
- Self :: Esplora {
1246
+ match & self . kind {
1247
+ ChainSourceKind :: Esplora {
1225
1248
esplora_client,
1226
1249
fee_estimator,
1227
1250
config,
@@ -1305,7 +1328,7 @@ impl ChainSource {
1305
1328
1306
1329
Ok ( ( ) )
1307
1330
} ,
1308
- Self :: Electrum {
1331
+ ChainSourceKind :: Electrum {
1309
1332
electrum_runtime_status,
1310
1333
fee_estimator,
1311
1334
kv_store,
@@ -1350,7 +1373,7 @@ impl ChainSource {
1350
1373
1351
1374
Ok ( ( ) )
1352
1375
} ,
1353
- Self :: Bitcoind {
1376
+ ChainSourceKind :: Bitcoind {
1354
1377
api_client,
1355
1378
fee_estimator,
1356
1379
config,
@@ -1483,8 +1506,8 @@ impl ChainSource {
1483
1506
}
1484
1507
1485
1508
pub ( crate ) async fn process_broadcast_queue ( & self ) {
1486
- match self {
1487
- Self :: Esplora { esplora_client, tx_broadcaster, logger, .. } => {
1509
+ match & self . kind {
1510
+ ChainSourceKind :: Esplora { esplora_client, tx_broadcaster, logger, .. } => {
1488
1511
let mut receiver = tx_broadcaster. get_broadcast_queue ( ) . await ;
1489
1512
while let Some ( next_package) = receiver. recv ( ) . await {
1490
1513
for tx in & next_package {
@@ -1560,7 +1583,7 @@ impl ChainSource {
1560
1583
}
1561
1584
}
1562
1585
} ,
1563
- Self :: Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
1586
+ ChainSourceKind :: Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
1564
1587
let electrum_client: Arc < ElectrumRuntimeClient > = if let Some ( client) =
1565
1588
electrum_runtime_status. read ( ) . unwrap ( ) . client ( ) . as_ref ( )
1566
1589
{
@@ -1580,7 +1603,7 @@ impl ChainSource {
1580
1603
}
1581
1604
}
1582
1605
} ,
1583
- Self :: Bitcoind { api_client, tx_broadcaster, logger, .. } => {
1606
+ ChainSourceKind :: Bitcoind { api_client, tx_broadcaster, logger, .. } => {
1584
1607
// While it's a bit unclear when we'd be able to lean on Bitcoin Core >v28
1585
1608
// features, we should eventually switch to use `submitpackage` via the
1586
1609
// `rust-bitcoind-json-rpc` crate rather than just broadcasting individual
@@ -1640,21 +1663,21 @@ impl ChainSource {
1640
1663
1641
1664
impl Filter for ChainSource {
1642
1665
fn register_tx ( & self , txid : & Txid , script_pubkey : & Script ) {
1643
- match self {
1644
- Self :: Esplora { tx_sync, .. } => tx_sync. register_tx ( txid, script_pubkey) ,
1645
- Self :: Electrum { electrum_runtime_status, .. } => {
1666
+ match & self . kind {
1667
+ ChainSourceKind :: Esplora { tx_sync, .. } => tx_sync. register_tx ( txid, script_pubkey) ,
1668
+ ChainSourceKind :: Electrum { electrum_runtime_status, .. } => {
1646
1669
electrum_runtime_status. write ( ) . unwrap ( ) . register_tx ( txid, script_pubkey)
1647
1670
} ,
1648
- Self :: Bitcoind { .. } => ( ) ,
1671
+ ChainSourceKind :: Bitcoind { .. } => ( ) ,
1649
1672
}
1650
1673
}
1651
1674
fn register_output ( & self , output : lightning:: chain:: WatchedOutput ) {
1652
- match self {
1653
- Self :: Esplora { tx_sync, .. } => tx_sync. register_output ( output) ,
1654
- Self :: Electrum { electrum_runtime_status, .. } => {
1675
+ match & self . kind {
1676
+ ChainSourceKind :: Esplora { tx_sync, .. } => tx_sync. register_output ( output) ,
1677
+ ChainSourceKind :: Electrum { electrum_runtime_status, .. } => {
1655
1678
electrum_runtime_status. write ( ) . unwrap ( ) . register_output ( output)
1656
1679
} ,
1657
- Self :: Bitcoind { .. } => ( ) ,
1680
+ ChainSourceKind :: Bitcoind { .. } => ( ) ,
1658
1681
}
1659
1682
}
1660
1683
}
0 commit comments