Skip to content

Commit aceeca6

Browse files
committed
CRC: add comments to the multiple miner tests helper functions
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 00eb14d commit aceeca6

File tree

1 file changed

+62
-16
lines changed
  • testnet/stacks-node/src/tests/signer

1 file changed

+62
-16
lines changed

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

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,21 @@ pub struct MultipleMinerTest {
457457
}
458458

459459
impl MultipleMinerTest {
460-
pub fn new(num_signers: usize, num_transfer_txs: u64) -> MultipleMinerTest {
461-
Self::new_with_config_modifications(num_signers, num_transfer_txs, |_| {}, |_| {}, |_| {})
462-
}
460+
/// Create a new test harness for running multiple miners with num_signers underlying signers and enough funds to send
461+
/// num_txs transfer transactions.
462+
///
463+
/// Will partition the signer set so that ~half are listening and using node 1 for RPC and events,
464+
/// and the rest are using node 2
465+
pub fn new(num_signers: usize, num_txs: u64) -> MultipleMinerTest {
466+
Self::new_with_config_modifications(num_signers, num_txs, |_| {}, |_| {}, |_| {})
467+
}
468+
469+
/// Create a new test harness for running multiple miners with num_signers underlying signers and enough funds to send
470+
/// num_txs transfer transactions.
471+
///
472+
/// Will also modify the signer config and the node 1 and node 2 configs with the provided
473+
/// modifiers. Will partition the signer set so that ~half are listening and using node 1 for RPC and events,
474+
/// and the rest are using node 2 unless otherwise specified via the signer config modifier.
463475
pub fn new_with_config_modifications<
464476
F: FnMut(&mut SignerConfig),
465477
G: FnMut(&mut NeonConfig),
@@ -589,6 +601,7 @@ impl MultipleMinerTest {
589601
}
590602
}
591603

