sp_consensus_babe/
inherents.rs1use sp_inherents::{Error, InherentData, InherentIdentifier};
21
22pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babeslot";
24
25pub type InherentType = sp_consensus_slots::Slot;
27
28#[cfg(feature = "std")]
30pub type BabeCreateInherentDataProviders<Block> = std::sync::Arc<
31 dyn sp_inherents::CreateInherentDataProviders<
32 Block,
33 (),
34 InherentDataProviders = (InherentDataProvider, sp_timestamp::InherentDataProvider),
35 >,
36>;
37
38pub trait BabeInherentData {
40 fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error>;
42 fn babe_replace_inherent_data(&mut self, new: InherentType);
44}
45
46impl BabeInherentData for InherentData {
47 fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error> {
48 self.get_data(&INHERENT_IDENTIFIER)
49 }
50
51 fn babe_replace_inherent_data(&mut self, new: InherentType) {
52 self.replace_data(INHERENT_IDENTIFIER, &new);
53 }
54}
55
56#[cfg(feature = "std")]
59pub struct InherentDataProvider {
60 slot: InherentType,
61}
62
63#[cfg(feature = "std")]
64impl InherentDataProvider {
65 pub fn new(slot: InherentType) -> Self {
67 Self { slot }
68 }
69
70 pub fn from_timestamp_and_slot_duration(
73 timestamp: sp_timestamp::Timestamp,
74 slot_duration: sp_consensus_slots::SlotDuration,
75 ) -> Self {
76 let slot = InherentType::from_timestamp(timestamp, slot_duration);
77
78 Self { slot }
79 }
80
81 pub fn slot(&self) -> InherentType {
83 self.slot
84 }
85}
86
87#[cfg(feature = "std")]
88impl core::ops::Deref for InherentDataProvider {
89 type Target = InherentType;
90
91 fn deref(&self) -> &Self::Target {
92 &self.slot
93 }
94}
95
96#[cfg(feature = "std")]
97#[async_trait::async_trait]
98impl sp_inherents::InherentDataProvider for InherentDataProvider {
99 async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
100 inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
101 }
102
103 async fn try_handle_error(
104 &self,
105 _: &InherentIdentifier,
106 _: &[u8],
107 ) -> Option<Result<(), Error>> {
108 None
110 }
111}