Skip to content

Commit 7e93a8b

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 7e93a8b

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,19 +413,27 @@ 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
}
406421

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+
407430
pub(crate) async fn continuously_sync_wallets(
408431
&self, mut stop_sync_receiver: tokio::sync::watch::Receiver<()>,
409432
channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
410433
output_sweeper: Arc<Sweeper>,
411434
) {
412-
match self {
413-
Self::Esplora { sync_config, logger, .. } => {
435+
match &self.kind {
436+
ChainSourceKind::Esplora { sync_config, logger, .. } => {
414437
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
415438
self.start_tx_based_sync_loop(
416439
stop_sync_receiver,
@@ -430,7 +453,7 @@ impl ChainSource {
430453
return;
431454
}
432455
},
433-
Self::Electrum { sync_config, logger, .. } => {
456+
ChainSourceKind::Electrum { sync_config, logger, .. } => {
434457
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
435458
self.start_tx_based_sync_loop(
436459
stop_sync_receiver,
@@ -450,7 +473,7 @@ impl ChainSource {
450473
return;
451474
}
452475
},
453-
Self::Bitcoind {
476+
ChainSourceKind::Bitcoind {
454477
api_client,
455478
header_cache,
456479
latest_chain_tip,
@@ -681,8 +704,8 @@ impl ChainSource {
681704
// Synchronize the onchain wallet via transaction-based protocols (i.e., Esplora, Electrum,
682705
// etc.)
683706
pub(crate) async fn sync_onchain_wallet(&self) -> Result<(), Error> {
684-
match self {
685-
Self::Esplora {
707+
match &self.kind {
708+
ChainSourceKind::Esplora {
686709
esplora_client,
687710
onchain_wallet,
688711
onchain_wallet_sync_status,
@@ -795,7 +818,7 @@ impl ChainSource {
795818

796819
res
797820
},
798-
Self::Electrum {
821+
ChainSourceKind::Electrum {
799822
electrum_runtime_status,
800823
onchain_wallet,
801824
onchain_wallet_sync_status,
@@ -887,7 +910,7 @@ impl ChainSource {
887910

888911
res
889912
},
890-
Self::Bitcoind { .. } => {
913+
ChainSourceKind::Bitcoind { .. } => {
891914
// In BitcoindRpc mode we sync lightning and onchain wallet in one go via
892915
// `ChainPoller`. So nothing to do here.
893916
unreachable!("Onchain wallet will be synced via chain polling")
@@ -901,8 +924,8 @@ impl ChainSource {
901924
&self, channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
902925
output_sweeper: Arc<Sweeper>,
903926
) -> Result<(), Error> {
904-
match self {
905-
Self::Esplora {
927+
match &self.kind {
928+
ChainSourceKind::Esplora {
906929
tx_sync,
907930
lightning_wallet_sync_status,
908931
kv_store,
@@ -986,7 +1009,7 @@ impl ChainSource {
9861009

9871010
res
9881011
},
989-
Self::Electrum {
1012+
ChainSourceKind::Electrum {
9901013
electrum_runtime_status,
9911014
lightning_wallet_sync_status,
9921015
kv_store,
@@ -1057,7 +1080,7 @@ impl ChainSource {
10571080

10581081
res
10591082
},
1060-
Self::Bitcoind { .. } => {
1083+
ChainSourceKind::Bitcoind { .. } => {
10611084
// In BitcoindRpc mode we sync lightning and onchain wallet in one go via
10621085
// `ChainPoller`. So nothing to do here.
10631086
unreachable!("Lightning wallet will be synced via chain polling")
@@ -1069,18 +1092,18 @@ impl ChainSource {
10691092
&self, channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
10701093
output_sweeper: Arc<Sweeper>,
10711094
) -> Result<(), Error> {
1072-
match self {
1073-
Self::Esplora { .. } => {
1095+
match &self.kind {
1096+
ChainSourceKind::Esplora { .. } => {
10741097
// In Esplora mode we sync lightning and onchain wallets via
10751098
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
10761099
unreachable!("Listeners will be synced via transction-based syncing")
10771100
},
1078-
Self::Electrum { .. } => {
1101+
ChainSourceKind::Electrum { .. } => {
10791102
// In Electrum mode we sync lightning and onchain wallets via
10801103
// `sync_onchain_wallet` and `sync_lightning_wallet`. So nothing to do here.
10811104
unreachable!("Listeners will be synced via transction-based syncing")
10821105
},
1083-
Self::Bitcoind {
1106+
ChainSourceKind::Bitcoind {
10841107
api_client,
10851108
header_cache,
10861109
latest_chain_tip,
@@ -1220,8 +1243,8 @@ impl ChainSource {
12201243
}
12211244

12221245
pub(crate) async fn update_fee_rate_estimates(&self) -> Result<(), Error> {
1223-
match self {
1224-
Self::Esplora {
1246+
match &self.kind {
1247+
ChainSourceKind::Esplora {
12251248
esplora_client,
12261249
fee_estimator,
12271250
config,
@@ -1305,7 +1328,7 @@ impl ChainSource {
13051328

13061329
Ok(())
13071330
},
1308-
Self::Electrum {
1331+
ChainSourceKind::Electrum {
13091332
electrum_runtime_status,
13101333
fee_estimator,
13111334
kv_store,
@@ -1350,7 +1373,7 @@ impl ChainSource {
13501373

13511374
Ok(())
13521375
},
1353-
Self::Bitcoind {
1376+
ChainSourceKind::Bitcoind {
13541377
api_client,
13551378
fee_estimator,
13561379
config,
@@ -1483,8 +1506,8 @@ impl ChainSource {
14831506
}
14841507

14851508
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, .. } => {
14881511
let mut receiver = tx_broadcaster.get_broadcast_queue().await;
14891512
while let Some(next_package) = receiver.recv().await {
14901513
for tx in &next_package {
@@ -1560,7 +1583,7 @@ impl ChainSource {
15601583
}
15611584
}
15621585
},
1563-
Self::Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
1586+
ChainSourceKind::Electrum { electrum_runtime_status, tx_broadcaster, .. } => {
15641587
let electrum_client: Arc<ElectrumRuntimeClient> = if let Some(client) =
15651588
electrum_runtime_status.read().unwrap().client().as_ref()
15661589
{
@@ -1580,7 +1603,7 @@ impl ChainSource {
15801603
}
15811604
}
15821605
},
1583-
Self::Bitcoind { api_client, tx_broadcaster, logger, .. } => {
1606+
ChainSourceKind::Bitcoind { api_client, tx_broadcaster, logger, .. } => {
15841607
// While it's a bit unclear when we'd be able to lean on Bitcoin Core >v28
15851608
// features, we should eventually switch to use `submitpackage` via the
15861609
// `rust-bitcoind-json-rpc` crate rather than just broadcasting individual
@@ -1640,21 +1663,21 @@ impl ChainSource {
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)