Skip to content

Commit

Permalink
Aura and Slots refactoring (paritytech#8386)
Browse files Browse the repository at this point in the history
* Make slot duration being exposed as `Duration` to the outside

* Some slot info love

* Add `build_aura_worker` utility function

* Copy copy copy
  • Loading branch information
bkchr authored and jordy25519 committed Sep 17, 2021
1 parent eede1ae commit 2562333
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ impl SlotCompatible for AuraSlotCompatible {
fn extract_timestamp_and_slot(
&self,
data: &InherentData,
) -> Result<(u64, AuraInherent, std::time::Duration), sp_consensus::Error> {
) -> Result<(sp_timestamp::Timestamp, AuraInherent, std::time::Duration), sp_consensus::Error> {
data.timestamp_inherent_data()
.and_then(|t| data.aura_inherent_data().map(|a| (t, a)))
.map_err(Into::into)
.map_err(sp_consensus::Error::InherentData)
.map(|(x, y)| (*x, y, Default::default()))
.map(|(x, y)| (x, y, Default::default()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,13 +919,13 @@ impl SlotCompatible for TimeSource {
fn extract_timestamp_and_slot(
&self,
data: &InherentData,
) -> Result<(u64, Slot, std::time::Duration), sp_consensus::Error> {
) -> Result<(sp_timestamp::Timestamp, Slot, std::time::Duration), sp_consensus::Error> {
trace!(target: "babe", "extract timestamp");
data.timestamp_inherent_data()
.and_then(|t| data.babe_inherent_data().map(|a| (t, a)))
.map_err(Into::into)
.map_err(sp_consensus::Error::InherentData)
.map(|(x, y)| (*x, y, self.0.lock().0.take().unwrap_or_default()))
.map(|(x, y)| (x, y, self.0.lock().0.take().unwrap_or_default()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/consensus/aura/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl ProvideInherentData for InherentDataProvider {
use sp_timestamp::TimestampInherentData;

let timestamp = inherent_data.timestamp_inherent_data()?;
let slot = *timestamp / self.slot_duration;
let slot = *timestamp / self.slot_duration.as_millis() as u64;
inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/consensus/babe/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ProvideInherentData for InherentDataProvider {

fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
let timestamp = inherent_data.timestamp_inherent_data()?;
let slot = *timestamp / self.slot_duration;
let slot = *timestamp / self.slot_duration.as_millis() as u64;
inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
}

Expand Down
9 changes: 7 additions & 2 deletions primitives/timestamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ impl Timestamp {
pub const fn new(inner: u64) -> Self {
Self(inner)
}

/// Returns `self` as [`Duration`].
pub fn as_duration(&self) -> Duration {
Duration::from_millis(self.0)
}
}

impl sp_std::ops::Deref for Timestamp {
Expand Down Expand Up @@ -101,8 +106,8 @@ impl From<Timestamp> for u64 {
}
}

impl From<sp_std::time::Duration> for Timestamp {
fn from(duration: sp_std::time::Duration) -> Self {
impl From<Duration> for Timestamp {
fn from(duration: Duration) -> Self {
Timestamp(duration.as_millis() as u64)
}
}
Expand Down

0 comments on commit 2562333

Please sign in to comment.