subspace_runtime_primitives/
extensions.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Extensions for Runtimes

use crate::AllowedUnsignedExtrinsics;
use codec::{Decode, Encode};
use frame_support::pallet_prelude::{PhantomData, TypeInfo};
use frame_system::pallet_prelude::RuntimeCallFor;
use frame_system::Config;
use scale_info::prelude::fmt;
use sp_runtime::impl_tx_ext_default;
use sp_runtime::traits::{
    AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, Implication,
    TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{InvalidTransaction, TransactionSource, ValidTransaction};

/// Checks allowed General Extrinsics
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct CheckAllowedGeneralExtrinsics<Runtime>(PhantomData<Runtime>);

impl<Runtime> CheckAllowedGeneralExtrinsics<Runtime> {
    pub fn new() -> Self {
        Self(PhantomData)
    }
}

impl<Runtime> Default for CheckAllowedGeneralExtrinsics<Runtime> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Config> fmt::Debug for CheckAllowedGeneralExtrinsics<T> {
    #[cfg(feature = "std")]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CheckAllowedGeneralExtrinsics",)
    }

    #[cfg(not(feature = "std"))]
    fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
        Ok(())
    }
}

impl<Runtime> TransactionExtension<RuntimeCallFor<Runtime>>
    for CheckAllowedGeneralExtrinsics<Runtime>
where
    Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync,
    <RuntimeCallFor<Runtime> as Dispatchable>::RuntimeOrigin:
        AsSystemOriginSigner<<Runtime as Config>::AccountId> + Clone,
    RuntimeCallFor<Runtime>: AllowedUnsignedExtrinsics,
{
    const IDENTIFIER: &'static str = "CheckAllowedGeneralExtrinsics";
    type Implicit = ();
    type Val = ();
    type Pre = ();

    fn validate(
        &self,
        origin: DispatchOriginOf<RuntimeCallFor<Runtime>>,
        call: &RuntimeCallFor<Runtime>,
        _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
        _len: usize,
        _self_implicit: Self::Implicit,
        _inherited_implication: &impl Implication,
        _source: TransactionSource,
    ) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
        if origin.as_system_origin_signer().is_none() && !call.is_allowed_unsigned() {
            Err(InvalidTransaction::Call.into())
        } else {
            Ok((ValidTransaction::default(), (), origin))
        }
    }

    impl_tx_ext_default!(RuntimeCallFor<Runtime>; weight);
    impl_tx_ext_default!(RuntimeCallFor<Runtime>; prepare);
}