|
| 1 | +use frame_support::pallet_prelude::{TransactionSource, TransactionValidityError}; |
| 2 | +use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode}; |
| 3 | +use scale_info::TypeInfo; |
| 4 | + |
| 5 | +use sp_core::H160; |
| 6 | +use sp_runtime::{ |
| 7 | + traits::{AsSystemOriginSigner, DispatchInfoOf, Dispatchable, TransactionExtension}, |
| 8 | + Weight, |
| 9 | +}; |
| 10 | +use sp_std::{ |
| 11 | + fmt::{Debug, Formatter}, |
| 12 | + marker::PhantomData, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Transaction extension that blocks extrinsics from specific origins |
| 16 | +#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo, DecodeWithMemTracking)] |
| 17 | +#[scale_info(skip_type_params(T))] |
| 18 | +#[codec(encode_bound())] |
| 19 | +#[codec(decode_bound())] |
| 20 | +pub struct CheckBlockedOrigin<T, Call>(PhantomData<(T, Call)>); |
| 21 | + |
| 22 | +impl<T, Call> Debug for CheckBlockedOrigin<T, Call> { |
| 23 | + fn fmt(&self, f: &mut Formatter) -> sp_std::fmt::Result { |
| 24 | + write!(f, "CheckBlockedOrigin") |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<T, Call> Default for CheckBlockedOrigin<T, Call> { |
| 29 | + fn default() -> Self { |
| 30 | + Self::new() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<T, Call> CheckBlockedOrigin<T, Call> { |
| 35 | + pub fn new() -> Self { |
| 36 | + Self(PhantomData) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<T, Call> TransactionExtension<Call> for CheckBlockedOrigin<T, Call> |
| 41 | +where |
| 42 | + T: frame_system::Config + Send + Sync, |
| 43 | + T::AccountId: From<H160>, |
| 44 | + T: pallet_bfc_utility::Config, |
| 45 | + Call: Dispatchable + Clone + Eq + TypeInfo + Send + Sync + 'static, |
| 46 | + <Call as Dispatchable>::RuntimeOrigin: AsSystemOriginSigner<T::AccountId>, |
| 47 | +{ |
| 48 | + const IDENTIFIER: &'static str = "CheckBlockedOrigin"; |
| 49 | + type Implicit = (); |
| 50 | + type Pre = (); |
| 51 | + type Val = (); |
| 52 | + |
| 53 | + fn weight(&self, _call: &Call) -> Weight { |
| 54 | + Weight::zero() |
| 55 | + } |
| 56 | + |
| 57 | + fn validate( |
| 58 | + &self, |
| 59 | + origin: <Call as Dispatchable>::RuntimeOrigin, |
| 60 | + _call: &Call, |
| 61 | + _info: &DispatchInfoOf<Call>, |
| 62 | + _len: usize, |
| 63 | + _self_implicit: Self::Implicit, |
| 64 | + _inherited_implication: &impl Encode, |
| 65 | + _source: TransactionSource, |
| 66 | + ) -> Result< |
| 67 | + ( |
| 68 | + sp_runtime::transaction_validity::ValidTransaction, |
| 69 | + Self::Val, |
| 70 | + <Call as Dispatchable>::RuntimeOrigin, |
| 71 | + ), |
| 72 | + TransactionValidityError, |
| 73 | + > { |
| 74 | + if let Some(who) = origin.as_system_origin_signer() { |
| 75 | + if pallet_bfc_utility::Pallet::<T>::is_blocked_account(who) { |
| 76 | + return Err(TransactionValidityError::Invalid( |
| 77 | + sp_runtime::transaction_validity::InvalidTransaction::BadSigner, |
| 78 | + )); |
| 79 | + } |
| 80 | + } |
| 81 | + Ok((sp_runtime::transaction_validity::ValidTransaction::default(), (), origin)) |
| 82 | + } |
| 83 | + |
| 84 | + fn prepare( |
| 85 | + self, |
| 86 | + _val: Self::Val, |
| 87 | + _origin: &<Call as Dispatchable>::RuntimeOrigin, |
| 88 | + _call: &Call, |
| 89 | + _info: &DispatchInfoOf<Call>, |
| 90 | + _len: usize, |
| 91 | + ) -> Result<Self::Pre, TransactionValidityError> { |
| 92 | + Ok(()) |
| 93 | + } |
| 94 | +} |
0 commit comments