Skip to content

Commit a758c74

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

File tree

6 files changed

+108
-72
lines changed

6 files changed

+108
-72
lines changed

bindings/ldk_node.udl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ dictionary AnchorChannelsConfig {
2020
};
2121

2222
dictionary EsploraSyncConfig {
23-
u64 onchain_wallet_sync_interval_secs;
24-
u64 lightning_wallet_sync_interval_secs;
25-
u64 fee_rate_cache_update_interval_secs;
23+
u64? onchain_wallet_sync_interval_secs;
24+
u64? lightning_wallet_sync_interval_secs;
25+
u64? fee_rate_cache_update_interval_secs;
2626
};
2727

2828
dictionary LSPS2ServiceConfig {

src/chain/mod.rs

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -207,57 +207,81 @@ impl ChainSource {
207207
) {
208208
match self {
209209
Self::Esplora { sync_config, logger, .. } => {
210-
// 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+
// Setup background syncing intervals if enabled
211+
if let Some(background_sync_config) = sync_config.background_sync_config {
212+
let onchain_interval_secs = background_sync_config
213+
.onchain_wallet_sync_interval_secs
214+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
215+
let mut onchain_interval =
216+
tokio::time::interval(Duration::from_secs(onchain_interval_secs));
217+
onchain_interval
218+
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
219+
log_info!(
220+
logger,
221+
"Onchain wallet background syncing enabled with interval of {} seconds",
222+
onchain_interval_secs
223+
);
218224

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);
225+
let lightning_interval_secs = background_sync_config
226+
.lightning_wallet_sync_interval_secs
227+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
228+
let mut lightning_interval =
229+
tokio::time::interval(Duration::from_secs(lightning_interval_secs));
230+
lightning_interval
231+
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
232+
log_info!(
233+
logger,
234+
"Lightning wallet background syncing enabled with interval of {} seconds",
235+
lightning_interval_secs
236+
);
228237

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);
238+
let fee_rate_interval_secs = background_sync_config
239+
.fee_rate_cache_update_interval_secs
240+
.max(WALLET_SYNC_INTERVAL_MINIMUM_SECS);
241+
let mut fee_rate_interval =
242+
tokio::time::interval(Duration::from_secs(fee_rate_interval_secs));
243+
// When starting up, we just blocked on updating, so skip the first tick.
244+
fee_rate_interval.reset();
245+
fee_rate_interval
246+
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
247+
log_info!(
248+
logger,
249+
"Fee rate background syncing enabled with interval of {} seconds",
250+
fee_rate_interval_secs
251+
);
236252

237-
// Start the syncing loop.
238-
loop {
239-
tokio::select! {
240-
_ = stop_sync_receiver.changed() => {
241-
log_trace!(
242-
logger,
243-
"Stopping background syncing on-chain wallet.",
244-
);
245-
return;
246-
}
247-
_ = onchain_wallet_sync_interval.tick() => {
248-
let _ = self.sync_onchain_wallet().await;
249-
}
250-
_ = fee_rate_update_interval.tick() => {
251-
let _ = self.update_fee_rate_estimates().await;
252-
}
253-
_ = lightning_wallet_sync_interval.tick() => {
254-
let _ = self.sync_lightning_wallet(
255-
Arc::clone(&channel_manager),
256-
Arc::clone(&chain_monitor),
257-
Arc::clone(&output_sweeper),
258-
).await;
253+
loop {
254+
tokio::select! {
255+
_ = stop_sync_receiver.changed() => {
256+
log_trace!(logger, "Stopping Esplora background syncing.");
257+
return;
258+
}
259+
260+
_ = onchain_interval.tick() => {
261+
let _ = self.sync_onchain_wallet().await;
262+
}
263+
264+
_ = fee_rate_interval.tick() => {
265+
let _ = self.update_fee_rate_estimates().await;
266+
}
267+
268+
_ = lightning_interval.tick() => {
269+
let _ = self.sync_lightning_wallet(
270+
Arc::clone(&channel_manager),
271+
Arc::clone(&chain_monitor),
272+
Arc::clone(&output_sweeper),
273+
).await;
274+
}
259275
}
260276
}
277+
} else {
278+
// Background syncing is disabled
279+
log_info!(
280+
logger,
281+
"Background syncing disabled. Manual syncing required for onchain wallet, lightning wallet, and fee rate updates.",
282+
);
283+
284+
return;
261285
}
262286
},
263287
Self::BitcoindRpc {

src/config.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use lightning::util::config::UserConfig;
1919
use bitcoin::secp256k1::PublicKey;
2020
use bitcoin::Network;
2121

22+
use crate::Node;
2223
use std::time::Duration;
2324

2425
// Config defaults
@@ -282,7 +283,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
282283
user_config
283284
}
284285

285-
/// Options related to syncing the Lightning and on-chain wallets via an Esplora backend.
286+
/// Options related to background syncing the Lightning and on-chain wallets via an Esplora backend.
286287
///
287288
/// ### Defaults
288289
///
@@ -292,27 +293,39 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
292293
/// | `lightning_wallet_sync_interval_secs` | 30 |
293294
/// | `fee_rate_cache_update_interval_secs` | 600 |
294295
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
295-
pub struct EsploraSyncConfig {
296+
pub struct BackgroundSyncConfig {
296297
/// The time in-between background sync attempts of the onchain wallet, in seconds.
297298
///
298-
/// **Note:** A minimum of 10 seconds is always enforced.
299+
/// **Note:** A minimum of 10 seconds is enforced when background syncing is enabled.
299300
pub onchain_wallet_sync_interval_secs: u64,
301+
300302
/// The time in-between background sync attempts of the LDK wallet, in seconds.
301303
///
302-
/// **Note:** A minimum of 10 seconds is always enforced.
304+
/// **Note:** A minimum of 10 seconds is enforced when background syncing is enabled.
303305
pub lightning_wallet_sync_interval_secs: u64,
306+
304307
/// The time in-between background update attempts to our fee rate cache, in seconds.
305308
///
306-
/// **Note:** A minimum of 10 seconds is always enforced.
309+
/// **Note:** A minimum of 10 seconds is enforced when background syncing is enabled.
307310
pub fee_rate_cache_update_interval_secs: u64,
308311
}
309312

313+
/// Configuration for syncing with an Esplora backend.
314+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
315+
pub struct EsploraSyncConfig {
316+
/// Background sync configuration. If set to `None`, background syncing will be disabled.
317+
/// Users will need to manually sync via [`Node::sync_wallets`] for the wallets and fee rate updates.
318+
pub background_sync_config: Option<BackgroundSyncConfig>,
319+
}
320+
310321
impl Default for EsploraSyncConfig {
311322
fn default() -> Self {
312323
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,
324+
background_sync_config: Some(BackgroundSyncConfig {
325+
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
326+
lightning_wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
327+
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
328+
}),
316329
}
317330
}
318331
}

src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,14 +1219,13 @@ impl Node {
12191219
/// Manually sync the LDK and BDK wallets with the current chain state and update the fee rate
12201220
/// cache.
12211221
///
1222-
/// **Note:** The wallets are regularly synced in the background, which is configurable via the
1223-
/// respective config object, e.g., via
1224-
/// [`EsploraSyncConfig::onchain_wallet_sync_interval_secs`] and
1225-
/// [`EsploraSyncConfig::lightning_wallet_sync_interval_secs`]. Therefore, using this blocking
1226-
/// sync method is almost always redundant and should be avoided where possible.
1222+
/// **Note:** The wallets are regularly synced in the background if background syncing is enabled
1223+
/// via [`EsploraSyncConfig::background_sync_config`]. Therefore, using this blocking sync method
1224+
/// is almost always redundant when background syncing is enabled and should be avoided where possible.
1225+
/// However, if background syncing is disabled (i.e., `background_sync_config` is set to `None`),
1226+
/// this method must be called manually to keep wallets in sync with the chain state.
12271227
///
1228-
/// [`EsploraSyncConfig::onchain_wallet_sync_interval_secs`]: crate::config::EsploraSyncConfig::onchain_wallet_sync_interval_secs
1229-
/// [`EsploraSyncConfig::lightning_wallet_sync_interval_secs`]: crate::config::EsploraSyncConfig::lightning_wallet_sync_interval_secs
1228+
/// [`EsploraSyncConfig::background_sync_config`]: crate::config::EsploraSyncConfig::background_sync_config
12301229
pub fn sync_wallets(&self) -> Result<(), Error> {
12311230
let rt_lock = self.runtime.read().unwrap();
12321231
if rt_lock.is_none() {

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)