Skip to content

Commit 10c049d

Browse files
committed
Improve sync_wallet docs and use current thread runtime for LDK
We improve the docs, in particular mentioning now that users should always prefer background syncing over blocking sync if possible. Moreover, as we already have a current thread runtime setup for the BDK wallet, we now also use it to sync the LDK wallet. This is a good idea as we want to avoid calling `block_on` on our main runtime where possible, which is always in danger of (temporarily or permanently) blocking the world if something goes wrong.
1 parent 1111451 commit 10c049d

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

src/lib.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,15 +1342,17 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
13421342
}
13431343
}
13441344

1345-
/// Sync the LDK and BDK wallets with the current chain state.
1345+
/// Manually sync the LDK and BDK wallets with the current chain state.
13461346
///
1347-
/// Note that the wallets will be also synced regularly in the background.
1347+
/// **Note:** The wallets are regularly synced in the background, which is configurable via
1348+
/// [`Config::onchain_wallet_sync_interval_secs`] and [`Config::wallet_sync_interval_secs`].
1349+
/// Therefore, using this blocking sync method is almost always redudant and should be avoided
1350+
/// where possible.
13481351
pub fn sync_wallets(&self) -> Result<(), Error> {
13491352
let rt_lock = self.runtime.read().unwrap();
13501353
if rt_lock.is_none() {
13511354
return Err(Error::NotRunning);
13521355
}
1353-
let runtime = rt_lock.as_ref().unwrap();
13541356

13551357
let wallet = Arc::clone(&self.wallet);
13561358
let tx_sync = Arc::clone(&self.tx_sync);
@@ -1373,39 +1375,31 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
13731375
"Sync of on-chain wallet finished in {}ms.",
13741376
now.elapsed().as_millis()
13751377
);
1376-
Ok(())
13771378
}
13781379
Err(e) => {
13791380
log_error!(sync_logger, "Sync of on-chain wallet failed: {}", e);
1380-
Err(e)
1381+
return Err(e);
1382+
}
1383+
};
1384+
1385+
let now = Instant::now();
1386+
match tx_sync.sync(confirmables).await {
1387+
Ok(()) => {
1388+
log_info!(
1389+
sync_logger,
1390+
"Sync of Lightning wallet finished in {}ms.",
1391+
now.elapsed().as_millis()
1392+
);
1393+
Ok(())
1394+
}
1395+
Err(e) => {
1396+
log_error!(sync_logger, "Sync of Lightning wallet failed: {}", e);
1397+
Err(e.into())
13811398
}
13821399
}
13831400
},
13841401
)
1385-
})?;
1386-
1387-
let sync_logger = Arc::clone(&self.logger);
1388-
tokio::task::block_in_place(move || {
1389-
runtime.block_on(async move {
1390-
let now = Instant::now();
1391-
match tx_sync.sync(confirmables).await {
1392-
Ok(()) => {
1393-
log_info!(
1394-
sync_logger,
1395-
"Sync of Lightning wallet finished in {}ms.",
1396-
now.elapsed().as_millis()
1397-
);
1398-
Ok(())
1399-
}
1400-
Err(e) => {
1401-
log_error!(sync_logger, "Sync of Lightning wallet failed: {}", e);
1402-
Err(e)
1403-
}
1404-
}
1405-
})
1406-
})?;
1407-
1408-
Ok(())
1402+
})
14091403
}
14101404

14111405
/// Close a previously opened channel.

0 commit comments

Comments
 (0)