Skip to content

test: add disallow_reorg_within_first_proposal_burn_block_timing_secs_but_more_than_one_block_scenario #6039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions testnet/stacks-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ tempfile = "3.3"
mockito = "1.5"
serial_test = "3.2.0"
pinny = { git = "https://github.com/BitcoinL2-Labs/pinny-rs.git", rev = "54ba9d533a7b84525a5e65a3eae1a3ae76b9ea49" } #v0.0.2
madhouse = { git = "https://github.com/stacks-network/madhouse-rs.git", rev = "fc651ddcbaf85e888b06d4a87aa788c4b7ba9309" }
proptest = { git = "https://github.com/proptest-rs/proptest.git", rev = "c9bdf18c232665b2b740c667c81866b598d06dc7" }
madhouse = { git = "https://github.com/stacks-network/madhouse-rs.git", tag = "0.2.0" }
proptest = "1.6.*"

[[bin]]
name = "stacks-node"
Expand Down
52 changes: 50 additions & 2 deletions testnet/stacks-node/src/tests/signer/commands/bitcoin_mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Command<SignerTestState, SignerTestContext> for MineBitcoinBlockTenureChang
&& burn_height > miner_2_submitted_commit_last_burn_height
}

fn apply(&self, _state: &mut SignerTestState) {
fn apply(&self, state: &mut SignerTestState) {
info!("Applying: Miner 1 mining Bitcoin block and tenure change tx");

let (stacks_height_before, conf_1, miner_pk_1) = {
Expand Down Expand Up @@ -76,6 +76,9 @@ impl Command<SignerTestState, SignerTestContext> for MineBitcoinBlockTenureChang
wait_for_block_pushed_by_miner_key(30, stacks_height_before + 1, &miner_pk_1)
.expect("Failed to get block");

state.blocks_mined += 1;
info!("Increased blocks mined count to {}", state.blocks_mined);

let mined_block_height = miner_1_block.header.chain_length;
info!(
"Miner 1 mined Nakamoto block height: {}",
Expand Down Expand Up @@ -139,7 +142,7 @@ impl Command<SignerTestState, SignerTestContext> for MineBitcoinBlockTenureChang
&& burn_height > miner_1_submitted_commit_last_burn_height
}

fn apply(&self, _state: &mut SignerTestState) {
fn apply(&self, state: &mut SignerTestState) {
info!("Applying: Miner 2 mining Bitcoin block and tenure change tx");

let stacks_height_before = self.miners.lock().unwrap().get_peer_stacks_tip_height();
Expand All @@ -164,6 +167,9 @@ impl Command<SignerTestState, SignerTestContext> for MineBitcoinBlockTenureChang
wait_for_block_pushed_by_miner_key(30, stacks_height_before + 1, &miner_pk_2)
.expect("Failed to get block N");

state.blocks_mined += 1;
info!("Increased blocks mined count to {}", state.blocks_mined);

let mined_block_height = secondary_miner_block.header.chain_length;

let info_after = get_chain_info(&conf_2);
Expand Down Expand Up @@ -242,3 +248,45 @@ impl Command<SignerTestState, SignerTestContext> for MineBitcoinBlock {
})
}
}

pub struct BuildNextBitcoinBlocks {
miners: Arc<Mutex<MultipleMinerTest>>,
num_blocks: u64,
}

impl BuildNextBitcoinBlocks {
pub fn new(miners: Arc<Mutex<MultipleMinerTest>>, num_blocks: u64) -> Self {
Self { miners, num_blocks }
}
}

impl Command<SignerTestState, SignerTestContext> for BuildNextBitcoinBlocks {
fn check(&self, _state: &SignerTestState) -> bool {
info!(
"Checking: Build next {} Bitcoin block(s). Result: {:?}",
self.num_blocks, true
);
true
}

fn apply(&self, _state: &mut SignerTestState) {
info!("Applying: Build next {} Bitcoin block(s)", self.num_blocks);

let mut miners = self.miners.lock().unwrap();
miners
.btc_regtest_controller_mut()
.build_next_block(self.num_blocks);
}

fn label(&self) -> String {
"BUILD_NEXT_BITCOIN_BLOCKS".to_string()
}

fn build(
ctx: Arc<SignerTestContext>,
) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> {
(1u64..=5u64).prop_map(move |num_blocks| {
CommandWrapper::new(BuildNextBitcoinBlocks::new(ctx.miners.clone(), num_blocks))
})
}
}
180 changes: 179 additions & 1 deletion testnet/stacks-node/src/tests/signer/commands/block_wait.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};

use libsigner::v0::messages::RejectReason;
use madhouse::{Command, CommandWrapper};
use proptest::prelude::{Just, Strategy};

use super::context::{SignerTestContext, SignerTestState};
use crate::tests::signer::v0::{wait_for_block_pushed_by_miner_key, MultipleMinerTest};
use crate::stacks_common::types::PublicKey;
use crate::tests::signer::v0::{
get_nakamoto_headers, wait_for_block_global_rejection_with_reject_reason,
wait_for_block_proposal, wait_for_block_pushed_by_miner_key, MultipleMinerTest,
};

pub struct WaitForTenureChangeBlockFromMiner1 {
miners: Arc<Mutex<MultipleMinerTest>>,
Expand Down Expand Up @@ -123,3 +128,176 @@ impl Command<SignerTestState, SignerTestContext> for WaitForTenureChangeBlockFro
))
}
}

