|
| 1 | +use codec::{Decode, DecodeWithMemTracking, Encode}; |
| 2 | +use frame_support::dispatch::{DispatchInfo, PostDispatchInfo}; |
| 3 | +use frame_support::traits::IsSubType; |
| 4 | +use frame_system::Config; |
| 5 | +use pallet_sudo::Call as SudoCall; |
| 6 | +use scale_info::TypeInfo; |
| 7 | +use sp_runtime::impl_tx_ext_default; |
| 8 | +use sp_runtime::traits::{ |
| 9 | + AsSystemOriginSigner, DispatchInfoOf, Dispatchable, Implication, TransactionExtension, |
| 10 | + ValidateResult, |
| 11 | +}; |
| 12 | +use sp_runtime::transaction_validity::{InvalidTransaction, TransactionSource}; |
| 13 | +use sp_std::marker::PhantomData; |
| 14 | +use subtensor_macros::freeze_struct; |
| 15 | + |
| 16 | +#[freeze_struct("99dce71278b36b44")] |
| 17 | +#[derive(Default, Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)] |
| 18 | +pub struct SudoTransactionExtension<T: Config + Send + Sync + TypeInfo>(pub PhantomData<T>); |
| 19 | + |
| 20 | +impl<T: Config + Send + Sync + TypeInfo> sp_std::fmt::Debug for SudoTransactionExtension<T> { |
| 21 | + #[cfg(feature = "std")] |
| 22 | + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 23 | + write!(f, "SudoTransactionExtension",) |
| 24 | + } |
| 25 | + #[cfg(not(feature = "std"))] |
| 26 | + fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 27 | + Ok(()) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<T: Config + Send + Sync + TypeInfo> SudoTransactionExtension<T> { |
| 32 | + pub fn new() -> Self { |
| 33 | + Self(Default::default()) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<T: Config + Send + Sync + TypeInfo + pallet_sudo::Config> |
| 38 | + TransactionExtension<<T as Config>::RuntimeCall> for SudoTransactionExtension<T> |
| 39 | +where |
| 40 | + <T as Config>::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>, |
| 41 | + <T as Config>::RuntimeOrigin: AsSystemOriginSigner<T::AccountId> + Clone, |
| 42 | + <T as Config>::RuntimeCall: IsSubType<SudoCall<T>>, |
| 43 | +{ |
| 44 | + const IDENTIFIER: &'static str = "SudoTransactionExtension"; |
| 45 | + |
| 46 | + type Implicit = (); |
| 47 | + type Val = (); |
| 48 | + type Pre = (); |
| 49 | + |
| 50 | + impl_tx_ext_default!(<T as Config>::RuntimeCall; weight prepare); |
| 51 | + |
| 52 | + fn validate( |
| 53 | + &self, |
| 54 | + origin: <T as Config>::RuntimeOrigin, |
| 55 | + call: &<T as Config>::RuntimeCall, |
| 56 | + _info: &DispatchInfoOf<<T as Config>::RuntimeCall>, |
| 57 | + _len: usize, |
| 58 | + _self_implicit: Self::Implicit, |
| 59 | + _inherited_implication: &impl Implication, |
| 60 | + _source: TransactionSource, |
| 61 | + ) -> ValidateResult<Self::Val, <T as Config>::RuntimeCall> { |
| 62 | + // Ensure the transaction is signed, else we just skip the extension. |
| 63 | + let Some(who) = origin.as_system_origin_signer() else { |
| 64 | + return Ok((Default::default(), (), origin)); |
| 65 | + }; |
| 66 | + |
| 67 | + // Check validity of the signer for sudo call |
| 68 | + if let Some(_sudo_call) = IsSubType::<pallet_sudo::Call<T>>::is_sub_type(call) { |
| 69 | + let sudo_key = pallet_sudo::pallet::Key::<T>::get(); |
| 70 | + |
| 71 | + // No sudo key configured → reject |
| 72 | + let Some(expected_who) = sudo_key else { |
| 73 | + return Err(InvalidTransaction::BadSigner.into()); |
| 74 | + }; |
| 75 | + |
| 76 | + // Signer does not match the sudo key → reject |
| 77 | + if *who != expected_who { |
| 78 | + return Err(InvalidTransaction::BadSigner.into()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Ok((Default::default(), (), origin)) |
| 83 | + } |
| 84 | +} |
0 commit comments