From bc9175a23ab9cfe641f15191bd5420f593ec0c63 Mon Sep 17 00:00:00 2001 From: cygnet Date: Thu, 14 Nov 2024 00:05:15 +0100 Subject: [PATCH] Pass start and end to record scan progress --- src/scanner/scanner.rs | 40 ++++++++++++++++++---------------------- src/updater/updater.rs | 4 ++-- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/scanner/scanner.rs b/src/scanner/scanner.rs index 0038464..976a63f 100644 --- a/src/scanner/scanner.rs +++ b/src/scanner/scanner.rs @@ -66,15 +66,9 @@ impl SpScanner { .get_block_data_for_range(range, dust_limit, with_cutthrough); // process blocks using block data stream - self.process_blocks(block_data_stream, keep_scanning) + self.process_blocks(start, end, block_data_stream, keep_scanning) .await?; - // after processing, send update - if keep_scanning.load(std::sync::atomic::Ordering::Relaxed) { - self.updater.record_scan_height(end)?; - self.updater.save_to_persistent_storage()?; - } - // time elapsed for the scan info!( "Blindbit scan complete in {} seconds", @@ -86,6 +80,8 @@ impl SpScanner { async fn process_blocks( &mut self, + start: Height, + end: Height, block_data_stream: impl Stream>, keep_scanning: &AtomicBool, ) -> Result<()> { @@ -98,39 +94,39 @@ impl SpScanner { let blkheight = blockdata.blkheight; let blkhash = blockdata.blkhash; - let mut send_update = false; + // stop scanning and return if interrupted + if self.interrupt_requested() { + self.updater.save_to_persistent_storage()?; + return Ok(()); + } + + let mut save_to_storage = false; - // send update after 30 seconds since last update - if update_time.elapsed() > Duration::from_secs(30) { - send_update = true; - update_time = Instant::now(); + // always save on last block or after 30 seconds since last save + if blkheight == end || update_time.elapsed() > Duration::from_secs(30) { + save_to_storage = true; } let (found_outputs, found_inputs) = self.process_block(blockdata).await?; if !found_outputs.is_empty() { - send_update = true; + save_to_storage = true; self.updater .record_block_outputs(blkheight, blkhash, found_outputs)?; } if !found_inputs.is_empty() { - send_update = true; + save_to_storage = true; self.updater .record_block_inputs(blkheight, blkhash, found_inputs)?; } // tell the updater we scanned this block - self.updater.record_scan_height(blkheight)?; + self.updater.record_scan_progress(start, blkheight, end)?; - // stop scanning and return if keep_scanning is set to false - if !keep_scanning.load(std::sync::atomic::Ordering::Relaxed) { - self.updater.save_to_persistent_storage()?; - return Ok(()); - } - - if send_update { + if save_to_storage { self.updater.save_to_persistent_storage()?; + update_time = Instant::now(); } } diff --git a/src/updater/updater.rs b/src/updater/updater.rs index 86f1dc3..28ff001 100644 --- a/src/updater/updater.rs +++ b/src/updater/updater.rs @@ -7,8 +7,8 @@ use anyhow::Result; use crate::client::OwnedOutput; pub trait Updater { - /// Ask the updater to record the scanning height. - fn record_scan_height(&mut self, height: Height) -> Result<()>; + /// Ask the updater to record the scanning progress. + fn record_scan_progress(&mut self, start: Height, current: Height, end: Height) -> Result<()>; /// Ask the updater to record the outputs found in a block. fn record_block_outputs(