Skip to content

Commit e304a48

Browse files
committed
Fix flakiness by ensuring block commits confirm the expected stacks block
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent ef21447 commit e304a48

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

stacks-node/src/tests/signer/mod.rs

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use super::nakamoto_integrations::{
7070
use super::neon_integrations::{
7171
copy_dir_all, get_account, get_sortition_info_ch, submit_tx_fallible, Account,
7272
};
73+
use crate::nakamoto_node::miner::TEST_MINE_SKIP;
7374
use crate::neon::Counters;
7475
use crate::run_loop::boot_nakamoto;
7576
use crate::tests::bitcoin_regtest::BitcoinCoreController;
@@ -80,6 +81,7 @@ use crate::tests::neon_integrations::{
8081
get_chain_info, next_block_and_wait, run_until_burnchain_height, test_observer,
8182
wait_for_runloop,
8283
};
84+
use crate::tests::signer::v0::wait_for_state_machine_update_by_miner_tenure_id;
8385
use crate::tests::to_addr;
8486
use crate::BitcoinRegtestController;
8587

@@ -1101,23 +1103,59 @@ impl<Z: SpawnedSignerTrait> SignerTest<Z> {
11011103
/// Mine a BTC block and wait for a new Stacks block to be mined
11021104
/// Note: do not use nakamoto blocks mined heuristic if running a test with multiple miners
11031105
fn mine_nakamoto_block(&self, timeout: Duration, use_nakamoto_blocks_mined: bool) {
1104-
let mined_block_time = Instant::now();
1105-
let mined_before = self.running_nodes.counters.naka_mined_blocks.get();
1106-
let info_before = self.get_peer_info();
1107-
1106+
let info_before = get_chain_info(&self.running_nodes.conf);
1107+
info!("Pausing stacks block mining");
1108+
TEST_MINE_SKIP.set(true);
1109+
1110+
let Counters {
1111+
naka_submitted_commits: commits_submitted,
1112+
naka_submitted_commit_last_burn_height: commits_last_burn_height,
1113+
naka_submitted_commit_last_stacks_tip: commits_last_stacks_tip,
1114+
naka_mined_blocks: mined_blocks,
1115+
..
1116+
} = self.running_nodes.counters.clone();
1117+
1118+
let commits_before = commits_submitted.load(Ordering::SeqCst);
1119+
let commit_burn_height_before = commits_last_burn_height.load(Ordering::SeqCst);
1120+
let commits_stacks_tip_before = commits_last_stacks_tip.load(Ordering::SeqCst);
1121+
let mined_before = mined_blocks.load(Ordering::SeqCst);
1122+
1123+
let mined_btc_block_time = Instant::now();
11081124
next_block_and(
11091125
&self.running_nodes.btc_regtest_controller,
11101126
timeout.as_secs(),
1111-
|| {
1112-
let info_after = self.get_peer_info();
1113-
let blocks_mined = self.running_nodes.counters.naka_mined_blocks.get();
1114-
Ok(info_after.stacks_tip_height > info_before.stacks_tip_height
1115-
&& (!use_nakamoto_blocks_mined || blocks_mined > mined_before))
1116-
},
1127+
|| Ok(self.get_peer_info().burn_block_height > info_before.burn_block_height),
11171128
)
11181129
.unwrap();
1119-
let mined_block_elapsed_time = mined_block_time.elapsed();
1120-
info!("Nakamoto block mine time elapsed: {mined_block_elapsed_time:?}");
1130+
info!(
1131+
"Bitcoin block mine time elapsed: {:?}",
1132+
mined_btc_block_time.elapsed()
1133+
);
1134+
wait_for_state_machine_update_by_miner_tenure_id(
1135+
timeout.as_secs(),
1136+
&get_chain_info(&self.running_nodes.conf).pox_consensus,
1137+
&self.signer_addresses_versions(),
1138+
)
1139+
.expect("Failed to update signer state machine");
1140+
1141+
info!("Unpausing stacks block mining");
1142+
let mined_block_time = Instant::now();
1143+
TEST_MINE_SKIP.set(false);
1144+
// Ensure that the tenure change transaction is mined and that the subsequent block commit confirms it
1145+
wait_for(timeout.as_secs(), || {
1146+
Ok(commits_submitted.load(Ordering::SeqCst) > commits_before
1147+
&& commits_last_burn_height.load(Ordering::SeqCst) >= commit_burn_height_before
1148+
&& commits_last_stacks_tip.load(Ordering::SeqCst) >= commits_stacks_tip_before
1149+
&& get_chain_info(&self.running_nodes.conf).stacks_tip_height
1150+
> info_before.stacks_tip_height
1151+
&& (!use_nakamoto_blocks_mined
1152+
|| mined_blocks.load(Ordering::SeqCst) > mined_before))
1153+
})
1154+
.expect("Failed to mine Tenure Change block");
1155+
info!(
1156+
"Nakamoto block mine time elapsed: {:?}",
1157+
mined_block_time.elapsed()
1158+
);
11211159
}
11221160

11231161
fn mine_block_wait_on_processing(

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,31 +175,7 @@ impl<Z: SpawnedSignerTrait> SignerTest<Z> {
175175
// Note, we don't use `nakamoto_blocks_mined` counter, because there
176176
// could be other miners mining blocks.
177177
info!("Waiting for first Epoch 3.0 tenure to start");
178-
let info = get_chain_info(&self.running_nodes.conf);
179-
next_block_and(&self.running_nodes.btc_regtest_controller, 30, || {
180-
Ok(get_chain_info(&self.running_nodes.conf).burn_block_height > info.burn_block_height)
181-
})
182-
.unwrap();
183-
let info = get_chain_info(&self.running_nodes.conf);
184-
info!(
185-
"Waiting for signers to agree on expected tenure id: {}",
186-
info.pox_consensus
187-
);
188-
let res = wait_for_state_machine_update_by_miner_tenure_id(
189-
30,
190-
&info.pox_consensus,
191-
&self.signer_addresses_versions(),
192-
);
193-
if res.is_err() {
194-
warn!("Signer updates failed to update but attempting to continue test anyway");
195-
}
196-
TEST_MINE_SKIP.set(false);
197-
let height_before = info.stacks_tip_height;
198-
info!("Waiting for first Nakamoto block: {}", height_before + 1);
199-
wait_for(30, || {
200-
Ok(get_chain_info(&self.running_nodes.conf).stacks_tip_height > height_before)
201-
})
202-
.expect("Timed out waiting for first Nakamoto block after 3.0 boundary");
178+
self.mine_nakamoto_block(Duration::from_secs(60), false);
203179
info!("Ready to mine Nakamoto blocks!");
204180
}
205181
}

0 commit comments

Comments
 (0)