Skip to content

Commit e4e5ab6

Browse files
Cannot use prepare phase length to determine reward set calculation
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 381398d commit e4e5ab6

File tree

2 files changed

+46
-8
lines changed
  • stacks-common/src/types
  • stackslib/src/chainstate/nakamoto/coordinator

2 files changed

+46
-8
lines changed

stacks-common/src/types/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,31 @@ impl StacksEpochId {
663663
self >= &StacksEpochId::Epoch30
664664
}
665665

666+
/// Does this epoch use the nakamoto reward set, or the epoch2 reward set?
667+
/// We use the epoch2 reward set in all pre-3.0 epochs.
668+
/// We also use the epoch2 reward set in the first 3.0 reward cycle.
669+
/// After that, we use the nakamoto reward set.
670+
pub fn uses_nakamoto_reward_set(
671+
&self,
672+
cur_reward_cycle: u64,
673+
first_epoch30_reward_cycle: u64,
674+
) -> bool {
675+
match self {
676+
StacksEpochId::Epoch10
677+
| StacksEpochId::Epoch20
678+
| StacksEpochId::Epoch2_05
679+
| StacksEpochId::Epoch21
680+
| StacksEpochId::Epoch22
681+
| StacksEpochId::Epoch23
682+
| StacksEpochId::Epoch24
683+
| StacksEpochId::Epoch25 => false,
684+
StacksEpochId::Epoch30
685+
| StacksEpochId::Epoch31
686+
| StacksEpochId::Epoch32
687+
| StacksEpochId::Epoch33 => cur_reward_cycle > first_epoch30_reward_cycle,
688+
}
689+
}
690+
666691
/// What is the coinbase (in uSTX) to award for the given burnchain height?
667692
/// Applies prior to SIP-029
668693
fn coinbase_reward_pre_sip029(

stackslib/src/chainstate/nakamoto/coordinator/mod.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,28 @@ pub fn load_nakamoto_reward_set<U: RewardSetProvider>(
362362
sort_db: &SortitionDB,
363363
provider: &U,
364364
) -> Result<Option<(RewardCycleInfo, StacksHeaderInfo)>, Error> {
365-
// If the prepare phase started in pre-Nakamoto, it should be using Epoch 2.5 reward
366-
// set calculation rules.
367365
let cycle_start_height = burnchain.nakamoto_first_block_of_cycle(reward_cycle);
368-
// This is safe to cast because a u32 always fits in a u64
369-
let prepare_phase_start =
370-
cycle_start_height.saturating_sub(u64::from(burnchain.pox_constants.prepare_length));
371-
let epoch_at_height = SortitionDB::get_stacks_epoch(sort_db.conn(), prepare_phase_start)?
372-
.unwrap_or_else(|| panic!("FATAL: no epoch defined for burn height {prepare_phase_start}"));
373-
if epoch_at_height.epoch_id < StacksEpochId::Epoch30 {
366+
let epoch_at_height = SortitionDB::get_stacks_epoch(sort_db.conn(), cycle_start_height)?
367+
.unwrap_or_else(|| panic!("FATAL: no epoch defined for burn height {cycle_start_height}"));
368+
let is_pre_naka_epoch = if epoch_at_height.epoch_id < StacksEpochId::Epoch30 {
369+
true
370+
} else {
371+
let epoch_30 =
372+
SortitionDB::get_stacks_epoch_by_epoch_id(sort_db.conn(), &StacksEpochId::Epoch30)?
373+
.unwrap_or_else(|| panic!("FATAL: no Nakamoto epoch defined"));
374+
// Find the first Stacks block in this reward cycle's preceding prepare phase.
375+
// This block will have invoked `.signers.stackerdb-set-signer-slots()` with the reward set.
376+
// Note that we may not have processed it yet. But, if we do find it, then it's
377+
// unique (and since Nakamoto Stacks blocks are processed in order, the anchor block
378+
// cannot change later).
379+
let first_epoch30_reward_cycle = burnchain
380+
.block_height_to_reward_cycle(epoch_30.start_height)
381+
.expect("FATAL: no reward cycle for epoch 3.0 start height");
382+
!epoch_at_height
383+
.epoch_id
384+
.uses_nakamoto_reward_set(reward_cycle, first_epoch30_reward_cycle)
385+
};
386+
if is_pre_naka_epoch {
374387
// in epoch 2.5, and in the first reward cycle of epoch 3.0, the reward set can *only* be found in the sortition DB.
375388
// The nakamoto chain-processing rules aren't active yet, so we can't look for the reward
376389
// cycle info in the nakamoto chain state.

0 commit comments

Comments
 (0)