Skip to content

Commit 572c547

Browse files
committed
fix: remove sleeps in favor of wait_for()
1 parent 4ada7e2 commit 572c547

File tree

2 files changed

+112
-24
lines changed

2 files changed

+112
-24
lines changed

testnet/stacks-node/src/tests/nakamoto_integrations.rs

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,8 +3914,6 @@ fn forked_tenure_is_ignored() {
39143914
thread::sleep(Duration::from_secs(1));
39153915
}
39163916

3917-
sleep_ms(1000);
3918-
39193917
info!("Tenure B broadcasted but did not process a block. Issue the next bitcon block and unstall block commits.");
39203918

39213919
// the block will be stored, not processed, so load it out of staging
@@ -3947,18 +3945,25 @@ fn forked_tenure_is_ignored() {
39473945
// It should also build on block A, since the node has paused processing of block B.
39483946
let commits_before = commits_submitted.load(Ordering::SeqCst);
39493947
let blocks_before = mined_blocks.load(Ordering::SeqCst);
3948+
let blocks_processed_before = coord_channel
3949+
.lock()
3950+
.expect("Mutex poisoned")
3951+
.get_stacks_blocks_processed();
39503952
next_block_and(&mut btc_regtest_controller, 60, || {
39513953
test_skip_commit_op.0.lock().unwrap().replace(false);
39523954
TEST_BLOCK_ANNOUNCE_STALL.lock().unwrap().replace(false);
39533955
let commits_count = commits_submitted.load(Ordering::SeqCst);
39543956
let blocks_count = mined_blocks.load(Ordering::SeqCst);
3955-
Ok(commits_count > commits_before && blocks_count > blocks_before)
3957+
let blocks_processed = coord_channel
3958+
.lock()
3959+
.expect("Mutex poisoned")
3960+
.get_stacks_blocks_processed();
3961+
Ok(commits_count > commits_before
3962+
&& blocks_count > blocks_before
3963+
&& blocks_processed > blocks_processed_before)
39563964
})
39573965
.unwrap();
39583966

3959-
// allow blocks B and C to be processed
3960-
sleep_ms(1000);
3961-
39623967
info!("Tenure C produced a block!");
39633968
let block_tenure_c = NakamotoChainState::get_canonical_block_header(chainstate.db(), &sortdb)
39643969
.unwrap()
@@ -3976,6 +3981,10 @@ fn forked_tenure_is_ignored() {
39763981

39773982
// Now let's produce a second block for tenure C and ensure it builds off of block C.
39783983
let blocks_before = mined_blocks.load(Ordering::SeqCst);
3984+
let blocks_processed_before = coord_channel
3985+
.lock()
3986+
.expect("Mutex poisoned")
3987+
.get_stacks_blocks_processed();
39793988
let start_time = Instant::now();
39803989

39813990
// submit a tx so that the miner will mine an extra block
@@ -3993,8 +4002,14 @@ fn forked_tenure_is_ignored() {
39934002
thread::sleep(Duration::from_secs(1));
39944003
}
39954004

3996-
// give C's second block a moment to process
3997-
sleep_ms(1000);
4005+
wait_for(10, || {
4006+
let blocks_processed = coord_channel
4007+
.lock()
4008+
.expect("Mutex poisoned")
4009+
.get_stacks_blocks_processed();
4010+
Ok(blocks_processed > blocks_processed_before)
4011+
})
4012+
.unwrap();
39984013

39994014
info!("Tenure C produced a second block!");
40004015

@@ -4014,16 +4029,23 @@ fn forked_tenure_is_ignored() {
40144029
// Submit a block commit op for tenure D and mine a stacks block
40154030
let commits_before = commits_submitted.load(Ordering::SeqCst);
40164031
let blocks_before = mined_blocks.load(Ordering::SeqCst);
4032+
let blocks_processed_before = coord_channel
4033+
.lock()
4034+
.expect("Mutex poisoned")
4035+
.get_stacks_blocks_processed();
40174036
next_block_and(&mut btc_regtest_controller, 60, || {
40184037
let commits_count = commits_submitted.load(Ordering::SeqCst);
40194038
let blocks_count = mined_blocks.load(Ordering::SeqCst);
4020-
Ok(commits_count > commits_before && blocks_count > blocks_before)
4039+
let blocks_processed = coord_channel
4040+
.lock()
4041+
.expect("Mutex poisoned")
4042+
.get_stacks_blocks_processed();
4043+
Ok(commits_count > commits_before
4044+
&& blocks_count > blocks_before
4045+
&& blocks_processed > blocks_processed_before)
40214046
})
40224047
.unwrap();
40234048

4024-
// give tenure D's block a moment to process
4025-
sleep_ms(1000);
4026-
40274049
let block_tenure_d = NakamotoChainState::get_canonical_block_header(chainstate.db(), &sortdb)
40284050
.unwrap()
40294051
.unwrap();
@@ -5703,6 +5725,11 @@ fn continue_tenure_extend() {
57035725
info!("Nakamoto miner started...");
57045726
blind_signer(&naka_conf, &signers, proposals_submitted);
57055727

5728+
let blocks_processed_before = coord_channel
5729+
.lock()
5730+
.expect("Mutex poisoned")
5731+
.get_stacks_blocks_processed();
5732+
57065733
wait_for_first_naka_block_commit(60, &commits_submitted);
57075734

57085735
// Mine a regular nakamoto tenure
@@ -5720,7 +5747,20 @@ fn continue_tenure_extend() {
57205747
&[sender_signer_sk],
57215748
&signers,
57225749
);
5723-
sleep_ms(5_000);
5750+
5751+
wait_for(5, || {
5752+
let blocks_processed = coord_channel
5753+
.lock()
5754+
.expect("Mutex poisoned")
5755+
.get_stacks_blocks_processed();
5756+
Ok(blocks_processed > blocks_processed_before)
5757+
})
5758+
.unwrap();
5759+
5760+
let blocks_processed_before = coord_channel
5761+
.lock()
5762+
.expect("Mutex poisoned")
5763+
.get_stacks_blocks_processed();
57245764

57255765
info!("Pausing commit ops to trigger a tenure extend.");
57265766
test_skip_commit_op.0.lock().unwrap().replace(true);
@@ -5733,7 +5773,15 @@ fn continue_tenure_extend() {
57335773
&[sender_signer_sk],
57345774
&signers,
57355775
);
5736-
sleep_ms(5_000);
5776+
5777+
wait_for(5, || {
5778+
let blocks_processed = coord_channel
5779+
.lock()
5780+
.expect("Mutex poisoned")
5781+
.get_stacks_blocks_processed();
5782+
Ok(blocks_processed > blocks_processed_before)
5783+
})
5784+
.unwrap();
57375785

57385786
// Submit a TX
57395787
let transfer_tx = make_stacks_transfer(&sender_sk, 0, send_fee, &recipient, send_amt);
@@ -5759,6 +5807,11 @@ fn continue_tenure_extend() {
57595807
)
57605808
.unwrap();
57615809

5810+
let blocks_processed_before = coord_channel
5811+
.lock()
5812+
.expect("Mutex poisoned")
5813+
.get_stacks_blocks_processed();
5814+
57625815
next_block_and_process_new_stacks_block(&mut btc_regtest_controller, 60, &coord_channel)
57635816
.unwrap();
57645817

@@ -5768,7 +5821,20 @@ fn continue_tenure_extend() {
57685821
&[sender_signer_sk],
57695822
&signers,
57705823
);
5771-
sleep_ms(5_000);
5824+
5825+
wait_for(5, || {
5826+
let blocks_processed = coord_channel
5827+
.lock()
5828+
.expect("Mutex poisoned")
5829+
.get_stacks_blocks_processed();
5830+
Ok(blocks_processed > blocks_processed_before)
5831+
})
5832+
.unwrap();
5833+
5834+
let blocks_processed_before = coord_channel
5835+
.lock()
5836+
.expect("Mutex poisoned")
5837+
.get_stacks_blocks_processed();
57725838

57735839
next_block_and(&mut btc_regtest_controller, 60, || Ok(true)).unwrap();
57745840

@@ -5778,7 +5844,15 @@ fn continue_tenure_extend() {
57785844
&[sender_signer_sk],
57795845
&signers,
57805846
);
5781-
sleep_ms(5_000);
5847+
5848+
wait_for(5, || {
5849+
let blocks_processed = coord_channel
5850+
.lock()
5851+
.expect("Mutex poisoned")
5852+
.get_stacks_blocks_processed();
5853+
Ok(blocks_processed > blocks_processed_before)
5854+
})
5855+
.unwrap();
57825856

57835857
info!("Resuming commit ops to mine regular tenures.");
57845858
test_skip_commit_op.0.lock().unwrap().replace(false);
@@ -5792,11 +5866,7 @@ fn continue_tenure_extend() {
57925866
.get_stacks_blocks_processed();
57935867
next_block_and(&mut btc_regtest_controller, 60, || {
57945868
let commits_count = commits_submitted.load(Ordering::SeqCst);
5795-
let blocks_processed = coord_channel
5796-
.lock()
5797-
.expect("Mutex poisoned")
5798-
.get_stacks_blocks_processed();
5799-
Ok(commits_count > commits_before && blocks_processed > blocks_processed_before)
5869+
Ok(commits_count > commits_before)
58005870
})
58015871
.unwrap();
58025872

@@ -5807,6 +5877,15 @@ fn continue_tenure_extend() {
58075877
&signers,
58085878
);
58095879

5880+
wait_for(5, || {
5881+
let blocks_processed = coord_channel
5882+
.lock()
5883+
.expect("Mutex poisoned")
5884+
.get_stacks_blocks_processed();
5885+
Ok(blocks_processed > blocks_processed_before)
5886+
})
5887+
.unwrap();
5888+
58105889
sleep_ms(5_000);
58115890
}
58125891

testnet/stacks-node/src/tests/signer/v0.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,19 @@ fn miner_gather_signatures() {
540540
info!("------------------------- Test Setup -------------------------");
541541
let num_signers = 5;
542542
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new(num_signers, vec![], None);
543-
signer_test.boot_to_epoch_3();
544543
let timeout = Duration::from_secs(30);
544+
let mined_blocks = signer_test.running_nodes.nakamoto_blocks_mined.clone();
545+
let blocks_mined_before = mined_blocks.load(Ordering::SeqCst);
545546

546-
// give the system a chance to mine a Nakamoto block
547-
sleep_ms(30_000);
547+
signer_test.boot_to_epoch_3();
548+
549+
// give the system a chance to reach the Nakamoto start tip
550+
// mine a Nakamoto block
551+
wait_for(30, || {
552+
let blocks_mined = mined_blocks.load(Ordering::SeqCst);
553+
Ok(blocks_mined > blocks_mined_before)
554+
})
555+
.unwrap();
548556

549557
info!("------------------------- Test Mine and Verify Confirmed Nakamoto Block -------------------------");
550558
signer_test.mine_and_verify_confirmed_naka_block(timeout, num_signers);
@@ -943,6 +951,7 @@ fn forked_tenure_testing(
943951
// In the next block, the miner should win the tenure and submit a stacks block
944952
let commits_before = commits_submitted.load(Ordering::SeqCst);
945953
let blocks_before = mined_blocks.load(Ordering::SeqCst);
954+
946955
next_block_and(
947956
&mut signer_test.running_nodes.btc_regtest_controller,
948957
60,

0 commit comments

Comments
 (0)