Skip to content

Commit 994bc83

Browse files
committed
test: further improvements to integration test
Ensure that each block has the expected parent block in `allow_reorg_within_first_proposal_burn_block_timing_secs`.
1 parent bc51cbb commit 994bc83

File tree

1 file changed

+85
-25
lines changed
  • testnet/stacks-node/src/tests/signer

1 file changed

+85
-25
lines changed

testnet/stacks-node/src/tests/signer/v0.rs

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use libsigner::v0::messages::{
2727
SignerMessage,
2828
};
2929
use libsigner::{BlockProposal, SignerSession, StackerDBSession, VERSION_STRING};
30+
use serde::Deserialize;
3031
use stacks::address::AddressHashMode;
3132
use stacks::burnchains::Txid;
3233
use stacks::chainstate::burn::db::sortdb::SortitionDB;
@@ -10816,6 +10817,31 @@ fn injected_signatures_are_ignored_across_boundaries() {
1081610817
assert!(new_spawned_signer.stop().is_none());
1081710818
}
1081810819

10820+
#[derive(Deserialize, Debug)]
10821+
struct ObserverBlock {
10822+
block_height: u64,
10823+
#[serde(deserialize_with = "strip_0x")]
10824+
block_hash: String,
10825+
#[serde(deserialize_with = "strip_0x")]
10826+
parent_block_hash: String,
10827+
}
10828+
10829+
fn strip_0x<'de, D>(deserializer: D) -> Result<String, D::Error>
10830+
where
10831+
D: serde::Deserializer<'de>,
10832+
{
10833+
let s: String = Deserialize::deserialize(deserializer)?;
10834+
Ok(s.strip_prefix("0x").unwrap_or(&s).to_string())
10835+
}
10836+
10837+
fn get_last_observed_block() -> ObserverBlock {
10838+
let blocks = test_observer::get_blocks();
10839+
let last_block_value = blocks.last().expect("No blocks mined");
10840+
let last_block: ObserverBlock =
10841+
serde_json::from_value(last_block_value.clone()).expect("Failed to parse block");
10842+
last_block
10843+
}
10844+
1081910845
/// Test a scenario where:
1082010846
/// Two miners boot to Nakamoto.
1082110847
/// Sortition occurs. Miner 1 wins.
@@ -11033,26 +11059,24 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1103311059
.expect("Timed out waiting for Miner 1 to Mine Block N");
1103411060

1103511061
let blocks = test_observer::get_mined_nakamoto_blocks();
11036-
let block_n = blocks.last().unwrap().clone();
11062+
let block_n = blocks.last().expect("No blocks mined");
1103711063
let block_n_height = block_n.stacks_height;
11064+
let block_n_hash = block_n.block_hash.clone();
1103811065
info!("Block N: {block_n_height}");
11039-
let block_n_signature_hash = block_n.signer_signature_hash;
1104011066

1104111067
let info_after = get_chain_info(&conf);
1104211068
assert_eq!(info_after.stacks_tip.to_string(), block_n.block_hash);
11043-
assert_eq!(block_n.signer_signature_hash, block_n_signature_hash);
1104411069
assert_eq!(
1104511070
info_after.stacks_tip_height,
1104611071
info_before.stacks_tip_height + 1
1104711072
);
11073+
assert_eq!(info_after.stacks_tip_height, block_n_height);
1104811074

1104911075
// assure we have a successful sortition that miner 1 won
1105011076
let tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn()).unwrap();
1105111077
assert!(tip.sortition);
1105211078
assert_eq!(tip.miner_pk_hash.unwrap(), mining_pkh_1);
1105311079

11054-
debug!("Miner 1 mined block N: {block_n_signature_hash}");
11055-
1105611080
info!("------------------------- Miner 2 Submits a Block Commit -------------------------");
1105711081
let rl2_commits_before = rl2_commits.load(Ordering::SeqCst);
1105811082
rl2_skip_commit_op.set(false);
@@ -11097,14 +11121,15 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1109711121
.nakamoto_test_skip_commit_op
1109811122
.set(true);
1109911123

11100-
info!("------------------------- Miner 2 Mines Block N + 1 -------------------------");
11124+
info!("------------------------- Miner 2 Mines Block N+1 -------------------------");
1110111125
let blocks_processed_before_2 = blocks_mined2.load(Ordering::SeqCst);
1110211126
let stacks_height_before = signer_test
1110311127
.stacks_client
1110411128
.get_peer_info()
1110511129
.expect("Failed to get peer info")
1110611130
.stacks_tip_height;
1110711131
let info_before = get_chain_info(&conf);
11132+
let mined_before = test_observer::get_blocks().len();
1110811133

