Expose SlotHistory::oldest() -> Slot (#10799)

This commit is contained in:
Ryo Onodera 2020-06-25 18:23:31 +09:00 committed by GitHub
parent b3e382ab3f
commit 9f5bd5fe81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -65,7 +65,7 @@ impl SlotHistory {
pub fn check(&self, slot: Slot) -> Check { pub fn check(&self, slot: Slot) -> Check {
if slot >= self.next_slot { if slot >= self.next_slot {
Check::Future Check::Future
} else if self.next_slot - slot > MAX_ENTRIES { } else if slot < self.oldest() {
Check::TooOld Check::TooOld
} else if self.bits.get(slot % MAX_ENTRIES) { } else if self.bits.get(slot % MAX_ENTRIES) {
Check::Found Check::Found
@ -73,6 +73,10 @@ impl SlotHistory {
Check::NotFound Check::NotFound
} }
} }
pub fn oldest(&self) -> Slot {
self.next_slot.saturating_sub(MAX_ENTRIES)
}
} }
#[cfg(test)] #[cfg(test)]
@ -183,4 +187,16 @@ mod tests {
assert_eq!(slot_history.check(6), Check::Future); assert_eq!(slot_history.check(6), Check::Future);
assert_eq!(slot_history.check(11), Check::Future); assert_eq!(slot_history.check(11), Check::Future);
} }
#[test]
fn test_oldest() {
let mut slot_history = SlotHistory::default();
assert_eq!(slot_history.oldest(), 0);
slot_history.add(10);
assert_eq!(slot_history.oldest(), 0);
slot_history.add(MAX_ENTRIES - 1);
assert_eq!(slot_history.oldest(), 0);
slot_history.add(MAX_ENTRIES);
assert_eq!(slot_history.oldest(), 1);
}
} }