|
| 1 | +use super::context::{SignerTestContext, SignerTestState}; |
| 2 | +use crate::tests::signer::v0::{wait_for_block_pushed_by_miner_key, MultipleMinerTest}; |
| 3 | +use madhouse::{Command, CommandWrapper}; |
| 4 | +use proptest::prelude::{Just, Strategy}; |
| 5 | +use std::sync::{atomic::Ordering, Arc, Mutex}; |
| 6 | + |
| 7 | +pub struct WaitForBlockFromMiner1Command { |
| 8 | + miners: Arc<Mutex<MultipleMinerTest>>, |
| 9 | +} |
| 10 | + |
| 11 | +impl WaitForBlockFromMiner1Command { |
| 12 | + pub fn new(miners: Arc<Mutex<MultipleMinerTest>>) -> Self { |
| 13 | + Self { miners } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl Command<SignerTestState, SignerTestContext> for WaitForBlockFromMiner1Command { |
| 18 | + fn check(&self, state: &SignerTestState) -> bool { |
| 19 | + println!( |
| 20 | + "Checking: Waiting for Nakamoto block from miner 1. Result: {:?}", |
| 21 | + !state.mining_stalled |
| 22 | + ); |
| 23 | + !state.mining_stalled |
| 24 | + } |
| 25 | + |
| 26 | + fn apply(&self, _state: &mut SignerTestState) { |
| 27 | + println!("Applying: Waiting for Nakamoto block from miner 1"); |
| 28 | + |
| 29 | + let miners_arc = self.miners.clone(); |
| 30 | + |
| 31 | + let (miner_pk_1, last_confirmed_nakamoto_height_counter) = { |
| 32 | + let miners = miners_arc.lock().unwrap(); |
| 33 | + let (miner_pk_1, _) = miners.get_miner_public_keys(); |
| 34 | + let last_confirmed_nakamoto_height = miners.get_primary_last_stacks_tip_counter(); |
| 35 | + (miner_pk_1, last_confirmed_nakamoto_height) |
| 36 | + }; |
| 37 | + |
| 38 | + let last_confirmed_height = last_confirmed_nakamoto_height_counter |
| 39 | + .0 |
| 40 | + .load(Ordering::SeqCst); |
| 41 | + let expected_height = last_confirmed_height + 1; |
| 42 | + |
| 43 | + println!( |
| 44 | + "Waiting for Nakamoto block {} pushed by miner 1", |
| 45 | + expected_height |
| 46 | + ); |
| 47 | + |
| 48 | + let _miner_1_block = wait_for_block_pushed_by_miner_key(30, expected_height, &miner_pk_1) |
| 49 | + .expect(&format!("Failed to get block {}", expected_height)); |
| 50 | + } |
| 51 | + |
| 52 | + fn label(&self) -> String { |
| 53 | + "WAIT_FOR_BLOCK_FROM_MINER_1".to_string() |
| 54 | + } |
| 55 | + |
| 56 | + fn build( |
| 57 | + ctx: Arc<SignerTestContext>, |
| 58 | + ) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> { |
| 59 | + Just(CommandWrapper::new(WaitForBlockFromMiner1Command::new( |
| 60 | + ctx.miners.clone(), |
| 61 | + ))) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +pub struct WaitForBlockFromMiner2Command { |
| 66 | + miners: Arc<Mutex<MultipleMinerTest>>, |
| 67 | +} |
| 68 | + |
| 69 | +impl WaitForBlockFromMiner2Command { |
| 70 | + pub fn new(miners: Arc<Mutex<MultipleMinerTest>>) -> Self { |
| 71 | + Self { miners } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Command<SignerTestState, SignerTestContext> for WaitForBlockFromMiner2Command { |
| 76 | + fn check(&self, state: &SignerTestState) -> bool { |
| 77 | + println!( |
| 78 | + "Checking: Waiting for Nakamoto block from miner 2. Result: {:?}", |
| 79 | + !state.mining_stalled |
| 80 | + ); |
| 81 | + !state.mining_stalled |
| 82 | + } |
| 83 | + |
| 84 | + fn apply(&self, _state: &mut SignerTestState) { |
| 85 | + println!("Applying: Waiting for Nakamoto block from miner 2"); |
| 86 | + |
| 87 | + let miners_arc = self.miners.clone(); |
| 88 | + |
| 89 | + let (miner_pk_2, last_confirmed_nakamoto_height_counter) = { |
| 90 | + let miners = miners_arc.lock().unwrap(); |
| 91 | + let (_, miner_pk_2) = miners.get_miner_public_keys(); |
| 92 | + let last_confirmed_nakamoto_height = miners.get_secondary_last_stacks_tip_counter(); |
| 93 | + (miner_pk_2, last_confirmed_nakamoto_height) |
| 94 | + }; |
| 95 | + |
| 96 | + let last_confirmed_height = last_confirmed_nakamoto_height_counter |
| 97 | + .0 |
| 98 | + .load(Ordering::SeqCst); |
| 99 | + let expected_stacks_height = last_confirmed_height + 1; |
| 100 | + |
| 101 | + println!( |
| 102 | + "Waiting for Nakamoto block {} pushed by miner 2", |
| 103 | + expected_stacks_height |
| 104 | + ); |
| 105 | + |
| 106 | + let _miner_2_block_n_1 = |
| 107 | + wait_for_block_pushed_by_miner_key(30, expected_stacks_height, &miner_pk_2) |
| 108 | + .expect(&format!("Failed to get block {:?}", expected_stacks_height)); |
| 109 | + } |
| 110 | + |
| 111 | + fn label(&self) -> String { |
| 112 | + "WAIT_FOR_BLOCK_FROM_MINER_2".to_string() |
| 113 | + } |
| 114 | + |
| 115 | + fn build( |
| 116 | + ctx: Arc<SignerTestContext>, |
| 117 | + ) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> { |
| 118 | + Just(CommandWrapper::new(WaitForBlockFromMiner2Command::new( |
| 119 | + ctx.miners.clone(), |
| 120 | + ))) |
| 121 | + } |
| 122 | +} |
0 commit comments