Skip to content

Commit 8dd4771

Browse files
committed
CRC: remove potential race condition in signing_in_0th_tenure_of_reward_cycle
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 3260a2c commit 8dd4771

File tree

1 file changed

+43
-91
lines changed
  • testnet/stacks-node/src/tests/signer

1 file changed

+43
-91
lines changed

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

Lines changed: 43 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,35 +1992,29 @@ fn end_of_tenure() {
19921992
std::thread::sleep(Duration::from_millis(100));
19931993
}
19941994

1995-
while signer_test.get_current_reward_cycle() != final_reward_cycle {
1996-
next_block_and(
1997-
&mut signer_test.running_nodes.btc_regtest_controller,
1998-
10,
1999-
|| Ok(true),
2000-
)
2001-
.unwrap();
2002-
assert!(
2003-
start_time.elapsed() <= short_timeout,
2004-
"Timed out waiting to enter the next reward cycle"
2005-
);
2006-
std::thread::sleep(Duration::from_millis(100));
2007-
}
1995+
wait_for(short_timeout.as_secs(), || {
1996+
let result = signer_test.get_current_reward_cycle() == final_reward_cycle;
1997+
if !result {
1998+
signer_test
1999+
.running_nodes
2000+
.btc_regtest_controller
2001+
.build_next_block(1);
2002+
}
2003+
Ok(result)
2004+
})
2005+
.expect("Timed out waiting to enter the next reward cycle");
20082006

2009-
while test_observer::get_burn_blocks()
2010-
.last()
2011-
.unwrap()
2012-
.get("burn_block_height")
2013-
.unwrap()
2014-
.as_u64()
2015-
.unwrap()
2016-
< final_reward_cycle_height_boundary + 1
2017-
{
2018-
assert!(
2019-
start_time.elapsed() <= short_timeout,
2020-
"Timed out waiting for burn block events"
2021-
);
2022-
std::thread::sleep(Duration::from_millis(100));
2023-
}
2007+
wait_for(short_timeout.as_secs(), || {
2008+
let blocks = test_observer::get_burn_blocks()
2009+
.last()
2010+
.unwrap()
2011+
.get("burn_block_height")
2012+
.unwrap()
2013+
.as_u64()
2014+
.unwrap();
2015+
Ok(blocks > final_reward_cycle_height_boundary)
2016+
})
2017+
.expect("Timed out waiting for burn block events");
20242018

20252019
signer_test.wait_for_cycle(30, final_reward_cycle);
20262020

