From 58e22dac42e0af8799b20665385fdaec39938702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 17 Mar 2021 11:35:15 +0100 Subject: [PATCH 1/4] Make slot duration being exposed as `Duration` to the outside --- Cargo.lock | 2 ++ bin/node-template/runtime/src/lib.rs | 4 +-- client/consensus/aura/src/import_queue.rs | 6 ++-- client/consensus/aura/src/lib.rs | 16 ++++----- client/consensus/babe/src/lib.rs | 12 +++---- .../manual-seal/src/consensus/babe.rs | 2 +- client/consensus/slots/Cargo.toml | 1 + client/consensus/slots/src/lib.rs | 11 +++--- client/consensus/slots/src/slots.rs | 8 ++--- primitives/consensus/aura/Cargo.toml | 3 ++ primitives/consensus/aura/src/inherents.rs | 6 ++-- primitives/consensus/aura/src/lib.rs | 35 ++++++++++++++++--- primitives/consensus/babe/src/inherents.rs | 6 ++-- primitives/consensus/babe/src/lib.rs | 4 +-- primitives/consensus/common/src/lib.rs | 10 +----- primitives/timestamp/src/lib.rs | 10 ++++-- test-utils/runtime/src/lib.rs | 10 ++++-- 17 files changed, 89 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ada75934c79b7..be95ca238f31e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7208,6 +7208,7 @@ dependencies = [ "sp-inherents", "sp-runtime", "sp-state-machine", + "sp-timestamp", "sp-trie", "substrate-test-runtime-client", "thiserror", @@ -8559,6 +8560,7 @@ dependencies = [ "parity-scale-codec", "sp-api", "sp-application-crypto", + "sp-consensus", "sp-consensus-slots", "sp-inherents", "sp-runtime", diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 0f026db5735cc..9436ae269bbb0 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -378,8 +378,8 @@ impl_runtime_apis! { } impl sp_consensus_aura::AuraApi for Runtime { - fn slot_duration() -> u64 { - Aura::slot_duration() + fn slot_duration() -> sp_consensus_aura::SlotDuration { + sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration()) } fn authorities() -> Vec { diff --git a/client/consensus/aura/src/import_queue.rs b/client/consensus/aura/src/import_queue.rs index d3ed2bea3e115..51e9dcf87f9ce 100644 --- a/client/consensus/aura/src/import_queue.rs +++ b/client/consensus/aura/src/import_queue.rs @@ -30,7 +30,7 @@ use log::{debug, info, trace}; use prometheus_endpoint::Registry; use codec::{Encode, Decode, Codec}; use sp_consensus::{ - BlockImport, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, + BlockImport, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, SlotData, BlockOrigin, Error as ConsensusError, BlockCheckParams, ImportResult, import_queue::{ Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport, @@ -284,7 +284,7 @@ impl Verifier for AuraVerifier where block.clone(), BlockId::Hash(parent_hash), inherent_data, - timestamp_now, + *timestamp_now, ).map_err(|e| e.to_string())?; } @@ -541,7 +541,7 @@ pub fn import_queue<'a, P, Block, I, C, S, CAW>( S: sp_core::traits::SpawnEssentialNamed, CAW: CanAuthorWith + Send + Sync + 'static, { - register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.get())?; + register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.slot_duration())?; initialize_authorities_cache(&*client)?; let verifier = AuraVerifier::<_, P, _>::new( diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index cce58304d0d02..6fdc6ed56b564 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -75,7 +75,7 @@ pub use sc_consensus_slots::SlotProportion; type AuthorityId

=

::Public; /// Slot duration type for Aura. -pub type SlotDuration = sc_consensus_slots::SlotDuration; +pub type SlotDuration = sc_consensus_slots::SlotDuration; /// Get type of `SlotDuration` for Aura. pub fn slot_duration(client: &C) -> CResult where @@ -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())) } } @@ -477,7 +477,7 @@ fn find_pre_digest(header: &B::Header) -> Result Result<(), sp_consensus::Error> { if !inherent_data_providers.has_provider(&INHERENT_IDENTIFIER) { inherent_data_providers @@ -596,10 +596,10 @@ mod tests { let inherent_data_providers = InherentDataProviders::new(); register_aura_inherent_data_provider( &inherent_data_providers, - slot_duration.get() + slot_duration.slot_duration() ).expect("Registers aura inherent data provider"); - assert_eq!(slot_duration.get(), SLOT_DURATION); + assert_eq!(slot_duration.slot_duration().as_millis() as u64, SLOT_DURATION); import_queue::AuraVerifier::new( client, inherent_data_providers, @@ -665,7 +665,7 @@ mod tests { let inherent_data_providers = InherentDataProviders::new(); register_aura_inherent_data_provider( - &inherent_data_providers, slot_duration.get() + &inherent_data_providers, slot_duration.slot_duration() ).expect("Registers aura inherent data provider"); aura_futures.push(start_aura::(StartAuraParams { @@ -801,7 +801,7 @@ mod tests { head, SlotInfo { slot: 0.into(), - timestamp: 0, + timestamp: 0.into(), ends_at: Instant::now() + Duration::from_secs(100), inherent_data: InherentData::new(), duration: Duration::from_millis(1000), diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 3d72c436361c5..27d33970d4f14 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -345,8 +345,8 @@ impl Config { } } - /// Get the inner slot duration, in milliseconds. - pub fn slot_duration(&self) -> u64 { + /// Get the inner slot duration + pub fn slot_duration(&self) -> Duration { self.0.slot_duration() } } @@ -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())) } } @@ -1220,7 +1220,7 @@ where /// Register the babe inherent data provider, if not registered already. pub fn register_babe_inherent_data_provider( inherent_data_providers: &InherentDataProviders, - slot_duration: u64, + slot_duration: Duration, ) -> Result<(), sp_consensus::Error> { debug!(target: "babe", "Registering"); if !inherent_data_providers.has_provider(&sp_consensus_babe::inherents::INHERENT_IDENTIFIER) { @@ -1626,7 +1626,7 @@ pub fn import_queue( SelectChain: sp_consensus::SelectChain + 'static, CAW: CanAuthorWith + Send + Sync + 'static, { - register_babe_inherent_data_provider(&inherent_data_providers, babe_link.config.slot_duration)?; + register_babe_inherent_data_provider(&inherent_data_providers, babe_link.config.slot_duration())?; let verifier = BabeVerifier { select_chain, diff --git a/client/consensus/manual-seal/src/consensus/babe.rs b/client/consensus/manual-seal/src/consensus/babe.rs index 7fe51c7b79ce5..a3f8a825e61dd 100644 --- a/client/consensus/manual-seal/src/consensus/babe.rs +++ b/client/consensus/manual-seal/src/consensus/babe.rs @@ -90,7 +90,7 @@ impl BabeConsensusDataProvider let timestamp_provider = SlotTimestampProvider::new(client.clone())?; provider.register_provider(timestamp_provider)?; - register_babe_inherent_data_provider(provider, config.slot_duration)?; + register_babe_inherent_data_provider(provider, config.slot_duration())?; Ok(Self { config, diff --git a/client/consensus/slots/Cargo.toml b/client/consensus/slots/Cargo.toml index 7ca413630e26e..34162cfae71e2 100644 --- a/client/consensus/slots/Cargo.toml +++ b/client/consensus/slots/Cargo.toml @@ -28,6 +28,7 @@ sp-api = { version = "3.0.0", path = "../../../primitives/api" } sc-telemetry = { version = "3.0.0", path = "../../telemetry" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } +sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" } futures = "0.3.9" futures-timer = "3.0.1" parking_lot = "0.11.1" diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 037402260c0d3..83dd88a8d49ff 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -274,7 +274,7 @@ pub trait SimpleSlotWorker { 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 Deref for SlotDuration { } impl SlotData for SlotDuration { - /// 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 SlotDuration { } }?; - 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)))) } diff --git a/client/consensus/slots/src/slots.rs b/client/consensus/slots/src/slots.rs index b23d676035696..8f6fb691626f5 100644 --- a/client/consensus/slots/src/slots.rs +++ b/client/consensus/slots/src/slots.rs @@ -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. @@ -73,13 +73,13 @@ pub(crate) struct Slots { impl Slots { /// 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,7 +122,7 @@ impl Stream for Slots { }; // reschedule delay for next slot. let ends_in = offset + - time_until_next(Duration::from_millis(timestamp), slot_duration); + time_until_next(timestamp.as_duration(), slot_duration); let ends_at = Instant::now() + ends_in; self.inner_delay = Some(Delay::new(ends_in)); diff --git a/primitives/consensus/aura/Cargo.toml b/primitives/consensus/aura/Cargo.toml index 100c323024952..105c74bb317d7 100644 --- a/primitives/consensus/aura/Cargo.toml +++ b/primitives/consensus/aura/Cargo.toml @@ -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", ] diff --git a/primitives/consensus/aura/src/inherents.rs b/primitives/consensus/aura/src/inherents.rs index 750b13c77ff66..32af901311a30 100644 --- a/primitives/consensus/aura/src/inherents.rs +++ b/primitives/consensus/aura/src/inherents.rs @@ -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) } diff --git a/primitives/consensus/aura/src/lib.rs b/primitives/consensus/aura/src/lib.rs index 8c9c57567c43f..873aee7b4ecdb 100644 --- a/primitives/consensus/aura/src/lib.rs +++ b/primitives/consensus/aura/src/lib.rs @@ -84,14 +84,39 @@ pub enum ConsensusLog { sp_api::decl_runtime_apis! { /// API necessary for block authorship with aura. pub trait AuraApi { - /// 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; } } + +/// Aura slot duration. +/// +/// Internally stored as milliseconds. +#[derive(sp_runtime::RuntimeDebug, Encode, Decode, PartialEq, Clone)] +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"; +} diff --git a/primitives/consensus/babe/src/inherents.rs b/primitives/consensus/babe/src/inherents.rs index 8aeab94df34a2..4c7c55f1cfd55 100644 --- a/primitives/consensus/babe/src/inherents.rs +++ b/primitives/consensus/babe/src/inherents.rs @@ -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) } diff --git a/primitives/consensus/babe/src/lib.rs b/primitives/consensus/babe/src/lib.rs index 1b416c996fcf0..da9f089e4561c 100644 --- a/primitives/consensus/babe/src/lib.rs +++ b/primitives/consensus/babe/src/lib.rs @@ -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"; diff --git a/primitives/consensus/common/src/lib.rs b/primitives/consensus/common/src/lib.rs index b3aceb45e180f..27a43dbe02208 100644 --- a/primitives/consensus/common/src/lib.rs +++ b/primitives/consensus/common/src/lib.rs @@ -303,16 +303,8 @@ impl CanAuthorWith 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"; -} diff --git a/primitives/timestamp/src/lib.rs b/primitives/timestamp/src/lib.rs index e6ef62b5c59c8..846ba67aec739 100644 --- a/primitives/timestamp/src/lib.rs +++ b/primitives/timestamp/src/lib.rs @@ -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 for u64 { } } -impl From for Timestamp { - fn from(duration: sp_std::time::Duration) -> Self { +impl From for Timestamp { + fn from(duration: Duration) -> Self { Timestamp(duration.as_millis() as u64) } } diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 5f80dc93a95f2..f285eba1d8e40 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -762,7 +762,10 @@ cfg_if! { } impl sp_consensus_aura::AuraApi for Runtime { - fn slot_duration() -> u64 { 1000 } + fn slot_duration() -> sp_consensus_aura::SlotDuration { + sp_consensus_aura::SlotDuration::from_millis(1000) + } + fn authorities() -> Vec { system::authorities().into_iter().map(|a| { let authority: sr25519::Public = a.into(); @@ -1020,7 +1023,10 @@ cfg_if! { } impl sp_consensus_aura::AuraApi for Runtime { - fn slot_duration() -> u64 { 1000 } + fn slot_duration() -> sp_consensus_aura::SlotDuration { + sp_consensus_aura::SlotDuration::from_millis(1000) + } + fn authorities() -> Vec { system::authorities().into_iter().map(|a| { let authority: sr25519::Public = a.into(); From 313dcdf3ea0edd9d047f238dfd076f8e1672bdb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 17 Mar 2021 11:45:08 +0100 Subject: [PATCH 2/4] Some slot info love --- client/consensus/slots/src/slots.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/client/consensus/slots/src/slots.rs b/client/consensus/slots/src/slots.rs index 8f6fb691626f5..1cf7c30b9ed9e 100644 --- a/client/consensus/slots/src/slots.rs +++ b/client/consensus/slots/src/slots.rs @@ -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 { last_slot: Slot, @@ -123,20 +143,18 @@ impl Stream for Slots { // reschedule delay for next slot. let ends_in = offset + time_until_next(timestamp.as_duration(), slot_duration); - let ends_at = Instant::now() + ends_in; 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, + )))) } } } From cb16666994bd97bf6ef6e8a3f2b5bd6f05c26140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 17 Mar 2021 14:41:59 +0100 Subject: [PATCH 3/4] Add `build_aura_worker` utility function --- client/consensus/aura/src/lib.rs | 82 +++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 6fdc6ed56b564..81c6015ac7efa 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -161,7 +161,7 @@ pub fn start_aura( client, select_chain, block_import, - proposer_factory: env, + proposer_factory, sync_oracle, inherent_data_providers, force_authoring, @@ -187,22 +187,23 @@ pub fn start_aura( CAW: CanAuthorWith + Send, BS: BackoffAuthoringBlocksStrategy> + Send + 'static, { - let worker = AuraWorker { + let worker = build_aura_worker::(BuildAuraWorkerParams { client: client.clone(), - block_import: Arc::new(Mutex::new(block_import)), - env, + block_import, + proposer_factory, keystore, sync_oracle: sync_oracle.clone(), force_authoring, backoff_authoring_blocks, telemetry, - _key_type: PhantomData::

, block_proposal_slot_portion, - }; + }); + register_aura_inherent_data_provider( &inherent_data_providers, slot_duration.slot_duration() )?; + Ok(sc_consensus_slots::start_slot_worker::<_, _, _, _, _, AuraSlotCompatible, _, _>( slot_duration, select_chain, @@ -214,6 +215,75 @@ pub fn start_aura( )) } +/// Parameters of [`build_aura_worker`]. +pub struct BuildAuraWorkerParams { + /// The client to interact with the chain. + pub client: Arc, + /// The block import. + pub block_import: I, + /// The proposer factory to build proposer instances. + pub proposer_factory: PF, + /// The sync oracle that can give us the current sync status. + pub sync_oracle: SO, + /// Should we force the authoring of blocks? + pub force_authoring: bool, + /// The backoff strategy when we miss slots. + pub backoff_authoring_blocks: Option, + /// The keystore used by the node. + pub keystore: SyncCryptoStorePtr, + /// The proportion of the slot dedicated to proposing. + /// + /// The block proposing will be limited to this proportion of the slot from the starting of the + /// slot. However, the proposing can still take longer when there is some lenience factor applied, + /// because there were no blocks produced for some slots. + pub block_proposal_slot_portion: SlotProportion, + /// Telemetry instance used to report telemetry metrics. + pub telemetry: Option, +} + +/// Build the aura worker. +/// +/// The caller is responsible for running this worker, otherwise it will do nothing. +pub fn build_aura_worker( + BuildAuraWorkerParams { + client, + block_import, + proposer_factory, + sync_oracle, + backoff_authoring_blocks, + keystore, + block_proposal_slot_portion, + telemetry, + force_authoring, + }: BuildAuraWorkerParams, +) -> impl sc_consensus_slots::SlotWorker>::Proof> where + B: BlockT, + C: ProvideRuntimeApi + BlockOf + ProvideCache + AuxStore + HeaderBackend + Send + Sync, + C::Api: AuraApi>, + PF: Environment + Send + Sync + 'static, + PF::Proposer: Proposer>, + P: Pair + Send + Sync, + P::Public: AppPublic + Hash + Member + Encode + Decode, + P::Signature: TryFrom> + Hash + Member + Encode + Decode, + I: BlockImport> + Send + Sync + 'static, + Error: std::error::Error + Send + From + 'static, + SO: SyncOracle + Send + Sync + Clone, + BS: BackoffAuthoringBlocksStrategy> + Send + 'static, +{ + AuraWorker { + client, + block_import: Arc::new(Mutex::new(block_import)), + env: proposer_factory, + keystore, + sync_oracle, + force_authoring, + backoff_authoring_blocks, + telemetry, + _key_type: PhantomData::

, + block_proposal_slot_portion, + } +} + struct AuraWorker { client: Arc, block_import: Arc>, From ce128c556db26031a6df2d4cc640d07af28d4228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 17 Mar 2021 19:25:44 +0100 Subject: [PATCH 4/4] Copy copy copy --- primitives/consensus/aura/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/consensus/aura/src/lib.rs b/primitives/consensus/aura/src/lib.rs index 873aee7b4ecdb..a28e681fda27f 100644 --- a/primitives/consensus/aura/src/lib.rs +++ b/primitives/consensus/aura/src/lib.rs @@ -97,7 +97,7 @@ sp_api::decl_runtime_apis! { /// Aura slot duration. /// /// Internally stored as milliseconds. -#[derive(sp_runtime::RuntimeDebug, Encode, Decode, PartialEq, Clone)] +#[derive(sp_runtime::RuntimeDebug, Encode, Decode, PartialEq, Clone, Copy)] pub struct SlotDuration(u64); impl SlotDuration {