tp_consensus_aura/
inherents.rs1use tp_inherents::{InherentIdentifier, InherentData, Error};
21
22#[cfg(feature = "std")]
23use tp_inherents::{InherentDataProviders, ProvideInherentData};
24
25pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
27
28pub type InherentType = tp_consensus_slots::Slot;
30
31pub trait AuraInherentData {
33 fn aura_inherent_data(&self) ->Result<InherentType, Error>;
35 fn aura_replace_inherent_data(&mut self, new: InherentType);
37}
38
39impl AuraInherentData for InherentData {
40 fn aura_inherent_data(&self) ->Result<InherentType, Error> {
41 self.get_data(&INHERENT_IDENTIFIER)
42 .and_then(|r| r.ok_or_else(|| "Aura inherent data not found".into()))
43 }
44
45 fn aura_replace_inherent_data(&mut self, new: InherentType) {
46 self.replace_data(INHERENT_IDENTIFIER, &new);
47 }
48}
49
50#[cfg(feature = "std")]
52pub struct InherentDataProvider {
53 slot_duration: u64,
54}
55
56#[cfg(feature = "std")]
57impl InherentDataProvider {
58 pub fn new(slot_duration: u64) -> Self {
59 Self {
60 slot_duration
61 }
62 }
63}
64
65#[cfg(feature = "std")]
66impl ProvideInherentData for InherentDataProvider {
67 fn on_register(
68 &self,
69 providers: &InherentDataProviders,
70 ) ->Result<(), Error> {
71 if !providers.has_provider(&tp_timestamp::INHERENT_IDENTIFIER) {
72 providers.register_provider(tp_timestamp::InherentDataProvider)
74 } else {
75 Ok(())
76 }
77 }
78
79 fn inherent_identifier(&self) -> &'static InherentIdentifier {
80 &INHERENT_IDENTIFIER
81 }
82
83 fn provide_inherent_data(
84 &self,
85 inherent_data: &mut InherentData,
86 ) ->Result<(), Error> {
87 use tp_timestamp::TimestampInherentData;
88
89 let timestamp = inherent_data.timestamp_inherent_data()?;
90 let slot = timestamp / self.slot_duration;
91 inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
92 }
93
94 fn error_to_string(&self, error: &[u8]) -> Option<String> {
95 use codec::Decode;
96
97 tp_inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
98 }
99}