|
| 1 | +use super::context::{SignerTestContext, SignerTestState}; |
| 2 | +use crate::tests::signer::v0::{ |
| 3 | + get_chain_info_wrapper, wait_for_block_pushed_by_miner_key, MultipleMinerTest, |
| 4 | +}; |
| 5 | +use madhouse::{Command, CommandWrapper}; |
| 6 | +use proptest::prelude::{Just, Strategy}; |
| 7 | +use stacks::chainstate::stacks::TenureChangeCause; |
| 8 | +use std::sync::{Arc, Mutex}; |
| 9 | +use tracing::info; |
| 10 | + |
| 11 | +pub struct MineBitcoinBlockTenureChangePrimaryMinerCommand { |
| 12 | + miners: Arc<Mutex<MultipleMinerTest>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl MineBitcoinBlockTenureChangePrimaryMinerCommand { |
| 16 | + pub fn new(miners: Arc<Mutex<MultipleMinerTest>>) -> Self { |
| 17 | + Self { miners } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl Command<SignerTestState, SignerTestContext> |
| 22 | + for MineBitcoinBlockTenureChangePrimaryMinerCommand |
| 23 | +{ |
| 24 | + fn check(&self, state: &SignerTestState) -> bool { |
| 25 | + println!( |
| 26 | + "Checking: Miner 1 mining Bitcoin block and tenure change tx. Result: {:?}", |
| 27 | + state.is_booted_to_nakamoto |
| 28 | + ); |
| 29 | + state.is_booted_to_nakamoto |
| 30 | + } |
| 31 | + |
| 32 | + fn apply(&self, _state: &mut SignerTestState) { |
| 33 | + println!("Applying: Miner 1 mining Bitcoin block and tenure change tx"); |
| 34 | + |
| 35 | + let (stacks_height_before, conf_1, miner_pk_1) = { |
| 36 | + let mut miners = self.miners.lock().unwrap(); |
| 37 | + let stacks_height_before = miners.get_peer_stacks_tip_height(); |
| 38 | + let (conf_1, _) = miners.get_node_configs(); |
| 39 | + let burnchain = conf_1.get_burnchain(); |
| 40 | + let sortdb = burnchain.open_sortition_db(true).unwrap(); |
| 41 | + |
| 42 | + miners |
| 43 | + .mine_bitcoin_block_and_tenure_change_tx(&sortdb, TenureChangeCause::BlockFound, 60) |
| 44 | + .expect("Failed to mine BTC block"); |
| 45 | + |
| 46 | + let (miner_pk_1, _) = miners.get_miner_public_keys(); |
| 47 | + |
| 48 | + (stacks_height_before, conf_1, miner_pk_1) |
| 49 | + }; |
| 50 | + |
| 51 | + println!( |
| 52 | + "Waiting for Nakamoto block {} pushed by miner 1", |
| 53 | + stacks_height_before + 1 |
| 54 | + ); |
| 55 | + |
| 56 | + let miner_1_block = |
| 57 | + wait_for_block_pushed_by_miner_key(30, stacks_height_before + 1, &miner_pk_1) |
| 58 | + .expect("Failed to get block"); |
| 59 | + |
| 60 | + let mined_block_height = miner_1_block.header.chain_length; |
| 61 | + info!( |
| 62 | + "Miner 1 mined Nakamoto block height: {}", |
| 63 | + mined_block_height |
| 64 | + ); |
| 65 | + |
| 66 | + let info_after = get_chain_info_wrapper(&conf_1); |
| 67 | + assert_eq!(info_after.stacks_tip, miner_1_block.header.block_hash()); |
| 68 | + assert_eq!(info_after.stacks_tip_height, mined_block_height); |
| 69 | + assert_eq!(mined_block_height, stacks_height_before + 1); |
| 70 | + } |
| 71 | + |
| 72 | + fn label(&self) -> String { |
| 73 | + "MINE_BITCOIN_BLOCK_AND_TENURE_CHANGE_MINER_1".to_string() |
| 74 | + } |
| 75 | + |
| 76 | + fn build( |
| 77 | + ctx: Arc<SignerTestContext>, |
| 78 | + ) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> { |
| 79 | + Just(CommandWrapper::new( |
| 80 | + MineBitcoinBlockTenureChangePrimaryMinerCommand::new(ctx.miners.clone()), |
| 81 | + )) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +pub struct MineBitcoinBlockTenureChangeSecondaryMinerCommand { |
| 86 | + miners: Arc<Mutex<MultipleMinerTest>>, |
| 87 | +} |
| 88 | + |
| 89 | +impl MineBitcoinBlockTenureChangeSecondaryMinerCommand { |
| 90 | + pub fn new(miners: Arc<Mutex<MultipleMinerTest>>) -> Self { |
| 91 | + Self { miners } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Command<SignerTestState, SignerTestContext> |
| 96 | + for MineBitcoinBlockTenureChangeSecondaryMinerCommand |
| 97 | +{ |
| 98 | + fn check(&self, state: &SignerTestState) -> bool { |
| 99 | + println!( |
| 100 | + "Checking: Miner 2 mining Bitcoin block and tenure change tx. Result: {:?}", |
| 101 | + state.is_booted_to_nakamoto |
| 102 | + ); |
| 103 | + state.is_booted_to_nakamoto |
| 104 | + } |
| 105 | + |
| 106 | + fn apply(&self, _state: &mut SignerTestState) { |
| 107 | + println!("Applying: Miner 2 mining Bitcoin block and tenure change tx"); |
| 108 | + |
| 109 | + let stacks_height_before = self.miners.lock().unwrap().get_peer_stacks_tip_height(); |
| 110 | + |
| 111 | + let (conf_1, conf_2) = self.miners.lock().unwrap().get_node_configs(); |
| 112 | + let burnchain = conf_1.get_burnchain(); |
| 113 | + let sortdb = burnchain.open_sortition_db(true).unwrap(); |
| 114 | + self.miners |
| 115 | + .lock() |
| 116 | + .unwrap() |
| 117 | + .mine_bitcoin_block_and_tenure_change_tx(&sortdb, TenureChangeCause::BlockFound, 60) |
| 118 | + .expect("Failed to mine BTC block"); |
| 119 | + |
| 120 | + let (_, miner_pk_2) = self.miners.lock().unwrap().get_miner_public_keys(); |
| 121 | + |
| 122 | + println!( |
| 123 | + "Waiting for Nakamoto block {} pushed by miner 2", |
| 124 | + stacks_height_before + 1 |
| 125 | + ); |
| 126 | + |
| 127 | + let secondary_miner_block = |
| 128 | + wait_for_block_pushed_by_miner_key(30, stacks_height_before + 1, &miner_pk_2) |
| 129 | + .expect("Failed to get block N"); |
| 130 | + |
| 131 | + let mined_block_height = secondary_miner_block.header.chain_length; |
| 132 | + |
| 133 | + let info_after = get_chain_info_wrapper(&conf_2); |
| 134 | + assert_eq!( |
| 135 | + info_after.stacks_tip, |
| 136 | + secondary_miner_block.header.block_hash() |
| 137 | + ); |
| 138 | + assert_eq!(info_after.stacks_tip_height, mined_block_height); |
| 139 | + assert_eq!(mined_block_height, stacks_height_before + 1); |
| 140 | + } |
| 141 | + |
| 142 | + fn label(&self) -> String { |
| 143 | + "MINE_BITCOIN_BLOCK_AND_TENURE_CHANGE_MINER_2".to_string() |
| 144 | + } |
| 145 | + |
| 146 | + fn build( |
| 147 | + ctx: Arc<SignerTestContext>, |
| 148 | + ) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> { |
| 149 | + Just(CommandWrapper::new( |
| 150 | + MineBitcoinBlockTenureChangeSecondaryMinerCommand::new(ctx.miners.clone()), |
| 151 | + )) |
| 152 | + } |
| 153 | +} |
0 commit comments