Skip to content

Commit 0e694a5

Browse files
committed
CRC: add a unit test for need_block_found
Signed-off-by: Jacinta Ferrant <jacinta@trustmachines.co>
1 parent 3144969 commit 0e694a5

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

testnet/stacks-node/src/nakamoto_node/relayer.rs

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl RelayerThread {
677677
&last_winning_snapshot.consensus_hash
678678
);
679679

680-
if self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot) {
680+
if Self::need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot) {
681681
info!(
682682
"Relayer: will submit late BlockFound for {}",
683683
&last_winning_snapshot.consensus_hash
@@ -738,7 +738,6 @@ impl RelayerThread {
738738
/// Returns true if the stacks tip's snapshot is an ancestor of the last-won sortition
739739
/// Returns false otherwise.
740740
fn need_block_found(
741-
&mut self,
742741
canonical_stacks_snapshot: &BlockSnapshot,
743742
last_winning_snapshot: &BlockSnapshot,
744743
) -> bool {
@@ -1838,7 +1837,7 @@ impl RelayerThread {
18381837
let won_last_winning_snapshot =
18391838
last_winning_snapshot.miner_pk_hash == Some(mining_pkh);
18401839
if won_last_winning_snapshot
1841-
&& self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot)
1840+
&& Self::need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot)
18421841
{
18431842
info!("Will not tenure extend yet -- need to issue a BlockFound first");
18441843
// We may manage to extend later, so don't set the timer to None.
@@ -2059,6 +2058,10 @@ pub mod test {
20592058
use std::io::Write;
20602059
use std::path::Path;
20612060

2061+
use rand::{thread_rng, Rng};
2062+
use stacks::burnchains::Txid;
2063+
use stacks::chainstate::burn::{BlockSnapshot, ConsensusHash, OpsHash, SortitionHash};
2064+
use stacks::types::chainstate::{BlockHeaderHash, BurnchainHeaderHash, SortitionId, TrieHash};
20622065
use stacks::util::hash::Hash160;
20632066
use stacks::util::secp256k1::Secp256k1PublicKey;
20642067
use stacks::util::vrf::VRFPublicKey;
@@ -2169,4 +2172,80 @@ pub mod test {
21692172

21702173
std::fs::remove_file(path).expect("Failed to delete test file");
21712174
}
2175+
2176+
#[test]
2177+
fn check_need_block_found() {
2178+
let consensus_hash_byte = thread_rng().gen();
2179+
let canonical_stacks_snapshot = BlockSnapshot {
2180+
block_height: thread_rng().gen::<u64>().wrapping_add(1), // Add one to ensure we can always decrease by 1 without underflowing.
2181+
burn_header_timestamp: thread_rng().gen(),
2182+
burn_header_hash: BurnchainHeaderHash([thread_rng().gen(); 32]),
2183+
consensus_hash: ConsensusHash([consensus_hash_byte; 20]),
2184+
parent_burn_header_hash: BurnchainHeaderHash([thread_rng().gen(); 32]),
2185+
ops_hash: OpsHash([thread_rng().gen(); 32]),
2186+
total_burn: thread_rng().gen(),
2187+
sortition: true,
2188+
sortition_hash: SortitionHash([thread_rng().gen(); 32]),
2189+
winning_block_txid: Txid([thread_rng().gen(); 32]),
2190+
winning_stacks_block_hash: BlockHeaderHash([thread_rng().gen(); 32]),
2191+
index_root: TrieHash([thread_rng().gen(); 32]),
2192+
num_sortitions: thread_rng().gen(),
2193+
stacks_block_accepted: true,
2194+
stacks_block_height: thread_rng().gen(),
2195+
arrival_index: thread_rng().gen(),
2196+
canonical_stacks_tip_consensus_hash: ConsensusHash([thread_rng().gen(); 20]),
2197+
canonical_stacks_tip_hash: BlockHeaderHash([thread_rng().gen(); 32]),
2198+
canonical_stacks_tip_height: thread_rng().gen(),
2199+
sortition_id: SortitionId([thread_rng().gen(); 32]),
2200+
parent_sortition_id: SortitionId([thread_rng().gen(); 32]),
2201+
pox_valid: true,
2202+
accumulated_coinbase_ustx: thread_rng().gen::<u64>() as u128,
2203+
miner_pk_hash: Some(Hash160([thread_rng().gen(); 20])),
2204+
};
2205+
2206+
// The consensus_hashes are the same, and the block heights are the same. Therefore, don't need a block found.
2207+
let last_winning_block_snapshot = canonical_stacks_snapshot.clone();
2208+
assert!(!RelayerThread::need_block_found(
2209+
&canonical_stacks_snapshot,
2210+
&last_winning_block_snapshot
2211+
));
2212+
2213+
// The block height of the canonical tip is higher than the last winning snapshot. We already issued a block found.
2214+
let mut canonical_stacks_snapshot_is_higher_than_last_winning_snapshot =
2215+
last_winning_block_snapshot.clone();
2216+
canonical_stacks_snapshot_is_higher_than_last_winning_snapshot.block_height =
2217+
canonical_stacks_snapshot.block_height.saturating_sub(1);
2218+
assert!(!RelayerThread::need_block_found(
2219+
&canonical_stacks_snapshot,
2220+
&canonical_stacks_snapshot_is_higher_than_last_winning_snapshot
2221+
));
2222+
2223+
// The block height is the same, but we have different consensus hashes. We need to issue a block found.
2224+
let mut tip_consensus_hash_mismatch = last_winning_block_snapshot.clone();
2225+
tip_consensus_hash_mismatch.consensus_hash =
2226+
ConsensusHash([consensus_hash_byte.wrapping_add(1); 20]);
2227+
assert!(RelayerThread::need_block_found(
2228+
&canonical_stacks_snapshot,
2229+
&tip_consensus_hash_mismatch
2230+
));
2231+
2232+
// The block height is the same, but we have different consensus hashes. We need to issue a block found.
2233+
let mut tip_consensus_hash_mismatch = last_winning_block_snapshot.clone();
2234+
tip_consensus_hash_mismatch.consensus_hash =
2235+
ConsensusHash([consensus_hash_byte.wrapping_add(1); 20]);
2236+
assert!(RelayerThread::need_block_found(
2237+
&canonical_stacks_snapshot,
2238+
&tip_consensus_hash_mismatch
2239+
));
2240+
2241+
// The block height of the canonical tip is lower than the last winning snapshot blockheight. We need to issue a block found.
2242+
let mut canonical_stacks_snapshot_is_lower_than_last_winning_snapshot =
2243+
last_winning_block_snapshot.clone();
2244+
canonical_stacks_snapshot_is_lower_than_last_winning_snapshot.block_height =
2245+
canonical_stacks_snapshot.block_height.saturating_add(1);
2246+
assert!(RelayerThread::need_block_found(
2247+
&canonical_stacks_snapshot,
2248+
&canonical_stacks_snapshot_is_lower_than_last_winning_snapshot
2249+
));
2250+
}
21722251
}

0 commit comments

Comments
 (0)