From 4694eaaef9f6650047ab5a527c8c7852a01ab7fe Mon Sep 17 00:00:00 2001 From: Michael Mallan Date: Tue, 15 Oct 2024 16:39:05 +0100 Subject: [PATCH] poller: don't poll now if blockchain syncing If a user broadcasts a spend while the blockchain is syncing (e.g. via liana-cli), we tell the poller to run now. However, if the blockchain is still syncing, the blockchain height is likely to increase before the poll completes and so the poller is likely to restart multiple times. Even if the poller doesn't restart multiple times, this change makes the poller behaviour consistent with regard to a syncing blockchain. --- src/bitcoin/poller/mod.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/bitcoin/poller/mod.rs b/src/bitcoin/poller/mod.rs index c2986238..b2304394 100644 --- a/src/bitcoin/poller/mod.rs +++ b/src/bitcoin/poller/mod.rs @@ -89,9 +89,28 @@ impl Poller { } Ok(PollerMessage::PollNow(sender)) => { // We've been asked to poll, don't wait any further and signal completion to - // the caller. + // the caller, unless the block chain is still syncing. + // Polling while the block chain is syncing could lead to poller restarts + // if the height increases before completion, and in any case this is consistent + // with regular poller behaviour. + if !synced { + let progress = self.bit.sync_progress(); + log::info!( + "Block chain synchronization progress: {:.2}% ({} blocks / {} headers)", + progress.rounded_up_progress() * 100.0, + progress.blocks, + progress.headers + ); + synced = progress.is_complete(); + } + // Update `last_poll` even if we don't poll now so that we don't attempt another + // poll too soon. last_poll = Some(time::Instant::now()); - looper::poll(&mut self.bit, &self.db, &self.secp, &self.descs); + if synced { + looper::poll(&mut self.bit, &self.db, &self.secp, &self.descs); + } else { + log::warn!("Skipped poll as block chain is still synchronizing."); + } if let Err(e) = sender.send(()) { log::error!("Error sending immediate poll completion signal: {}.", e); }