mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-19 11:11:03 +00:00
consensus-slots: cleanup SlotDuration config (#10878)
* consensus-slots: cleanup the SlotDuration config * fix tests * address review comments
This commit is contained in:
@@ -58,12 +58,11 @@ impl InherentDataProvider {
|
||||
|
||||
/// Creates the inherent data provider by calculating the slot from the given
|
||||
/// `timestamp` and `duration`.
|
||||
pub fn from_timestamp_and_duration(
|
||||
pub fn from_timestamp_and_slot_duration(
|
||||
timestamp: sp_timestamp::Timestamp,
|
||||
duration: std::time::Duration,
|
||||
slot_duration: sp_consensus_slots::SlotDuration,
|
||||
) -> Self {
|
||||
let slot =
|
||||
InherentType::from((timestamp.as_duration().as_millis() / duration.as_millis()) as u64);
|
||||
let slot = InherentType::from_timestamp(timestamp, slot_duration);
|
||||
|
||||
Self { slot }
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ pub mod ed25519 {
|
||||
pub type AuthorityId = app_ed25519::Public;
|
||||
}
|
||||
|
||||
pub use sp_consensus_slots::Slot;
|
||||
pub use sp_consensus_slots::{Slot, SlotDuration};
|
||||
|
||||
/// The `ConsensusEngineId` of AuRa.
|
||||
pub const AURA_ENGINE_ID: ConsensusEngineId = [b'a', b'u', b'r', b'a'];
|
||||
@@ -93,28 +93,3 @@ sp_api::decl_runtime_apis! {
|
||||
fn authorities() -> Vec<AuthorityId>;
|
||||
}
|
||||
}
|
||||
|
||||
/// Aura slot duration.
|
||||
///
|
||||
/// Internally stored as milliseconds.
|
||||
#[derive(sp_runtime::RuntimeDebug, Encode, Decode, PartialEq, Clone, Copy)]
|
||||
pub struct SlotDuration(u64);
|
||||
|
||||
impl SlotDuration {
|
||||
/// Initialize from the given milliseconds.
|
||||
pub fn from_millis(val: u64) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
|
||||
/// Returns the slot duration in milli seconds.
|
||||
pub fn get(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl sp_consensus::SlotData for SlotDuration {
|
||||
fn slot_duration(&self) -> std::time::Duration {
|
||||
std::time::Duration::from_millis(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
//! Inherents for BABE
|
||||
|
||||
use sp_inherents::{Error, InherentData, InherentIdentifier};
|
||||
|
||||
use sp_std::result::Result;
|
||||
|
||||
/// The BABE inherent identifier.
|
||||
@@ -60,12 +59,11 @@ impl InherentDataProvider {
|
||||
|
||||
/// Creates the inherent data provider by calculating the slot from the given
|
||||
/// `timestamp` and `duration`.
|
||||
pub fn from_timestamp_and_duration(
|
||||
pub fn from_timestamp_and_slot_duration(
|
||||
timestamp: sp_timestamp::Timestamp,
|
||||
duration: std::time::Duration,
|
||||
slot_duration: sp_consensus_slots::SlotDuration,
|
||||
) -> Self {
|
||||
let slot =
|
||||
InherentType::from((timestamp.as_duration().as_millis() / duration.as_millis()) as u64);
|
||||
let slot = InherentType::from_timestamp(timestamp, slot_duration);
|
||||
|
||||
Self { slot }
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ pub const MEDIAN_ALGORITHM_CARDINALITY: usize = 1200; // arbitrary suggestion by
|
||||
/// The index of an authority.
|
||||
pub type AuthorityIndex = u32;
|
||||
|
||||
pub use sp_consensus_slots::Slot;
|
||||
pub use sp_consensus_slots::{Slot, SlotDuration};
|
||||
|
||||
/// An equivocation proof for multiple block authorships on the same slot (i.e. double vote).
|
||||
pub type EquivocationProof<H> = sp_consensus_slots::EquivocationProof<H, AuthorityId>;
|
||||
@@ -237,13 +237,6 @@ impl AllowedSlots {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl sp_consensus::SlotData for BabeGenesisConfiguration {
|
||||
fn slot_duration(&self) -> std::time::Duration {
|
||||
std::time::Duration::from_millis(self.slot_duration)
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration data used by the BABE consensus engine.
|
||||
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
|
||||
@@ -327,9 +327,3 @@ impl<Block: BlockT> CanAuthorWith<Block> for NeverCanAuthor {
|
||||
Err("Authoring is always disabled.".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// A type from which a slot duration can be obtained.
|
||||
pub trait SlotData {
|
||||
/// Gets the slot duration.
|
||||
fn slot_duration(&self) -> sp_std::time::Duration;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
|
||||
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
sp-runtime = { version = "5.0.0", default-features = false, path = "../../runtime" }
|
||||
sp-arithmetic = { version = "4.0.0", default-features = false, path = "../../arithmetic" }
|
||||
sp-runtime = { version = "5.0.0", default-features = false, path = "../../runtime" }
|
||||
sp-std = { version = "4.0.0", default-features = false, path = "../../std" }
|
||||
sp-timestamp = { version = "4.0.0-dev", default-features = false, path = "../../timestamp" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -25,6 +27,8 @@ std = [
|
||||
"codec/std",
|
||||
"scale-info/std",
|
||||
"serde",
|
||||
"sp-runtime/std",
|
||||
"sp-arithmetic/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"sp-timestamp/std",
|
||||
]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -42,10 +42,16 @@ impl Timestamp {
|
||||
}
|
||||
|
||||
/// Returns `self` as [`Duration`].
|
||||
pub fn as_duration(self) -> Duration {
|
||||
pub const fn as_duration(self) -> Duration {
|
||||
Duration::from_millis(self.0)
|
||||
}
|
||||
|
||||
/// Returns `self` as a `u64` representing the elapsed time since the UNIX_EPOCH in
|
||||
/// milliseconds.
|
||||
pub const fn as_millis(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Checked subtraction that returns `None` on an underflow.
|
||||
pub fn checked_sub(self, other: Self) -> Option<Self> {
|
||||
self.0.checked_sub(other.0).map(Self)
|
||||
|
||||
Reference in New Issue
Block a user