From 224c7f10b9cf17ae64f7321ddcf091fb05fb53a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 7 Sep 2021 13:15:44 +0200 Subject: [PATCH] SimpleSlotWorker make `claim_slot` async (#9713) * SimpleSlotWorker make `claim_slot` async * FMT --- substrate/client/consensus/aura/src/lib.rs | 34 +++++++++++---------- substrate/client/consensus/babe/src/lib.rs | 11 ++++--- substrate/client/consensus/slots/src/lib.rs | 15 +++++---- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/substrate/client/consensus/aura/src/lib.rs b/substrate/client/consensus/aura/src/lib.rs index d038db97cb..946e0b90c4 100644 --- a/substrate/client/consensus/aura/src/lib.rs +++ b/substrate/client/consensus/aura/src/lib.rs @@ -188,7 +188,7 @@ where L: sc_consensus::JustificationSyncLink, CIDP: CreateInherentDataProviders + Send, CIDP::InherentDataProviders: InherentDataProviderExt + Send, - BS: BackoffAuthoringBlocksStrategy> + Send + 'static, + BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, CAW: CanAuthorWith + Send, Error: std::error::Error + Send + From + 'static, { @@ -278,7 +278,7 @@ where Error: std::error::Error + Send + From + 'static, SO: SyncOracle + Send + Sync + Clone, L: sc_consensus::JustificationSyncLink, - BS: BackoffAuthoringBlocksStrategy> + Send + 'static, + BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, { AuraWorker { client, @@ -311,21 +311,22 @@ struct AuraWorker { _key_type: PhantomData

, } +#[async_trait::async_trait] impl sc_consensus_slots::SimpleSlotWorker for AuraWorker where B: BlockT, C: ProvideRuntimeApi + BlockOf + ProvideCache + HeaderBackend + Sync, C::Api: AuraApi>, - E: Environment, + E: Environment + Send + Sync, E::Proposer: Proposer>, I: BlockImport> + Send + Sync + 'static, P: Pair + Send + Sync, P::Public: AppPublic + Public + Member + Encode + Decode + Hash, P::Signature: TryFrom> + Member + Encode + Decode + Hash + Debug, - SO: SyncOracle + Send + Clone, + SO: SyncOracle + Send + Clone + Sync, L: sc_consensus::JustificationSyncLink, - BS: BackoffAuthoringBlocksStrategy> + Send + 'static, + BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, Error: std::error::Error + Send + From + 'static, { type BlockImport = I; @@ -357,7 +358,7 @@ where Some(epoch_data.len()) } - fn claim_slot( + async fn claim_slot( &self, _header: &B::Header, slot: Slot, @@ -557,6 +558,7 @@ where #[cfg(test)] mod tests { use super::*; + use futures::executor; use parking_lot::Mutex; use sc_block_builder::BlockBuilderProvider; use sc_client_api::BlockchainEvents; @@ -777,7 +779,7 @@ mod tests { ); } - futures::executor::block_on(future::select( + executor::block_on(future::select( future::poll_fn(move |cx| { net.lock().poll(cx); Poll::<()>::Pending @@ -846,14 +848,14 @@ mod tests { Default::default(), Default::default(), ); - assert!(worker.claim_slot(&head, 0.into(), &authorities).is_none()); - assert!(worker.claim_slot(&head, 1.into(), &authorities).is_none()); - assert!(worker.claim_slot(&head, 2.into(), &authorities).is_none()); - assert!(worker.claim_slot(&head, 3.into(), &authorities).is_some()); - assert!(worker.claim_slot(&head, 4.into(), &authorities).is_none()); - assert!(worker.claim_slot(&head, 5.into(), &authorities).is_none()); - assert!(worker.claim_slot(&head, 6.into(), &authorities).is_none()); - assert!(worker.claim_slot(&head, 7.into(), &authorities).is_some()); + assert!(executor::block_on(worker.claim_slot(&head, 0.into(), &authorities)).is_none()); + assert!(executor::block_on(worker.claim_slot(&head, 1.into(), &authorities)).is_none()); + assert!(executor::block_on(worker.claim_slot(&head, 2.into(), &authorities)).is_none()); + assert!(executor::block_on(worker.claim_slot(&head, 3.into(), &authorities)).is_some()); + assert!(executor::block_on(worker.claim_slot(&head, 4.into(), &authorities)).is_none()); + assert!(executor::block_on(worker.claim_slot(&head, 5.into(), &authorities)).is_none()); + assert!(executor::block_on(worker.claim_slot(&head, 6.into(), &authorities)).is_none()); + assert!(executor::block_on(worker.claim_slot(&head, 7.into(), &authorities)).is_some()); } #[test] @@ -893,7 +895,7 @@ mod tests { let head = client.header(&BlockId::Number(0)).unwrap().unwrap(); - let res = futures::executor::block_on(worker.on_slot(SlotInfo { + let res = executor::block_on(worker.on_slot(SlotInfo { slot: 0.into(), timestamp: 0.into(), ends_at: Instant::now() + Duration::from_secs(100), diff --git a/substrate/client/consensus/babe/src/lib.rs b/substrate/client/consensus/babe/src/lib.rs index 21fba61866..a0b6bde025 100644 --- a/substrate/client/consensus/babe/src/lib.rs +++ b/substrate/client/consensus/babe/src/lib.rs @@ -485,7 +485,7 @@ where L: sc_consensus::JustificationSyncLink + 'static, CIDP: CreateInherentDataProviders + Send + Sync + 'static, CIDP::InherentDataProviders: InherentDataProviderExt + Send, - BS: BackoffAuthoringBlocksStrategy> + Send + 'static, + BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, CAW: CanAuthorWith + Send + Sync + 'static, Error: std::error::Error + Send + From + From + 'static, { @@ -672,6 +672,7 @@ struct BabeSlotWorker { telemetry: Option, } +#[async_trait::async_trait] impl sc_consensus_slots::SimpleSlotWorker for BabeSlotWorker where @@ -681,12 +682,12 @@ where + HeaderBackend + HeaderMetadata, C::Api: BabeApi, - E: Environment, + E: Environment + Sync, E::Proposer: Proposer>, I: BlockImport> + Send + Sync + 'static, - SO: SyncOracle + Send + Clone, + SO: SyncOracle + Send + Clone + Sync, L: sc_consensus::JustificationSyncLink, - BS: BackoffAuthoringBlocksStrategy>, + BS: BackoffAuthoringBlocksStrategy> + Sync, Error: std::error::Error + Send + From + From + 'static, { type EpochData = ViableEpochDescriptor, Epoch>; @@ -730,7 +731,7 @@ where .map(|epoch| epoch.as_ref().authorities.len()) } - fn claim_slot( + async fn claim_slot( &self, _parent_header: &B::Header, slot: Slot, diff --git a/substrate/client/consensus/slots/src/lib.rs b/substrate/client/consensus/slots/src/lib.rs index da04b98cce..bfaa388014 100644 --- a/substrate/client/consensus/slots/src/lib.rs +++ b/substrate/client/consensus/slots/src/lib.rs @@ -108,7 +108,7 @@ pub trait SimpleSlotWorker { type Claim: Send + 'static; /// Epoch data necessary for authoring. - type EpochData: Send + 'static; + type EpochData: Send + Sync + 'static; /// The logging target to use when logging messages. fn logging_target(&self) -> &'static str; @@ -129,7 +129,7 @@ pub trait SimpleSlotWorker { fn authorities_len(&self, epoch_data: &Self::EpochData) -> Option; /// Tries to claim the given slot, returning an object with claim data if successful. - fn claim_slot( + async fn claim_slot( &self, header: &B::Header, slot: Slot, @@ -200,7 +200,10 @@ pub trait SimpleSlotWorker { async fn on_slot( &mut self, slot_info: SlotInfo, - ) -> Option>::Proof>> { + ) -> Option>::Proof>> + where + Self: Sync, + { let (timestamp, slot) = (slot_info.timestamp, slot_info.slot); let telemetry = self.telemetry(); let logging_target = self.logging_target(); @@ -259,7 +262,7 @@ pub trait SimpleSlotWorker { return None } - let claim = self.claim_slot(&slot_info.chain_head, slot, &epoch_data)?; + let claim = self.claim_slot(&slot_info.chain_head, slot, &epoch_data).await?; if self.should_backoff(slot, &slot_info.chain_head) { return None @@ -415,8 +418,8 @@ pub trait SimpleSlotWorker { } #[async_trait::async_trait] -impl + Send> SlotWorker>::Proof> - for T +impl + Send + Sync> + SlotWorker>::Proof> for T { async fn on_slot( &mut self,