sp_block_fees/
lib.rs

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