pub struct WaitForAndVerifyBlockRejection {
miners: Arc<Mutex<MultipleMinerTest>>,
reason: RejectReason,
num_signers: usize,
}

impl WaitForAndVerifyBlockRejection {
pub fn new(
miners: Arc<Mutex<MultipleMinerTest>>,
reason: RejectReason,
num_signers: usize,
) -> Self {
Self {
miners,
reason,
num_signers,
}
}
}

impl Command<SignerTestState, SignerTestContext> for WaitForAndVerifyBlockRejection {
fn check(&self, _state: &SignerTestState) -> bool {
info!(
"Checking: Waiting for block proposal from miner 1 and verifying rejection with reason {:?}",
self.reason
);
true
}

fn apply(&self, state: &mut SignerTestState) {
info!("Applying: Waiting for block proposal from miner 1 and verifying rejection with reason {:?}", self.reason);

let (block_height, miner_pk_1) = {
let miners = self.miners.lock().unwrap();
let (conf_1, _) = miners.get_node_configs();
let chain_info = crate::tests::neon_integrations::get_chain_info(&conf_1);
let current_height = chain_info.stacks_tip_height;
let block_n_height = current_height - state.blocks_mined as u64;
let (miner_pk_1, _) = miners.get_miner_public_keys();
(block_n_height, miner_pk_1)
};

info!("Waiting for block proposal at height {}", block_height + 1);

let proposed_block = wait_for_block_proposal(30, block_height + 1, &miner_pk_1)
.expect("Timed out waiting for block proposal");

let block_hash = proposed_block.header.signer_signature_hash();

info!(
"Received block proposal at height {} with hash {:?}",
block_height + 1,
block_hash
);

wait_for_block_global_rejection_with_reject_reason(
30,
block_hash,
self.num_signers,
self.reason.clone(),
)
.expect("Timed out waiting for block rejection");

info!(
"Block was rejected with the expected reason: {:?}",
self.reason
);
}

fn label(&self) -> String {
format!(
"WAIT_FOR_AND_VERIFY_BLOCK_REJECTION_WITH_REASON_{:?}",
self.reason
)
}

fn build(
ctx: Arc<SignerTestContext>,
) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> {
(1usize..=5usize).prop_map(move |num_signers: usize| {
CommandWrapper::new(WaitForAndVerifyBlockRejection::new(
ctx.miners.clone(),
RejectReason::ReorgNotAllowed,
num_signers,
))
})
}
}

pub struct VerifyMiner1BlockCount {
miners: Arc<Mutex<MultipleMinerTest>>,
expected_block_count: usize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid this field that can affect the assertion, an idea off the top of my head would be to separate blocks_mined state field into two, one for each miner, or even a map with n keys for n miners. From there, the total could be calculated as:

let total_mined_blocks_count = blocks_mined_per_miner.values().fold(0, |acc, &count| acc + count);

By doing this, you can retrieve the miner 1 blocks directly in this command, and use it inside the assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! I love this approach!
This way we also keep track if each miner's activity

}

impl VerifyMiner1BlockCount {
pub fn new(miners: Arc<Mutex<MultipleMinerTest>>, expected_block_count: usize) -> Self {
Self {
miners,
expected_block_count,
}
}
}

impl Command<SignerTestState, SignerTestContext> for VerifyMiner1BlockCount {
fn check(&self, state: &SignerTestState) -> bool {
info!(
"Checking: Verifying miner 1 block count. Will run if miner 1 commit ops are paused: {:?}",
state.is_primary_miner_skip_commit_op
);
state.is_primary_miner_skip_commit_op
}

fn apply(&self, state: &mut SignerTestState) {
info!(
"Applying: Verifying miner 1 block count is {}",
self.expected_block_count
);

let (stacks_height_before, conf_1, miner_pk_1) = {
let miners = self.miners.lock().unwrap();
let current_height = miners.get_peer_stacks_tip_height();
let stacks_height_before = current_height - state.blocks_mined as u64;

let (conf_1, _) = miners.get_node_configs();
let (miner_pk_1, _) = miners.get_miner_public_keys();

(stacks_height_before, conf_1, miner_pk_1)
};

// Check only expected_block_count blocks from miner1 have been added after the epoch3 boot
let miner1_blocks_after_boot_to_epoch3 = get_nakamoto_headers(&conf_1)
.into_iter()
.filter(|block| {
// Skip first nakamoto block
if block.stacks_block_height == stacks_height_before {
return false;
}
let nakamoto_block_header = block.anchored_header.as_stacks_nakamoto().unwrap();
miner_pk_1
.verify(
nakamoto_block_header.miner_signature_hash().as_bytes(),
&nakamoto_block_header.miner_signature,
)
.unwrap()
})
.count();

assert_eq!(
miner1_blocks_after_boot_to_epoch3, self.expected_block_count,
"Expected {} blocks from miner 1, but found {}",
self.expected_block_count, miner1_blocks_after_boot_to_epoch3
);

info!(
"Verified miner 1 has exactly {} blocks after epoch 3 boot",
self.expected_block_count
);
}

fn label(&self) -> String {
format!("VERIFY_MINER_1_BLOCK_COUNT_{}", self.expected_block_count)
}

fn build(
ctx: Arc<SignerTestContext>,
) -> impl Strategy<Value = CommandWrapper<SignerTestState, SignerTestContext>> {
Just(CommandWrapper::new(VerifyMiner1BlockCount::new(
ctx.miners.clone(),
1,
)))
}
}
Loading
Loading