Skip to content

Commit

Permalink
feat(app): Include Lightning wallet sync in refresh_wallet_info
Browse files Browse the repository at this point in the history
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.).
  • Loading branch information
luckysori committed Sep 20, 2023
1 parent 94ff866 commit 0cd3ffa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
49 changes: 34 additions & 15 deletions crates/ln-dlc-node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -603,26 +611,15 @@ fn spawn_background_processor(
remote_handle
}

async fn lightning_wallet_sync(
async fn periodic_lightning_wallet_sync(
channel_manager: Arc<ChannelManager>,
chain_monitor: Arc<ChainMonitor>,
settings: Arc<RwLock<LnDlcNodeSettings>>,
esplora_client: Arc<EsploraSyncClient<Arc<TracingLogger>>>,
) {
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 = {
Expand All @@ -633,6 +630,28 @@ async fn lightning_wallet_sync(
}
}

fn lightning_wallet_sync(
channel_manager: &ChannelManager,
chain_monitor: &ChainMonitor,
esplora_client: &EsploraSyncClient<Arc<TracingLogger>>,
) -> 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<S: Storage + Sync + Send + 'static>(
settings: Arc<RwLock<LnDlcNodeSettings>>,
node_storage: Arc<S>,
Expand Down
5 changes: 5 additions & 0 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#}");
}
Expand Down

0 comments on commit 0cd3ffa

Please sign in to comment.