Skip to content

Commit 44862d5

Browse files
committed
Do not use the burn block timestamp when comparing the min gap between blocks
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 4bf673f commit 44862d5

File tree

1 file changed

+73
-61
lines changed
  • testnet/stacks-node/src/tests/signer

1 file changed

+73
-61
lines changed

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

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,7 +3242,8 @@ fn signer_set_rollover() {
32423242

32433243
#[test]
32443244
#[ignore]
3245-
/// This test checks that the signers will broadcast a block once they receive enough signatures.
3245+
/// This test checks that the miners and signers will not produce Nakamoto blocks
3246+
/// until the minimum time has passed between blocks.
32463247
fn min_gap_between_blocks() {
32473248
if env::var("BITCOIND_TEST") != Ok("1".into()) {
32483249
return;
@@ -3259,11 +3260,14 @@ fn min_gap_between_blocks() {
32593260
let sender_addr = tests::to_addr(&sender_sk);
32603261
let send_amt = 100;
32613262
let send_fee = 180;
3263+
3264+
let mut sender_nonce = 0;
3265+
let interim_blocks = 5;
32623266
let recipient = PrincipalData::from(StacksAddress::burn_address(false));
32633267
let time_between_blocks_ms = 10_000;
32643268
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new_with_config_modifications(
32653269
num_signers,
3266-
vec![(sender_addr.clone(), send_amt + send_fee)],
3270+
vec![(sender_addr.clone(), (send_amt + send_fee) * interim_blocks)],
32673271
|_config| {},
32683272
|config| {
32693273
config.miner.min_time_between_blocks_ms = time_between_blocks_ms;
@@ -3276,73 +3280,81 @@ fn min_gap_between_blocks() {
32763280

32773281
signer_test.boot_to_epoch_3();
32783282

3279-
info!("Ensure that the first Nakamoto block is mined after the gap is exceeded");
3283+
info!("Ensure that the first Nakamoto block was mined");
32803284
let blocks = get_nakamoto_headers(&signer_test.running_nodes.conf);
32813285
assert_eq!(blocks.len(), 1);
3282-
let first_block = blocks.last().unwrap();
3283-
let blocks = test_observer::get_blocks();
3284-
let parent = blocks
3285-
.iter()
3286-
.find(|b| b.get("block_height").unwrap() == first_block.stacks_block_height - 1)
3287-
.unwrap();
3288-
let first_block_time = first_block
3289-
.anchored_header
3290-
.as_stacks_nakamoto()
3291-
.unwrap()
3292-
.timestamp;
3293-
let parent_block_time = parent.get("burn_block_time").unwrap().as_u64().unwrap();
3294-
assert!(
3295-
Duration::from_secs(first_block_time - parent_block_time)
3296-
>= Duration::from_millis(time_between_blocks_ms),
3297-
"First block proposed before gap was exceeded: {}s - {}s > {}ms",
3298-
first_block_time,
3299-
parent_block_time,
3300-
time_between_blocks_ms
3301-
);
3286+
// mine the interim blocks
3287+
info!("Mining interim blocks");
3288+
for interim_block_ix in 0..interim_blocks {
3289+
let blocks_processed_before = signer_test
3290+
.running_nodes
3291+
.nakamoto_blocks_mined
3292+
.load(Ordering::SeqCst);
3293+
// submit a tx so that the miner will mine an extra block
3294+
let transfer_tx = make_stacks_transfer(
3295+
&sender_sk,
3296+
sender_nonce,
3297+
send_fee,
3298+
signer_test.running_nodes.conf.burnchain.chain_id,
3299+
&recipient,
3300+
send_amt,
3301+
);
3302+
sender_nonce += 1;
3303+
submit_tx(&http_origin, &transfer_tx);
33023304

3303-
// Submit a tx so that the miner will mine a block
3304-
let sender_nonce = 0;
3305-
let transfer_tx = make_stacks_transfer(
3306-
&sender_sk,
3307-
sender_nonce,
3308-
send_fee,
3309-
signer_test.running_nodes.conf.burnchain.chain_id,
3310-
&recipient,
3311-
send_amt,
3312-
);
3313-
submit_tx(&http_origin, &transfer_tx);
3305+
info!("Submitted transfer tx and waiting for block to be processed");
3306+
wait_for(60, || {
3307+
let blocks_processed = signer_test
3308+
.running_nodes
3309+
.nakamoto_blocks_mined
3310+
.load(Ordering::SeqCst);
3311+
Ok(blocks_processed > blocks_processed_before)
3312+
})
3313+
.unwrap();
3314+
info!("Mined interim block:{}", interim_block_ix);
3315+
}
33143316

3315-
info!("Submitted transfer tx and waiting for block to be processed. Ensure it does not arrive before the gap is exceeded");
33163317
wait_for(60, || {
3317-
let blocks = get_nakamoto_headers(&signer_test.running_nodes.conf);
3318-
Ok(blocks.len() >= 2)
3318+
let new_blocks = get_nakamoto_headers(&signer_test.running_nodes.conf);
3319+
Ok(new_blocks.len() == blocks.len() + interim_blocks as usize)
33193320
})
33203321
.unwrap();
33213322

3322-
// Verify that the second Nakamoto block is mined after the gap is exceeded
3323-
let blocks = get_nakamoto_headers(&signer_test.running_nodes.conf);
3324-
let last_block = blocks.last().unwrap();
3325-
let last_block_time = last_block
3326-
.anchored_header
3327-
.as_stacks_nakamoto()
3328-
.unwrap()
3329-
.timestamp;
3330-
assert!(blocks.len() >= 2, "Expected at least 2 mined blocks");
3331-
let penultimate_block = blocks.get(blocks.len() - 2).unwrap();
3332-
let penultimate_block_time = penultimate_block
3333-
.anchored_header
3334-
.as_stacks_nakamoto()
3335-
.unwrap()
3336-
.timestamp;
3337-
assert!(
3338-
Duration::from_secs(last_block_time - penultimate_block_time)
3339-
>= Duration::from_millis(time_between_blocks_ms),
3340-
"Block proposed before gap was exceeded: {}s - {}s > {}ms",
3341-
last_block_time,
3342-
penultimate_block_time,
3343-
time_between_blocks_ms
3344-
);
3345-
3323+
// Verify that every Nakamoto block is mined after the gap is exceeded between each
3324+
let mut blocks = get_nakamoto_headers(&signer_test.running_nodes.conf);
3325+
blocks.sort_by(|a, b| a.stacks_block_height.cmp(&b.stacks_block_height));
3326+
for i in 1..blocks.len() {
3327+
let block = &blocks[i];
3328+
let parent_block = &blocks[i - 1];
3329+
assert_eq!(
3330+
block.stacks_block_height,
3331+
parent_block.stacks_block_height + 1
3332+
);
3333+
info!(
3334+
"Checking that the time between blocks {} and {} is respected",
3335+
parent_block.stacks_block_height, block.stacks_block_height
3336+
);
3337+
let block_time = block
3338+
.anchored_header
3339+
.as_stacks_nakamoto()
3340+
.unwrap()
3341+
.timestamp;
3342+
let parent_block_time = parent_block
3343+
.anchored_header
3344+
.as_stacks_nakamoto()
3345+
.unwrap()
3346+
.timestamp;
3347+
assert!(
3348+
block_time > parent_block_time,
3349+
"Block time is BEFORE parent block time"
3350+
);
3351+
assert!(
3352+
Duration::from_secs(block_time - parent_block_time)
3353+
>= Duration::from_millis(time_between_blocks_ms),
3354+
"Block mined before gap was exceeded: {block_time}s - {parent_block_time}s > {time_between_blocks_ms}ms",
3355+
);
3356+
}
3357+
debug!("Shutting down min_gap_between_blocks test");
33463358
signer_test.shutdown();
33473359
}
33483360

0 commit comments

Comments
 (0)