API for getting first and last slots in epoch (#4241)

automerge
This commit is contained in:
carllin 2019-05-09 16:06:57 -07:00 committed by Grimes
parent 575a0e318b
commit ab3c988146
1 changed files with 20 additions and 1 deletions

View File

@ -87,7 +87,6 @@ impl EpochSchedule {
pub fn get_stakers_epoch(&self, slot: u64) -> u64 { pub fn get_stakers_epoch(&self, slot: u64) -> u64 {
if slot < self.first_normal_slot { if slot < self.first_normal_slot {
// until we get to normal slots, behave as if stakers_slot_offset == slots_per_epoch // until we get to normal slots, behave as if stakers_slot_offset == slots_per_epoch
self.get_epoch_and_slot_index(slot).0 + 1 self.get_epoch_and_slot_index(slot).0 + 1
} else { } else {
self.first_normal_epoch self.first_normal_epoch
@ -117,6 +116,18 @@ impl EpochSchedule {
) )
} }
} }
pub fn get_first_slot_in_epoch(&self, epoch: u64) -> u64 {
if epoch <= self.first_normal_epoch {
(2u64.pow(epoch as u32) - 1) * MINIMUM_SLOT_LENGTH as u64
} else {
(epoch - self.first_normal_epoch) * self.slots_per_epoch + self.first_normal_slot
}
}
pub fn get_last_slot_in_epoch(&self, epoch: u64) -> u64 {
self.get_first_slot_in_epoch(epoch) + self.get_slots_in_epoch(epoch) - 1
}
} }
type BankStatusCache = StatusCache<Result<()>>; type BankStatusCache = StatusCache<Result<()>>;
@ -1749,6 +1760,12 @@ mod tests {
for slots_per_epoch in MINIMUM_SLOT_LENGTH as u64..=MINIMUM_SLOT_LENGTH as u64 * 16 { for slots_per_epoch in MINIMUM_SLOT_LENGTH as u64..=MINIMUM_SLOT_LENGTH as u64 * 16 {
let epoch_schedule = EpochSchedule::new(slots_per_epoch, slots_per_epoch / 2, true); let epoch_schedule = EpochSchedule::new(slots_per_epoch, slots_per_epoch / 2, true);
assert_eq!(epoch_schedule.get_first_slot_in_epoch(0), 0);
assert_eq!(
epoch_schedule.get_last_slot_in_epoch(0),
MINIMUM_SLOT_LENGTH as u64 - 1
);
let mut last_stakers = 0; let mut last_stakers = 0;
let mut last_epoch = 0; let mut last_epoch = 0;
let mut last_slots_in_epoch = MINIMUM_SLOT_LENGTH as u64; let mut last_slots_in_epoch = MINIMUM_SLOT_LENGTH as u64;
@ -1768,6 +1785,8 @@ mod tests {
if epoch != last_epoch { if epoch != last_epoch {
assert_eq!(epoch, last_epoch + 1); assert_eq!(epoch, last_epoch + 1);
last_epoch = epoch; last_epoch = epoch;
assert_eq!(epoch_schedule.get_first_slot_in_epoch(epoch), slot);
assert_eq!(epoch_schedule.get_last_slot_in_epoch(epoch - 1), slot - 1);
// verify that slots in an epoch double continuously // verify that slots in an epoch double continuously
// until they reach slots_per_epoch // until they reach slots_per_epoch