Allow slot clock to work on genesis (#573)

* Allow slot clock to work on genesis

* Loose over-strict requirements for slot clock tests
This commit is contained in:
Paul Hauner 2019-10-29 12:51:32 +11:00 committed by GitHub
parent a488c4dccd
commit 2c6b40be78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,10 +29,10 @@ impl SlotClock for SystemTimeSlotClock {
let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?; let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?;
let genesis = self.genesis_duration; let genesis = self.genesis_duration;
if now > genesis { if now >= genesis {
let since_genesis = now let since_genesis = now
.checked_sub(genesis) .checked_sub(genesis)
.expect("Control flow ensures now is greater than genesis"); .expect("Control flow ensures now is greater than or equal to genesis");
let slot = let slot =
Slot::from((since_genesis.as_millis() / self.slot_duration.as_millis()) as u64); Slot::from((since_genesis.as_millis() / self.slot_duration.as_millis()) as u64);
Some(slot + self.genesis_slot) Some(slot + self.genesis_slot)
@ -50,7 +50,7 @@ impl SlotClock for SystemTimeSlotClock {
genesis + slot * self.slot_duration genesis + slot * self.slot_duration
}; };
if now > genesis { if now >= genesis {
Some( Some(
slot_start(self.now()? + 1) slot_start(self.now()? + 1)
.checked_sub(now) .checked_sub(now)
@ -100,12 +100,12 @@ mod tests {
let clock = let clock =
SystemTimeSlotClock::new(genesis_slot, prior_genesis(500), Duration::from_secs(1)); SystemTimeSlotClock::new(genesis_slot, prior_genesis(500), Duration::from_secs(1));
assert_eq!(clock.now(), Some(Slot::new(0))); assert_eq!(clock.now(), Some(Slot::new(0)));
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500)); assert!(clock.duration_to_next_slot().unwrap() <= Duration::from_millis(500));
let clock = let clock =
SystemTimeSlotClock::new(genesis_slot, prior_genesis(1_500), Duration::from_secs(1)); SystemTimeSlotClock::new(genesis_slot, prior_genesis(1_500), Duration::from_secs(1));
assert_eq!(clock.now(), Some(Slot::new(1))); assert_eq!(clock.now(), Some(Slot::new(1)));
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500)); assert!(clock.duration_to_next_slot().unwrap() <= Duration::from_millis(500));
} }
#[test] #[test]