Skip to content

Commit 56819cf

Browse files
committed
Implement continuously_sync_wallets for ChainSource::Electrum
1 parent c3b5a48 commit 56819cf

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

src/chain/mod.rs

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -304,73 +304,88 @@ impl ChainSource {
304304
channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
305305
output_sweeper: Arc<Sweeper>,
306306
) {
307-
match self {
308-
Self::Esplora { sync_config, logger, .. } => {
309-
// Setup syncing intervals if enabled
310-
if let Some(background_sync_config) = sync_config.background_sync_config {
311-
let onchain_wallet_sync_interval_secs = background_sync_config
312-
.onchain_wallet_sync_interval_secs
313-
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
314-
let mut onchain_wallet_sync_interval = tokio::time::interval(
315-
Duration::from_secs(onchain_wallet_sync_interval_secs),
316-
);
317-
onchain_wallet_sync_interval
318-
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
319-
320-
let fee_rate_cache_update_interval_secs = background_sync_config
321-
.fee_rate_cache_update_interval_secs
322-
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
323-
let mut fee_rate_update_interval = tokio::time::interval(Duration::from_secs(
324-
fee_rate_cache_update_interval_secs,
325-
));
326-
// When starting up, we just blocked on updating, so skip the first tick.
327-
fee_rate_update_interval.reset();
328-
fee_rate_update_interval
329-
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
330-
331-
let lightning_wallet_sync_interval_secs = background_sync_config
332-
.lightning_wallet_sync_interval_secs
333-
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
334-
let mut lightning_wallet_sync_interval = tokio::time::interval(
335-
Duration::from_secs(lightning_wallet_sync_interval_secs),
336-
);
337-
lightning_wallet_sync_interval
338-
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
307+
macro_rules! start_tx_based_sync_loop {
308+
($background_sync_config: expr, $logger: expr) => {{
309+
// Setup syncing intervals
310+
let onchain_wallet_sync_interval_secs = $background_sync_config
311+
.onchain_wallet_sync_interval_secs
312+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
313+
let mut onchain_wallet_sync_interval =
314+
tokio::time::interval(Duration::from_secs(onchain_wallet_sync_interval_secs));
315+
onchain_wallet_sync_interval
316+
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
339317

340-
// Start the syncing loop.
341-
loop {
342-
tokio::select! {
343-
_ = stop_sync_receiver.changed() => {
344-
log_trace!(
345-
logger,
346-
"Stopping background syncing on-chain wallet.",
318+
let fee_rate_cache_update_interval_secs = $background_sync_config
319+
.fee_rate_cache_update_interval_secs
320+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
321+
let mut fee_rate_update_interval =
322+
tokio::time::interval(Duration::from_secs(fee_rate_cache_update_interval_secs));
323+
// When starting up, we just blocked on updating, so skip the first tick.
324+
fee_rate_update_interval.reset();
325+
fee_rate_update_interval
326+
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
327+
328+
let lightning_wallet_sync_interval_secs = $background_sync_config
329+
.lightning_wallet_sync_interval_secs
330+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
331+
let mut lightning_wallet_sync_interval =
332+
tokio::time::interval(Duration::from_secs(lightning_wallet_sync_interval_secs));
333+
lightning_wallet_sync_interval
334+
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
335+
336+
// Start the syncing loop.
337+
loop {
338+
tokio::select! {
339+
_ = stop_sync_receiver.changed() => {
340+
log_trace!(
341+
$logger,
342+
"Stopping background syncing on-chain wallet.",
347343
);
348-
return;
349-
}
350-
_ = onchain_wallet_sync_interval.tick() => {
351-
let _ = self.sync_onchain_wallet().await;
352-
}
353-
_ = fee_rate_update_interval.tick() => {
354-
let _ = self.update_fee_rate_estimates().await;
355-
}
356-
_ = lightning_wallet_sync_interval.tick() => {
357-
let _ = self.sync_lightning_wallet(
358-
Arc::clone(&channel_manager),
359-
Arc::clone(&chain_monitor),
360-
Arc::clone(&output_sweeper),
344+
return;
345+
}
346+
_ = onchain_wallet_sync_interval.tick() => {
347+
let _ = self.sync_onchain_wallet().await;
348+
}
349+
_ = fee_rate_update_interval.tick() => {
350+
let _ = self.update_fee_rate_estimates().await;
351+
}
352+
_ = lightning_wallet_sync_interval.tick() => {
353+
let _ = self.sync_lightning_wallet(
354+
Arc::clone(&channel_manager),
355+
Arc::clone(&chain_monitor),
356+
Arc::clone(&output_sweeper),
361357
).await;
362-
}
363358
}
364359
}
360+
}
361+
}};
362+
}
363+
364+
match self {
365+
Self::Esplora { sync_config, logger, .. } => {
366+
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
367+
start_tx_based_sync_loop!(background_sync_config, logger)
365368
} else {
366369
// Background syncing is disabled
367370
log_info!(
368-
logger, "Background syncing disabled. Manual syncing required for onchain wallet, lightning wallet, and fee rate updates.",
371+
logger,
372+
"Background syncing is disabled. Manual syncing required for onchain wallet, lightning wallet, and fee rate updates.",
373+
);
374+
return;
375+
}
376+
},
377+
Self::Electrum { sync_config, logger, .. } => {
378+
if let Some(background_sync_config) = sync_config.background_sync_config.as_ref() {
379+
start_tx_based_sync_loop!(background_sync_config, logger)
380+
} else {
381+
// Background syncing is disabled
382+
log_info!(
383+
logger,
384+
"Background syncing is disabled. Manual syncing required for onchain wallet, lightning wallet, and fee rate updates.",
369385
);
370386
return;
371387
}
372388
},
373-
Self::Electrum { .. } => todo!(),
374389
Self::BitcoindRpc {
375390
bitcoind_rpc_client,
376391
header_cache,

0 commit comments

Comments
 (0)