@@ -677,7 +677,7 @@ impl RelayerThread {
677
677
& last_winning_snapshot. consensus_hash
678
678
) ;
679
679
680
- if self . need_block_found ( & canonical_stacks_snapshot, & last_winning_snapshot) {
680
+ if Self :: need_block_found ( & canonical_stacks_snapshot, & last_winning_snapshot) {
681
681
info ! (
682
682
"Relayer: will submit late BlockFound for {}" ,
683
683
& last_winning_snapshot. consensus_hash
@@ -738,7 +738,6 @@ impl RelayerThread {
738
738
/// Returns true if the stacks tip's snapshot is an ancestor of the last-won sortition
739
739
/// Returns false otherwise.
740
740
fn need_block_found (
741
- & mut self ,
742
741
canonical_stacks_snapshot : & BlockSnapshot ,
743
742
last_winning_snapshot : & BlockSnapshot ,
744
743
) -> bool {
@@ -1838,7 +1837,7 @@ impl RelayerThread {
1838
1837
let won_last_winning_snapshot =
1839
1838
last_winning_snapshot. miner_pk_hash == Some ( mining_pkh) ;
1840
1839
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)
1842
1841
{
1843
1842
info ! ( "Will not tenure extend yet -- need to issue a BlockFound first" ) ;
1844
1843
// We may manage to extend later, so don't set the timer to None.
@@ -2059,6 +2058,10 @@ pub mod test {
2059
2058
use std:: io:: Write ;
2060
2059
use std:: path:: Path ;
2061
2060
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 } ;
2062
2065
use stacks:: util:: hash:: Hash160 ;
2063
2066
use stacks:: util:: secp256k1:: Secp256k1PublicKey ;
2064
2067
use stacks:: util:: vrf:: VRFPublicKey ;
@@ -2169,4 +2172,80 @@ pub mod test {
2169
2172
2170
2173
std:: fs:: remove_file ( path) . expect ( "Failed to delete test file" ) ;
2171
2174
}
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
+ }
2172
2251
}
0 commit comments