sp_domain_sudo/
lib.rs

1//! Inherents for Domain sudo
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"dmnsudo_";
16
17#[derive(Debug, Encode)]
18#[cfg_attr(feature = "std", derive(Decode))]
19pub enum InherentError {
20    MissingRuntimeCall,
21    InvalidRuntimeCall,
22    IncorrectRuntimeCall,
23}
24
25impl IsFatalError for InherentError {
26    fn is_fatal_error(&self) -> bool {
27        true
28    }
29}
30
31/// The type of the Subspace inherent data.
32#[derive(Debug, Encode, Decode)]
33pub struct InherentType {
34    /// Sudo Call
35    pub maybe_call: Option<Vec<u8>>,
36}
37
38/// Provides the set code inherent data.
39#[cfg(feature = "std")]
40pub struct InherentDataProvider {
41    data: InherentType,
42}
43
44#[cfg(feature = "std")]
45impl InherentDataProvider {
46    /// Create new inherent data provider from the given `data`.
47    pub fn new(maybe_call: Option<Vec<u8>>) -> Self {
48        Self {
49            data: InherentType { maybe_call },
50        }
51    }
52
53    /// Returns the `data` of this inherent data provider.
54    pub fn data(&self) -> &InherentType {
55        &self.data
56    }
57}
58
59#[cfg(feature = "std")]
60#[async_trait::async_trait]
61impl sp_inherents::InherentDataProvider for InherentDataProvider {
62    async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
63        inherent_data.put_data(INHERENT_IDENTIFIER, &self.data)
64    }
65
66    async fn try_handle_error(
67        &self,
68        identifier: &InherentIdentifier,
69        error: &[u8],
70    ) -> Option<Result<(), Error>> {
71        if *identifier != INHERENT_IDENTIFIER {
72            return None;
73        }
74
75        let error = InherentError::decode(&mut &*error).ok()?;
76
77        Some(Err(Error::Application(Box::from(format!("{error:?}")))))
78    }
79}
80
81/// Trait to convert Unchecked extrinsic into a Runtime specific call
82pub trait IntoRuntimeCall<RuntimeCall> {
83    fn runtime_call(call: Vec<u8>) -> RuntimeCall;
84}
85
86sp_api::decl_runtime_apis! {
87    /// Api to check and verify the Sudo calls
88    pub trait DomainSudoApi {
89        /// Returns true if the domain_sudo exists in the runtime
90        /// and extrinsic is valid
91        fn is_valid_sudo_call(extrinsic: Vec<u8>) -> bool;
92
93        /// Returns an encoded extrinsic for domain sudo call.
94        fn construct_domain_sudo_extrinsic(inner: Vec<u8>) -> Block::Extrinsic;
95    }
96}