Merge latest from branch clock-disparity

* Account for genesis time

* Use checked mul

* Account for genesis slot

* Change API

* Refactor "duration to..." functions
This commit is contained in:
Paul Hauner
2020-03-18 11:51:29 +11:00
committed by Age Manning
parent 6ca4f4709b
commit 3606f0447d
3 changed files with 219 additions and 41 deletions

View File

@@ -40,4 +40,18 @@ pub trait SlotClock: Send + Sync + Sized {
/// Returns the duration until the first slot of the next epoch.
fn duration_to_next_epoch(&self, slots_per_epoch: u64) -> Option<Duration>;
/// Returns the first slot to be returned at the genesis time.
fn genesis_slot(&self) -> Slot;
/// Returns the slot if the internal clock were advanced by `duration`.
fn now_with_future_tolerance(&self, tolerance: Duration) -> Option<Slot> {
self.slot_of(self.now_duration()?.checked_add(tolerance)?)
}
/// Returns the slot if the internal clock were reversed by `duration`.
fn now_with_past_tolerance(&self, tolerance: Duration) -> Option<Slot> {
self.slot_of(self.now_duration()?.checked_sub(tolerance)?)
.or_else(|| Some(self.genesis_slot()))
}
}