Skip to content

Commit

Permalink
Merge pull request #5337 from stacks-network/fix/partial-tenure-fork
Browse files Browse the repository at this point in the history
Fix partial tenure fork test
  • Loading branch information
kantai authored Oct 21, 2024
2 parents 76b3094 + 899ff22 commit da15d83
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions testnet/stacks-node/src/tests/signer/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3867,7 +3867,14 @@ fn partial_tenure_fork() {
let mut min_miner_2_tenures = u64::MAX;
let mut ignore_block = 0;

while miner_1_tenures < min_miner_1_tenures || miner_2_tenures < min_miner_2_tenures {
let mut miner_1_blocks = 0;
let mut miner_2_blocks = 0;
// Make sure that both miner 1 and 2 mine at least 1 block each
while miner_1_tenures < min_miner_1_tenures
|| miner_2_tenures < min_miner_2_tenures
|| miner_1_blocks == 0
|| miner_2_blocks == 0
{
if btc_blocks_mined >= max_nakamoto_tenures {
panic!("Produced {btc_blocks_mined} sortitions, but didn't cover the test scenarios, aborting");
}
Expand Down Expand Up @@ -3959,6 +3966,7 @@ fn partial_tenure_fork() {
min_miner_1_tenures = miner_1_tenures + 1;
}

let mut blocks = inter_blocks_per_tenure;
// mine (or attempt to mine) the interim blocks
for interim_block_ix in 0..inter_blocks_per_tenure {
let mined_before_1 = blocks_mined1.load(Ordering::SeqCst);
Expand Down Expand Up @@ -4030,6 +4038,7 @@ fn partial_tenure_fork() {
Err(e) => {
if e.to_string().contains("TooMuchChaining") {
info!("TooMuchChaining error, skipping block");
blocks = interim_block_ix;
break;
} else {
panic!("Failed to submit tx: {}", e);
Expand All @@ -4044,21 +4053,24 @@ fn partial_tenure_fork() {

if miner == 1 {
miner_1_tenures += 1;
miner_1_blocks += blocks;
} else {
miner_2_tenures += 1;
miner_2_blocks += blocks;
}
info!(
"Miner 1 tenures: {}, Miner 2 tenures: {}, Miner 1 before: {}, Miner 2 before: {}",
miner_1_tenures, miner_2_tenures, mined_before_1, mined_before_2,
);

let mined_1 = blocks_mined1.load(Ordering::SeqCst);
let mined_2 = blocks_mined2.load(Ordering::SeqCst);

info!(
"Miner 1 tenures: {miner_1_tenures}, Miner 2 tenures: {miner_2_tenures}, Miner 1 before: {mined_before_1}, Miner 2 before: {mined_before_2}, Miner 1 blocks: {mined_1}, Miner 2 blocks: {mined_2}",
);

if miner == 1 {
assert_eq!(mined_1, mined_before_1 + inter_blocks_per_tenure + 1);
assert_eq!(mined_1, mined_before_1 + blocks + 1);
} else {
if miner_2_tenures < min_miner_2_tenures {
assert_eq!(mined_2, mined_before_2 + inter_blocks_per_tenure + 1);
assert_eq!(mined_2, mined_before_2 + blocks + 1);
} else {
// Miner 2 should have mined 0 blocks after the fork
assert_eq!(mined_2, mined_before_2);
Expand All @@ -4078,11 +4090,16 @@ fn partial_tenure_fork() {
assert_eq!(peer_2_height, ignore_block - 1);
// The height may be higher than expected due to extra transactions waiting
// to be mined during the forking miner's tenure.
// We cannot guarantee due to TooMuchChaining that the miner will mine inter_blocks_per_tenure
let min_num_miner_2_blocks = std::cmp::min(
miner_2_blocks,
min_miner_2_tenures * (inter_blocks_per_tenure + 1),
);
assert!(
peer_1_height
>= pre_nakamoto_peer_1_height
+ (miner_1_tenures + min_miner_2_tenures - 1) * (inter_blocks_per_tenure + 1)
miner_2_tenures >= min_miner_2_tenures,
"Miner 2 failed to win its minimum number of tenures"
);
assert!(peer_1_height >= pre_nakamoto_peer_1_height + miner_1_blocks + min_num_miner_2_blocks,);
assert_eq!(
btc_blocks_mined,
u64::try_from(miner_1_tenures + miner_2_tenures).unwrap()
Expand Down

0 comments on commit da15d83

Please sign in to comment.