Skip to content

Commit

Permalink
Add keep_scanning atomic bool to interrupt scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
cygnet3 committed Oct 6, 2024
1 parent 09a70e4 commit 787165a
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/scanner/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{HashMap, HashSet},
sync::atomic::AtomicBool,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -48,6 +49,7 @@ impl SpScanner {
start: Height,
end: Height,
dust_limit: Amount,
keep_scanning: &AtomicBool,
) -> Result<()> {
if start > end {
bail!("bigger start than end: {} > {}", start, end);
Expand All @@ -61,10 +63,13 @@ impl SpScanner {
let block_data_stream = self.backend.get_block_data_for_range(range, dust_limit);

// process blocks using block data stream
self.process_blocks(block_data_stream).await?;
self.process_blocks(block_data_stream, keep_scanning)
.await?;

// after processing, always send update
self.updater.update_last_scan(end);
// after processing, send update
if keep_scanning.load(std::sync::atomic::Ordering::Relaxed) {
self.updater.update_last_scan(end);
}

// time elapsed for the scan
info!(
Expand All @@ -78,6 +83,7 @@ impl SpScanner {
async fn process_blocks(
&mut self,
block_data_stream: impl Stream<Item = Result<BlockData>>,
keep_scanning: &AtomicBool,
) -> Result<()> {
pin_mut!(block_data_stream);

Expand Down Expand Up @@ -111,6 +117,12 @@ impl SpScanner {
.record_block_inputs(blkheight, blkhash, found_inputs)?;
}

// stop scanning and return if keep_scanning is set to false
if !keep_scanning.load(std::sync::atomic::Ordering::Relaxed) {
self.updater.update_last_scan(blkheight);
return Ok(());
}

if send_update {
self.updater.update_last_scan(blkheight);
}
Expand Down

0 comments on commit 787165a

Please sign in to comment.