consensus-slots: cleanup SlotDuration config (#10878)

* consensus-slots: cleanup the SlotDuration config

* fix tests

* address review comments
This commit is contained in:
André Silva
2022-02-22 19:39:16 +00:00
committed by GitHub
parent f00404bfd6
commit 2561e11ed7
19 changed files with 161 additions and 191 deletions
@@ -21,6 +21,7 @@
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_timestamp::Timestamp;
/// Unit type wrapper that represents a slot.
#[derive(Debug, Encode, MaxEncodedLen, Decode, Eq, Clone, Copy, Default, Ord, TypeInfo)]
@@ -64,6 +65,11 @@ impl<T: Into<u64> + Copy> core::cmp::PartialOrd<T> for Slot {
}
impl Slot {
/// Create a new slot by calculating it from the given timestamp and slot duration.
pub const fn from_timestamp(timestamp: Timestamp, slot_duration: SlotDuration) -> Self {
Slot(timestamp.as_millis() / slot_duration.as_millis())
}
/// Saturating addition.
pub fn saturating_add<T: Into<u64>>(self, rhs: T) -> Self {
Self(self.0.saturating_add(rhs.into()))
@@ -94,6 +100,32 @@ impl From<Slot> for u64 {
}
}
/// A slot duration defined in milliseconds.
#[derive(Clone, Copy, Debug, Encode, Decode, Hash, PartialOrd, Ord, PartialEq, Eq, TypeInfo)]
pub struct SlotDuration(u64);
impl SlotDuration {
/// Initialize from the given milliseconds.
pub const fn from_millis(millis: u64) -> Self {
Self(millis)
}
}
impl SlotDuration {
/// Returns `self` as a `u64` representing the duration in milliseconds.
pub const fn as_millis(&self) -> u64 {
self.0
}
}
#[cfg(feature = "std")]
impl SlotDuration {
/// Returns `self` as [`sp_std::time::Duration`].
pub const fn as_duration(&self) -> sp_std::time::Duration {
sp_std::time::Duration::from_millis(self.0)
}
}
/// Represents an equivocation proof. An equivocation happens when a validator
/// produces more than one block on the same slot. The proof of equivocation
/// are the given distinct headers that were signed by the validator and which