sc_proof_of_time/
slots.rs

1use sc_consensus_slots::SlotInfo;
2use sp_consensus::SelectChain;
3use sp_consensus_slots::Slot;
4use sp_inherents::CreateInherentDataProviders;
5use sp_runtime::traits::{Block as BlockT, Header};
6use std::time::Duration;
7use tracing::error;
8
9pub(super) struct SlotInfoProducer<Block, SC, IDP> {
10    slot_duration: Duration,
11    create_inherent_data_providers: IDP,
12    select_chain: SC,
13    _phantom: std::marker::PhantomData<Block>,
14}
15
16impl<Block, SC, IDP> SlotInfoProducer<Block, SC, IDP> {
17    /// Create a new `Slots` stream.
18    pub(super) fn new(
19        slot_duration: Duration,
20        create_inherent_data_providers: IDP,
21        select_chain: SC,
22    ) -> Self {
23        SlotInfoProducer {
24            slot_duration,
25            create_inherent_data_providers,
26            select_chain,
27            _phantom: Default::default(),
28        }
29    }
30}
31
32impl<Block, SC, IDP> SlotInfoProducer<Block, SC, IDP>
33where
34    Block: BlockT,
35    SC: SelectChain<Block>,
36    IDP: CreateInherentDataProviders<Block, ()> + 'static,
37{
38    pub(super) async fn produce_slot_info(&self, slot: Slot) -> Option<SlotInfo<Block>> {
39        let best_header = match self.select_chain.best_chain().await {
40            Ok(best_header) => best_header,
41            Err(error) => {
42                error!(
43                    %error,
44                    "Unable to author block in slot. No best block header.",
45                );
46
47                return None;
48            }
49        };
50
51        let inherent_data_providers = match self
52            .create_inherent_data_providers
53            .create_inherent_data_providers(best_header.hash(), ())
54            .await
55        {
56            Ok(inherent_data_providers) => inherent_data_providers,
57            Err(error) => {
58                error!(
59                    %error,
60                    "Unable to author block in slot. Failure creating inherent data provider.",
61                );
62
63                return None;
64            }
65        };
66
67        Some(SlotInfo::new(
68            slot,
69            Box::new(inherent_data_providers),
70            self.slot_duration,
71            best_header,
72            None,
73        ))
74    }
75}