mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 19:51:05 +00:00
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:
@@ -21,6 +21,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../runtim
|
||||
sp-inherents = { version = "3.0.0", default-features = false, path = "../../inherents" }
|
||||
sp-timestamp = { version = "3.0.0", default-features = false, path = "../../timestamp" }
|
||||
sp-consensus-slots = { version = "0.9.0", default-features = false, path = "../slots" }
|
||||
sp-consensus = { version = "0.9.0", path = "../common", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -32,4 +33,6 @@ std = [
|
||||
"sp-runtime/std",
|
||||
"sp-inherents/std",
|
||||
"sp-timestamp/std",
|
||||
"sp-consensus-slots/std",
|
||||
"sp-consensus",
|
||||
]
|
||||
|
||||
@@ -51,12 +51,12 @@ impl AuraInherentData for InherentData {
|
||||
// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider {
|
||||
slot_duration: u64,
|
||||
slot_duration: std::time::Duration,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProvider {
|
||||
pub fn new(slot_duration: u64) -> Self {
|
||||
pub fn new(slot_duration: std::time::Duration) -> Self {
|
||||
Self {
|
||||
slot_duration
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -84,14 +84,39 @@ pub enum ConsensusLog<AuthorityId: Codec> {
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API necessary for block authorship with aura.
|
||||
pub trait AuraApi<AuthorityId: Codec> {
|
||||
/// Return the slot duration in seconds for Aura.
|
||||
/// Currently, only the value provided by this type at genesis
|
||||
/// will be used.
|
||||
/// Returns the slot duration for Aura.
|
||||
///
|
||||
/// Dynamic slot duration may be supported in the future.
|
||||
fn slot_duration() -> u64;
|
||||
/// Currently, only the value provided by this type at genesis will be used.
|
||||
fn slot_duration() -> SlotDuration;
|
||||
|
||||
// Return the current set of authorities.
|
||||
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)
|
||||
}
|
||||
|
||||
const SLOT_KEY: &'static [u8] = b"aura_slot_duration";
|
||||
}
|
||||
|
||||
@@ -55,13 +55,13 @@ impl BabeInherentData for InherentData {
|
||||
// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029
|
||||
#[cfg(feature = "std")]
|
||||
pub struct InherentDataProvider {
|
||||
slot_duration: u64,
|
||||
slot_duration: std::time::Duration,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl InherentDataProvider {
|
||||
/// Constructs `Self`
|
||||
pub fn new(slot_duration: u64) -> Self {
|
||||
pub fn new(slot_duration: std::time::Duration) -> Self {
|
||||
Self { slot_duration }
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -242,8 +242,8 @@ impl AllowedSlots {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl sp_consensus::SlotData for BabeGenesisConfiguration {
|
||||
fn slot_duration(&self) -> u64 {
|
||||
self.slot_duration
|
||||
fn slot_duration(&self) -> std::time::Duration {
|
||||
std::time::Duration::from_millis(self.slot_duration)
|
||||
}
|
||||
|
||||
const SLOT_KEY: &'static [u8] = b"babe_configuration";
|
||||
|
||||
@@ -303,16 +303,8 @@ impl<Block: BlockT> CanAuthorWith<Block> for NeverCanAuthor {
|
||||
/// A type from which a slot duration can be obtained.
|
||||
pub trait SlotData {
|
||||
/// Gets the slot duration.
|
||||
fn slot_duration(&self) -> u64;
|
||||
fn slot_duration(&self) -> sp_std::time::Duration;
|
||||
|
||||
/// The static slot key
|
||||
const SLOT_KEY: &'static [u8];
|
||||
}
|
||||
|
||||
impl SlotData for u64 {
|
||||
fn slot_duration(&self) -> u64 {
|
||||
*self
|
||||
}
|
||||
|
||||
const SLOT_KEY: &'static [u8] = b"aura_slot_duration";
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ use codec::{Encode, Decode};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_inherents::ProvideInherentData;
|
||||
use sp_inherents::{InherentIdentifier, IsFatalError, InherentData};
|
||||
use sp_std::time::Duration;
|
||||
|
||||
use sp_runtime::RuntimeString;
|
||||
|
||||
@@ -43,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 {
|
||||
@@ -100,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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user