@@ -1357,9 +1357,30 @@ pub fn wait_for_block_pushed_by_miner_key(
13571357 }
13581358 Ok ( false )
13591359 } ) ?;
1360+
13601361 block. ok_or_else ( || "Failed to find block pushed" . to_string ( ) )
13611362}
13621363
1364+ /// Waits for a block to be pushed by the specified miner, then waits for
1365+ /// the node's tip to advance to that block. This prevents race conditions
1366+ /// where subsequent calls read a stale `stacks_tip_height` because the
1367+ /// coordinator hasn't yet processed the pushed block.
1368+ ///
1369+ /// `get_tip` should return the current stacks tip hash (e.g. from
1370+ /// `get_peer_info().stacks_tip` or `get_chain_info().stacks_tip`).
1371+ pub fn wait_for_block_pushed_and_tip (
1372+ timeout_secs : u64 ,
1373+ expected_height : u64 ,
1374+ expected_miner : & StacksPublicKey ,
1375+ get_tip : impl Fn ( ) -> BlockHeaderHash ,
1376+ ) -> Result < NakamotoBlock , String > {
1377+ let block = wait_for_block_pushed_by_miner_key ( timeout_secs, expected_height, expected_miner) ?;
1378+ let block_hash = block. header . block_hash ( ) ;
1379+ wait_for ( timeout_secs, || Ok ( get_tip ( ) == block_hash) )
1380+ . map_err ( |e| format ! ( "Tip did not advance to pushed block: {e}" ) ) ?;
1381+ Ok ( block)
1382+ }
1383+
13631384/// Waits for all of the provided signers to send a pre-commit for a block
13641385/// with the provided signer signature hash
13651386pub fn wait_for_block_pre_commits_from_signers (
@@ -4058,14 +4079,10 @@ fn miner_recovers_when_broadcast_block_delay_across_tenures_occurs() {
40584079 info ! ( "Submitted tx {tx} in to mine block N" ) ;
40594080 sender_nonce += 1 ;
40604081 let block_n =
4061- wait_for_block_pushed_by_miner_key ( 30 , info_before. stacks_tip_height + 1 , & miner_pk)
4062- . expect ( "Timed out waiting for block N to be mined" ) ;
4063-
4064- wait_for ( 30 , || {
4065- let info = signer_test. get_peer_info ( ) ;
4066- Ok ( info. stacks_tip == block_n. header . block_hash ( ) )
4067- } )
4068- . expect ( "Tip did not advance to block N" ) ;
4082+ wait_for_block_pushed_and_tip ( 30 , info_before. stacks_tip_height + 1 , & miner_pk, || {
4083+ signer_test. get_peer_info ( ) . stacks_tip
4084+ } )
4085+ . expect ( "Timed out waiting for block N to be mined" ) ;
40694086
40704087 info ! ( "------------------------- Attempt to Mine Nakamoto Block N+1 -------------------------" ) ;
40714088 // Propose a valid block, but force the miner to ignore the returned signatures and delay the block being
@@ -4212,14 +4229,10 @@ fn miner_recovers_when_broadcast_block_delay_across_tenures_occurs() {
42124229 info_before. stacks_tip_height + 2
42134230 ) ;
42144231 let block_n_2 =
4215- wait_for_block_pushed_by_miner_key ( 30 , info_before. stacks_tip_height + 2 , & miner_pk)
4216- . expect ( "Timed out waiting for block N+2 to be mined" ) ;
4217-
4218- wait_for ( 30 , || {
4219- let info = signer_test. get_peer_info ( ) ;
4220- Ok ( info. stacks_tip == block_n_2. header . block_hash ( ) )
4221- } )
4222- . expect ( "Tip did not advance to block N+2" ) ;
4232+ wait_for_block_pushed_and_tip ( 30 , info_before. stacks_tip_height + 2 , & miner_pk, || {
4233+ signer_test. get_peer_info ( ) . stacks_tip
4234+ } )
4235+ . expect ( "Timed out waiting for block N+2 to be mined" ) ;
42234236 assert_eq ! (
42244237 block_n_2. header. parent_block_id,
42254238 block_n_1. header. block_id( )
0 commit comments