forked from ghostchain/ghost-node
		
	rustfmt ghost sudo and fix typos
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
		
							parent
							
								
									eb3b4f2651
								
							
						
					
					
						commit
						792e70c988
					
				@ -1,6 +1,6 @@
 | 
				
			|||||||
[package]
 | 
					[package]
 | 
				
			||||||
name = "ghost-sudo"
 | 
					name = "ghost-sudo"
 | 
				
			||||||
version = "0.0.1"
 | 
					version = "0.0.2"
 | 
				
			||||||
description = "Port of the sudo pallet because of dependencies issue"
 | 
					description = "Port of the sudo pallet because of dependencies issue"
 | 
				
			||||||
license.workspace = true
 | 
					license.workspace = true
 | 
				
			||||||
authors.workspace = true
 | 
					authors.workspace = true
 | 
				
			||||||
 | 
				
			|||||||
@ -4,67 +4,74 @@ use frame_benchmarking::v2::*;
 | 
				
			|||||||
use frame_system::RawOrigin;
 | 
					use frame_system::RawOrigin;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn assert_last_event<T: Config>(generic_event: crate::Event<T>) {
 | 
					fn assert_last_event<T: Config>(generic_event: crate::Event<T>) {
 | 
				
			||||||
	let re: <T as Config>::RuntimeEvent = generic_event.into();
 | 
					    let re: <T as Config>::RuntimeEvent = generic_event.into();
 | 
				
			||||||
	frame_system::Pallet::<T>::assert_last_event(re.into());
 | 
					    frame_system::Pallet::<T>::assert_last_event(re.into());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[benchmarks(where <T as Config>::RuntimeCall: From<frame_system::Call<T>>)]
 | 
					#[benchmarks(where <T as Config>::RuntimeCall: From<frame_system::Call<T>>)]
 | 
				
			||||||
mod benchmarks {
 | 
					mod benchmarks {
 | 
				
			||||||
	use super::*;
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[benchmark]
 | 
					    #[benchmark]
 | 
				
			||||||
	fn set_key() {
 | 
					    fn set_key() {
 | 
				
			||||||
		let caller: T::AccountId = whitelisted_caller();
 | 
					        let caller: T::AccountId = whitelisted_caller();
 | 
				
			||||||
		Key::<T>::put(&caller);
 | 
					        Key::<T>::put(&caller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let new_sudoer: T::AccountId = account("sudoer", 0, 0);
 | 
					        let new_sudoer: T::AccountId = account("sudoer", 0, 0);
 | 
				
			||||||
		let new_sudoer_lookup = T::Lookup::unlookup(new_sudoer.clone());
 | 
					        let new_sudoer_lookup = T::Lookup::unlookup(new_sudoer.clone());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[extrinsic_call]
 | 
					        #[extrinsic_call]
 | 
				
			||||||
		_(RawOrigin::Signed(caller.clone()), new_sudoer_lookup);
 | 
					        _(RawOrigin::Signed(caller.clone()), new_sudoer_lookup);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert_last_event::<T>(Event::KeyChanged { old: Some(caller), new: new_sudoer });
 | 
					        assert_last_event::<T>(Event::KeyChanged {
 | 
				
			||||||
	}
 | 
					            old: Some(caller),
 | 
				
			||||||
 | 
					            new: new_sudoer,
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[benchmark]
 | 
					    #[benchmark]
 | 
				
			||||||
	fn sudo() {
 | 
					    fn sudo() {
 | 
				
			||||||
		let caller: T::AccountId = whitelisted_caller();
 | 
					        let caller: T::AccountId = whitelisted_caller();
 | 
				
			||||||
		Key::<T>::put(&caller);
 | 
					        Key::<T>::put(&caller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = frame_system::Call::remark { remark: vec![] }.into();
 | 
					        let call = frame_system::Call::remark { remark: vec![] }.into();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[extrinsic_call]
 | 
					        #[extrinsic_call]
 | 
				
			||||||
		_(RawOrigin::Signed(caller), Box::new(call));
 | 
					        _(RawOrigin::Signed(caller), Box::new(call));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert_last_event::<T>(Event::Sudid { sudo_result: Ok(()) })
 | 
					        assert_last_event::<T>(Event::Sudid {
 | 
				
			||||||
	}
 | 
					            sudo_result: Ok(()),
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[benchmark]
 | 
					    #[benchmark]
 | 
				
			||||||
	fn sudo_as() {
 | 
					    fn sudo_as() {
 | 
				
			||||||
		let caller: T::AccountId = whitelisted_caller();
 | 
					        let caller: T::AccountId = whitelisted_caller();
 | 
				
			||||||
		Key::<T>::put(caller.clone());
 | 
					        Key::<T>::put(caller.clone());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = frame_system::Call::remark { remark: vec![] }.into();
 | 
					        let call = frame_system::Call::remark { remark: vec![] }.into();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let who: T::AccountId = account("as", 0, 0);
 | 
					        let who: T::AccountId = account("as", 0, 0);
 | 
				
			||||||
		let who_lookup = T::Lookup::unlookup(who);
 | 
					        let who_lookup = T::Lookup::unlookup(who);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[extrinsic_call]
 | 
					        #[extrinsic_call]
 | 
				
			||||||
		_(RawOrigin::Signed(caller), who_lookup, Box::new(call));
 | 
					        _(RawOrigin::Signed(caller), who_lookup, Box::new(call));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert_last_event::<T>(Event::SudoAsDone { sudo_result: Ok(()) })
 | 
					        assert_last_event::<T>(Event::SudoAsDone {
 | 
				
			||||||
	}
 | 
					            sudo_result: Ok(()),
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[benchmark]
 | 
					    #[benchmark]
 | 
				
			||||||
	fn remove_key() {
 | 
					    fn remove_key() {
 | 
				
			||||||
		let caller: T::AccountId = whitelisted_caller();
 | 
					        let caller: T::AccountId = whitelisted_caller();
 | 
				
			||||||
		Key::<T>::put(&caller);
 | 
					        Key::<T>::put(&caller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[extrinsic_call]
 | 
					        #[extrinsic_call]
 | 
				
			||||||
		_(RawOrigin::Signed(caller.clone()));
 | 
					        _(RawOrigin::Signed(caller.clone()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert_last_event::<T>(Event::KeyRemoved {});
 | 
					        assert_last_event::<T>(Event::KeyRemoved {});
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	impl_benchmark_test_suite!(Pallet, crate::mock::new_bench_ext(), crate::mock::Test);
 | 
					    impl_benchmark_test_suite!(Pallet, crate::mock::new_bench_ext(), crate::mock::Test);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -3,11 +3,11 @@ use codec::{Decode, Encode};
 | 
				
			|||||||
use frame_support::{dispatch::DispatchInfo, ensure};
 | 
					use frame_support::{dispatch::DispatchInfo, ensure};
 | 
				
			||||||
use scale_info::TypeInfo;
 | 
					use scale_info::TypeInfo;
 | 
				
			||||||
use sp_runtime::{
 | 
					use sp_runtime::{
 | 
				
			||||||
	traits::{DispatchInfoOf, Dispatchable, SignedExtension},
 | 
					    traits::{DispatchInfoOf, Dispatchable, SignedExtension},
 | 
				
			||||||
	transaction_validity::{
 | 
					    transaction_validity::{
 | 
				
			||||||
		InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError,
 | 
					        InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError,
 | 
				
			||||||
		UnknownTransaction, ValidTransaction,
 | 
					        UnknownTransaction, ValidTransaction,
 | 
				
			||||||
	},
 | 
					    },
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use sp_std::{fmt, marker::PhantomData};
 | 
					use sp_std::{fmt, marker::PhantomData};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -16,67 +16,67 @@ use sp_std::{fmt, marker::PhantomData};
 | 
				
			|||||||
pub struct CheckOnlySudoAccount<T: Config + Send + Sync>(PhantomData<T>);
 | 
					pub struct CheckOnlySudoAccount<T: Config + Send + Sync>(PhantomData<T>);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<T: Config + Send + Sync> Default for CheckOnlySudoAccount<T> {
 | 
					impl<T: Config + Send + Sync> Default for CheckOnlySudoAccount<T> {
 | 
				
			||||||
	fn default() -> Self {
 | 
					    fn default() -> Self {
 | 
				
			||||||
		Self(Default::default())
 | 
					        Self(Default::default())
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<T: Config + Send + Sync> fmt::Debug for CheckOnlySudoAccount<T> {
 | 
					impl<T: Config + Send + Sync> fmt::Debug for CheckOnlySudoAccount<T> {
 | 
				
			||||||
	#[cfg(feature = "std")]
 | 
					    #[cfg(feature = "std")]
 | 
				
			||||||
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
					    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
				
			||||||
		write!(f, "CheckOnlySudoAccount")
 | 
					        write!(f, "CheckOnlySudoAccount")
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[cfg(not(feature = "std"))]
 | 
					    #[cfg(not(feature = "std"))]
 | 
				
			||||||
	fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
 | 
					    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
 | 
				
			||||||
		Ok(())
 | 
					        Ok(())
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<T: Config + Send + Sync> CheckOnlySudoAccount<T> {
 | 
					impl<T: Config + Send + Sync> CheckOnlySudoAccount<T> {
 | 
				
			||||||
	/// Creates new `SignedExtension` to check sudo key.
 | 
					    /// Creates new `SignedExtension` to check sudo key.
 | 
				
			||||||
	pub fn new() -> Self {
 | 
					    pub fn new() -> Self {
 | 
				
			||||||
		Self::default()
 | 
					        Self::default()
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<T: Config + Send + Sync> SignedExtension for CheckOnlySudoAccount<T>
 | 
					impl<T: Config + Send + Sync> SignedExtension for CheckOnlySudoAccount<T>
 | 
				
			||||||
where
 | 
					where
 | 
				
			||||||
	<T as Config>::RuntimeCall: Dispatchable<Info = DispatchInfo>,
 | 
					    <T as Config>::RuntimeCall: Dispatchable<Info = DispatchInfo>,
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	const IDENTIFIER: &'static str = "CheckOnlySudoAccount";
 | 
					    const IDENTIFIER: &'static str = "CheckOnlySudoAccount";
 | 
				
			||||||
	type AccountId = T::AccountId;
 | 
					    type AccountId = T::AccountId;
 | 
				
			||||||
	type Call = <T as Config>::RuntimeCall;
 | 
					    type Call = <T as Config>::RuntimeCall;
 | 
				
			||||||
	type AdditionalSigned = ();
 | 
					    type AdditionalSigned = ();
 | 
				
			||||||
	type Pre = ();
 | 
					    type Pre = ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
 | 
					    fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
 | 
				
			||||||
		Ok(())
 | 
					        Ok(())
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fn validate(
 | 
					    fn validate(
 | 
				
			||||||
		&self,
 | 
					        &self,
 | 
				
			||||||
		who: &Self::AccountId,
 | 
					        who: &Self::AccountId,
 | 
				
			||||||
		_call: &Self::Call,
 | 
					        _call: &Self::Call,
 | 
				
			||||||
		info: &DispatchInfoOf<Self::Call>,
 | 
					        info: &DispatchInfoOf<Self::Call>,
 | 
				
			||||||
		_len: usize,
 | 
					        _len: usize,
 | 
				
			||||||
	) -> TransactionValidity {
 | 
					    ) -> TransactionValidity {
 | 
				
			||||||
		let sudo_key: T::AccountId = Key::<T>::get().ok_or(UnknownTransaction::CannotLookup)?;
 | 
					        let sudo_key: T::AccountId = Key::<T>::get().ok_or(UnknownTransaction::CannotLookup)?;
 | 
				
			||||||
		ensure!(*who == sudo_key, InvalidTransaction::BadSigner);
 | 
					        ensure!(*who == sudo_key, InvalidTransaction::BadSigner);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Ok(ValidTransaction {
 | 
					        Ok(ValidTransaction {
 | 
				
			||||||
			priority: info.weight.ref_time() as TransactionPriority,
 | 
					            priority: info.weight.ref_time() as TransactionPriority,
 | 
				
			||||||
			..Default::default()
 | 
					            ..Default::default()
 | 
				
			||||||
		})
 | 
					        })
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fn pre_dispatch(
 | 
					    fn pre_dispatch(
 | 
				
			||||||
		self,
 | 
					        self,
 | 
				
			||||||
		who: &Self::AccountId,
 | 
					        who: &Self::AccountId,
 | 
				
			||||||
		call: &Self::Call,
 | 
					        call: &Self::Call,
 | 
				
			||||||
		info: &DispatchInfoOf<Self::Call>,
 | 
					        info: &DispatchInfoOf<Self::Call>,
 | 
				
			||||||
		len: usize,
 | 
					        len: usize,
 | 
				
			||||||
	) -> Result<Self::Pre, TransactionValidityError> {
 | 
					    ) -> Result<Self::Pre, TransactionValidityError> {
 | 
				
			||||||
		self.validate(who, call, info, len).map(|_| ())
 | 
					        self.validate(who, call, info, len).map(|_| ())
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -23,181 +23,190 @@ type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[frame_support::pallet]
 | 
					#[frame_support::pallet]
 | 
				
			||||||
pub mod pallet {
 | 
					pub mod pallet {
 | 
				
			||||||
	use super::{DispatchResult, *};
 | 
					    use super::{DispatchResult, *};
 | 
				
			||||||
	use frame_support::pallet_prelude::*;
 | 
					    use frame_support::pallet_prelude::*;
 | 
				
			||||||
	use frame_system::{pallet_prelude::*, RawOrigin};
 | 
					    use frame_system::{pallet_prelude::*, RawOrigin};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pub mod config_preludes {
 | 
					    pub mod config_preludes {
 | 
				
			||||||
		use super::*;
 | 
					        use super::*;
 | 
				
			||||||
		use frame_support::derive_impl;
 | 
					        use frame_support::derive_impl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		pub struct TestDefaultConfig;
 | 
					        pub struct TestDefaultConfig;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
 | 
					        #[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
 | 
				
			||||||
		impl frame_system::DefaultConfig for TestDefaultConfig {}
 | 
					        impl frame_system::DefaultConfig for TestDefaultConfig {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[frame_support::register_default_impl(TestDefaultConfig)]
 | 
					        #[frame_support::register_default_impl(TestDefaultConfig)]
 | 
				
			||||||
		impl DefaultConfig for TestDefaultConfig {
 | 
					        impl DefaultConfig for TestDefaultConfig {
 | 
				
			||||||
			type WeightInfo = ();
 | 
					            type WeightInfo = ();
 | 
				
			||||||
			#[inject_runtime_type]
 | 
					            #[inject_runtime_type]
 | 
				
			||||||
			type RuntimeEvent = ();
 | 
					            type RuntimeEvent = ();
 | 
				
			||||||
			#[inject_runtime_type]
 | 
					            #[inject_runtime_type]
 | 
				
			||||||
			type RuntimeCall = ();
 | 
					            type RuntimeCall = ();
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
	#[pallet::config(with_default)]
 | 
					    #[pallet::config(with_default)]
 | 
				
			||||||
	pub trait Config: frame_system::Config {
 | 
					    pub trait Config: frame_system::Config {
 | 
				
			||||||
		#[pallet::no_default_bounds]
 | 
					        #[pallet::no_default_bounds]
 | 
				
			||||||
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
 | 
					        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[pallet::no_default_bounds]
 | 
					        #[pallet::no_default_bounds]
 | 
				
			||||||
		type RuntimeCall: Parameter
 | 
					        type RuntimeCall: Parameter
 | 
				
			||||||
			+ UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
 | 
					            + UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
 | 
				
			||||||
			+ GetDispatchInfo;
 | 
					            + GetDispatchInfo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		type WeightInfo: WeightInfo;
 | 
					        type WeightInfo: WeightInfo;
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::pallet]
 | 
					    #[pallet::pallet]
 | 
				
			||||||
	pub struct Pallet<T>(_);
 | 
					    pub struct Pallet<T>(_);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::call]
 | 
					    #[pallet::call]
 | 
				
			||||||
	impl<T: Config> Pallet<T> {
 | 
					    impl<T: Config> Pallet<T> {
 | 
				
			||||||
		#[pallet::call_index(0)]
 | 
					        #[pallet::call_index(0)]
 | 
				
			||||||
		#[pallet::weight({
 | 
					        #[pallet::weight({
 | 
				
			||||||
			let dispatch_info = call.get_dispatch_info();
 | 
								let dispatch_info = call.get_dispatch_info();
 | 
				
			||||||
			(
 | 
								(
 | 
				
			||||||
				T::WeightInfo::sudo().saturating_add(dispatch_info.weight),
 | 
									T::WeightInfo::sudo().saturating_add(dispatch_info.weight),
 | 
				
			||||||
				dispatch_info.class
 | 
									dispatch_info.class
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		})]
 | 
							})]
 | 
				
			||||||
		pub fn sudo(
 | 
					        pub fn sudo(
 | 
				
			||||||
			origin: OriginFor<T>,
 | 
					            origin: OriginFor<T>,
 | 
				
			||||||
			call: Box<<T as Config>::RuntimeCall>,
 | 
					            call: Box<<T as Config>::RuntimeCall>,
 | 
				
			||||||
		) -> DispatchResultWithPostInfo {
 | 
					        ) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			Self::ensure_sudo(origin)?;
 | 
					            Self::ensure_sudo(origin)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			let res = call.dispatch_bypass_filter(RawOrigin::Root.into());
 | 
					            let res = call.dispatch_bypass_filter(RawOrigin::Root.into());
 | 
				
			||||||
			Self::deposit_event(Event::Sudid { sudo_result: res.map(|_| ()).map_err(|e| e.error) });
 | 
					            Self::deposit_event(Event::Sudid {
 | 
				
			||||||
 | 
					                sudo_result: res.map(|_| ()).map_err(|e| e.error),
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			Ok(Pays::No.into())
 | 
					            Ok(Pays::No.into())
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[pallet::call_index(1)]
 | 
					        #[pallet::call_index(1)]
 | 
				
			||||||
		#[pallet::weight((*weight, call.get_dispatch_info().class))]
 | 
					        #[pallet::weight((*weight, call.get_dispatch_info().class))]
 | 
				
			||||||
		pub fn sudo_unchecked_weight(
 | 
					        pub fn sudo_unchecked_weight(
 | 
				
			||||||
			origin: OriginFor<T>,
 | 
					            origin: OriginFor<T>,
 | 
				
			||||||
			call: Box<<T as Config>::RuntimeCall>,
 | 
					            call: Box<<T as Config>::RuntimeCall>,
 | 
				
			||||||
			weight: Weight,
 | 
					            weight: Weight,
 | 
				
			||||||
		) -> DispatchResultWithPostInfo {
 | 
					        ) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			Self::ensure_sudo(origin)?;
 | 
					            Self::ensure_sudo(origin)?;
 | 
				
			||||||
			let _ = weight;
 | 
					            let _ = weight;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			let res = call.dispatch_bypass_filter(RawOrigin::Root.into());
 | 
					            let res = call.dispatch_bypass_filter(RawOrigin::Root.into());
 | 
				
			||||||
			Self::deposit_event(Event::Sudid { sudo_result: res.map(|_| ()).map_err(|e| e.error) });
 | 
					            Self::deposit_event(Event::Sudid {
 | 
				
			||||||
 | 
					                sudo_result: res.map(|_| ()).map_err(|e| e.error),
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			Ok(Pays::No.into())
 | 
					            Ok(Pays::No.into())
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[pallet::call_index(2)]
 | 
					        #[pallet::call_index(2)]
 | 
				
			||||||
		#[pallet::weight(T::WeightInfo::set_key())]
 | 
					        #[pallet::weight(T::WeightInfo::set_key())]
 | 
				
			||||||
		pub fn set_key(
 | 
					        pub fn set_key(
 | 
				
			||||||
			origin: OriginFor<T>,
 | 
					            origin: OriginFor<T>,
 | 
				
			||||||
			new: AccountIdLookupOf<T>,
 | 
					            new: AccountIdLookupOf<T>,
 | 
				
			||||||
		) -> DispatchResultWithPostInfo {
 | 
					        ) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			Self::ensure_sudo(origin)?;
 | 
					            Self::ensure_sudo(origin)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			let new = T::Lookup::lookup(new)?;
 | 
					            let new = T::Lookup::lookup(new)?;
 | 
				
			||||||
			Self::deposit_event(Event::KeyChanged { old: Key::<T>::get(), new: new.clone() });
 | 
					            Self::deposit_event(Event::KeyChanged {
 | 
				
			||||||
			Key::<T>::put(new);
 | 
					                old: Key::<T>::get(),
 | 
				
			||||||
 | 
					                new: new.clone(),
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            Key::<T>::put(new);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			Ok(Pays::No.into())
 | 
					            Ok(Pays::No.into())
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[pallet::call_index(3)]
 | 
					        #[pallet::call_index(3)]
 | 
				
			||||||
		#[pallet::weight({
 | 
					        #[pallet::weight({
 | 
				
			||||||
			let dispatch_info = call.get_dispatch_info();
 | 
								let dispatch_info = call.get_dispatch_info();
 | 
				
			||||||
			(
 | 
								(
 | 
				
			||||||
				T::WeightInfo::sudo_as().saturating_add(dispatch_info.weight),
 | 
									T::WeightInfo::sudo_as().saturating_add(dispatch_info.weight),
 | 
				
			||||||
				dispatch_info.class,
 | 
									dispatch_info.class,
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		})]
 | 
							})]
 | 
				
			||||||
		pub fn sudo_as(
 | 
					        pub fn sudo_as(
 | 
				
			||||||
			origin: OriginFor<T>,
 | 
					            origin: OriginFor<T>,
 | 
				
			||||||
			who: AccountIdLookupOf<T>,
 | 
					            who: AccountIdLookupOf<T>,
 | 
				
			||||||
			call: Box<<T as Config>::RuntimeCall>,
 | 
					            call: Box<<T as Config>::RuntimeCall>,
 | 
				
			||||||
		) -> DispatchResultWithPostInfo {
 | 
					        ) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			Self::ensure_sudo(origin)?;
 | 
					            Self::ensure_sudo(origin)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			let who = T::Lookup::lookup(who)?;
 | 
					            let who = T::Lookup::lookup(who)?;
 | 
				
			||||||
			let res = call.dispatch_bypass_filter(RawOrigin::Signed(who).into());
 | 
					            let res = call.dispatch_bypass_filter(RawOrigin::Signed(who).into());
 | 
				
			||||||
			Self::deposit_event(Event::SudoAsDone {
 | 
					            Self::deposit_event(Event::SudoAsDone {
 | 
				
			||||||
				sudo_result: res.map(|_| ()).map_err(|e| e.error),
 | 
					                sudo_result: res.map(|_| ()).map_err(|e| e.error),
 | 
				
			||||||
			});
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			Ok(Pays::No.into())
 | 
					            Ok(Pays::No.into())
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[pallet::call_index(4)]
 | 
					        #[pallet::call_index(4)]
 | 
				
			||||||
		#[pallet::weight(T::WeightInfo::remove_key())]
 | 
					        #[pallet::weight(T::WeightInfo::remove_key())]
 | 
				
			||||||
		pub fn remove_key(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
 | 
					        pub fn remove_key(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			Self::ensure_sudo(origin)?;
 | 
					            Self::ensure_sudo(origin)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			Self::deposit_event(Event::KeyRemoved {});
 | 
					            Self::deposit_event(Event::KeyRemoved {});
 | 
				
			||||||
			Key::<T>::kill();
 | 
					            Key::<T>::kill();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			Ok(Pays::No.into())
 | 
					            Ok(Pays::No.into())
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::event]
 | 
					    #[pallet::event]
 | 
				
			||||||
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
 | 
					    #[pallet::generate_deposit(pub(super) fn deposit_event)]
 | 
				
			||||||
	pub enum Event<T: Config> {
 | 
					    pub enum Event<T: Config> {
 | 
				
			||||||
		Sudid {
 | 
					        Sudid {
 | 
				
			||||||
			sudo_result: DispatchResult,
 | 
					            sudo_result: DispatchResult,
 | 
				
			||||||
		},
 | 
					        },
 | 
				
			||||||
		KeyChanged {
 | 
					        KeyChanged {
 | 
				
			||||||
			old: Option<T::AccountId>,
 | 
					            old: Option<T::AccountId>,
 | 
				
			||||||
			new: T::AccountId,
 | 
					            new: T::AccountId,
 | 
				
			||||||
		},
 | 
					        },
 | 
				
			||||||
		KeyRemoved,
 | 
					        KeyRemoved,
 | 
				
			||||||
		SudoAsDone { sudo_result: DispatchResult },
 | 
					        SudoAsDone {
 | 
				
			||||||
	}
 | 
					            sudo_result: DispatchResult,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::error]
 | 
					    #[pallet::error]
 | 
				
			||||||
	pub enum Error<T> {
 | 
					    pub enum Error<T> {
 | 
				
			||||||
		RequireSudo,
 | 
					        RequireSudo,
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::storage]
 | 
					    #[pallet::storage]
 | 
				
			||||||
	pub(super) type Key<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
 | 
					    pub(super) type Key<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::genesis_config]
 | 
					    #[pallet::genesis_config]
 | 
				
			||||||
	#[derive(frame_support::DefaultNoBound)]
 | 
					    #[derive(frame_support::DefaultNoBound)]
 | 
				
			||||||
	pub struct GenesisConfig<T: Config> {
 | 
					    pub struct GenesisConfig<T: Config> {
 | 
				
			||||||
		pub key: Option<T::AccountId>,
 | 
					        pub key: Option<T::AccountId>,
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::genesis_build]
 | 
					    #[pallet::genesis_build]
 | 
				
			||||||
	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
 | 
					    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
 | 
				
			||||||
		fn build(&self) {
 | 
					        fn build(&self) {
 | 
				
			||||||
			Key::<T>::set(self.key.clone());
 | 
					            Key::<T>::set(self.key.clone());
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	impl<T: Config> Pallet<T> {
 | 
					    impl<T: Config> Pallet<T> {
 | 
				
			||||||
		pub(crate) fn ensure_sudo(origin: OriginFor<T>) -> DispatchResult {
 | 
					        pub(crate) fn ensure_sudo(origin: OriginFor<T>) -> DispatchResult {
 | 
				
			||||||
			let sender = ensure_signed_or_root(origin)?;
 | 
					            let sender = ensure_signed_or_root(origin)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if let Some(sender) = sender {
 | 
					            if let Some(sender) = sender {
 | 
				
			||||||
				if Key::<T>::get().map_or(false, |k| k == sender) {
 | 
					                if Key::<T>::get().map_or(false, |k| k == sender) {
 | 
				
			||||||
					Ok(())
 | 
					                    Ok(())
 | 
				
			||||||
				} else {
 | 
					                } else {
 | 
				
			||||||
					Err(Error::<T>::RequireSudo.into())
 | 
					                    Err(Error::<T>::RequireSudo.into())
 | 
				
			||||||
				}
 | 
					                }
 | 
				
			||||||
			} else {
 | 
					            } else {
 | 
				
			||||||
				Ok(())
 | 
					                Ok(())
 | 
				
			||||||
			}
 | 
					            }
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -6,104 +6,122 @@ use sp_runtime::BuildStorage;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[frame_support::pallet]
 | 
					#[frame_support::pallet]
 | 
				
			||||||
pub mod logger {
 | 
					pub mod logger {
 | 
				
			||||||
	use frame_support::pallet_prelude::*;
 | 
					    use frame_support::pallet_prelude::*;
 | 
				
			||||||
	use frame_system::pallet_prelude::*;
 | 
					    use frame_system::pallet_prelude::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::config]
 | 
					    #[pallet::config]
 | 
				
			||||||
	pub trait Config: frame_system::Config {
 | 
					    pub trait Config: frame_system::Config {
 | 
				
			||||||
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
 | 
					        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::pallet]
 | 
					    #[pallet::pallet]
 | 
				
			||||||
	pub struct Pallet<T>(_);
 | 
					    pub struct Pallet<T>(_);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::call]
 | 
					    #[pallet::call]
 | 
				
			||||||
	impl<T: Config> Pallet<T> {
 | 
					    impl<T: Config> Pallet<T> {
 | 
				
			||||||
		#[pallet::call_index(0)]
 | 
					        #[pallet::call_index(0)]
 | 
				
			||||||
		#[pallet::weight(*weight)]
 | 
					        #[pallet::weight(*weight)]
 | 
				
			||||||
		pub fn privileged_i32_log(
 | 
					        pub fn privileged_i32_log(
 | 
				
			||||||
			origin: OriginFor<T>,
 | 
					            origin: OriginFor<T>,
 | 
				
			||||||
			i: i32,
 | 
					            i: i32,
 | 
				
			||||||
			weight: Weight,
 | 
					            weight: Weight,
 | 
				
			||||||
		) -> DispatchResultWithPostInfo {
 | 
					        ) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			ensure_root(origin)?;
 | 
					            ensure_root(origin)?;
 | 
				
			||||||
			<I32Log<T>>::try_append(i).map_err(|_| "could not append")?;
 | 
					            <I32Log<T>>::try_append(i).map_err(|_| "could not append")?;
 | 
				
			||||||
			Self::deposit_event(Event::AppendI32 { value: i, weight });
 | 
					            Self::deposit_event(Event::AppendI32 { value: i, weight });
 | 
				
			||||||
			Ok(().into())
 | 
					            Ok(().into())
 | 
				
			||||||
		}
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		#[pallet::call_index(1)]
 | 
					        #[pallet::call_index(1)]
 | 
				
			||||||
		#[pallet::weight(*weight)]
 | 
					        #[pallet::weight(*weight)]
 | 
				
			||||||
		pub fn non_privileged_log(
 | 
					        pub fn non_privileged_log(
 | 
				
			||||||
			origin: OriginFor<T>,
 | 
					            origin: OriginFor<T>,
 | 
				
			||||||
			i: i32,
 | 
					            i: i32,
 | 
				
			||||||
			weight: Weight,
 | 
					            weight: Weight,
 | 
				
			||||||
		) -> DispatchResultWithPostInfo {
 | 
					        ) -> DispatchResultWithPostInfo {
 | 
				
			||||||
			let sender = ensure_signed(origin)?;
 | 
					            let sender = ensure_signed(origin)?;
 | 
				
			||||||
			<I32Log<T>>::try_append(i).map_err(|_| "could not append")?;
 | 
					            <I32Log<T>>::try_append(i).map_err(|_| "could not append")?;
 | 
				
			||||||
			<AccountLog<T>>::try_append(sender.clone()).map_err(|_| "could not append")?;
 | 
					            <AccountLog<T>>::try_append(sender.clone()).map_err(|_| "could not append")?;
 | 
				
			||||||
			Self::deposit_event(Event::AppendI32AndAccount { sender, value: i, weight });
 | 
					            Self::deposit_event(Event::AppendI32AndAccount {
 | 
				
			||||||
			Ok(().into())
 | 
					                sender,
 | 
				
			||||||
		}
 | 
					                value: i,
 | 
				
			||||||
	}
 | 
					                weight,
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            Ok(().into())
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::event]
 | 
					    #[pallet::event]
 | 
				
			||||||
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
 | 
					    #[pallet::generate_deposit(pub(super) fn deposit_event)]
 | 
				
			||||||
	pub enum Event<T: Config> {
 | 
					    pub enum Event<T: Config> {
 | 
				
			||||||
		AppendI32 { value: i32, weight: Weight },
 | 
					        AppendI32 {
 | 
				
			||||||
		AppendI32AndAccount { sender: T::AccountId, value: i32, weight: Weight },
 | 
					            value: i32,
 | 
				
			||||||
	}
 | 
					            weight: Weight,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        AppendI32AndAccount {
 | 
				
			||||||
 | 
					            sender: T::AccountId,
 | 
				
			||||||
 | 
					            value: i32,
 | 
				
			||||||
 | 
					            weight: Weight,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::storage]
 | 
					    #[pallet::storage]
 | 
				
			||||||
	#[pallet::getter(fn account_log)]
 | 
					    #[pallet::getter(fn account_log)]
 | 
				
			||||||
	pub(super) type AccountLog<T: Config> =
 | 
					    pub(super) type AccountLog<T: Config> =
 | 
				
			||||||
		StorageValue<_, BoundedVec<T::AccountId, ConstU32<1_000>>, ValueQuery>;
 | 
					        StorageValue<_, BoundedVec<T::AccountId, ConstU32<1_000>>, ValueQuery>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[pallet::storage]
 | 
					    #[pallet::storage]
 | 
				
			||||||
	#[pallet::getter(fn i32_log)]
 | 
					    #[pallet::getter(fn i32_log)]
 | 
				
			||||||
	pub(super) type I32Log<T> = StorageValue<_, BoundedVec<i32, ConstU32<1_000>>, ValueQuery>;
 | 
					    pub(super) type I32Log<T> = StorageValue<_, BoundedVec<i32, ConstU32<1_000>>, ValueQuery>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Block = frame_system::mocking::MockBlock<Test>;
 | 
					type Block = frame_system::mocking::MockBlock<Test>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
frame_support::construct_runtime!(
 | 
					frame_support::construct_runtime!(
 | 
				
			||||||
	pub enum Test
 | 
					    pub enum Test
 | 
				
			||||||
	{
 | 
					    {
 | 
				
			||||||
		System: frame_system,
 | 
					        System: frame_system,
 | 
				
			||||||
		Sudo: sudo,
 | 
					        Sudo: sudo,
 | 
				
			||||||
		Logger: logger,
 | 
					        Logger: logger,
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
 | 
					#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
 | 
				
			||||||
impl frame_system::Config for Test {
 | 
					impl frame_system::Config for Test {
 | 
				
			||||||
	type Block = Block;
 | 
					    type Block = Block;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl logger::Config for Test {
 | 
					impl logger::Config for Test {
 | 
				
			||||||
	type RuntimeEvent = RuntimeEvent;
 | 
					    type RuntimeEvent = RuntimeEvent;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Config for Test {
 | 
					impl Config for Test {
 | 
				
			||||||
	type RuntimeEvent = RuntimeEvent;
 | 
					    type RuntimeEvent = RuntimeEvent;
 | 
				
			||||||
	type RuntimeCall = RuntimeCall;
 | 
					    type RuntimeCall = RuntimeCall;
 | 
				
			||||||
	type WeightInfo = ();
 | 
					    type WeightInfo = ();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub type SudoCall = sudo::Call<Test>;
 | 
					pub type SudoCall = sudo::Call<Test>;
 | 
				
			||||||
pub type LoggerCall = logger::Call<Test>;
 | 
					pub type LoggerCall = logger::Call<Test>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities {
 | 
					pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities {
 | 
				
			||||||
	let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
 | 
					    let mut t = frame_system::GenesisConfig::<Test>::default()
 | 
				
			||||||
	sudo::GenesisConfig::<Test> { key: Some(root_key) }
 | 
					        .build_storage()
 | 
				
			||||||
		.assimilate_storage(&mut t)
 | 
					        .unwrap();
 | 
				
			||||||
		.unwrap();
 | 
					    sudo::GenesisConfig::<Test> {
 | 
				
			||||||
	let mut ext: sp_io::TestExternalities = t.into();
 | 
					        key: Some(root_key),
 | 
				
			||||||
	ext.execute_with(|| System::set_block_number(1));
 | 
					    }
 | 
				
			||||||
	ext
 | 
					    .assimilate_storage(&mut t)
 | 
				
			||||||
 | 
					    .unwrap();
 | 
				
			||||||
 | 
					    let mut ext: sp_io::TestExternalities = t.into();
 | 
				
			||||||
 | 
					    ext.execute_with(|| System::set_block_number(1));
 | 
				
			||||||
 | 
					    ext
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[cfg(feature = "runtime-benchmarks")]
 | 
					#[cfg(feature = "runtime-benchmarks")]
 | 
				
			||||||
pub fn new_bench_ext() -> sp_io::TestExternalities {
 | 
					pub fn new_bench_ext() -> sp_io::TestExternalities {
 | 
				
			||||||
	frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
 | 
					    frame_system::GenesisConfig::<Test>::default()
 | 
				
			||||||
 | 
					        .build_storage()
 | 
				
			||||||
 | 
					        .unwrap()
 | 
				
			||||||
 | 
					        .into()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,188 +1,217 @@
 | 
				
			|||||||
use super::*;
 | 
					use super::*;
 | 
				
			||||||
use frame_support::{assert_noop, assert_ok, weights::Weight};
 | 
					use frame_support::{assert_noop, assert_ok, weights::Weight};
 | 
				
			||||||
use mock::{
 | 
					use mock::{
 | 
				
			||||||
	new_test_ext, Logger, LoggerCall, RuntimeCall, RuntimeEvent as TestEvent, RuntimeOrigin, Sudo,
 | 
					    new_test_ext, Logger, LoggerCall, RuntimeCall, RuntimeEvent as TestEvent, RuntimeOrigin, Sudo,
 | 
				
			||||||
	SudoCall, System, Test,
 | 
					    SudoCall, System, Test,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn test_setup_works() {
 | 
					fn test_setup_works() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		assert_eq!(Key::<Test>::get(), Some(1u64));
 | 
					        assert_eq!(Key::<Test>::get(), Some(1u64));
 | 
				
			||||||
		assert!(Logger::i32_log().is_empty());
 | 
					        assert!(Logger::i32_log().is_empty());
 | 
				
			||||||
		assert!(Logger::account_log().is_empty());
 | 
					        assert!(Logger::account_log().is_empty());
 | 
				
			||||||
	});
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[docify::export]
 | 
					#[docify::export]
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn sudo_basics() {
 | 
					fn sudo_basics() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1_000, 0),
 | 
					            weight: Weight::from_parts(1_000, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call));
 | 
					        assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call));
 | 
				
			||||||
		assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
					        assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1_000, 0),
 | 
					            weight: Weight::from_parts(1_000, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_noop!(Sudo::sudo(RuntimeOrigin::signed(2), call), Error::<Test>::RequireSudo);
 | 
					        assert_noop!(
 | 
				
			||||||
	});
 | 
					            Sudo::sudo(RuntimeOrigin::signed(2), call),
 | 
				
			||||||
 | 
					            Error::<Test>::RequireSudo
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn sudo_emits_events_correctly() {
 | 
					fn sudo_emits_events_correctly() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1, 0),
 | 
					            weight: Weight::from_parts(1, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call));
 | 
					        assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call));
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::Sudid { sudo_result: Ok(()) }));
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::Sudid {
 | 
				
			||||||
	})
 | 
					            sudo_result: Ok(()),
 | 
				
			||||||
 | 
					        }));
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn sudo_unchecked_weight_basics() {
 | 
					fn sudo_unchecked_weight_basics() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1_000, 0),
 | 
					            weight: Weight::from_parts(1_000, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo_unchecked_weight(
 | 
					        assert_ok!(Sudo::sudo_unchecked_weight(
 | 
				
			||||||
			RuntimeOrigin::signed(1),
 | 
					            RuntimeOrigin::signed(1),
 | 
				
			||||||
			call,
 | 
					            call,
 | 
				
			||||||
			Weight::from_parts(1_000, 0)
 | 
					            Weight::from_parts(1_000, 0)
 | 
				
			||||||
		));
 | 
					        ));
 | 
				
			||||||
		assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
					        assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1_000, 0),
 | 
					            weight: Weight::from_parts(1_000, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_noop!(
 | 
					        assert_noop!(
 | 
				
			||||||
			Sudo::sudo_unchecked_weight(
 | 
					            Sudo::sudo_unchecked_weight(
 | 
				
			||||||
				RuntimeOrigin::signed(2),
 | 
					                RuntimeOrigin::signed(2),
 | 
				
			||||||
				call,
 | 
					                call,
 | 
				
			||||||
				Weight::from_parts(1_000, 0)
 | 
					                Weight::from_parts(1_000, 0)
 | 
				
			||||||
			),
 | 
					            ),
 | 
				
			||||||
			Error::<Test>::RequireSudo,
 | 
					            Error::<Test>::RequireSudo,
 | 
				
			||||||
		);
 | 
					        );
 | 
				
			||||||
		assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
					        assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1, 0),
 | 
					            weight: Weight::from_parts(1, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		let sudo_unchecked_weight_call =
 | 
					        let sudo_unchecked_weight_call = SudoCall::sudo_unchecked_weight {
 | 
				
			||||||
			SudoCall::sudo_unchecked_weight { call, weight: Weight::from_parts(1_000, 0) };
 | 
					            call,
 | 
				
			||||||
		let info = sudo_unchecked_weight_call.get_dispatch_info();
 | 
					            weight: Weight::from_parts(1_000, 0),
 | 
				
			||||||
		assert_eq!(info.weight, Weight::from_parts(1_000, 0));
 | 
					        };
 | 
				
			||||||
	});
 | 
					        let info = sudo_unchecked_weight_call.get_dispatch_info();
 | 
				
			||||||
 | 
					        assert_eq!(info.weight, Weight::from_parts(1_000, 0));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn sudo_unchecked_weight_emits_events_correctly() {
 | 
					fn sudo_unchecked_weight_emits_events_correctly() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1, 0),
 | 
					            weight: Weight::from_parts(1, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo_unchecked_weight(
 | 
					        assert_ok!(Sudo::sudo_unchecked_weight(
 | 
				
			||||||
			RuntimeOrigin::signed(1),
 | 
					            RuntimeOrigin::signed(1),
 | 
				
			||||||
			call,
 | 
					            call,
 | 
				
			||||||
			Weight::from_parts(1_000, 0)
 | 
					            Weight::from_parts(1_000, 0)
 | 
				
			||||||
		));
 | 
					        ));
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::Sudid { sudo_result: Ok(()) }));
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::Sudid {
 | 
				
			||||||
	})
 | 
					            sudo_result: Ok(()),
 | 
				
			||||||
 | 
					        }));
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[docify::export]
 | 
					#[docify::export]
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn set_key_basics() {
 | 
					fn set_key_basics() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2));
 | 
					        assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2));
 | 
				
			||||||
		assert_eq!(Key::<Test>::get(), Some(2u64));
 | 
					        assert_eq!(Key::<Test>::get(), Some(2u64));
 | 
				
			||||||
	});
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		assert_noop!(Sudo::set_key(RuntimeOrigin::signed(2), 3), Error::<Test>::RequireSudo);
 | 
					        assert_noop!(
 | 
				
			||||||
	});
 | 
					            Sudo::set_key(RuntimeOrigin::signed(2), 3),
 | 
				
			||||||
 | 
					            Error::<Test>::RequireSudo
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn set_key_emits_events_correctly() {
 | 
					fn set_key_emits_events_correctly() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2));
 | 
					        assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2));
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::KeyChanged { old: Some(1), new: 2 }));
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::KeyChanged {
 | 
				
			||||||
		assert_ok!(Sudo::set_key(RuntimeOrigin::signed(2), 4));
 | 
					            old: Some(1),
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::KeyChanged { old: Some(2), new: 4 }));
 | 
					            new: 2,
 | 
				
			||||||
	});
 | 
					        }));
 | 
				
			||||||
 | 
					        assert_ok!(Sudo::set_key(RuntimeOrigin::signed(2), 4));
 | 
				
			||||||
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::KeyChanged {
 | 
				
			||||||
 | 
					            old: Some(2),
 | 
				
			||||||
 | 
					            new: 4,
 | 
				
			||||||
 | 
					        }));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn remove_key_works() {
 | 
					fn remove_key_works() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		assert_ok!(Sudo::remove_key(RuntimeOrigin::signed(1)));
 | 
					        assert_ok!(Sudo::remove_key(RuntimeOrigin::signed(1)));
 | 
				
			||||||
		assert!(Key::<Test>::get().is_none());
 | 
					        assert!(Key::<Test>::get().is_none());
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert_noop!(Sudo::remove_key(RuntimeOrigin::signed(1)), Error::<Test>::RequireSudo);
 | 
					        assert_noop!(
 | 
				
			||||||
		assert_noop!(Sudo::set_key(RuntimeOrigin::signed(1), 1), Error::<Test>::RequireSudo);
 | 
					            Sudo::remove_key(RuntimeOrigin::signed(1)),
 | 
				
			||||||
	});
 | 
					            Error::<Test>::RequireSudo
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					        assert_noop!(
 | 
				
			||||||
 | 
					            Sudo::set_key(RuntimeOrigin::signed(1), 1),
 | 
				
			||||||
 | 
					            Error::<Test>::RequireSudo
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn using_root_origin_works() {
 | 
					fn using_root_origin_works() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		assert_ok!(Sudo::remove_key(RuntimeOrigin::root()));
 | 
					        assert_ok!(Sudo::remove_key(RuntimeOrigin::root()));
 | 
				
			||||||
		assert!(Key::<Test>::get().is_none());
 | 
					        assert!(Key::<Test>::get().is_none());
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {}));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1));
 | 
					        assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1));
 | 
				
			||||||
		assert_eq!(Some(1), Key::<Test>::get());
 | 
					        assert_eq!(Some(1), Key::<Test>::get());
 | 
				
			||||||
	});
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn sudo_as_basics() {
 | 
					fn sudo_as_basics() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1_000, 0),
 | 
					            weight: Weight::from_parts(1_000, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
 | 
					        assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
 | 
				
			||||||
		assert!(Logger::i32_log().is_empty());
 | 
					        assert!(Logger::i32_log().is_empty());
 | 
				
			||||||
		assert!(Logger::account_log().is_empty());
 | 
					        assert!(Logger::account_log().is_empty());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1, 0),
 | 
					            weight: Weight::from_parts(1, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_noop!(Sudo::sudo_as(RuntimeOrigin::signed(3), 2, call), Error::<Test>::RequireSudo);
 | 
					        assert_noop!(
 | 
				
			||||||
 | 
					            Sudo::sudo_as(RuntimeOrigin::signed(3), 2, call),
 | 
				
			||||||
 | 
					            Error::<Test>::RequireSudo
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1, 0),
 | 
					            weight: Weight::from_parts(1, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
 | 
					        assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
 | 
				
			||||||
		assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
					        assert_eq!(Logger::i32_log(), vec![42i32]);
 | 
				
			||||||
		assert_eq!(Logger::account_log(), vec![2]);
 | 
					        assert_eq!(Logger::account_log(), vec![2]);
 | 
				
			||||||
	});
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[docify::export]
 | 
					#[docify::export]
 | 
				
			||||||
#[test]
 | 
					#[test]
 | 
				
			||||||
fn sudo_as_emits_events_correctly() {
 | 
					fn sudo_as_emits_events_correctly() {
 | 
				
			||||||
	new_test_ext(1).execute_with(|| {
 | 
					    new_test_ext(1).execute_with(|| {
 | 
				
			||||||
		let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
 | 
					        let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log {
 | 
				
			||||||
			i: 42,
 | 
					            i: 42,
 | 
				
			||||||
			weight: Weight::from_parts(1, 0),
 | 
					            weight: Weight::from_parts(1, 0),
 | 
				
			||||||
		}));
 | 
					        }));
 | 
				
			||||||
		assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
 | 
					        assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call));
 | 
				
			||||||
		System::assert_has_event(TestEvent::Sudo(Event::SudoAsDone { sudo_result: Ok(()) }));
 | 
					        System::assert_has_event(TestEvent::Sudo(Event::SudoAsDone {
 | 
				
			||||||
	});
 | 
					            sudo_result: Ok(()),
 | 
				
			||||||
 | 
					        }));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user