Skip to content

Commit

Permalink
fix: fix utxo scan edge case when pc awakes from sleep (#3160)
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Aug 6, 2021
2 parents fbc35c3 + 4d09eae commit 5bdc9f3
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions base_layer/wallet/src/utxo_scanner_service/utxo_scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ where TBackend: WalletBackend + 'static
match self.get_next_peer() {
Some(peer) => match self.attempt_sync(peer.clone()).await {
Ok((total_scanned, final_utxo_pos, elapsed)) => {
debug!(target: LOG_TARGET, "Scanning to UTXO #{}", final_utxo_pos);
debug!(target: LOG_TARGET, "Scanned to UTXO #{}", final_utxo_pos);
self.finalize(total_scanned, final_utxo_pos, elapsed).await?;
return Ok(());
},
Expand Down Expand Up @@ -709,21 +709,34 @@ where TBackend: WalletBackend + 'static
let mut shutdown = self.shutdown_signal.clone();
let start_at = Instant::now() + Duration::from_secs(1);
let mut work_interval = time::interval_at(start_at.into(), self.scan_for_utxo_interval).fuse();
let mut previous = Instant::now();
loop {
futures::select! {
_ = work_interval.select_next_some() => {
let running_flag = self.is_running.clone();
if !running_flag.load(Ordering::SeqCst) {
let task = self.create_task();
debug!(target: LOG_TARGET, "UTXO scanning service starting scan for utxos");
task::spawn(async move {
if let Err(err) = task.run().await {
error!(target: LOG_TARGET, "Error scanning UTXOs: {}", err);
}
//we make sure the flag is set to false here
running_flag.store(false, Ordering::Relaxed);
});
// This bit of code prevents bottled up tokio interval events to be fired successively for the edge
// case where a computer wakes up from sleep.
if start_at.elapsed() > self.scan_for_utxo_interval &&
previous.elapsed() < self.scan_for_utxo_interval.mul_f32(0.9)
{
debug!(
target: LOG_TARGET,
"UTXO scanning work interval event fired too quickly, not running the task"
);
} else {
let running_flag = self.is_running.clone();
if !running_flag.load(Ordering::SeqCst) {
let task = self.create_task();
debug!(target: LOG_TARGET, "UTXO scanning service starting scan for utxos");
task::spawn(async move {
if let Err(err) = task.run().await {
error!(target: LOG_TARGET, "Error scanning UTXOs: {}", err);
}
//we make sure the flag is set to false here
running_flag.store(false, Ordering::Relaxed);
});
}
}
previous = Instant::now();
},
request_context = request_stream.select_next_some() => {
trace!(target: LOG_TARGET, "Handling Service API Request");
Expand Down

0 comments on commit 5bdc9f3

Please sign in to comment.