Aura and Slots refactoring (#8386)

* Make slot duration being exposed as `Duration` to the outside

* Some slot info love

* Add `build_aura_worker` utility function

* Copy copy copy
This commit is contained in:
Bastian Köcher
2021-03-18 00:25:58 +01:00
committed by GitHub
parent 0d6884b919
commit 15e15e7d8e
17 changed files with 188 additions and 68 deletions
+27 -9
View File
@@ -52,7 +52,7 @@ pub struct SlotInfo {
/// The slot number.
pub slot: Slot,
/// Current timestamp.
pub timestamp: u64,
pub timestamp: sp_timestamp::Timestamp,
/// The instant at which the slot ends.
pub ends_at: Instant,
/// The inherent data.
@@ -61,6 +61,26 @@ pub struct SlotInfo {
pub duration: Duration,
}
impl SlotInfo {
/// Create a new [`SlotInfo`].
///
/// `ends_at` is calculated using `timestamp` and `duration`.
pub fn new(
slot: Slot,
timestamp: sp_timestamp::Timestamp,
inherent_data: InherentData,
duration: Duration,
) -> Self {
Self {
slot,
timestamp,
inherent_data,
duration,
ends_at: Instant::now() + time_until_next(timestamp.as_duration(), duration),
}
}
}
/// A stream that returns every time there is a new slot.
pub(crate) struct Slots<SC> {
last_slot: Slot,
@@ -73,13 +93,13 @@ pub(crate) struct Slots<SC> {
impl<SC> Slots<SC> {
/// Create a new `Slots` stream.
pub fn new(
slot_duration: u64,
slot_duration: Duration,
inherent_data_providers: InherentDataProviders,
timestamp_extractor: SC,
) -> Self {
Slots {
last_slot: 0.into(),
slot_duration: Duration::from_millis(slot_duration),
slot_duration,
inner_delay: None,
inherent_data_providers,
timestamp_extractor,
@@ -122,21 +142,19 @@ impl<SC: SlotCompatible> Stream for Slots<SC> {
};
// reschedule delay for next slot.
let ends_in = offset +
time_until_next(Duration::from_millis(timestamp), slot_duration);
let ends_at = Instant::now() + ends_in;
time_until_next(timestamp.as_duration(), 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;
break Poll::Ready(Some(Ok(SlotInfo {
break Poll::Ready(Some(Ok(SlotInfo::new(
slot,
duration: self.slot_duration,
timestamp,
ends_at,
inherent_data,
})))
self.slot_duration,
))))
}
}
}