@@ -3243,7 +3243,8 @@ fn signer_set_rollover() {
3243
3243
3244
3244
#[ test]
3245
3245
#[ ignore]
3246
- /// This test checks that the signers will broadcast a block once they receive enough signatures.
3246
+ /// This test checks that the miners and signers will not produce Nakamoto blocks
3247
+ /// until the minimum time has passed between blocks.
3247
3248
fn min_gap_between_blocks ( ) {
3248
3249
if env:: var ( "BITCOIND_TEST" ) != Ok ( "1" . into ( ) ) {
3249
3250
return ;
@@ -3260,11 +3261,14 @@ fn min_gap_between_blocks() {
3260
3261
let sender_addr = tests:: to_addr ( & sender_sk) ;
3261
3262
let send_amt = 100 ;
3262
3263
let send_fee = 180 ;
3264
+
3265
+ let mut sender_nonce = 0 ;
3266
+ let interim_blocks = 5 ;
3263
3267
let recipient = PrincipalData :: from ( StacksAddress :: burn_address ( false ) ) ;
3264
3268
let time_between_blocks_ms = 10_000 ;
3265
3269
let mut signer_test: SignerTest < SpawnedSigner > = SignerTest :: new_with_config_modifications (
3266
3270
num_signers,
3267
- vec ! [ ( sender_addr. clone( ) , send_amt + send_fee) ] ,
3271
+ vec ! [ ( sender_addr. clone( ) , ( send_amt + send_fee) * interim_blocks ) ] ,
3268
3272
|_config| { } ,
3269
3273
|config| {
3270
3274
config. miner . min_time_between_blocks_ms = time_between_blocks_ms;
@@ -3277,73 +3281,81 @@ fn min_gap_between_blocks() {
3277
3281
3278
3282
signer_test. boot_to_epoch_3 ( ) ;
3279
3283
3280
- info ! ( "Ensure that the first Nakamoto block is mined after the gap is exceeded " ) ;
3284
+ info ! ( "Ensure that the first Nakamoto block was mined" ) ;
3281
3285
let blocks = get_nakamoto_headers ( & signer_test. running_nodes . conf ) ;
3282
3286
assert_eq ! ( blocks. len( ) , 1 ) ;
3283
- let first_block = blocks. last ( ) . unwrap ( ) ;
3284
- let blocks = test_observer:: get_blocks ( ) ;
3285
- let parent = blocks
3286
- . iter ( )
3287
- . find ( |b| b. get ( "block_height" ) . unwrap ( ) == first_block. stacks_block_height - 1 )
3288
- . unwrap ( ) ;
3289
- let first_block_time = first_block
3290
- . anchored_header
3291
- . as_stacks_nakamoto ( )
3292
- . unwrap ( )
3293
- . timestamp ;
3294
- let parent_block_time = parent. get ( "burn_block_time" ) . unwrap ( ) . as_u64 ( ) . unwrap ( ) ;
3295
- assert ! (
3296
- Duration :: from_secs( first_block_time - parent_block_time)
3297
- >= Duration :: from_millis( time_between_blocks_ms) ,
3298
- "First block proposed before gap was exceeded: {}s - {}s > {}ms" ,
3299
- first_block_time,
3300
- parent_block_time,
3301
- time_between_blocks_ms
3302
- ) ;
3287
+ // mine the interim blocks
3288
+ info ! ( "Mining interim blocks" ) ;
3289
+ for interim_block_ix in 0 ..interim_blocks {
3290
+ let blocks_processed_before = signer_test
3291
+ . running_nodes
3292
+ . nakamoto_blocks_mined
3293
+ . load ( Ordering :: SeqCst ) ;
3294
+ // submit a tx so that the miner will mine an extra block
3295
+ let transfer_tx = make_stacks_transfer (
3296
+ & sender_sk,
3297
+ sender_nonce,
3298
+ send_fee,
3299
+ signer_test. running_nodes . conf . burnchain . chain_id ,
3300
+ & recipient,
3301
+ send_amt,
3302
+ ) ;
3303
+ sender_nonce += 1 ;
3304
+ submit_tx ( & http_origin, & transfer_tx) ;
3303
3305
3304
- // Submit a tx so that the miner will mine a block
3305
- let sender_nonce = 0 ;
3306
- let transfer_tx = make_stacks_transfer (
3307
- & sender_sk ,
3308
- sender_nonce ,
3309
- send_fee ,
3310
- signer_test . running_nodes . conf . burnchain . chain_id ,
3311
- & recipient ,
3312
- send_amt ,
3313
- ) ;
3314
- submit_tx ( & http_origin , & transfer_tx ) ;
3306
+ info ! ( "Submitted transfer tx and waiting for block to be processed" ) ;
3307
+ wait_for ( 60 , || {
3308
+ let blocks_processed = signer_test
3309
+ . running_nodes
3310
+ . nakamoto_blocks_mined
3311
+ . load ( Ordering :: SeqCst ) ;
3312
+ Ok ( blocks_processed > blocks_processed_before )
3313
+ } )
3314
+ . unwrap ( ) ;
3315
+ info ! ( "Mined interim block:{}" , interim_block_ix ) ;
3316
+ }
3315
3317
3316
- info ! ( "Submitted transfer tx and waiting for block to be processed. Ensure it does not arrive before the gap is exceeded" ) ;
3317
3318
wait_for ( 60 , || {
3318
- let blocks = get_nakamoto_headers ( & signer_test. running_nodes . conf ) ;
3319
- Ok ( blocks . len ( ) >= 2 )
3319
+ let new_blocks = get_nakamoto_headers ( & signer_test. running_nodes . conf ) ;
3320
+ Ok ( new_blocks . len ( ) == blocks . len ( ) + interim_blocks as usize )
3320
3321
} )
3321
3322
. unwrap ( ) ;
3322
3323
3323
- // Verify that the second Nakamoto block is mined after the gap is exceeded
3324
- let blocks = get_nakamoto_headers ( & signer_test. running_nodes . conf ) ;
3325
- let last_block = blocks. last ( ) . unwrap ( ) ;
3326
- let last_block_time = last_block
3327
- . anchored_header
3328
- . as_stacks_nakamoto ( )
3329
- . unwrap ( )
3330
- . timestamp ;
3331
- assert ! ( blocks. len( ) >= 2 , "Expected at least 2 mined blocks" ) ;
3332
- let penultimate_block = blocks. get ( blocks. len ( ) - 2 ) . unwrap ( ) ;
3333
- let penultimate_block_time = penultimate_block
3334
- . anchored_header
3335
- . as_stacks_nakamoto ( )
3336
- . unwrap ( )
3337
- . timestamp ;
3338
- assert ! (
3339
- Duration :: from_secs( last_block_time - penultimate_block_time)
3340
- >= Duration :: from_millis( time_between_blocks_ms) ,
3341
- "Block proposed before gap was exceeded: {}s - {}s > {}ms" ,
3342
- last_block_time,
3343
- penultimate_block_time,
3344
- time_between_blocks_ms
3345
- ) ;
3346
-
3324
+ // Verify that every Nakamoto block is mined after the gap is exceeded between each
3325
+ let mut blocks = get_nakamoto_headers ( & signer_test. running_nodes . conf ) ;
3326
+ blocks. sort_by ( |a, b| a. stacks_block_height . cmp ( & b. stacks_block_height ) ) ;
3327
+ for i in 1 ..blocks. len ( ) {
3328
+ let block = & blocks[ i] ;
3329
+ let parent_block = & blocks[ i - 1 ] ;
3330
+ assert_eq ! (
3331
+ block. stacks_block_height,
3332
+ parent_block. stacks_block_height + 1
3333
+ ) ;
3334
+ info ! (
3335
+ "Checking that the time between blocks {} and {} is respected" ,
3336
+ parent_block. stacks_block_height, block. stacks_block_height
3337
+ ) ;
3338
+ let block_time = block
3339
+ . anchored_header
3340
+ . as_stacks_nakamoto ( )
3341
+ . unwrap ( )
3342
+ . timestamp ;
3343
+ let parent_block_time = parent_block
3344
+ . anchored_header
3345
+ . as_stacks_nakamoto ( )
3346
+ . unwrap ( )
3347
+ . timestamp ;
3348
+ assert ! (
3349
+ block_time > parent_block_time,
3350
+ "Block time is BEFORE parent block time"
3351
+ ) ;
3352
+ assert ! (
3353
+ Duration :: from_secs( block_time - parent_block_time)
3354
+ >= Duration :: from_millis( time_between_blocks_ms) ,
3355
+ "Block mined before gap was exceeded: {block_time}s - {parent_block_time}s > {time_between_blocks_ms}ms" ,
3356
+ ) ;
3357
+ }
3358
+ debug ! ( "Shutting down min_gap_between_blocks test" ) ;
3347
3359
signer_test. shutdown ( ) ;
3348
3360
}
3349
3361
0 commit comments