Skip to content

Commit ffcec7f

Browse files
committed
Introduce ChainSourceKind type
We introduce a new `ChainSourceKind` that is held as a field by `ChainSource`, which better encapsulates the chain syncing logic, and in future commits allows us to move some common fields to `ChainSource`.
1 parent d7226f3 commit ffcec7f

File tree

2 files changed

+81
-68
lines changed

2 files changed

+81
-68
lines changed

src/chain/mod.rs

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ impl ElectrumRuntimeStatus {
187187
}
188188
}
189189

190-
pub(crate) enum ChainSource {
190+
pub(crate) struct ChainSource {
191+
kind: ChainSourceKind,
192+
}
193+
194+
enum ChainSourceKind {
191195
Esplora {
192196
sync_config: EsploraSyncConfig,
193197
esplora_client: EsploraAsyncClient,
@@ -262,7 +266,7 @@ impl ChainSource {
262266

263267
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
264268
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
265-
Self::Esplora {
269+
let kind = ChainSourceKind::Esplora {
266270
sync_config,
267271
esplora_client,
268272
onchain_wallet,
@@ -275,7 +279,9 @@ impl ChainSource {
275279
config,
276280
logger,
277281
node_metrics,
278-
}
282+
};
283+
284+
Self { kind }
279285
}
280286

281287
pub(crate) fn new_electrum(
@@ -287,7 +293,7 @@ impl ChainSource {
287293
let electrum_runtime_status = RwLock::new(ElectrumRuntimeStatus::new());
288294
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
289295
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
290-
Self::Electrum {
296+
let kind = ChainSourceKind::Electrum {
291297
server_url,
292298
sync_config,
293299
electrum_runtime_status,
@@ -300,7 +306,8 @@ impl ChainSource {
300306
config,
301307
logger,
302308
node_metrics,
303-
}
309+
};
310+
Self { kind }
304311
}
305312

306313
pub(crate) fn new_bitcoind_rpc(
@@ -319,7 +326,7 @@ impl ChainSource {
319326
let header_cache = tokio::sync::Mutex::new(BoundedHeaderCache::new());
320327
let latest_chain_tip = RwLock::new(None);
321328
let wallet_polling_status = Mutex::new(WalletSyncStatus::Completed);
322-
Self::Bitcoind {
329+
let kind = ChainSourceKind::Bitcoind {
323330
api_client,
324331
header_cache,
325332
latest_chain_tip,
@@ -331,7 +338,8 @@ impl ChainSource {
331338
config,
332339
logger,
333340
node_metrics,
334-
}
341+
};
342+
Self { kind }
335343
}
336344

337345
pub(crate) fn new_bitcoind_rest(
@@ -354,7 +362,7 @@ impl ChainSource {
354362
let latest_chain_tip = RwLock::new(None);
355363
let wallet_polling_status = Mutex::new(WalletSyncStatus::Completed);
356364

357-
Self::Bitcoind {
365+
let kind = ChainSourceKind::Bitcoind {
358366
api_client,
359367
header_cache,
360368
latest_chain_tip,
@@ -366,12 +374,19 @@ impl ChainSource {
366374
config,
367375
logger,
368376
node_metrics,
369-
}
377+
};
378+
Self { kind }
370379
}
371380

372381
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+
} => {
375390
electrum_runtime_status.write().unwrap().start(
376391
server_url.clone(),
377392
Arc::clone(&runtime),
@@ -387,8 +402,8 @@ impl ChainSource {
387402
}
388403

389404
pub(crate) fn stop(&self) {
390-
match self {
391-
Self::Electrum { electrum_runtime_status, .. } => {
405+
match &self.kind {
406+
ChainSourceKind::Electrum { electrum_runtime_status, .. } => {
392407
electrum_runtime_status.write().unwrap().stop();
393408
},
394409
_ => {
@@ -398,8 +413,8 @@ impl ChainSource {
398413
}
399414

400415
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()),
403418
_ => None,
404419
}
405420
}
@@ -409,8 +424,8 @@ impl ChainSource {
409424
channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
410425
output_sweeper: Arc<Sweeper>,
411426
) {
412-
match self {
413-
Self::Esplora { sync_config, logger, .. } => {
427+
match &self.kind {
428+
ChainSourceKind::Esplora { sync_config, logger, .. } => {
414429
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
415430
self.start_tx_based_sync_loop(
416431
stop_sync_receiver,
@@ -430,7 +445,7 @@ impl ChainSource {
430445
return;
431446
}
432447
},
433-
Self::Electrum { sync_config, logger, .. } => {
448+
ChainSourceKind::Electrum { sync_config, logger, .. } => {
434449
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
435450
self.start_tx_based_sync_loop(
436451
stop_sync_receiver,
@@ -450,7 +465,7 @@ impl ChainSource {
450465
return;
451466
}
452467
},
453-
Self::Bitcoind {
468+
ChainSourceKind::Bitcoind {
454469
api_client,
455470
header_cache,
456471
latest_chain_tip,
@@ -681,8 +696,8 @@ impl ChainSource {
681696
// Synchronize the onchain wallet via transaction-based protocols (i.e., Esplora, Electrum,
682697
// etc.)
683698
pub(crate) async fn sync_onchain_wallet(&self) -> Result<(), Error> {
684-
match self {
685-
Self::Esplora {
699+
match &self.kind {
700+
ChainSourceKind::Esplora {
686701
esplora_client,
687702
onchain_wallet,
688703
onchain_wallet_sync_status,
@@ -795,7 +810,7 @@ impl ChainSource {
795810

796811
res
797812
},
798-
Self::Electrum {
813+
ChainSourceKind::Electrum {
799814
electrum_runtime_status,
800815
onchain_wallet,
801816
onchain_wallet_sync_status,
@@ -887,7 +902,7 @@ impl ChainSource {
887902

888903
res
889904
},
890-
Self::Bitcoind { .. } => {
905+
ChainSourceKind::Bitcoind { .. } => {
891906
// In BitcoindRpc mode we sync lightning and onchain wallet in one go via
892907
// `ChainPoller`. So nothing to do here.
893908
unreachable!("Onchain wallet will be synced via chain polling")
@@ -901,8 +916,8 @@ impl ChainSource {
901916
&self, channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
902917
output_sweeper: Arc<Sweeper>,
903918
) -> Result<(), Error> {
904-
match self {
905-
Self::Esplora {
919+
match &self.kind {
920+
ChainSourceKind::Esplora {
906921
tx_sync,
907922
lightning_wallet_sync_status,
908923
kv_store,
@@ -986,7 +1001,7 @@ impl ChainSource {
9861001

9871002
res
9881003
},
989-
Self::Electrum {
1004+
ChainSourceKind::Electrum {
9901005
electrum_runtime_status,
9911006
lightning_wallet_sync_status,
9921007
kv_store,
@@ -1057,7 +1072,7 @@ impl ChainSource {
10571072

10581073
res
10591074
},
1060-
Self::Bitcoind { .. } => {
1075+
ChainSourceKind::Bitcoind { .. } => {
10611076
// In BitcoindRpc mode we sync lightning and onchain wallet in one go via
10621077
// `ChainPoller`. So nothing to do here.
10631078
unreachable!("Lightning wallet will be synced via chain polling")
@@ -1069,18 +1084,18 @@ impl ChainSource {
10691084
&self, channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
10701085
output_sweeper: Arc<Sweeper>,
10711086
) -> Result<(), Error> {
1072-
match self {
1073-
Self::Esplora { .. } => {
1087+
match &self.kind {
1088+
ChainSourceKind::Esplora { .. } => {
10741089
// In Esplora mode we sync lightning and onchain wallets via
10751090
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
10761091
unreachable!("Listeners will be synced via transction-based syncing")
10771092
},
1078-
Self::Electrum { .. } => {
1093+
ChainSourceKind::Electrum { .. } => {
10791094
// In Electrum mode we sync lightning and onchain wallets via
10801095
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
10811096
unreachable!("Listeners will be synced via transction-based syncing")
10821097
},
1083-
Self::Bitcoind {
1098+
ChainSourceKind::Bitcoind {
10841099
api_client,
10851100
header_cache,
10861101
latest_chain_tip,
@@ -1220,8 +1235,8 @@ impl ChainSource {
12201235
}
12211236

12221237
pub(crate) async fn update_fee_rate_estimates(&self) -> Result<(), Error> {
1223-
match self {
1224-
Self::Esplora {
1238+
match &self.kind {
1239+
ChainSourceKind::Esplora {
12251240
esplora_client,
12261241
fee_estimator,
12271242
config,
@@ -1305,7 +1320,7 @@ impl ChainSource {
13051320

13061321
Ok(())
13071322
},
1308-
Self::Electrum {
1323+
ChainSourceKind::Electrum {
13091324
electrum_runtime_status,
13101325
fee_estimator,
13111326
kv_store,
@@ -1350,7 +1365,7 @@ impl ChainSource {
13501365

13511366
Ok(())
13521367
},
1353-
Self::Bitcoind {
1368+
ChainSourceKind::Bitcoind {
13541369
api_client,
13551370
fee_estimator,
13561371
config,
@@ -1483,8 +1498,8 @@ impl ChainSource {
14831498
}
14841499

14851500
pub(crate) async fn process_broadcast_queue(&self) {
1486-
match self {
1487-
Self::Esplora { esplora_client, tx_broadcaster, logger, .. } => {
1501+
match &self.kind {
1502+
ChainSourceKind::Esplora { esplora_client, tx_broadcaster, logger, .. } => {
14881503
let mut receiver = tx_broadcaster.get_broadcast_queue().await;
14891504
while let Some(next_package) = receiver.recv().await {
14901505
for tx in &next_package {
@@ -1560,7 +1575,7 @@ impl ChainSource {
15601575
}
15611576
}
15621577
},
1563-
Self::Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
1578+
ChainSourceKind::Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
15641579
let electrum_client: Arc<ElectrumRuntimeClient> = if let Some(client) =
15651580
electrum_runtime_status.read().unwrap().client().as_ref()
15661581
{
@@ -1580,7 +1595,7 @@ impl ChainSource {
15801595
}
15811596
}
15821597
},
1583-
Self::Bitcoind { api_client, tx_broadcaster, logger, .. } => {
1598+
ChainSourceKind::Bitcoind { api_client, tx_broadcaster, logger, .. } => {
15841599
// While it's a bit unclear when we'd be able to lean on Bitcoin Core >v28
15851600
// features, we should eventually switch to use `submitpackage` via the
15861601
// `rust-bitcoind-json-rpc` crate rather than just broadcasting individual
@@ -1636,25 +1651,33 @@ impl ChainSource {
16361651
},
16371652
}
16381653
}
1654+
1655+
pub(crate) fn is_transaction_based(&self) -> bool {
1656+
match &self.kind {
1657+
ChainSourceKind::Esplora { .. } => true,
1658+
ChainSourceKind::Electrum { .. } => true,
1659+
ChainSourceKind::Bitcoind { .. } => false,
1660+
}
1661+
}
16391662
}
16401663

16411664
impl Filter for ChainSource {
16421665
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, .. } => {
16461669
electrum_runtime_status.write().unwrap().register_tx(txid, script_pubkey)
16471670
},
1648-
Self::Bitcoind { .. } => (),
1671+
ChainSourceKind::Bitcoind { .. } => (),
16491672
}
16501673
}
16511674
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, .. } => {
16551678
electrum_runtime_status.write().unwrap().register_output(output)
16561679
},
1657-
Self::Bitcoind { .. } => (),
1680+
ChainSourceKind::Bitcoind { .. } => (),
16581681
}
16591682
}
16601683
}

src/lib.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,27 +1250,17 @@ impl Node {
12501250
tokio::task::block_in_place(move || {
12511251
tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap().block_on(
12521252
async move {
1253-
match chain_source.as_ref() {
1254-
ChainSource::Esplora { .. } => {
1255-
chain_source.update_fee_rate_estimates().await?;
1256-
chain_source
1257-
.sync_lightning_wallet(sync_cman, sync_cmon, sync_sweeper)
1258-
.await?;
1259-
chain_source.sync_onchain_wallet().await?;
1260-
},
1261-
ChainSource::Electrum { .. } => {
1262-
chain_source.update_fee_rate_estimates().await?;
1263-
chain_source
1264-
.sync_lightning_wallet(sync_cman, sync_cmon, sync_sweeper)
1265-
.await?;
1266-
chain_source.sync_onchain_wallet().await?;
1267-
},
1268-
ChainSource::Bitcoind { .. } => {
1269-
chain_source.update_fee_rate_estimates().await?;
1270-
chain_source
1271-
.poll_and_update_listeners(sync_cman, sync_cmon, sync_sweeper)
1272-
.await?;
1273-
},
1253+
if chain_source.is_transaction_based() {
1254+
chain_source.update_fee_rate_estimates().await?;
1255+
chain_source
1256+
.sync_lightning_wallet(sync_cman, sync_cmon, sync_sweeper)
1257+
.await?;
1258+
chain_source.sync_onchain_wallet().await?;
1259+
} else {
1260+
chain_source.update_fee_rate_estimates().await?;
1261+
chain_source
1262+
.poll_and_update_listeners(sync_cman, sync_cmon, sync_sweeper)
1263+
.await?;
12741264
}
12751265
Ok(())
12761266
},

0 commit comments

Comments
 (0)