Skip to content

Commit a85d587

Browse files
committed
feat: avoid unnecessary pause by looping and exiting early
1 parent 80c5c6f commit a85d587

File tree

1 file changed

+33
-1
lines changed
  • testnet/stacks-node/src/nakamoto_node

1 file changed

+33
-1
lines changed

testnet/stacks-node/src/nakamoto_node/miner.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,32 @@ impl BlockMinerThread {
574574
);
575575
self.reset_nonce_cache = false;
576576

577-
thread::sleep(self.config.miner.empty_mempool_sleep_time);
577+
// Pause the miner to wait for transactions to arrive
578+
let now = Instant::now();
579+
while now.elapsed() < self.config.miner.empty_mempool_sleep_time {
580+
if self.abort_flag.load(Ordering::SeqCst) {
581+
info!("Miner interrupted while mining in order to shut down");
582+
self.globals
583+
.raise_initiative(format!("MiningFailure: aborted by node"));
584+
return Err(ChainstateError::MinerAborted.into());
585+
}
586+
587+
// Check if the burnchain tip has changed
588+
let Ok(sort_db) = SortitionDB::open(
589+
&self.config.get_burn_db_file_path(),
590+
false,
591+
self.burnchain.pox_constants.clone(),
592+
) else {
593+
error!("Failed to open sortition DB. Will try mining again.");
594+
continue;
595+
};
596+
if self.check_burn_tip_changed(&sort_db).is_err() {
597+
return Err(NakamotoNodeError::BurnchainTipChanged);
598+
}
599+
600+
thread::sleep(Duration::from_millis(ABORT_TRY_AGAIN_MS));
601+
}
602+
578603
break None;
579604
}
580605
Err(e) => {
@@ -685,6 +710,13 @@ impl BlockMinerThread {
685710

686711
thread::sleep(Duration::from_millis(ABORT_TRY_AGAIN_MS));
687712

713+
if self.abort_flag.load(Ordering::SeqCst) {
714+
info!("Miner interrupted while mining in order to shut down");
715+
self.globals
716+
.raise_initiative(format!("MiningFailure: aborted by node"));
717+
return Err(ChainstateError::MinerAborted.into());
718+
}
719+
688720
// Check if the burnchain tip has changed
689721
let Ok(sort_db) = SortitionDB::open(
690722
&self.config.get_burn_db_file_path(),

0 commit comments

Comments
 (0)