mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 22:11:06 +00:00
SimpleSlotWorker make claim_slot async (#9713)
* SimpleSlotWorker make `claim_slot` async * FMT
This commit is contained in:
@@ -188,7 +188,7 @@ where
|
||||
L: sc_consensus::JustificationSyncLink<B>,
|
||||
CIDP: CreateInherentDataProviders<B, ()> + Send,
|
||||
CIDP::InherentDataProviders: InherentDataProviderExt + Send,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + Sync + 'static,
|
||||
CAW: CanAuthorWith<B> + Send,
|
||||
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
|
||||
{
|
||||
@@ -278,7 +278,7 @@ where
|
||||
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
|
||||
SO: SyncOracle + Send + Sync + Clone,
|
||||
L: sc_consensus::JustificationSyncLink<B>,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + Sync + 'static,
|
||||
{
|
||||
AuraWorker {
|
||||
client,
|
||||
@@ -311,21 +311,22 @@ struct AuraWorker<C, E, I, P, SO, L, BS> {
|
||||
_key_type: PhantomData<P>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<B, C, E, I, P, Error, SO, L, BS> sc_consensus_slots::SimpleSlotWorker<B>
|
||||
for AuraWorker<C, E, I, P, SO, L, BS>
|
||||
where
|
||||
B: BlockT,
|
||||
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + HeaderBackend<B> + Sync,
|
||||
C::Api: AuraApi<B, AuthorityId<P>>,
|
||||
E: Environment<B, Error = Error>,
|
||||
E: Environment<B, Error = Error> + Send + Sync,
|
||||
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
|
||||
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
|
||||
P: Pair + Send + Sync,
|
||||
P::Public: AppPublic + Public + Member + Encode + Decode + Hash,
|
||||
P::Signature: TryFrom<Vec<u8>> + Member + Encode + Decode + Hash + Debug,
|
||||
SO: SyncOracle + Send + Clone,
|
||||
SO: SyncOracle + Send + Clone + Sync,
|
||||
L: sc_consensus::JustificationSyncLink<B>,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + Sync + 'static,
|
||||
Error: std::error::Error + Send + From<sp_consensus::Error> + '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),
|
||||
|
||||
@@ -485,7 +485,7 @@ where
|
||||
L: sc_consensus::JustificationSyncLink<B> + 'static,
|
||||
CIDP: CreateInherentDataProviders<B, ()> + Send + Sync + 'static,
|
||||
CIDP::InherentDataProviders: InherentDataProviderExt + Send,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + Sync + 'static,
|
||||
CAW: CanAuthorWith<B> + Send + Sync + 'static,
|
||||
Error: std::error::Error + Send + From<ConsensusError> + From<I::Error> + 'static,
|
||||
{
|
||||
@@ -672,6 +672,7 @@ struct BabeSlotWorker<B: BlockT, C, E, I, SO, L, BS> {
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<B, C, E, I, Error, SO, L, BS> sc_consensus_slots::SimpleSlotWorker<B>
|
||||
for BabeSlotWorker<B, C, E, I, SO, L, BS>
|
||||
where
|
||||
@@ -681,12 +682,12 @@ where
|
||||
+ HeaderBackend<B>
|
||||
+ HeaderMetadata<B, Error = ClientError>,
|
||||
C::Api: BabeApi<B>,
|
||||
E: Environment<B, Error = Error>,
|
||||
E: Environment<B, Error = Error> + Sync,
|
||||
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
|
||||
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
|
||||
SO: SyncOracle + Send + Clone,
|
||||
SO: SyncOracle + Send + Clone + Sync,
|
||||
L: sc_consensus::JustificationSyncLink<B>,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>>,
|
||||
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Sync,
|
||||
Error: std::error::Error + Send + From<ConsensusError> + From<I::Error> + 'static,
|
||||
{
|
||||
type EpochData = ViableEpochDescriptor<B::Hash, NumberFor<B>, 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,
|
||||
|
||||
@@ -108,7 +108,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
|
||||
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<B: BlockT> {
|
||||
fn authorities_len(&self, epoch_data: &Self::EpochData) -> Option<usize>;
|
||||
|
||||
/// 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<B: BlockT> {
|
||||
async fn on_slot(
|
||||
&mut self,
|
||||
slot_info: SlotInfo<B>,
|
||||
) -> Option<SlotResult<B, <Self::Proposer as Proposer<B>>::Proof>> {
|
||||
) -> Option<SlotResult<B, <Self::Proposer as Proposer<B>>::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<B: BlockT> {
|
||||
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<B: BlockT> {
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<B: BlockT, T: SimpleSlotWorker<B> + Send> SlotWorker<B, <T::Proposer as Proposer<B>>::Proof>
|
||||
for T
|
||||
impl<B: BlockT, T: SimpleSlotWorker<B> + Send + Sync>
|
||||
SlotWorker<B, <T::Proposer as Proposer<B>>::Proof> for T
|
||||
{
|
||||
async fn on_slot(
|
||||
&mut self,
|
||||
|
||||
Reference in New Issue
Block a user