Skip to content

Commit e506206

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

File tree

7 files changed

+117
-83
lines changed

7 files changed

+117
-83
lines changed

bindings/ldk_node.udl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ dictionary AnchorChannelsConfig {
1919
u64 per_channel_reserve_sats;
2020
};
2121

22-
dictionary EsploraSyncConfig {
22+
dictionary BackgroundSyncConfig {
2323
u64 onchain_wallet_sync_interval_secs;
2424
u64 lightning_wallet_sync_interval_secs;
2525
u64 fee_rate_cache_update_interval_secs;
2626
};
2727

28+
dictionary EsploraSyncConfig {
29+
BackgroundSyncConfig? background_sync_config;
30+
};
31+
2832
dictionary LSPS2ServiceConfig {
2933
string? require_token;
3034
boolean advertise_service;

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: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -282,37 +282,51 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
282282
user_config
283283
}
284284

285-
/// Options related to syncing the Lightning and on-chain wallets via an Esplora backend.
286-
///
287-
/// ### Defaults
288-
///
289-
/// | Parameter | Value |
290-
/// |----------------------------------------|--------------------|
291-
/// | `onchain_wallet_sync_interval_secs` | 80 |
292-
/// | `lightning_wallet_sync_interval_secs` | 30 |
293-
/// | `fee_rate_cache_update_interval_secs` | 600 |
285+
/// Options related to background syncing the Lightning and on-chain wallets via an Esplora backend.
294286
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
295-
pub struct EsploraSyncConfig {
287+
pub struct BackgroundSyncConfig {
296288
/// The time in-between background sync attempts of the onchain wallet, in seconds.
297289
///
298-
/// **Note:** A minimum of 10 seconds is always enforced.
290+
/// **Note:** A minimum of 10 seconds is enforced when background syncing is enabled.
299291
pub onchain_wallet_sync_interval_secs: u64,
292+
300293
/// The time in-between background sync attempts of the LDK wallet, in seconds.
301294
///
302-
/// **Note:** A minimum of 10 seconds is always enforced.
295+
/// **Note:** A minimum of 10 seconds is enforced when background syncing is enabled.
303296
pub lightning_wallet_sync_interval_secs: u64,
297+
304298
/// The time in-between background update attempts to our fee rate cache, in seconds.
305299
///
306-
/// **Note:** A minimum of 10 seconds is always enforced.
300+
/// **Note:** A minimum of 10 seconds is enforced when background syncing is enabled.
307301
pub fee_rate_cache_update_interval_secs: u64,
308302
}
309303

304+
/// Configuration for syncing with an Esplora backend.
305+
///
306+
/// ### Defaults
307+
///
308+
/// By default, background syncing is enabled with the following intervals:
309+
///
310+
/// | Parameter | Value |
311+
/// |----------------------------------------|--------------------|
312+
/// | `onchain_wallet_sync_interval_secs` | 80 |
313+
/// | `lightning_wallet_sync_interval_secs` | 30 |
314+
/// | `fee_rate_cache_update_interval_secs` | 600 |
315+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
316+
pub struct EsploraSyncConfig {
317+
/// Background sync configuration. If set to `None`, background syncing will be disabled.
318+
/// Users will need to manually sync via [`Node::sync_wallets`] for the wallets and fee rate updates.
319+
pub background_sync_config: Option<BackgroundSyncConfig>,
320+
}
321+
310322
impl Default for EsploraSyncConfig {
311323
fn default() -> Self {
312324
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,
325+
background_sync_config: Some(BackgroundSyncConfig {
326+
onchain_wallet_sync_interval_secs: DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS,
327+
lightning_wallet_sync_interval_secs: DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS,
328+
fee_rate_cache_update_interval_secs: DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS,
329+
}),
316330
}
317331
}
318332
}

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() {

src/uniffi_types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
// Make sure to add any re-exported items that need to be used in uniffi below.
1212

1313
pub use crate::config::{
14-
default_config, AnchorChannelsConfig, EsploraSyncConfig, MaxDustHTLCExposure,
14+
default_config, AnchorChannelsConfig, BackgroundSyncConfig, EsploraSyncConfig,
15+
MaxDustHTLCExposure,
1516
};
1617
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
1718
pub use crate::liquidity::{LSPS1OrderStatus, LSPS2ServiceConfig, OnchainPaymentInfo, PaymentInfo};

tests/common/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ pub(crate) fn setup_node(
310310
match chain_source {
311311
TestChainSource::Esplora(electrsd) => {
312312
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
313-
let mut sync_config = EsploraSyncConfig::default();
314-
sync_config.onchain_wallet_sync_interval_secs = 100000;
315-
sync_config.lightning_wallet_sync_interval_secs = 100000;
313+
let sync_config = EsploraSyncConfig { background_sync_config: None };
316314
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
317315
},
318316
TestChainSource::BitcoindRpc(bitcoind) => {

tests/integration_tests_rust.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ fn multi_hop_sending() {
127127
let mut nodes = Vec::new();
128128
for _ in 0..5 {
129129
let config = random_config(true);
130-
let mut sync_config = EsploraSyncConfig::default();
131-
sync_config.onchain_wallet_sync_interval_secs = 100000;
132-
sync_config.lightning_wallet_sync_interval_secs = 100000;
130+
let sync_config = EsploraSyncConfig { background_sync_config: None };
133131
setup_builder!(builder, config.node_config);
134132
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
135133
let node = builder.build().unwrap();
@@ -227,9 +225,7 @@ fn start_stop_reinit() {
227225
let test_sync_store: Arc<dyn KVStore + Sync + Send> =
228226
Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.clone().into()));
229227

230-
let mut sync_config = EsploraSyncConfig::default();
231-
sync_config.onchain_wallet_sync_interval_secs = 100000;
232-
sync_config.lightning_wallet_sync_interval_secs = 100000;
228+
let sync_config = EsploraSyncConfig { background_sync_config: None };
233229
setup_builder!(builder, config.node_config);
234230
builder.set_chain_source_esplora(esplora_url.clone(), Some(sync_config));
235231

@@ -1048,9 +1044,7 @@ fn lsps2_client_service_integration() {
10481044

10491045
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
10501046

1051-
let mut sync_config = EsploraSyncConfig::default();
1052-
sync_config.onchain_wallet_sync_interval_secs = 100000;
1053-
sync_config.lightning_wallet_sync_interval_secs = 100000;
1047+
let sync_config = EsploraSyncConfig { background_sync_config: None };
10541048

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

0 commit comments

Comments
 (0)