@@ -2078,21 +2072,11 @@ fn retry_on_rejection() {
20782072
let burnchain = signer_test.running_nodes.conf.get_burnchain();
20792073
let sortdb = burnchain.open_sortition_db(true).unwrap();
20802074

2081-
loop {
2082-
next_block_and(
2083-
&mut signer_test.running_nodes.btc_regtest_controller,
2084-
60,
2085-
|| Ok(true),
2086-
)
2087-
.unwrap();
2088-
2089-
sleep_ms(10_000);
2090-
2075+
wait_for(30, || {
20912076
let tip = SortitionDB::get_canonical_burn_chain_tip(&sortdb.conn()).unwrap();
2092-
if tip.sortition {
2093-
break;
2094-
}
2095-
}
2077+
Ok(tip.sortition)
2078+
})
2079+
.expect("Timed out waiting for sortition");
20962080

20972081
// mine a nakamoto block
20982082
let mined_blocks = signer_test.running_nodes.nakamoto_blocks_mined.clone();
@@ -2534,12 +2518,10 @@ fn mock_sign_epoch_25() {
25342518
{
25352519
let mut mock_block_mesage = None;
25362520
let mock_poll_time = Instant::now();
2537-
next_block_and(
2538-
&mut signer_test.running_nodes.btc_regtest_controller,
2539-
60,
2540-
|| Ok(true),
2541-
)
2542-
.unwrap();
2521+
signer_test
2522+
.running_nodes
2523+
.btc_regtest_controller
2524+
.build_next_block(1);
25432525
let current_burn_block_height = signer_test
25442526
.running_nodes
25452527
.btc_regtest_controller
@@ -2747,12 +2729,10 @@ fn multiple_miners_mock_sign_epoch_25() {
27472729
{
27482730
let mut mock_block_mesage = None;
27492731
let mock_poll_time = Instant::now();
2750-
next_block_and(
2751-
&mut signer_test.running_nodes.btc_regtest_controller,
2752-
60,
2753-
|| Ok(true),
2754-
)
2755-
.unwrap();
2732+
signer_test
2733+
.running_nodes
2734+
.btc_regtest_controller
2735+
.build_next_block(1);
27562736
let current_burn_block_height = signer_test
27572737
.running_nodes
27582738
.btc_regtest_controller
@@ -4539,21 +4519,11 @@ fn miner_recovers_when_broadcast_block_delay_across_tenures_occurs() {
45394519
let burnchain = signer_test.running_nodes.conf.get_burnchain();
45404520
let sortdb = burnchain.open_sortition_db(true).unwrap();
45414521

4542-
loop {
4543-
next_block_and(
4544-
&mut signer_test.running_nodes.btc_regtest_controller,
4545-
60,
4546-
|| Ok(true),
4547-
)
4548-
.unwrap();
4549-
4550-
sleep_ms(10_000);
4551-
4522+
wait_for(30, || {
45524523
let tip = SortitionDB::get_canonical_burn_chain_tip(&sortdb.conn()).unwrap();
4553-
if tip.sortition {
4554-
break;
4555-
}
4556-
}
4524+
Ok(tip.sortition)
4525+
})
4526+
.expect("Timed out waiting for sortition");
45574527

45584528
// submit a tx so that the miner will mine a stacks block
45594529
let mut sender_nonce = 0;
@@ -4833,15 +4803,7 @@ fn signing_in_0th_tenure_of_reward_cycle() {
48334803

48344804
info!("------------------------- Test Setup -------------------------");
48354805
let num_signers = 5;
4836-
let sender_sk = Secp256k1PrivateKey::new();
4837-
let sender_addr = tests::to_addr(&sender_sk);
4838-
let send_amt = 100;
4839-
let send_fee = 180;
4840-
let recipient = PrincipalData::from(StacksAddress::burn_address(false));
4841-
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new(
4842-
num_signers,
4843-
vec![(sender_addr.clone(), send_amt + send_fee)],
4844-
);
4806+
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new(num_signers, vec![]);
48454807
let signer_public_keys = signer_test
48464808
.signer_stacks_private_keys
48474809
.iter()
@@ -4888,28 +4850,18 @@ fn signing_in_0th_tenure_of_reward_cycle() {
48884850
}
48894851

48904852
info!("------------------------- Enter Reward Cycle {next_reward_cycle} -------------------------");
4891-
next_block_and(
4892-
&mut signer_test.running_nodes.btc_regtest_controller,
4893-
60,
4894-
|| Ok(true),
4895-
)
4896-
.unwrap();
4897-
48984853
for signer in &signer_public_keys {
48994854
let blocks_signed = get_v3_signer(&signer, next_reward_cycle);
49004855
assert_eq!(blocks_signed, 0);
49014856
}
4902-
49034857
let blocks_before = signer_test
49044858
.running_nodes
49054859
.nakamoto_blocks_mined
49064860
.load(Ordering::SeqCst);
4907-
4908-
// submit a tx so that the miner will mine a stacks block in the 0th block of the new reward cycle
4909-
let sender_nonce = 0;
4910-
let transfer_tx =
4911-
make_stacks_transfer(&sender_sk, sender_nonce, send_fee, &recipient, send_amt);
4912-
let _tx = submit_tx(&http_origin, &transfer_tx);
4861+
signer_test
4862+
.running_nodes
4863+
.btc_regtest_controller
4864+
.build_next_block(1);
49134865

49144866
wait_for(30, || {
49154867
Ok(signer_test

0 commit comments

Comments
 (0)