@@ -109,7 +109,7 @@ use stacks_signer::v0::SpawnedSigner;
109
109
use super :: bitcoin_regtest:: BitcoinCoreController ;
110
110
use crate :: nakamoto_node:: miner:: {
111
111
TEST_BLOCK_ANNOUNCE_STALL , TEST_BROADCAST_PROPOSAL_STALL , TEST_MINE_STALL ,
112
- TEST_P2P_BROADCAST_SKIP ,
112
+ TEST_P2P_BROADCAST_SKIP , TEST_P2P_BROADCAST_STALL ,
113
113
} ;
114
114
use crate :: nakamoto_node:: relayer:: TEST_MINER_THREAD_STALL ;
115
115
use crate :: neon:: Counters ;
@@ -8901,24 +8901,6 @@ fn mock_mining() {
8901
8901
& mut btc_regtest_controller,
8902
8902
) ;
8903
8903
8904
- info ! ( "Bootstrapped to Epoch-3.0 boundary, starting nakamoto miner" ) ;
8905
-
8906
- let burnchain = naka_conf. get_burnchain ( ) ;
8907
- let sortdb = burnchain. open_sortition_db ( true ) . unwrap ( ) ;
8908
- let ( chainstate, _) = StacksChainState :: open (
8909
- naka_conf. is_mainnet ( ) ,
8910
- naka_conf. burnchain . chain_id ,
8911
- & naka_conf. get_chainstate_path_str ( ) ,
8912
- None ,
8913
- )
8914
- . unwrap ( ) ;
8915
-
8916
- let block_height_pre_3_0 =
8917
- NakamotoChainState :: get_canonical_block_header ( chainstate. db ( ) , & sortdb)
8918
- . unwrap ( )
8919
- . unwrap ( )
8920
- . stacks_block_height ;
8921
-
8922
8904
info ! ( "Nakamoto miner started..." ) ;
8923
8905
blind_signer ( & naka_conf, & signers, & counters) ;
8924
8906
@@ -8953,12 +8935,10 @@ fn mock_mining() {
8953
8935
let follower_coord_channel = follower_run_loop. coordinator_channels ( ) ;
8954
8936
8955
8937
let Counters {
8956
- naka_mined_blocks : follower_naka_mined_blocks ,
8938
+ naka_mined_blocks : follower_mined_blocks ,
8957
8939
..
8958
8940
} = follower_run_loop. counters ( ) ;
8959
8941
8960
- let mock_mining_blocks_start = follower_naka_mined_blocks. load ( Ordering :: SeqCst ) ;
8961
-
8962
8942
debug ! (
8963
8943
"Booting follower-thread ({},{})" ,
8964
8944
& follower_conf. node. p2p_bind, & follower_conf. node. rpc_bind
@@ -8993,21 +8973,43 @@ fn mock_mining() {
8993
8973
8994
8974
// Mine `tenure_count` nakamoto tenures
8995
8975
for tenure_ix in 0 ..tenure_count {
8996
- let follower_naka_mined_blocks_before = follower_naka_mined_blocks. load ( Ordering :: SeqCst ) ;
8997
-
8998
8976
let commits_before = commits_submitted. load ( Ordering :: SeqCst ) ;
8999
- next_block_and_process_new_stacks_block ( & mut btc_regtest_controller, 60 , & coord_channel)
9000
- . unwrap ( ) ;
8977
+ info ! ( "Mining tenure {tenure_ix}" ) ;
9001
8978
9002
- let mut last_tip = BlockHeaderHash ( [ 0x00 ; 32 ] ) ;
9003
- let mut last_tip_height = 0 ;
8979
+ let height_before = get_chain_info ( & naka_conf) . stacks_tip_height ;
8980
+ let follower_mined_before = follower_mined_blocks. load ( Ordering :: SeqCst ) ;
8981
+ let follower_height_before = get_chain_info ( & follower_conf) . stacks_tip_height ;
8982
+
8983
+ // Stall p2p broadcast so that the mock miner mines a block before
8984
+ // seeing the block from the real miner.
8985
+ TEST_P2P_BROADCAST_STALL . set ( true ) ;
8986
+ info ! ( "Waiting for the tenure {tenure_ix} start block to be mock-mined" ) ;
8987
+ next_block_and ( & mut btc_regtest_controller, 60 , || {
8988
+ Ok ( follower_mined_blocks. load ( Ordering :: SeqCst ) > follower_mined_before)
8989
+ } )
8990
+ . expect ( "Failed to start a new tenure" ) ;
8991
+
8992
+ // Unstall p2p broadcast so that miner broadcasts the block and both
8993
+ // nodes can process it.
8994
+ TEST_P2P_BROADCAST_STALL . set ( false ) ;
8995
+ wait_for ( 30 , || {
8996
+ Ok ( get_chain_info ( & naka_conf) . stacks_tip_height > height_before
8997
+ && get_chain_info ( & follower_conf) . stacks_tip_height > follower_height_before)
8998
+ } )
8999
+ . expect ( "Failed to advanced to the tenure start block" ) ;
9004
9000
9005
9001
// mine the interim blocks
9006
9002
for interim_block_ix in 0 ..inter_blocks_per_tenure {
9007
- let blocks_processed_before = coord_channel
9008
- . lock ( )
9009
- . expect ( "Mutex poisoned" )
9010
- . get_stacks_blocks_processed ( ) ;
9003
+ let height_before = get_chain_info ( & naka_conf) . stacks_tip_height ;
9004
+ let follower_mined_before = follower_mined_blocks. load ( Ordering :: SeqCst ) ;
9005
+ let follower_height_before = get_chain_info ( & follower_conf) . stacks_tip_height ;
9006
+
9007
+ // Stall p2p broadcast so that the mock miner mines a block before
9008
+ // seeing the block from the real miner.
9009
+ TEST_P2P_BROADCAST_STALL . set ( true ) ;
9010
+
9011
+ info ! ( "Sending transfer tx for interim block {interim_block_ix} of tenure {tenure_ix}" ) ;
9012
+
9011
9013
// submit a tx so that the miner will mine an extra block
9012
9014
let sender_nonce = tenure_ix * inter_blocks_per_tenure + interim_block_ix;
9013
9015
let transfer_tx = make_stacks_transfer_serialized (
@@ -9020,94 +9022,27 @@ fn mock_mining() {
9020
9022
) ;
9021
9023
submit_tx ( & http_origin, & transfer_tx) ;
9022
9024
9023
- loop {
9024
- let blocks_processed = coord_channel
9025
- . lock ( )
9026
- . expect ( "Mutex poisoned" )
9027
- . get_stacks_blocks_processed ( ) ;
9028
- if blocks_processed > blocks_processed_before {
9029
- break ;
9030
- }
9031
- thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
9032
- }
9033
-
9034
- let info = get_chain_info_result ( & naka_conf) . unwrap ( ) ;
9035
- assert_ne ! ( info. stacks_tip, last_tip) ;
9036
- assert_ne ! ( info. stacks_tip_height, last_tip_height) ;
9025
+ // Wait for the interim block to be mock-mined
9026
+ info ! ( "Waiting for the interim block {interim_block_ix} of tenure {tenure_ix} to be mock-mined" ) ;
9027
+ wait_for ( 30 , || {
9028
+ Ok ( follower_mined_blocks. load ( Ordering :: SeqCst ) > follower_mined_before)
9029
+ } )
9030
+ . expect ( "Failed to process the submitted transfer tx in a new nakamoto block" ) ;
9037
9031
9038
- last_tip = info. stacks_tip ;
9039
- last_tip_height = info. stacks_tip_height ;
9032
+ // Unstall p2p broadcast so that miner broadcasts the block and both
9033
+ // nodes can process it.
9034
+ TEST_P2P_BROADCAST_STALL . set ( false ) ;
9035
+ wait_for ( 30 , || {
9036
+ Ok ( get_chain_info ( & naka_conf) . stacks_tip_height > height_before
9037
+ && get_chain_info ( & follower_conf) . stacks_tip_height > follower_height_before)
9038
+ } )
9039
+ . expect ( "Failed to advanced to the interim block" ) ;
9040
9040
}
9041
9041
9042
- let miner_node_info = get_chain_info ( & naka_conf) ;
9043
- let follower_node_info = get_chain_info ( & follower_conf) ;
9044
- info ! ( "Node heights" ; "miner" => miner_node_info. stacks_tip_height, "follower" => follower_node_info. stacks_tip_height) ;
9045
-
9046
- // Wait for at least 2 blocks to be mined by the mock-miner
9047
- // This is to ensure that the mock miner has mined the tenure change
9048
- // block and at least one interim block.
9049
- wait_for ( 60 , || {
9050
- Ok ( follower_naka_mined_blocks. load ( Ordering :: SeqCst )
9051
- > follower_naka_mined_blocks_before + 1 )
9052
- } )
9053
- . unwrap_or_else ( |_| {
9054
- panic ! (
9055
- "Timed out waiting for mock miner block {}" ,
9056
- follower_naka_mined_blocks_before + 2
9057
- )
9058
- } ) ;
9059
-
9060
9042
wait_for ( 20 , || {
9061
9043
Ok ( commits_submitted. load ( Ordering :: SeqCst ) > commits_before)
9062
9044
} )
9063
- . unwrap_or_else ( |_| {
9064
- panic ! (
9065
- "Timed out waiting for mock miner block {}" ,
9066
- follower_naka_mined_blocks_before + 1
9067
- )
9068
- } ) ;
9069
- }
9070
-
9071
- // load the chain tip, and assert that it is a nakamoto block and at least 30 blocks have advanced in epoch 3
9072
- let tip = NakamotoChainState :: get_canonical_block_header ( chainstate. db ( ) , & sortdb)
9073
- . unwrap ( )
9074
- . unwrap ( ) ;
9075
- info ! (
9076
- "Latest tip" ;
9077
- "height" => tip. stacks_block_height,
9078
- "is_nakamoto" => tip. anchored_header. as_stacks_nakamoto( ) . is_some( ) ,
9079
- ) ;
9080
-
9081
- let expected_blocks_mined = ( inter_blocks_per_tenure + 1 ) * tenure_count;
9082
- let expected_tip_height = block_height_pre_3_0 + expected_blocks_mined;
9083
- assert ! ( tip. anchored_header. as_stacks_nakamoto( ) . is_some( ) ) ;
9084
- assert_eq ! (
9085
- tip. stacks_block_height, expected_tip_height,
9086
- "Should have mined (1 + interim_blocks_per_tenure) * tenure_count nakamoto blocks"
9087
- ) ;
9088
-
9089
- // Check follower's mock miner
9090
- let mock_mining_blocks_end = follower_naka_mined_blocks. load ( Ordering :: SeqCst ) ;
9091
- let blocks_mock_mined = mock_mining_blocks_end - mock_mining_blocks_start;
9092
- assert ! (
9093
- blocks_mock_mined >= tenure_count,
9094
- "Should have mock mined at least `tenure_count` nakamoto blocks. Mined = {blocks_mock_mined}. Expected = {tenure_count}"
9095
- ) ;
9096
-
9097
- // wait for follower to reach the chain tip
9098
- loop {
9099
- sleep_ms ( 1000 ) ;
9100
- let follower_node_info = get_chain_info ( & follower_conf) ;
9101
-
9102
- info ! (
9103
- "Follower tip is now {}/{}" ,
9104
- & follower_node_info. stacks_tip_consensus_hash, & follower_node_info. stacks_tip
9105
- ) ;
9106
- if follower_node_info. stacks_tip_consensus_hash == tip. consensus_hash
9107
- && follower_node_info. stacks_tip == tip. anchored_header . block_hash ( )
9108
- {
9109
- break ;
9110
- }
9045
+ . expect ( "Timed out waiting for the block commit to be submitted" ) ;
9111
9046
}
9112
9047
9113
9048
coord_channel
0 commit comments