@@ -3242,7 +3242,8 @@ fn signer_set_rollover() {
3242
3242
3243
3243
#[ test]
3244
3244
#[ 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.
3246
3247
fn min_gap_between_blocks ( ) {
3247
3248
if env:: var ( "BITCOIND_TEST" ) != Ok ( "1" . into ( ) ) {
3248
3249
return ;
@@ -3259,11 +3260,14 @@ fn min_gap_between_blocks() {
3259
3260
let sender_addr = tests:: to_addr ( & sender_sk) ;
3260
3261
let send_amt = 100 ;
3261
3262
let send_fee = 180 ;
3263
+
3264
+ let mut sender_nonce = 0 ;
3265
+ let interim_blocks = 5 ;
3262
3266
let recipient = PrincipalData :: from ( StacksAddress :: burn_address ( false ) ) ;
3263
3267
let time_between_blocks_ms = 10_000 ;
3264
3268
let mut signer_test: SignerTest < SpawnedSigner > = SignerTest :: new_with_config_modifications (
3265
3269
num_signers,
3266
- vec ! [ ( sender_addr. clone( ) , send_amt + send_fee) ] ,
3270
+ vec ! [ ( sender_addr. clone( ) , ( send_amt + send_fee) * interim_blocks ) ] ,
3267
3271
|_config| { } ,
3268
3272
|config| {
3269
3273
config. miner . min_time_between_blocks_ms = time_between_blocks_ms;
@@ -3276,73 +3280,81 @@ fn min_gap_between_blocks() {
3276
3280
3277
3281
signer_test. boot_to_epoch_3 ( ) ;
3278
3282
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" ) ;
3280
3284
let blocks = get_nakamoto_headers ( & signer_test. running_nodes . conf ) ;
3281
3285
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) ;
3302
3304
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
+ }
3314
3316
3315
- info ! ( "Submitted transfer tx and waiting for block to be processed. Ensure it does not arrive before the gap is exceeded" ) ;
3316
3317
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 )
3319
3320
} )
3320
3321
. unwrap ( ) ;
3321
3322
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" ) ;
3346
3358
signer_test. shutdown ( ) ;
3347
3359
}
3348
3360
0 commit comments