Skip to content

Commit 6892e3e

Browse files
Allow disabling wallet background syncing
1 parent 22e97da commit 6892e3e

File tree

4 files changed

+131
-41
lines changed

4 files changed

+131
-41
lines changed

src/chain/mod.rs

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::config::{
1414
Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP,
1515
BDK_WALLET_SYNC_TIMEOUT_SECS, FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS,
1616
RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL, TX_BROADCAST_TIMEOUT_SECS,
17-
WALLET_SYNC_INTERVAL_MINIMUM_SECS,
1817
};
1918
use crate::fee_estimator::{
2019
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
@@ -208,31 +207,67 @@ impl ChainSource {
208207
match self {
209208
Self::Esplora { sync_config, logger, .. } => {
210209
// Setup syncing intervals
211-
let onchain_wallet_sync_interval_secs = sync_config
212-
.onchain_wallet_sync_interval_secs
213-
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
214-
let mut onchain_wallet_sync_interval =
215-
tokio::time::interval(Duration::from_secs(onchain_wallet_sync_interval_secs));
216-
onchain_wallet_sync_interval
217-
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
210+
let mut onchain_wallet_sync_interval = match sync_config.onchain_wallet_sync_interval_secs() {
211+
Some(secs) => {
212+
log_info!(
213+
logger,
214+
"Onchain wallet background syncing enabled with interval of {} seconds",
215+
secs
216+
);
217+
let mut interval = tokio::time::interval(Duration::from_secs(secs));
218+
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
219+
Some(interval)
220+
},
221+
None => {
222+
log_info!(
223+
logger,
224+
"Onchain wallet background syncing disabled. Manual syncing required.",
225+
);
226+
None
227+
}
228+
};
218229

219-
let fee_rate_cache_update_interval_secs = sync_config
220-
.fee_rate_cache_update_interval_secs
221-
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
222-
let mut fee_rate_update_interval =
223-
tokio::time::interval(Duration::from_secs(fee_rate_cache_update_interval_secs));
224-
// When starting up, we just blocked on updating, so skip the first tick.
225-
fee_rate_update_interval.reset();
226-
fee_rate_update_interval
227-
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
230+
let mut fee_rate_update_interval = match sync_config.fee_rate_cache_update_interval_secs() {
231+
Some(secs) => {
232+
log_info!(
233+
logger,
234+
"Fee rate cache background updating enabled with interval of {} seconds",
235+
secs
236+
);
237+
let mut interval = tokio::time::interval(Duration::from_secs(secs));
238+
// When starting up, we just blocked on updating, so skip the first tick.
239+
interval.reset();
240+
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
241+
Some(interval)
242+
},
243+
None => {
244+
log_info!(
245+
logger,
246+
"Fee rate cache background updating disabled. Manual updates required.",
247+
);
248+
None
249+
}
250+
};
228251

229-
let lightning_wallet_sync_interval_secs = sync_config
230-
.lightning_wallet_sync_interval_secs
231-
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
232-
let mut lightning_wallet_sync_interval =
233-
tokio::time::interval(Duration::from_secs(lightning_wallet_sync_interval_secs));
234-
lightning_wallet_sync_interval
235-
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
252+
let mut lightning_wallet_sync_interval = match sync_config.lightning_wallet_sync_interval_secs() {
253+
Some(secs) => {
254+
log_info!(
255+
logger,
256+
"Lightning wallet background syncing enabled with interval of {} seconds",
257+
secs
258+
);
259+
let mut interval = tokio::time::interval(Duration::from_secs(secs));
260+
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
261+
Some(interval)
262+
},
263+
None => {
264+
log_info!(
265+
logger,
266+
"Lightning wallet background syncing disabled. Manual syncing required.",
267+
);
268+
None
269+
}
270+
};
236271

237272
// Start the syncing loop.
238273
loop {
@@ -244,13 +279,34 @@ impl ChainSource {
244279
);
245280
return;
246281
}
247-
_ = onchain_wallet_sync_interval.tick() => {
282+
283+
_ = async {
284+
if let Some(ref mut interval) = onchain_wallet_sync_interval {
285+
interval.tick().await
286+
} else {
287+
std::future::pending::<tokio::time::Instant>().await
288+
}
289+
}, if onchain_wallet_sync_interval.is_some() => {
248290
let _ = self.sync_onchain_wallet().await;
249291
}
250-
_ = fee_rate_update_interval.tick() => {
292+
293+
_ = async {
294+
if let Some(ref mut interval) = fee_rate_update_interval {
295+
interval.tick().await
296+
} else {
297+
std::future::pending::<tokio::time::Instant>().await
298+
}
299+
}, if fee_rate_update_interval.is_some() => {
251300
let _ = self.update_fee_rate_estimates().await;
252301
}
253-
_ = lightning_wallet_sync_interval.tick() => {
302+
303+
_ = async {
304+
if let Some(ref mut interval) = lightning_wallet_sync_interval {
305+
interval.tick().await
306+
} else {
307+
std::future::pending::<tokio::time::Instant>().await
308+
}
309+
}, if lightning_wallet_sync_interval.is_some() => {
254310
let _ = self.sync_lightning_wallet(
255311
Arc::clone(&channel_manager),
256312
Arc::clone(&chain_monitor),

src/config.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,57 @@ pub struct EsploraSyncConfig {
296296
/// The time in-between background sync attempts of the onchain wallet, in seconds.
297297
///
298298
/// **Note:** A minimum of 10 seconds is always enforced.
299-
pub onchain_wallet_sync_interval_secs: u64,
299+
///
300+
/// If set to `None`, backgrounf syncing of the onchain wallet will be disabled.
301+
/// Users will need to manually sync via `sync_wallets` in this case.
302+
pub onchain_wallet_sync_interval_secs: Option<u64>,
303+
300304
/// The time in-between background sync attempts of the LDK wallet, in seconds.
301305
///
302306
/// **Note:** A minimum of 10 seconds is always enforced.
303-
pub lightning_wallet_sync_interval_secs: u64,
307+
///
308+
/// If set to `None`, background syncing of the lightning wallet will be disabled.
309+
/// Users will need to manually sync via `sync_wallets` in this case.
310+
pub lightning_wallet_sync_interval_secs: Option<u64>,
311+
304312
/// The time in-between background update attempts to our fee rate cache, in seconds.
305313
///
306314
/// **Note:** A minimum of 10 seconds is always enforced.
307-
pub fee_rate_cache_update_interval_secs: u64,
315+
///
316+
/// If set to `None`, background updating of the fee rate cache will be disabled.
317+
/// Users will need to manually update the fee rate cache in this case.
318+
pub fee_rate_cache_update_interval_secs: Option<u64>,
319+
}
320+
321+
impl EsploraSyncConfig {
322+
/// Returns the safe onchain wallet sync interval that respects the minimum threshold
323+
/// when enabled. Returns `None` if background syncnig is disabled.
324+
pub fn onchain_wallet_sync_interval_secs(&self) -> Option<u64> {
325+
self.onchain_wallet_sync_interval_secs
326+
.map(|interval| interval.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS))
327+
}
328+
329+
/// Returns the safe lightning wallet sync interval that respects the minimum threshold
330+
/// when enabled. Returns `None` if background syncing is disabled.
331+
pub fn lightning_wallet_sync_interval_secs(&self) -> Option<u64> {
332+
self.lightning_wallet_sync_interval_secs
333+
.map(|interval| interval.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS))
334+
}
335+
336+
/// Returns the safe fee rate cache update interval that respects the minimum threshold
337+
/// when enabled. Returns `None` if background syncing is disabled.
338+
pub fn fee_rate_cache_update_interval_secs(&self) -> Option<u64> {
339+
self.fee_rate_cache_update_interval_secs
340+
.map(|interval| interval.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS))
341+
}
308342
}
309343

310344
impl Default for EsploraSyncConfig {
311345
fn default() -> Self {
312346
Self {
313-
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
314-
lightning_wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
315-
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
347+
onchain_wallet_sync_interval_secs: Some(DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS),
348+
lightning_wallet_sync_interval_secs: Some(DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS),
349+
fee_rate_cache_update_interval_secs: Some(DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS),
316350
}
317351
}
318352
}

tests/common/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ pub(crate) fn setup_node(
311311
TestChainSource::Esplora(electrsd) => {
312312
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
313313
let mut sync_config = EsploraSyncConfig::default();
314-
sync_config.onchain_wallet_sync_interval_secs = 100000;
315-
sync_config.lightning_wallet_sync_interval_secs = 100000;
314+
sync_config.onchain_wallet_sync_interval_secs = Some(100000);
315+
sync_config.lightning_wallet_sync_interval_secs = Some(100000);
316316
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
317317
},
318318
TestChainSource::BitcoindRpc(bitcoind) => {

tests/integration_tests_rust.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ fn multi_hop_sending() {
128128
for _ in 0..5 {
129129
let config = random_config(true);
130130
let mut sync_config = EsploraSyncConfig::default();
131-
sync_config.onchain_wallet_sync_interval_secs = 100000;
132-
sync_config.lightning_wallet_sync_interval_secs = 100000;
131+
sync_config.onchain_wallet_sync_interval_secs = None;
132+
sync_config.lightning_wallet_sync_interval_secs = None;
133133
setup_builder!(builder, config.node_config);
134134
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
135135
let node = builder.build().unwrap();
@@ -228,8 +228,8 @@ fn start_stop_reinit() {
228228
Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.clone().into()));
229229

230230
let mut sync_config = EsploraSyncConfig::default();
231-
sync_config.onchain_wallet_sync_interval_secs = 100000;
232-
sync_config.lightning_wallet_sync_interval_secs = 100000;
231+
sync_config.onchain_wallet_sync_interval_secs = Some(100000);
232+
sync_config.lightning_wallet_sync_interval_secs = Some(100000);
233233
setup_builder!(builder, config.node_config);
234234
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
235235

@@ -1049,8 +1049,8 @@ fn lsps2_client_service_integration() {
10491049
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
10501050

10511051
let mut sync_config = EsploraSyncConfig::default();
1052-
sync_config.onchain_wallet_sync_interval_secs = 100000;
1053-
sync_config.lightning_wallet_sync_interval_secs = 100000;
1052+
sync_config.onchain_wallet_sync_interval_secs = Some(100000);
1053+
sync_config.lightning_wallet_sync_interval_secs = Some(100000);
10541054

10551055
// Setup three nodes: service, client, and payer
10561056
let channel_opening_fee_ppm = 10_000;

0 commit comments

Comments
 (0)