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;
27pub trait BabeInherentData {
29 fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error>;
31 fn babe_replace_inherent_data(&mut self, new: InherentType);
33}
34
35impl BabeInherentData for InherentData {
36 fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error> {
37 self.get_data(&INHERENT_IDENTIFIER)
38 }
39
40 fn babe_replace_inherent_data(&mut self, new: InherentType) {
41 self.replace_data(INHERENT_IDENTIFIER, &new);
42 }
43}
44
45#[cfg(feature = "std")]
48pub struct InherentDataProvider {
49 slot: InherentType,
50}
51
52#[cfg(feature = "std")]
53impl InherentDataProvider {
54 pub fn new(slot: InherentType) -> Self {
56 Self { slot }
57 }
58
59 pub fn from_timestamp_and_slot_duration(
62 timestamp: sp_timestamp::Timestamp,
63 slot_duration: sp_consensus_slots::SlotDuration,
64 ) -> Self {
65 let slot = InherentType::from_timestamp(timestamp, slot_duration);
66
67 Self { slot }
68 }
69
70 pub fn slot(&self) -> InherentType {
72 self.slot
73 }
74}
75
76#[cfg(feature = "std")]
77impl core::ops::Deref for InherentDataProvider {
78 type Target = InherentType;
79
80 fn deref(&self) -> &Self::Target {
81 &self.slot
82 }
83}
84
85#[cfg(feature = "std")]
86#[async_trait::async_trait]
87impl sp_inherents::InherentDataProvider for InherentDataProvider {
88 async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
89 inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
90 }
91
92 async fn try_handle_error(
93 &self,
94 _: &InherentIdentifier,
95 _: &[u8],
96 ) -> Option<Result<(), Error>> {
97 None
99 }
100}