sp_consensus_subspace/
inherents.rs#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Decode, Encode};
use sp_inherents::{Error, InherentData, InherentIdentifier, IsFatalError};
use subspace_core_primitives::segments::SegmentHeader;
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"subspace";
#[derive(Debug, Encode)]
#[cfg_attr(feature = "std", derive(Decode))]
pub enum InherentError {
IncorrectSegmentHeadersList {
expected: Vec<SegmentHeader>,
actual: Vec<SegmentHeader>,
},
MissingSegmentHeadersList,
}
impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
true
}
}
#[derive(Debug, Encode, Decode)]
pub struct InherentType {
pub segment_headers: Vec<SegmentHeader>,
}
pub trait SubspaceInherentData {
fn subspace_inherent_data(&self) -> Result<Option<InherentType>, Error>;
fn replace_subspace_inherent_data(&mut self, new: InherentType);
}
impl SubspaceInherentData for InherentData {
fn subspace_inherent_data(&self) -> Result<Option<InherentType>, Error> {
self.get_data(&INHERENT_IDENTIFIER)
}
fn replace_subspace_inherent_data(&mut self, new: InherentType) {
self.replace_data(INHERENT_IDENTIFIER, &new);
}
}
#[cfg(feature = "std")]
pub struct InherentDataProvider {
data: InherentType,
}
#[cfg(feature = "std")]
impl InherentDataProvider {
pub fn new(segment_headers: Vec<SegmentHeader>) -> Self {
Self {
data: InherentType { segment_headers },
}
}
pub fn data(&self) -> &InherentType {
&self.data
}
}
#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for InherentDataProvider {
async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.data)
}
async fn try_handle_error(
&self,
identifier: &InherentIdentifier,
error: &[u8],
) -> Option<Result<(), Error>> {
if *identifier != INHERENT_IDENTIFIER {
return None;
}
let error = InherentError::decode(&mut &*error).ok()?;
Some(Err(Error::Application(Box::from(format!("{error:?}")))))
}
}