1110911134
TEST_MINE_STALL.lock().unwrap().replace(false);
1111011135

@@ -11116,9 +11141,10 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1111611141
.stacks_tip_height
1111711142
> stacks_height_before
1111811143
&& blocks_mined2.load(Ordering::SeqCst) > blocks_processed_before_2
11119-
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height)
11144+
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height
11145+
&& test_observer::get_blocks().len() > mined_before)
1112011146
})
11121-
.expect("Timed out waiting for Miner 2 to Mine Block N + 1");
11147+
.expect("Timed out waiting for Miner 2 to Mine Block N+1");
1112211148

1112311149
// assure we have a successful sortition that miner 2 won
1112411150
let tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn()).unwrap();
@@ -11127,6 +11153,9 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1112711153

1112811154
assert_eq!(get_chain_info(&conf).stacks_tip_height, block_n_height + 1);
1112911155

11156+
let last_block = get_last_observed_block();
11157+
assert_eq!(last_block.block_height, block_n_height + 1);
11158+
1113011159
info!("------------------------- Miner 2 Mines N+2 and N+3 -------------------------");
1113111160
let blocks_processed_before_2 = blocks_mined2.load(Ordering::SeqCst);
1113211161
let stacks_height_before = signer_test
@@ -11135,6 +11164,7 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1113511164
.expect("Failed to get peer info")
1113611165
.stacks_tip_height;
1113711166
let info_before = get_chain_info(&conf);
11167+
let mined_before = test_observer::get_blocks().len();
1113811168

1113911169
// submit a tx so that the miner will ATTEMPT to mine a stacks block N+2
1114011170
let transfer_tx = make_stacks_transfer(
@@ -11157,17 +11187,22 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1115711187
.stacks_tip_height
1115811188
> stacks_height_before
1115911189
&& blocks_mined2.load(Ordering::SeqCst) > blocks_processed_before_2
11160-
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height)
11190+
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height
11191+
&& test_observer::get_blocks().len() > mined_before)
1116111192
})
1116211193
.expect("Timed out waiting for Miner 2 to Mine Block N+2");
1116311194

11195+
let last_block = get_last_observed_block();
11196+
assert_eq!(last_block.block_height, block_n_height + 2);
11197+
1116411198
let blocks_processed_before_2 = blocks_mined2.load(Ordering::SeqCst);
1116511199
let stacks_height_before = signer_test
1116611200
.stacks_client
1116711201
.get_peer_info()
1116811202
.expect("Failed to get peer info")
1116911203
.stacks_tip_height;
1117011204
let info_before = get_chain_info(&conf);
11205+
let mined_before = test_observer::get_blocks().len();
1117111206

1117211207
// submit a tx so that the miner will ATTEMPT to mine a stacks block N+3
1117311208
let transfer_tx = make_stacks_transfer(
@@ -11190,32 +11225,38 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1119011225
.stacks_tip_height
1119111226
> stacks_height_before
1119211227
&& blocks_mined2.load(Ordering::SeqCst) > blocks_processed_before_2
11193-
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height)
11228+
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height
11229+
&& test_observer::get_blocks().len() > mined_before)
1119411230
})
1119511231
.expect("Timed out waiting for Miner 2 to Mine Block N+3");
1119611232

1119711233
assert_eq!(get_chain_info(&conf).stacks_tip_height, block_n_height + 3);
1119811234

11235+
let last_block = get_last_observed_block();
11236+
let block_n3_hash = last_block.block_hash.clone();
11237+
assert_eq!(last_block.block_height, block_n_height + 3);
11238+
1119911239
info!("------------------------- Miner 1 Wins the Next Tenure, Mines N+1' -------------------------");
1120011240

1120111241
let blocks_processed_before_1 = blocks_mined1.load(Ordering::SeqCst);
11202-
let mined_before = test_observer::get_mined_nakamoto_blocks().len();
11242+
let mined_before = test_observer::get_blocks().len();
1120311243

