use crate::inherents::{Error, InherentData, InherentIdentifier};
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babeslot";
pub type InherentType = crate::consensus::slots::Slot;
#[cfg(feature = "std")]
pub type BabeCreateInherentDataProviders<Block> = std::sync::Arc<
dyn crate::inherents::CreateInherentDataProviders<
Block,
(),
InherentDataProviders = (InherentDataProvider, crate::timestamp::InherentDataProvider),
>,
>;
pub trait BabeInherentData {
fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error>;
fn babe_replace_inherent_data(&mut self, new: InherentType);
}
impl BabeInherentData for InherentData {
fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error> {
self.get_data(&INHERENT_IDENTIFIER)
}
fn babe_replace_inherent_data(&mut self, new: InherentType) {
self.replace_data(INHERENT_IDENTIFIER, &new);
}
}
#[cfg(feature = "std")]
pub struct InherentDataProvider {
slot: InherentType,
}
#[cfg(feature = "std")]
impl InherentDataProvider {
pub fn new(slot: InherentType) -> Self {
Self { slot }
}
pub fn from_timestamp_and_slot_duration(
timestamp: crate::timestamp::Timestamp,
slot_duration: crate::consensus::slots::SlotDuration,
) -> Self {
let slot = InherentType::from_timestamp(timestamp, slot_duration);
Self { slot }
}
pub fn slot(&self) -> InherentType {
self.slot
}
}
#[cfg(feature = "std")]
impl core::ops::Deref for InherentDataProvider {
type Target = InherentType;
fn deref(&self) -> &Self::Target {
&self.slot
}
}
#[cfg(feature = "std")]
#[async_trait::async_trait]
impl crate::inherents::InherentDataProvider for InherentDataProvider {
async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
}
async fn try_handle_error(
&self,
_: &InherentIdentifier,
_: &[u8],
) -> Option<Result<(), Error>> {
None
}
}