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
+4 -7
View File
@@ -274,7 +274,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
CONSENSUS_DEBUG;
"slots.starting_authorship";
"slot_num" => *slot,
"timestamp" => timestamp,
"timestamp" => *timestamp,
);
let awaiting_proposer = {
@@ -408,7 +408,7 @@ pub trait SlotCompatible {
fn extract_timestamp_and_slot(
&self,
inherent: &InherentData,
) -> Result<(u64, Slot, std::time::Duration), sp_consensus::Error>;
) -> Result<(sp_timestamp::Timestamp, Slot, std::time::Duration), sp_consensus::Error>;
}
/// Start a new slot worker.
@@ -514,10 +514,7 @@ impl<T> Deref for SlotDuration<T> {
}
impl<T: SlotData> SlotData for SlotDuration<T> {
/// Get the slot duration in milliseconds.
fn slot_duration(&self) -> u64
where T: SlotData,
{
fn slot_duration(&self) -> std::time::Duration {
self.0.slot_duration()
}
@@ -562,7 +559,7 @@ impl<T: Clone + Send + Sync + 'static> SlotDuration<T> {
}
}?;
if slot_duration.slot_duration() == 0u64 {
if slot_duration.slot_duration() == Default::default() {
return Err(sp_blockchain::Error::Application(Box::new(Error::SlotDurationInvalid(slot_duration))))
}
+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,
))))
}
}
}