Skip to content

Commit b557827

Browse files
committed
test: insert stall to ensure proper timing of test
With this change in place, `process_next_nakamoto_block_deadlock` will timeout without the fix, and run successfully with the fix in place.
1 parent ae946dd commit b557827

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use crate::chainstate::nakamoto::tests::get_account;
4848
use crate::chainstate::nakamoto::tests::node::TestStacker;
4949
use crate::chainstate::nakamoto::{
5050
NakamotoBlock, NakamotoBlockObtainMethod, NakamotoChainState, NakamotoStagingBlocksConnRef,
51+
TEST_PROCESS_BLOCK_STALL,
5152
};
5253
use crate::chainstate::stacks::address::PoxAddress;
5354
use crate::chainstate::stacks::boot::pox_4_tests::{get_stacking_minimum, get_tip};
@@ -2495,6 +2496,7 @@ fn process_next_nakamoto_block_deadlock() {
24952496
info!("Creating peer");
24962497

24972498
let mut peer = boot_plan.boot_into_nakamoto_peer(vec![], None);
2499+
let mut sortition_db = peer.sortdb().reopen().unwrap();
24982500
let (chainstate, _) = &mut peer
24992501
.stacks_node
25002502
.as_mut()
@@ -2503,20 +2505,26 @@ fn process_next_nakamoto_block_deadlock() {
25032505
.reopen()
25042506
.unwrap();
25052507

2506-
// Lock the sortdb
2507-
info!(" ------------------------------- TRYING TO LOCK THE SORTDB");
2508-
let mut sortition_db = peer.sortdb().reopen().unwrap();
2509-
let sort_tx = sortition_db.tx_begin().unwrap();
2510-
info!(" ------------------------------- SORTDB LOCKED");
2508+
TEST_PROCESS_BLOCK_STALL.lock().unwrap().replace(true);
25112509

25122510
let miner_thread = std::thread::spawn(move || {
25132511
info!(" ------------------------------- MINING TENURE");
25142512
let (block, burn_height, ..) =
25152513
peer.single_block_tenure(&private_key, |_| {}, |_| {}, |_| true);
2516-
peer.try_process_block(&block).unwrap();
25172514
info!(" ------------------------------- TENURE MINED");
25182515
});
25192516

2517+
// Wait a bit, to ensure the miner has reached the stall
2518+
std::thread::sleep(std::time::Duration::from_secs(10));
2519+
2520+
// Lock the sortdb
2521+
info!(" ------------------------------- TRYING TO LOCK THE SORTDB");
2522+
let sort_tx = sortition_db.tx_begin().unwrap();
2523+
info!(" ------------------------------- SORTDB LOCKED");
2524+
2525+
// Un-stall the block processing
2526+
TEST_PROCESS_BLOCK_STALL.lock().unwrap().replace(false);
2527+
25202528
// Wait a bit, to ensure the tenure will have grabbed any locks it needs
25212529
std::thread::sleep(std::time::Duration::from_secs(10));
25222530

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ lazy_static! {
270270
];
271271
}
272272

273+
// Cause an artifical stall in block-processing, for testing.
274+
#[cfg(any(test, feature = "testing"))]
275+
pub static TEST_PROCESS_BLOCK_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
276+
273277
/// Trait for common MARF getters between StacksDBConn and StacksDBTx
274278
pub trait StacksDBIndexed {
275279
fn get(&mut self, tip: &StacksBlockId, key: &str) -> Result<Option<String>, DBError>;
@@ -1722,6 +1726,17 @@ impl NakamotoChainState {
17221726
canonical_sortition_tip: &SortitionId,
17231727
dispatcher_opt: Option<&'a T>,
17241728
) -> Result<Option<StacksEpochReceipt>, ChainstateError> {
1729+
#[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+
}
17251740
let nakamoto_blocks_db = stacks_chain_state.nakamoto_blocks_db();
17261741
let Some((next_ready_block, block_size)) =
17271742
nakamoto_blocks_db.next_ready_nakamoto_block(stacks_chain_state.db())?

0 commit comments

Comments
 (0)