@@ -28,8 +28,8 @@ use crate::chainstate::stacks::db::StacksChainState;
28
28
use crate :: net:: chat:: ConversationP2P ;
29
29
use crate :: net:: connection:: ConnectionOptions ;
30
30
use crate :: net:: download:: nakamoto:: {
31
- downloader_block_height_to_reward_cycle , AvailableTenures , NakamotoTenureDownloader ,
32
- NakamotoTenureDownloaderSet , NakamotoUnconfirmedTenureDownloader , TenureStartEnd , WantedTenure ,
31
+ AvailableTenures , NakamotoTenureDownloader , NakamotoTenureDownloaderSet ,
32
+ NakamotoUnconfirmedTenureDownloader , TenureStartEnd , WantedTenure ,
33
33
} ;
34
34
use crate :: net:: inv:: nakamoto:: NakamotoTenureInv ;
35
35
use crate :: net:: neighbors:: rpc:: NeighborRPC ;
@@ -122,6 +122,21 @@ impl NakamotoDownloadStateMachine {
122
122
}
123
123
}
124
124
125
+ /// Return the reward cycle which could be confirmed by a nakamoto block commit
126
+ /// in burn block height `burn_height`.
127
+ ///
128
+ /// Nakamoto block commits point at the parent of the tenure that
129
+ /// starts at `burn_height`. Therefore, a commit at height N
130
+ /// could confirm a tenure in the reward cycle active at height
131
+ /// `N-1`.
132
+ fn get_confirmable_reward_cycle (
133
+ pox_constants : & PoxConstants ,
134
+ first_burn_height : u64 ,
135
+ burn_height : u64 ,
136
+ ) -> Option < u64 > {
137
+ pox_constants. block_height_to_reward_cycle ( first_burn_height, burn_height. saturating_sub ( 1 ) )
138
+ }
139
+
125
140
/// Get a range of wanted tenures between two burnchain blocks.
126
141
/// Each wanted tenure's .processed flag will be set to false.
127
142
///
@@ -220,7 +235,13 @@ impl NakamotoDownloadStateMachine {
220
235
sortdb : & SortitionDB ,
221
236
loaded_so_far : & [ WantedTenure ] ,
222
237
) -> Result < Vec < WantedTenure > , NetError > {
223
- let tip_rc = downloader_block_height_to_reward_cycle (
238
+ // we want tenures that are *confirmable* from the current sortition tip.
239
+ // any miner commitment chosen in the sortition tip confirms a tenure with a lower
240
+ // block height, so only consider the reward cycle at height - 1.
241
+ // *Note: its possible that a the second or later sortition of a RC also confirms a tenure
242
+ // in the previous RC, but for the purposes of the wanted tenures calculations, we only
243
+ // are loading up wanted tenures in the current RC.
244
+ let tip_rc = Self :: get_confirmable_reward_cycle (
224
245
& sortdb. pox_constants ,
225
246
sortdb. first_block_height ,
226
247
tip. block_height ,
@@ -234,21 +255,17 @@ impl NakamotoDownloadStateMachine {
234
255
} else if let Some ( last_tip) = last_tip. as_ref ( ) {
235
256
last_tip. block_height . saturating_add ( 1 )
236
257
} else {
237
- // careful -- need .saturating_sub(1) since this calculation puts the reward cycle start at
238
- // block height 1 mod reward cycle len, but we really want 0 mod reward cycle len
239
258
sortdb
240
259
. pox_constants
241
- . reward_cycle_to_block_height ( sortdb. first_block_height , tip_rc)
242
- . saturating_sub ( 1 )
260
+ . nakamoto_first_block_of_cycle ( sortdb. first_block_height , tip_rc)
243
261
} ;
244
262
245
263
// be extra careful with last_block_height -- we not only account for the above, but also
246
264
// we need to account for the fact that `load_wanted_tenures` does not load the sortition
247
265
// of the last block height (but we want this!)
248
266
let last_block_height = sortdb
249
267
. pox_constants
250
- . reward_cycle_to_block_height ( sortdb. first_block_height , tip_rc. saturating_add ( 1 ) )
251
- . saturating_sub ( 1 )
268
+ . nakamoto_first_block_of_cycle ( sortdb. first_block_height , tip_rc. saturating_add ( 1 ) )
252
269
. min ( tip. block_height . saturating_add ( 1 ) ) ;
253
270
254
271
debug ! (
@@ -395,7 +412,7 @@ impl NakamotoDownloadStateMachine {
395
412
. expect ( "FATAL: usize cannot support reward cycle length" )
396
413
{
397
414
// this is the first-ever pass, so load up the last full reward cycle
398
- let prev_sort_rc = downloader_block_height_to_reward_cycle (
415
+ let prev_sort_rc = Self :: get_confirmable_reward_cycle (
399
416
& sortdb. pox_constants ,
400
417
sortdb. first_block_height ,
401
418
sort_tip. block_height ,
@@ -419,7 +436,7 @@ impl NakamotoDownloadStateMachine {
419
436
}
420
437
if self . wanted_tenures . is_empty ( ) {
421
438
// this is the first-ever pass, so load up the current reward cycle
422
- let sort_rc = downloader_block_height_to_reward_cycle (
439
+ let sort_rc = Self :: get_confirmable_reward_cycle (
423
440
& sortdb. pox_constants ,
424
441
sortdb. first_block_height ,
425
442
sort_tip. block_height ,
@@ -472,7 +489,7 @@ impl NakamotoDownloadStateMachine {
472
489
self . initialize_wanted_tenures ( sort_tip, sortdb) ?;
473
490
let last_sort_height_opt = self . last_sort_tip . as_ref ( ) . map ( |sn| sn. block_height ) ;
474
491
let last_sort_height = last_sort_height_opt. unwrap_or ( sort_tip. block_height ) ;
475
- let sort_rc = downloader_block_height_to_reward_cycle (
492
+ let sort_rc = Self :: get_confirmable_reward_cycle (
476
493
& sortdb. pox_constants ,
477
494
sortdb. first_block_height ,
478
495
last_sort_height,
0 commit comments