Make consensus SlotWorker don't assume a slot is time / duration (#7441)

* Make consensus `SlotWorker` don't assume a slot is time / duration

This removes the last bit of assumption that a slot is always `time /
duration`. This will be required by parachains where a slot will be the
relay chain block number. Besides this there are also some other drive
by changes. One more notable is that `on_slot` now returns a
`SlotResult` that holds the block and a potential storage proof.

To simplify the implementation and usage of the `SimpleSlotWorker` the
`SlotWorker` trait is now implemented for each type that implements
`SimpleSlotWorker`.

* Update client/consensus/slots/src/lib.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/slots/src/lib.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-10-28 00:01:06 +01:00
committed by GitHub
parent e4cfb2556d
commit 05af334bbc
7 changed files with 99 additions and 143 deletions
+1 -30
View File
@@ -37,30 +37,6 @@ pub fn duration_now() -> Duration {
))
}
/// A `Duration` with a sign (before or after). Immutable.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SignedDuration {
offset: Duration,
is_positive: bool,
}
impl SignedDuration {
/// Construct a `SignedDuration`
pub fn new(offset: Duration, is_positive: bool) -> Self {
Self { offset, is_positive }
}
/// Get the slot for now. Panics if `slot_duration` is 0.
pub fn slot_now(&self, slot_duration: u64) -> u64 {
(if self.is_positive {
duration_now() + self.offset
} else {
duration_now() - self.offset
}.as_millis() as u64) / slot_duration
}
}
/// Returns the duration until the next slot, based on current duration since
pub fn time_until_next(now: Duration, slot_duration: u64) -> Duration {
let remaining_full_millis = slot_duration - (now.as_millis() as u64 % slot_duration) - 1;
@@ -71,8 +47,6 @@ pub fn time_until_next(now: Duration, slot_duration: u64) -> Duration {
pub struct SlotInfo {
/// The slot number.
pub number: u64,
/// The last slot number produced.
pub last_number: u64,
/// Current timestamp.
pub timestamp: u64,
/// The instant at which the slot ends.
@@ -150,13 +124,11 @@ impl<SC: SlotCompatible> Stream for Slots<SC> {
// never yield the same slot twice.
if slot_num > self.last_slot {
let last_slot = self.last_slot;
self.last_slot = slot_num;
break Poll::Ready(Some(Ok(SlotInfo {
number: slot_num,
duration: self.slot_duration,
last_number: last_slot,
timestamp,
ends_at,
inherent_data,
@@ -166,5 +138,4 @@ impl<SC: SlotCompatible> Stream for Slots<SC> {
}
}
impl<SC> Unpin for Slots<SC> {
}
impl<SC> Unpin for Slots<SC> {}