From e1caa2979f21340921ed1c92463e9f8cb5b7d8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 7 May 2021 00:15:08 +0200 Subject: [PATCH] Fix the calculation of the time until the next slot (#8753) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix the calculation of the time until the next slot * Update client/consensus/slots/src/slots.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> --- substrate/client/consensus/slots/src/slots.rs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/substrate/client/consensus/slots/src/slots.rs b/substrate/client/consensus/slots/src/slots.rs index b5ce71dfbf..665f7c58ba 100644 --- a/substrate/client/consensus/slots/src/slots.rs +++ b/substrate/client/consensus/slots/src/slots.rs @@ -40,11 +40,12 @@ pub fn duration_now() -> Duration { } /// Returns the duration until the next slot from now. -pub fn time_until_next(slot_duration: Duration) -> Duration { - let remaining_full_millis = slot_duration.as_millis() - - (duration_now().as_millis() % slot_duration.as_millis()) - - 1; - Duration::from_millis(remaining_full_millis as u64) +pub fn time_until_next_slot(slot_duration: Duration) -> Duration { + let now = duration_now().as_millis(); + + let next_slot = (now + slot_duration.as_millis()) / slot_duration.as_millis(); + let remaining_millis = next_slot * slot_duration.as_millis() - now; + Duration::from_millis(remaining_millis as u64) } /// Information about a slot. @@ -86,7 +87,7 @@ impl SlotInfo { duration, chain_head, block_size_limit, - ends_at: Instant::now() + time_until_next(duration), + ends_at: Instant::now() + time_until_next_slot(duration), } } } @@ -132,7 +133,7 @@ where self.inner_delay = match self.inner_delay.take() { None => { // schedule wait. - let wait_dur = time_until_next(self.slot_duration); + let wait_dur = time_until_next_slot(self.slot_duration); Some(Delay::new(wait_dur)) } Some(d) => Some(d), @@ -143,7 +144,12 @@ where } // timeout has fired. - let ends_at = Instant::now() + time_until_next(self.slot_duration); + let ends_in = time_until_next_slot(self.slot_duration); + + // reschedule delay for next slot. + self.inner_delay = Some(Delay::new(ends_in)); + + let ends_at = Instant::now() + ends_in; let chain_head = match self.client.best_chain() { Ok(x) => x, @@ -174,10 +180,6 @@ where let slot = inherent_data_providers.slot(); let inherent_data = inherent_data_providers.create_inherent_data()?; - // reschedule delay for next slot. - let ends_in = time_until_next(self.slot_duration); - self.inner_delay = Some(Delay::new(ends_in)); - // never yield the same slot twice. if slot > self.last_slot { self.last_slot = slot;