1120411244
next_block_and(
1120511245
&mut signer_test.running_nodes.btc_regtest_controller,
1120611246
30,
1120711247
|| {
1120811248
Ok(
1120911249
blocks_mined1.load(Ordering::SeqCst) > blocks_processed_before_1
11210-
&& test_observer::get_mined_nakamoto_blocks().len() > mined_before,
11250+
&& test_observer::get_blocks().len() > mined_before,
1121111251
)
1121211252
},
1121311253
)
1121411254
.expect("Timed out waiting for Miner 1 to Mine Block N+1'");
1121511255

11216-
let blocks = test_observer::get_mined_nakamoto_blocks();
11217-
let last_block = blocks.last().expect("No blocks mined");
11218-
assert_eq!(last_block.stacks_height, block_n_height + 1);
11256+
let last_block = get_last_observed_block();
11257+
let block_n1_prime_hash = last_block.block_hash.clone();
11258+
assert_eq!(last_block.block_height, block_n_height + 1);
11259+
assert_eq!(last_block.parent_block_hash, block_n_hash);
1121911260

1122011261
info!("------------------------- Miner 1 Submits a Block Commit -------------------------");
1122111262

@@ -11238,7 +11279,7 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1123811279
info!("------------------------- Miner 1 Mines N+2' -------------------------");
1123911280

1124011281
let blocks_processed_before_1 = blocks_mined1.load(Ordering::SeqCst);
11241-
let mined_before = test_observer::get_mined_nakamoto_blocks().len();
11282+
let mined_before = test_observer::get_blocks().len();
1124211283

1124311284
// submit a tx so that the miner will ATTEMPT to mine a stacks block N+2
1124411285
let transfer_tx = make_stacks_transfer(
@@ -11255,27 +11296,46 @@ fn allow_reorg_within_first_proposal_burn_block_timing_secs() {
1125511296
wait_for(30, || {
1125611297
Ok(
1125711298
blocks_mined1.load(Ordering::SeqCst) > blocks_processed_before_1
11258-
&& test_observer::get_mined_nakamoto_blocks().len() > mined_before,
11299+
&& test_observer::get_blocks().len() > mined_before,
1125911300
)
1126011301
})
1126111302
.expect("Timed out waiting for Miner 1 to Mine Block N+2'");
1126211303

11263-
let blocks = test_observer::get_mined_nakamoto_blocks();
11264-
let last_block = blocks.last().expect("No blocks mined");
11265-
assert_eq!(last_block.stacks_height, block_n_height + 2);
11304+
let last_block = get_last_observed_block();
11305+
assert_eq!(last_block.block_height, block_n_height + 2);
11306+
assert_eq!(last_block.parent_block_hash, block_n1_prime_hash);
1126611307

1126711308
info!("------------------------- Miner 1 Mines N+4 in Next Tenure -------------------------");
1126811309

11269-
next_block_and_process_new_stacks_block(
11310+
let blocks_processed_before_1 = blocks_mined1.load(Ordering::SeqCst);
11311+
let stacks_height_before = signer_test
11312+
.stacks_client
11313+
.get_peer_info()
11314+
.expect("Failed to get peer info")
11315+
.stacks_tip_height;
11316+
let info_before = get_chain_info(&conf);
11317+
let mined_before = test_observer::get_blocks().len();
11318+
11319+
next_block_and(
1127011320
&mut signer_test.running_nodes.btc_regtest_controller,
1127111321
30,
11272-
&signer_test.running_nodes.coord_channel,
11322+
|| {
11323+
Ok(signer_test
11324+
.stacks_client
11325+
.get_peer_info()
11326+
.expect("Failed to get peer info")
11327+
.stacks_tip_height
11328+
> stacks_height_before
11329+
&& blocks_mined1.load(Ordering::SeqCst) > blocks_processed_before_1
11330+
&& get_chain_info(&conf).stacks_tip_height > info_before.stacks_tip_height
11331+
&& test_observer::get_blocks().len() > mined_before)
11332+
},
1127311333
)
1127411334
.expect("Timed out waiting for Miner 1 to Mine Block N+4");
1127511335

11276-
let blocks = test_observer::get_mined_nakamoto_blocks();
11277-
let last_block = blocks.last().expect("No blocks mined");
11278-
assert_eq!(last_block.stacks_height, block_n_height + 4);
11336+
let last_block = get_last_observed_block();
11337+
assert_eq!(last_block.block_height, block_n_height + 4);
11338+
assert_eq!(last_block.parent_block_hash, block_n3_hash);
1127911339

1128011340
info!("------------------------- Shutdown -------------------------");
1128111341
rl2_coord_channels

0 commit comments

Comments
 (0)