604+
/// Boot node 1 to epoch 3.0 and wait for node 2 to catch up.
592605
pub fn boot_to_epoch_3(&mut self) {
593606
info!(
594607
"------------------------- Booting Both Miners to Epoch 3.0 -------------------------"
@@ -601,6 +614,7 @@ impl MultipleMinerTest {
601614
info!("------------------------- Reached Epoch 3.0 -------------------------");
602615
}
603616

617+
/// Returns a tuple of the node 1 and node 2 miner private keys respectively
604618
pub fn get_miner_private_keys(&self) -> (StacksPrivateKey, StacksPrivateKey) {
605619
(
606620
self.signer_test
@@ -613,6 +627,7 @@ impl MultipleMinerTest {
613627
)
614628
}
615629

630+
/// Returns a tuple of the node 1 and node 2 miner public keys respectively
616631
pub fn get_miner_public_keys(&self) -> (StacksPublicKey, StacksPublicKey) {
617632
let (sk1, sk2) = self.get_miner_private_keys();
618633
(
@@ -621,6 +636,7 @@ impl MultipleMinerTest {
621636
)
622637
}
623638

639+
/// Returns a tuple of the node 1 and node 2 miner private key hashes respectively
624640
pub fn get_miner_public_key_hashes(&self) -> (Hash160, Hash160) {
625641
let (pk1, pk2) = self.get_miner_public_keys();
626642
(
@@ -629,6 +645,7 @@ impl MultipleMinerTest {
629645
)
630646
}
631647

648+
/// Returns a tuple of the node 1 and node 2 miner node configs respectively
632649
pub fn get_node_configs(&self) -> (NeonConfig, NeonConfig) {
633650
(
634651
self.signer_test.running_nodes.conf.clone(),
@@ -640,6 +657,8 @@ impl MultipleMinerTest {
640657
&mut self.signer_test.running_nodes.btc_regtest_controller
641658
}
642659

660+
/// Mine `nmb_blocks` blocks on the bitcoin regtest chain and wait for the sortition
661+
/// database to confirm the block.
643662
pub fn mine_bitcoin_blocks_and_confirm(
644663
&mut self,
645664
sortdb: &SortitionDB,
@@ -660,6 +679,8 @@ impl MultipleMinerTest {
660679
})
661680
}
662681

682+
/// Mine `nmb_blocks` blocks on the bitcoin regtest chain and wait for the sortition
683+
/// database to confirm the block and the test_observer to see the block.
663684
pub fn mine_bitcoin_blocks_and_confirm_with_test_observer(
664685
&mut self,
665686
sortdb: &SortitionDB,
@@ -683,6 +704,8 @@ impl MultipleMinerTest {
683704
})
684705
}
685706

707+
/// Mine a bitcoin block and wait for the sortition database to confirm the block and wait
708+
/// for a tenure change transaction to be subseqently mined in a stacks block at the appropriate height.
686709
pub fn mine_bitcoin_block_and_tenure_change_tx(
687710
&mut self,
688711
sortdb: &SortitionDB,
@@ -699,6 +722,7 @@ impl MultipleMinerTest {
699722
)
700723
}
701724

725+
/// Sends a transfer tx to the stacks node and returns the txid
702726
pub fn send_transfer_tx(&mut self) -> String {
703727
let http_origin = format!(
704728
"http://{}",
@@ -718,6 +742,8 @@ impl MultipleMinerTest {
718742
submit_tx(&http_origin, &transfer_tx)
719743
}
720744

745+
/// Sends a transfer tx to the stacks node and waits for the stacks node to mine it
746+
/// Returns the txid of the transfer tx.
721747
pub fn send_and_mine_transfer_tx(&mut self, timeout_secs: u64) -> Result<String, String> {
722748
let stacks_height_before = self.get_peer_stacks_tip_height();
723749
let txid = self.send_transfer_tx();
@@ -727,18 +753,22 @@ impl MultipleMinerTest {
727753
Ok(txid)
728754
}
729755

756+
/// Return the Peer Info from node 1
730757
pub fn get_peer_info(&self) -> PeerInfo {
731758
self.signer_test.get_peer_info()
732759
}
733760

761+
/// Returns the peer info's reported stacks tip height from node 1
734762
pub fn get_peer_stacks_tip_height(&self) -> u64 {
735763
self.get_peer_info().stacks_tip_height
736764
}
737765

766+
/// Returns the peer stacks tip hash from node 1
738767
pub fn get_peer_stacks_tip(&self) -> BlockHeaderHash {
739768
self.get_peer_info().stacks_tip
740769
}
741770

771+
/// Ensures that miner 2 submits a commit pointing to the current view reported by the stacks node as expected
742772
pub fn submit_commit_miner_2(&mut self, sortdb: &SortitionDB) {
743773
if !self.rl2_counters.naka_skip_commit_op.get() {
744774
warn!("Miner 2's commit ops were not paused. This may result in no commit being submitted.");
@@ -780,6 +810,7 @@ impl MultipleMinerTest {
780810
self.rl2_counters.naka_skip_commit_op.set(true);
781811
}
782812

813+
/// Ensures that miner 1 submits a commit pointing to the current view reported by the stacks node as expected
783814
pub fn submit_commit_miner_1(&mut self, sortdb: &SortitionDB) {
784815
if !self
785816
.signer_test
@@ -842,6 +873,7 @@ impl MultipleMinerTest {
842873
.set(true);
843874
}
844875

876+
/// Shutdown the test harness
845877
pub fn shutdown(self) {
846878
info!("------------------------- Shutting Down Multiple Miners Test -------------------------");
847879
self.rl2_coord_channels
@@ -871,6 +903,8 @@ impl MultipleMinerTest {
871903
}
872904
}
873905

906+
/// Returns whether the last block in the test observer contains a tenure change
907+
/// transaction with the given cause.
874908
fn last_block_contains_tenure_change_tx(cause: TenureChangeCause) -> bool {
875909
let blocks = test_observer::get_blocks();
876910
let last_block = &blocks.last().unwrap();
@@ -888,16 +922,19 @@ fn last_block_contains_tenure_change_tx(cause: TenureChangeCause) -> bool {
888922
}
889923
}
890924

925+
/// Asserts that the last block in the test observer contains a tenure change with the given cause.
891926
fn verify_last_block_contains_tenure_change_tx(cause: TenureChangeCause) {
892927
assert!(last_block_contains_tenure_change_tx(cause));
893928
}
894929

930+
/// Verifies that the tip of the sortition database was won by the provided miner public key hash
895931
fn verify_sortition_winner(sortdb: &SortitionDB, miner_pkh: &Hash160) {
896932
let tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn()).unwrap();
897933
assert!(tip.sortition);
898934
assert_eq!(&tip.miner_pk_hash.unwrap(), miner_pkh);
899935
}
900936

937+
/// Waits for a tenure change transaction to be observed in the test_observer at the expected height
901938
fn wait_for_tenure_change_tx(
902939
timeout_secs: u64,
903940
cause: TenureChangeCause,
@@ -927,6 +964,8 @@ fn wait_for_tenure_change_tx(
927964
})
928965
}
929966

967+
/// Waits for a block proposal to be observed in the test_observer stackerdb chunks at the expected height
968+
/// and signed by the expected miner
930969
fn wait_for_block_proposal(
931970
timeout_secs: u64,
932971
expected_height: u64,
@@ -958,6 +997,8 @@ fn wait_for_block_proposal(
958997
proposed_block.ok_or_else(|| "Failed to find block proposal".to_string())
959998
}
960999

1000+
/// Waits for a BlockPushed to be observed in the test_observer stackerdb chunks for a block
1001+
/// with the provided signer signature hash
9611002
fn wait_for_block_pushed(
9621003
timeout_secs: u64,
9631004
block_signer_signature_hash: Sha512Trunc256Sum,
@@ -982,6 +1023,7 @@ fn wait_for_block_pushed(
9821023
block.ok_or_else(|| "Failed to find block pushed".to_string())
9831024
}
9841025

1026+
/// Waits for a block with the provided expected height to be proposed and pushed by the miner with the provided public key.
9851027
fn wait_for_block_pushed_by_miner_key(
9861028
timeout_secs: u64,
9871029
expected_height: u64,
@@ -991,6 +1033,8 @@ fn wait_for_block_pushed_by_miner_key(
9911033
wait_for_block_pushed(timeout_secs, block_proposed.header.signer_signature_hash())
9921034
}
9931035

1036+
/// Waits for >30% of num_signers block rejection to be observed in the test_observer stackerdb chunks for a block
1037+
/// with the provided signer signature hash
9941038
fn wait_for_block_global_rejection(
9951039
timeout_secs: u64,
9961040
block_signer_signature_hash: Sha512Trunc256Sum,
@@ -1019,6 +1063,8 @@ fn wait_for_block_global_rejection(
10191063
})
10201064
}
10211065

1066+
/// Waits for the provided number of block rejections to be observed in the test_observer stackerdb chunks for a block
1067+
/// with the provided signer signature hash
10221068
fn wait_for_block_rejections(
10231069
timeout_secs: u64,
10241070
block_signer_signature_hash: Sha512Trunc256Sum,
@@ -1047,6 +1093,8 @@ fn wait_for_block_rejections(
10471093
})
10481094
}
10491095

1096+
/// Waits for >70% of the provided signers to send an acceptance for a block
1097+
/// with the provided signer signature hash
10501098
pub fn wait_for_block_global_acceptance_from_signers(
10511099
timeout_secs: u64,
10521100
signer_signature_hash: &Sha512Trunc256Sum,
@@ -1077,6 +1125,8 @@ pub fn wait_for_block_global_acceptance_from_signers(
10771125
})
10781126
}
10791127

1128+
/// Waits for all of the provided signers to send an acceptance for a block
1129+
/// with the provided signer signature hash
10801130
pub fn wait_for_block_acceptance_from_signers(
10811131
timeout_secs: u64,
10821132
signer_signature_hash: &Sha512Trunc256Sum,
@@ -1106,6 +1156,8 @@ pub fn wait_for_block_acceptance_from_signers(
11061156
})
11071157
}
11081158

1159+
/// Waits for all of the provided signers to send a rejection for a block
1160+
/// with the provided signer signature hash
11091161
pub fn wait_for_block_rejections_from_signers(
11101162
timeout_secs: u64,
11111163
expected_signers: &[StacksPublicKey],
@@ -3113,22 +3165,18 @@ fn tenure_extend_after_idle_miner() {
31133165
.expect("Failed to mine the tenure change block");
31143166

31153167
// Now, wait for a block with a tenure change due to the new block
3116-
wait_for(30, || {
3117-
Ok(last_block_contains_tenure_change_tx(
3118-
TenureChangeCause::BlockFound,
3119-
))
3120-
})
3121-
.expect("Timed out waiting for a block with a tenure change");
3168+
wait_for_tenure_change_tx(30, TenureChangeCause::BlockFound, tip_height_before + 1)
3169+
.expect("Timed out waiting for a block with a tenure change");
31223170

31233171
info!("---- Waiting for a tenure extend ----");
31243172

31253173
TEST_IGNORE_SIGNERS.set(false);
31263174
// Now, wait for a block with a tenure extend
3127-
wait_for(miner_idle_timeout.as_secs() + 20, || {
3128-
Ok(last_block_contains_tenure_change_tx(
3129-
TenureChangeCause::Extended,
3130-
))
3131-
})
3175+
wait_for_tenure_change_tx(
3176+
miner_idle_timeout.as_secs() + 20,
3177+
TenureChangeCause::Extended,
3178+
tip_height_before + 1,
3179+
)
31323180
.expect("Timed out waiting for a block with a tenure extend");
31333181
signer_test.shutdown();
31343182
}
@@ -7837,8 +7885,6 @@ fn tenure_extend_after_2_bad_commits() {
78377885
wait_for_tenure_change_tx(30, TenureChangeCause::Extended, stacks_height_before + 1)
78387886
.expect("Failed to mine tenure extend tx");
78397887

7840-
verify_last_block_contains_tenure_change_tx(TenureChangeCause::Extended);
7841-
78427888
info!("------------------------- Miner 1 Mines Another Block -------------------------");
78437889
miners
78447890
.send_and_mine_transfer_tx(30)

0 commit comments

Comments
 (0)