sp_executive/
lib.rs

1//! Inherents for Executive pallet
2#![cfg_attr(not(feature = "std"), no_std)]
3
4#[cfg(not(feature = "std"))]
5extern crate alloc;
6
7#[cfg(not(feature = "std"))]
8use alloc::vec::Vec;
9use parity_scale_codec::{Decode, Encode};
10#[cfg(feature = "std")]
11use sp_inherents::{Error, InherentData};
12use sp_inherents::{InherentIdentifier, IsFatalError};
13
14/// Executive inherent identifier.
15pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"executve";
16
17#[derive(Debug, Encode)]
18#[cfg_attr(feature = "std", derive(Decode))]
19pub enum InherentError {
20    MissingRuntimeCode,
21    IncorrectRuntimeCode,
22}
23
24impl IsFatalError for InherentError {
25    fn is_fatal_error(&self) -> bool {
26        true
27    }
28}
29
30/// The type of the Subspace inherent data.
31#[derive(Debug, Encode, Decode)]
32pub struct InherentType {
33    /// Upgraded runtime code.
34    pub maybe_code: Option<Vec<u8>>,
35}
36
37/// Provides the set code inherent data.
38#[cfg(feature = "std")]
39pub struct InherentDataProvider {
40    data: InherentType,
41}
42
43#[cfg(feature = "std")]
44impl InherentDataProvider {
45    /// Create new inherent data provider from the given `data`.
46    pub fn new(maybe_code: Option<Vec<u8>>) -> Self {
47        Self {
48            data: InherentType { maybe_code },
49        }
50    }
51
52    /// Returns the `data` of this inherent data provider.
53    pub fn data(&self) -> &InherentType {
54        &self.data
55    }
56}
57
58#[cfg(feature = "std")]
59#[async_trait::async_trait]
60impl sp_inherents::InherentDataProvider for InherentDataProvider {
61    async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
62        inherent_data.put_data(INHERENT_IDENTIFIER, &self.data)
63    }
64
65    async fn try_handle_error(
66        &self,
67        identifier: &InherentIdentifier,
68        error: &[u8],
69    ) -> Option<Result<(), Error>> {
70        if *identifier != INHERENT_IDENTIFIER {
71            return None;
72        }
73
74        let error = InherentError::decode(&mut &*error).ok()?;
75
76        Some(Err(Error::Application(Box::from(format!("{error:?}")))))
77    }
78}