Skip to content

Commit dfaf385

Browse files
committed
test: fix flakiness in signer_can_accept_rejected_block
1 parent 962aecf commit dfaf385

File tree

1 file changed

+31
-28
lines changed
  • testnet/stacks-node/src/tests/signer

1 file changed

+31
-28
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12355,13 +12355,10 @@ fn signer_can_accept_rejected_block() {
1235512355
None,
1235612356
);
1235712357
let http_origin = format!("http://{}", &signer_test.running_nodes.conf.node.rpc_bind);
12358-
signer_test.boot_to_epoch_3();
12358+
let miner_sk = signer_test.running_nodes.conf.miner.mining_key.unwrap();
12359+
let miner_pk = StacksPublicKey::from_private(&miner_sk);
1235912360

12360-
let proposed_blocks = signer_test
12361-
.running_nodes
12362-
.counters
12363-
.naka_proposed_blocks
12364-
.clone();
12361+
signer_test.boot_to_epoch_3();
1236512362

1236612363
signer_test.mine_nakamoto_block(Duration::from_secs(60), true);
1236712364

@@ -12377,7 +12374,8 @@ fn signer_can_accept_rejected_block() {
1237712374
let ignoring_signer = StacksPublicKey::from_private(&signer_test.signer_stacks_private_keys[1]);
1237812375
TEST_IGNORE_ALL_BLOCK_PROPOSALS.set(vec![ignoring_signer]);
1237912376

12380-
let proposals_before = proposed_blocks.load(Ordering::SeqCst);
12377+
// Stall block validation so we can ensure the timing we want to test
12378+
TEST_VALIDATE_STALL.set(true);
1238112379

1238212380
// submit a tx so that the miner will mine a block
1238312381
let transfer_tx = make_stacks_transfer(
@@ -12391,13 +12389,19 @@ fn signer_can_accept_rejected_block() {
1239112389
submit_tx(&http_origin, &transfer_tx);
1239212390

1239312391
info!("Submitted transfer tx and waiting for block proposal");
12394-
wait_for(60, || {
12395-
if proposed_blocks.load(Ordering::SeqCst) > proposals_before {
12396-
return Ok(true);
12397-
}
12398-
Ok(false)
12399-
})
12400-
.expect("Timed out waiting for block proposal");
12392+
let block = wait_for_block_proposal(30, block_height_before + 1, &miner_pk)
12393+
.expect("Timed out waiting for block proposal");
12394+
12395+
// Wait for signer[0] to reject the block
12396+
wait_for_block_rejections(30, block.header.signer_signature_hash(), 1)
12397+
.expect("Failed to get expected rejections for Miner 1's block");
12398+
12399+
info!("Disable signer 0 from rejecting proposals");
12400+
test_observer::clear();
12401+
TEST_REJECT_ALL_BLOCK_PROPOSAL.set(vec![]);
12402+
12403+
// Unstall the other signers
12404+
TEST_VALIDATE_STALL.set(false);
1240112405

1240212406
info!(
1240312407
"Block proposed, submitting another transaction that should not get included in the block"
@@ -12412,26 +12416,25 @@ fn signer_can_accept_rejected_block() {
1241212416
);
1241312417
submit_tx(&http_origin, &transfer_tx);
1241412418

12415-
info!("Disable signer 0 from rejecting proposals");
12416-
test_observer::clear();
12417-
TEST_REJECT_ALL_BLOCK_PROPOSAL.set(vec![]);
12418-
1241912419
info!("Waiting for the block to be approved");
1242012420
wait_for(60, || {
1242112421
let blocks = test_observer::get_blocks();
12422-
let last_block = blocks.last().expect("No blocks found");
12423-
let height = last_block["block_height"].as_u64().unwrap();
12424-
if height > block_height_before {
12425-
return Ok(true);
12422+
12423+
// Look for a block with height `block_height_before + 1`
12424+
if let Some(block) = blocks
12425+
.iter()
12426+
.find(|block| block["block_height"].as_u64() == Some(block_height_before + 1))
12427+
{
12428+
if transfers_in_block(block) == 1 {
12429+
Ok(true) // Success: found the block with exactly 1 transfer
12430+
} else {
12431+
Err("Transfer included in block".into()) // Found the block, but it has the wrong number of transfers
12432+
}
12433+
} else {
12434+
Ok(false) // Keep waiting if the block hasn't appeared yet
1242612435
}
12427-
Ok(false)
1242812436
})
1242912437
.expect("Timed out waiting for block");
1243012438

12431-
// Ensure that the block was the original block with just 1 transfer
12432-
let blocks = test_observer::get_blocks();
12433-
let block = blocks.last().expect("No blocks found");
12434-
assert_eq!(transfers_in_block(block), 1);
12435-
1243612439
signer_test.shutdown();
1243712440
}

0 commit comments

Comments
 (0)