Skip to content

Commit bef8c90

Browse files
committed
refactor: pull stalling logic out into functions
1 parent 74988b8 commit bef8c90

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

stackslib/src/chainstate/nakamoto/coordinator/tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ use crate::chainstate::nakamoto::test_signers::TestSigners;
4747
use crate::chainstate::nakamoto::tests::get_account;
4848
use crate::chainstate::nakamoto::tests::node::TestStacker;
4949
use crate::chainstate::nakamoto::{
50-
NakamotoBlock, NakamotoBlockObtainMethod, NakamotoChainState, NakamotoStagingBlocksConnRef,
50+
disable_process_block_stall, enable_process_block_stall, NakamotoBlock,
51+
NakamotoBlockObtainMethod, NakamotoChainState, NakamotoStagingBlocksConnRef,
5152
TEST_PROCESS_BLOCK_STALL,
5253
};
5354
use crate::chainstate::stacks::address::PoxAddress;
@@ -2505,7 +2506,7 @@ fn process_next_nakamoto_block_deadlock() {
25052506
.reopen()
25062507
.unwrap();
25072508

2508-
TEST_PROCESS_BLOCK_STALL.lock().unwrap().replace(true);
2509+
enable_process_block_stall();
25092510

25102511
let miner_thread = std::thread::spawn(move || {
25112512
info!(" ------------------------------- MINING TENURE");
@@ -2523,7 +2524,7 @@ fn process_next_nakamoto_block_deadlock() {
25232524
info!(" ------------------------------- SORTDB LOCKED");
25242525

25252526
// Un-stall the block processing
2526-
TEST_PROCESS_BLOCK_STALL.lock().unwrap().replace(false);
2527+
disable_process_block_stall();
25272528

25282529
// Wait a bit, to ensure the tenure will have grabbed any locks it needs
25292530
std::thread::sleep(std::time::Duration::from_secs(10));

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,25 @@ lazy_static! {
274274
#[cfg(any(test, feature = "testing"))]
275275
pub static TEST_PROCESS_BLOCK_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
276276

277+
fn stall_block_processing() {
278+
if *TEST_PROCESS_BLOCK_STALL.lock().unwrap() == Some(true) {
279+
// Do an extra check just so we don't log EVERY time.
280+
warn!("Block processing is stalled due to testing directive.");
281+
while *TEST_PROCESS_BLOCK_STALL.lock().unwrap() == Some(true) {
282+
std::thread::sleep(std::time::Duration::from_millis(10));
283+
}
284+
info!("Block processing is no longer stalled due to testing directive.");
285+
}
286+
}
287+
288+
pub fn enable_process_block_stall() {
289+
TEST_PROCESS_BLOCK_STALL.lock().unwrap().replace(true);
290+
}
291+
292+
pub fn disable_process_block_stall() {
293+
TEST_PROCESS_BLOCK_STALL.lock().unwrap().replace(false);
294+
}
295+
277296
/// Trait for common MARF getters between StacksDBConn and StacksDBTx
278297
pub trait StacksDBIndexed {
279298
fn get(&mut self, tip: &StacksBlockId, key: &str) -> Result<Option<String>, DBError>;
@@ -1727,16 +1746,8 @@ impl NakamotoChainState {
17271746
dispatcher_opt: Option<&'a T>,
17281747
) -> Result<Option<StacksEpochReceipt>, ChainstateError> {
17291748
#[cfg(any(test, feature = "testing"))]
1730-
{
1731-
if *TEST_PROCESS_BLOCK_STALL.lock().unwrap() == Some(true) {
1732-
// Do an extra check just so we don't log EVERY time.
1733-
warn!("Block processing is stalled due to testing directive.");
1734-
while *TEST_PROCESS_BLOCK_STALL.lock().unwrap() == Some(true) {
1735-
std::thread::sleep(std::time::Duration::from_millis(10));
1736-
}
1737-
info!("Block processing is no longer stalled due to testing directive.");
1738-
}
1739-
}
1749+
stall_block_processing();
1750+
17401751
let nakamoto_blocks_db = stacks_chain_state.nakamoto_blocks_db();
17411752
let Some((next_ready_block, block_size)) =
17421753
nakamoto_blocks_db.next_ready_nakamoto_block(stacks_chain_state.db())?

0 commit comments

Comments
 (0)