Skip to content

Commit f7a02b8

Browse files
committed
test: reduce flakiness in microblocks_disabled
1 parent 7dc541f commit f7a02b8

File tree

1 file changed

+44
-63
lines changed

1 file changed

+44
-63
lines changed

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

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use stacks_common::types::chainstate::StacksPrivateKey;
2323

2424
use crate::config::InitialBalance;
2525
use crate::tests::bitcoin_regtest::BitcoinCoreController;
26-
use crate::tests::nakamoto_integrations::wait_for;
26+
use crate::tests::nakamoto_integrations::{next_block_and, wait_for};
2727
use crate::tests::neon_integrations::{
2828
get_account, get_chain_info, neon_integration_test_conf, next_block_and_wait, submit_tx,
2929
test_observer, wait_for_runloop,
@@ -162,6 +162,9 @@ fn microblocks_disabled() {
162162
// push us to block 205
163163
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
164164

165+
// Ensure we start off with 0 microblocks
166+
assert!(test_observer::get_microblocks().is_empty());
167+
165168
let tx = make_stacks_transfer_mblock_only(
166169
&spender_1_sk,
167170
0,
@@ -172,7 +175,11 @@ fn microblocks_disabled() {
172175
);
173176
submit_tx(&http_origin, &tx);
174177

175-
// wait until just before epoch 2.5
178+
// Wait for a microblock to be assembled
179+
wait_for(60, || Ok(test_observer::get_microblocks().len() == 1))
180+
.expect("Failed to wait for microblocks to be assembled");
181+
182+
// mine Bitcoin blocks up until just before epoch 2.5
176183
wait_for(120, || {
177184
let tip_info = get_chain_info(&conf);
178185
if tip_info.burn_block_height >= epoch_2_5 - 2 {
@@ -183,6 +190,14 @@ fn microblocks_disabled() {
183190
})
184191
.expect("Failed to wait until just before epoch 2.5");
185192

193+
// Verify that the microblock was processed
194+
let account = get_account(&http_origin, &spender_1_addr);
195+
assert_eq!(
196+
u64::try_from(account.balance).unwrap(),
197+
spender_1_bal - 1_000
198+
);
199+
assert_eq!(account.nonce, 1);
200+
186201
let old_tip_info = get_chain_info(&conf);
187202
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
188203
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
@@ -194,13 +209,8 @@ fn microblocks_disabled() {
194209
.expect("Failed to process block");
195210

196211
info!("Test passed processing 2.5");
197-
let account = get_account(&http_origin, &spender_1_addr);
198-
assert_eq!(
199-
u64::try_from(account.balance).unwrap(),
200-
spender_1_bal - 1_000
201-
);
202-
assert_eq!(account.nonce, 1);
203212

213+
// Submit another microblock only transaction
204214
let tx = make_stacks_transfer_mblock_only(
205215
&spender_1_sk,
206216
1,
@@ -211,19 +221,12 @@ fn microblocks_disabled() {
211221
);
212222
submit_tx(&http_origin, &tx);
213223

214-
let mut last_block_height = get_chain_info(&conf).burn_block_height;
215-
for _i in 0..5 {
216-
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
217-
wait_for(30, || {
218-
let tip_info = get_chain_info(&conf);
219-
if tip_info.burn_block_height > last_block_height {
220-
last_block_height = tip_info.burn_block_height;
221-
return Ok(true);
222-
}
223-
Ok(false)
224-
})
225-
.expect("Failed to mine");
226-
}
224+
// Wait for a microblock to be assembled, but expect none to be assembled
225+
wait_for(30, || Ok(test_observer::get_microblocks().len() > 1))
226+
.expect_err("Microblocks should not have been assembled");
227+
228+
// Mine a block to see if the microblock gets processed
229+
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
227230

228231
// second transaction should not have been processed!
229232
let account = get_account(&http_origin, &spender_1_addr);
@@ -233,31 +236,18 @@ fn microblocks_disabled() {
233236
);
234237
assert_eq!(account.nonce, 1);
235238

236-
let microblocks_assembled = test_observer::get_microblocks().len();
237-
info!("Microblocks assembled: {microblocks_assembled}",);
238-
assert!(
239-
microblocks_assembled > 0,
240-
"There should be at least 1 microblock assembled"
241-
);
242-
243239
let miner_nonce_before_microblock_assembly = get_account(&http_origin, &miner_account).nonce;
244240

245241
// Now, lets tell the miner to try to mine microblocks, but don't try to confirm them!
242+
info!("Setting STACKS_TEST_FORCE_MICROBLOCKS_POST_25");
246243
env::set_var("STACKS_TEST_FORCE_MICROBLOCKS_POST_25", "1");
247244

248-
let mut last_block_height = get_chain_info(&conf).burn_block_height;
249-
for _i in 0..2 {
250-
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
251-
wait_for(30, || {
252-
let tip_info = get_chain_info(&conf);
253-
if tip_info.burn_block_height > last_block_height {
254-
last_block_height = tip_info.burn_block_height;
255-
return Ok(true);
256-
}
257-
Ok(false)
258-
})
259-
.expect("Failed to mine");
260-
}
245+
// Wait for a second microblock to be assembled
246+
wait_for(60, || Ok(test_observer::get_microblocks().len() == 2))
247+
.expect("Failed to wait for microblocks to be assembled");
248+
249+
// Mine a block to see if the microblock gets processed
250+
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
261251

262252
let miner_nonce_after_microblock_assembly = get_account(&http_origin, &miner_account).nonce;
263253

@@ -270,44 +260,35 @@ fn microblocks_disabled() {
270260
);
271261
assert_eq!(account.nonce, 1);
272262

273-
// but we should have assembled and announced at least 1 more block to the observer
274-
assert!(test_observer::get_microblocks().len() > microblocks_assembled);
275263
info!(
276264
"Microblocks assembled: {}",
277265
test_observer::get_microblocks().len()
278266
);
279267

280268
// and our miner should have gotten some blocks accepted
281-
assert!(
282-
miner_nonce_after_microblock_assembly > miner_nonce_before_microblock_assembly,
269+
assert_eq!(
270+
miner_nonce_after_microblock_assembly, miner_nonce_before_microblock_assembly + 1,
283271
"Mined before started microblock assembly: {miner_nonce_before_microblock_assembly}, Mined after started microblock assembly: {miner_nonce_after_microblock_assembly}"
284272
);
285273

286274
// Now, tell the miner to try to confirm microblocks as well.
287275
// This should test that the block gets rejected by append block
276+
info!("Setting STACKS_TEST_CONFIRM_MICROBLOCKS_POST_25");
288277
env::set_var("STACKS_TEST_CONFIRM_MICROBLOCKS_POST_25", "1");
289278

290-
let mut last_block_height = get_chain_info(&conf).burn_block_height;
291-
for _i in 0..2 {
292-
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
293-
wait_for(30, || {
294-
let tip_info = get_chain_info(&conf);
295-
if tip_info.burn_block_height > last_block_height {
296-
last_block_height = tip_info.burn_block_height;
297-
return Ok(true);
298-
}
299-
Ok(false)
300-
})
301-
.expect("Failed to mine");
302-
}
279+
// Wait for a third microblock to be assembled
280+
wait_for(60, || Ok(test_observer::get_microblocks().len() == 3))
281+
.expect("Failed to wait for microblocks to be assembled");
282+
283+
// Mine a block to see if the microblock gets processed
284+
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
303285

304286
let miner_nonce_after_microblock_confirmation = get_account(&http_origin, &miner_account).nonce;
305287

306-
// and our miner should have gotten at most one more block accepted
307-
// (because they may have had 1 block confirmation in the bitcoin mempool which didn't confirm a microblock
308-
// before we flipped the flag)
309-
assert!(
310-
miner_nonce_after_microblock_confirmation <= miner_nonce_after_microblock_assembly + 1,
288+
// our miner should not have gotten any more blocks accepted
289+
assert_eq!(
290+
miner_nonce_after_microblock_confirmation,
291+
miner_nonce_after_microblock_assembly + 1,
311292
"Mined after started microblock confimration: {miner_nonce_after_microblock_confirmation}",
312293
);
313294

0 commit comments

Comments
 (0)