From 0cd3ffa241db3a4db2aa4ec0f1ef3e7433e4eee9 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 20 Sep 2023 12:57:22 +0200 Subject: [PATCH] feat(app): Include Lightning wallet sync in refresh_wallet_info I do not think that the Lightning wallet (on-chain) sync should have an impact on the balances or history in most scenarios, but I think it is still useful if we are waiting for confirmations on Lightning-related on-chain transactions (fund, commitment, etc.). --- crates/ln-dlc-node/src/node/mod.rs | 49 +++++++++++++++++++++--------- mobile/native/src/ln_dlc/mod.rs | 5 +++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/crates/ln-dlc-node/src/node/mod.rs b/crates/ln-dlc-node/src/node/mod.rs index 47b7a30aa..dc3bfed81 100644 --- a/crates/ln-dlc-node/src/node/mod.rs +++ b/crates/ln-dlc-node/src/node/mod.rs @@ -439,7 +439,7 @@ where self.channel_manager.clone(), )); - tokio::spawn(lightning_wallet_sync( + tokio::spawn(periodic_lightning_wallet_sync( self.channel_manager.clone(), self.chain_monitor.clone(), self.settings.clone(), @@ -545,6 +545,14 @@ where pub fn sync_on_chain_wallet(&self) -> Result<()> { self.wallet.sync_and_update_address_cache() } + + pub fn sync_lightning_wallet(&self) -> Result<()> { + lightning_wallet_sync( + &self.channel_manager, + &self.chain_monitor, + &self.esplora_client, + ) + } } async fn update_fee_rate_estimates( @@ -603,26 +611,15 @@ fn spawn_background_processor( remote_handle } -async fn lightning_wallet_sync( +async fn periodic_lightning_wallet_sync( channel_manager: Arc, chain_monitor: Arc, settings: Arc>, esplora_client: Arc>>, ) { loop { - let now = Instant::now(); - let confirmables = vec![ - &*channel_manager as &(dyn Confirm + Sync + Send), - &*chain_monitor as &(dyn Confirm + Sync + Send), - ]; - match esplora_client.sync(confirmables) { - Ok(()) => tracing::info!( - "Background sync of Lightning wallet finished in {}ms.", - now.elapsed().as_millis() - ), - Err(e) => { - tracing::error!("Background sync of Lightning wallet failed: {e:#}") - } + if let Err(e) = lightning_wallet_sync(&channel_manager, &chain_monitor, &esplora_client) { + tracing::error!("Background sync of Lightning wallet failed: {e:#}") } let interval = { @@ -633,6 +630,28 @@ async fn lightning_wallet_sync( } } +fn lightning_wallet_sync( + channel_manager: &ChannelManager, + chain_monitor: &ChainMonitor, + esplora_client: &EsploraSyncClient>, +) -> Result<()> { + let now = Instant::now(); + let confirmables = vec![ + channel_manager as &(dyn Confirm + Sync + Send), + chain_monitor as &(dyn Confirm + Sync + Send), + ]; + esplora_client + .sync(confirmables) + .context("Lightning wallet sync failed")?; + + tracing::info!( + "Lightning wallet sync finished in {}ms.", + now.elapsed().as_millis() + ); + + Ok(()) +} + fn shadow_sync_periodically( settings: Arc>, node_storage: Arc, diff --git a/mobile/native/src/ln_dlc/mod.rs b/mobile/native/src/ln_dlc/mod.rs index 1c7be7668..8685492e7 100644 --- a/mobile/native/src/ln_dlc/mod.rs +++ b/mobile/native/src/ln_dlc/mod.rs @@ -105,6 +105,11 @@ pub async fn refresh_wallet_info() -> Result<()> { if let Err(e) = wallet.sync() { tracing::error!("Manually triggered on-chain sync failed: {e:#}"); } + + if let Err(e) = node.inner.sync_lightning_wallet() { + tracing::error!("Manually triggered Lightning wallet sync failed: {e:#}"); + } + if let Err(e) = keep_wallet_balance_and_history_up_to_date(node) { tracing::error!("Failed to keep wallet history up to date: {e:#}"); }