From 9f5bd5fe81be011b09621090db0a29b5f65bee04 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Thu, 25 Jun 2020 18:23:31 +0900 Subject: [PATCH] Expose SlotHistory::oldest() -> Slot (#10799) --- sdk/src/slot_history.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/sdk/src/slot_history.rs b/sdk/src/slot_history.rs index 695669aafd..4765fab704 100644 --- a/sdk/src/slot_history.rs +++ b/sdk/src/slot_history.rs @@ -65,7 +65,7 @@ impl SlotHistory { pub fn check(&self, slot: Slot) -> Check { if slot >= self.next_slot { Check::Future - } else if self.next_slot - slot > MAX_ENTRIES { + } else if slot < self.oldest() { Check::TooOld } else if self.bits.get(slot % MAX_ENTRIES) { Check::Found @@ -73,6 +73,10 @@ impl SlotHistory { Check::NotFound } } + + pub fn oldest(&self) -> Slot { + self.next_slot.saturating_sub(MAX_ENTRIES) + } } #[cfg(test)] @@ -183,4 +187,16 @@ mod tests { assert_eq!(slot_history.check(6), 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); + } }