From 792e70c9882410cf79bbdf0bc6c1fa4a05cce834 Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Tue, 29 Jul 2025 14:57:58 +0300 Subject: [PATCH] rustfmt ghost sudo and fix typos Signed-off-by: Uncle Stretch --- pallets/sudo/Cargo.toml | 2 +- pallets/sudo/src/benchmarking.rs | 91 +++++----- pallets/sudo/src/extension.rs | 104 +++++------ pallets/sudo/src/lib.rs | 281 +++++++++++++++--------------- pallets/sudo/src/mock.rs | 154 +++++++++-------- pallets/sudo/src/tests.rs | 287 +++++++++++++++++-------------- 6 files changed, 491 insertions(+), 428 deletions(-) diff --git a/pallets/sudo/Cargo.toml b/pallets/sudo/Cargo.toml index 4490927..255adc9 100644 --- a/pallets/sudo/Cargo.toml +++ b/pallets/sudo/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-sudo" -version = "0.0.1" +version = "0.0.2" description = "Port of the sudo pallet because of dependencies issue" license.workspace = true authors.workspace = true diff --git a/pallets/sudo/src/benchmarking.rs b/pallets/sudo/src/benchmarking.rs index 759f0d8..4823a47 100644 --- a/pallets/sudo/src/benchmarking.rs +++ b/pallets/sudo/src/benchmarking.rs @@ -4,67 +4,74 @@ use frame_benchmarking::v2::*; use frame_system::RawOrigin; fn assert_last_event(generic_event: crate::Event) { - let re: ::RuntimeEvent = generic_event.into(); - frame_system::Pallet::::assert_last_event(re.into()); + let re: ::RuntimeEvent = generic_event.into(); + frame_system::Pallet::::assert_last_event(re.into()); } #[benchmarks(where ::RuntimeCall: From>)] mod benchmarks { - use super::*; + use super::*; - #[benchmark] - fn set_key() { - let caller: T::AccountId = whitelisted_caller(); - Key::::put(&caller); + #[benchmark] + fn set_key() { + let caller: T::AccountId = whitelisted_caller(); + Key::::put(&caller); - let new_sudoer: T::AccountId = account("sudoer", 0, 0); - let new_sudoer_lookup = T::Lookup::unlookup(new_sudoer.clone()); + let new_sudoer: T::AccountId = account("sudoer", 0, 0); + let new_sudoer_lookup = T::Lookup::unlookup(new_sudoer.clone()); - #[extrinsic_call] - _(RawOrigin::Signed(caller.clone()), new_sudoer_lookup); + #[extrinsic_call] + _(RawOrigin::Signed(caller.clone()), new_sudoer_lookup); - assert_last_event::(Event::KeyChanged { old: Some(caller), new: new_sudoer }); - } + assert_last_event::(Event::KeyChanged { + old: Some(caller), + new: new_sudoer, + }); + } - #[benchmark] - fn sudo() { - let caller: T::AccountId = whitelisted_caller(); - Key::::put(&caller); + #[benchmark] + fn sudo() { + let caller: T::AccountId = whitelisted_caller(); + Key::::put(&caller); - let call = frame_system::Call::remark { remark: vec![] }.into(); + let call = frame_system::Call::remark { remark: vec![] }.into(); - #[extrinsic_call] - _(RawOrigin::Signed(caller), Box::new(call)); + #[extrinsic_call] + _(RawOrigin::Signed(caller), Box::new(call)); - assert_last_event::(Event::Sudid { sudo_result: Ok(()) }) - } + assert_last_event::(Event::Sudid { + sudo_result: Ok(()), + }) + } - #[benchmark] - fn sudo_as() { - let caller: T::AccountId = whitelisted_caller(); - Key::::put(caller.clone()); + #[benchmark] + fn sudo_as() { + let caller: T::AccountId = whitelisted_caller(); + Key::::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_lookup = T::Lookup::unlookup(who); + let who: T::AccountId = account("as", 0, 0); + let who_lookup = T::Lookup::unlookup(who); - #[extrinsic_call] - _(RawOrigin::Signed(caller), who_lookup, Box::new(call)); + #[extrinsic_call] + _(RawOrigin::Signed(caller), who_lookup, Box::new(call)); - assert_last_event::(Event::SudoAsDone { sudo_result: Ok(()) }) - } + assert_last_event::(Event::SudoAsDone { + sudo_result: Ok(()), + }) + } - #[benchmark] - fn remove_key() { - let caller: T::AccountId = whitelisted_caller(); - Key::::put(&caller); + #[benchmark] + fn remove_key() { + let caller: T::AccountId = whitelisted_caller(); + Key::::put(&caller); - #[extrinsic_call] - _(RawOrigin::Signed(caller.clone())); + #[extrinsic_call] + _(RawOrigin::Signed(caller.clone())); - assert_last_event::(Event::KeyRemoved {}); - } + assert_last_event::(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); } diff --git a/pallets/sudo/src/extension.rs b/pallets/sudo/src/extension.rs index 40d94b9..87f7173 100644 --- a/pallets/sudo/src/extension.rs +++ b/pallets/sudo/src/extension.rs @@ -3,11 +3,11 @@ use codec::{Decode, Encode}; use frame_support::{dispatch::DispatchInfo, ensure}; use scale_info::TypeInfo; use sp_runtime::{ - traits::{DispatchInfoOf, Dispatchable, SignedExtension}, - transaction_validity::{ - InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError, - UnknownTransaction, ValidTransaction, - }, + traits::{DispatchInfoOf, Dispatchable, SignedExtension}, + transaction_validity::{ + InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError, + UnknownTransaction, ValidTransaction, + }, }; use sp_std::{fmt, marker::PhantomData}; @@ -16,67 +16,67 @@ use sp_std::{fmt, marker::PhantomData}; pub struct CheckOnlySudoAccount(PhantomData); impl Default for CheckOnlySudoAccount { - fn default() -> Self { - Self(Default::default()) - } + fn default() -> Self { + Self(Default::default()) + } } impl fmt::Debug for CheckOnlySudoAccount { - #[cfg(feature = "std")] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "CheckOnlySudoAccount") - } + #[cfg(feature = "std")] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "CheckOnlySudoAccount") + } - #[cfg(not(feature = "std"))] - fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { - Ok(()) - } + #[cfg(not(feature = "std"))] + fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { + Ok(()) + } } impl CheckOnlySudoAccount { - /// Creates new `SignedExtension` to check sudo key. - pub fn new() -> Self { - Self::default() - } + /// Creates new `SignedExtension` to check sudo key. + pub fn new() -> Self { + Self::default() + } } impl SignedExtension for CheckOnlySudoAccount where - ::RuntimeCall: Dispatchable, + ::RuntimeCall: Dispatchable, { - const IDENTIFIER: &'static str = "CheckOnlySudoAccount"; - type AccountId = T::AccountId; - type Call = ::RuntimeCall; - type AdditionalSigned = (); - type Pre = (); + const IDENTIFIER: &'static str = "CheckOnlySudoAccount"; + type AccountId = T::AccountId; + type Call = ::RuntimeCall; + type AdditionalSigned = (); + type Pre = (); - fn additional_signed(&self) -> Result { - Ok(()) - } + fn additional_signed(&self) -> Result { + Ok(()) + } - fn validate( - &self, - who: &Self::AccountId, - _call: &Self::Call, - info: &DispatchInfoOf, - _len: usize, - ) -> TransactionValidity { - let sudo_key: T::AccountId = Key::::get().ok_or(UnknownTransaction::CannotLookup)?; - ensure!(*who == sudo_key, InvalidTransaction::BadSigner); + fn validate( + &self, + who: &Self::AccountId, + _call: &Self::Call, + info: &DispatchInfoOf, + _len: usize, + ) -> TransactionValidity { + let sudo_key: T::AccountId = Key::::get().ok_or(UnknownTransaction::CannotLookup)?; + ensure!(*who == sudo_key, InvalidTransaction::BadSigner); - Ok(ValidTransaction { - priority: info.weight.ref_time() as TransactionPriority, - ..Default::default() - }) - } + Ok(ValidTransaction { + priority: info.weight.ref_time() as TransactionPriority, + ..Default::default() + }) + } - fn pre_dispatch( - self, - who: &Self::AccountId, - call: &Self::Call, - info: &DispatchInfoOf, - len: usize, - ) -> Result { - self.validate(who, call, info, len).map(|_| ()) - } + fn pre_dispatch( + self, + who: &Self::AccountId, + call: &Self::Call, + info: &DispatchInfoOf, + len: usize, + ) -> Result { + self.validate(who, call, info, len).map(|_| ()) + } } diff --git a/pallets/sudo/src/lib.rs b/pallets/sudo/src/lib.rs index 768954c..c50473e 100644 --- a/pallets/sudo/src/lib.rs +++ b/pallets/sudo/src/lib.rs @@ -23,181 +23,190 @@ type AccountIdLookupOf = <::Lookup as StaticLookup #[frame_support::pallet] pub mod pallet { - use super::{DispatchResult, *}; - use frame_support::pallet_prelude::*; - use frame_system::{pallet_prelude::*, RawOrigin}; + use super::{DispatchResult, *}; + use frame_support::pallet_prelude::*; + use frame_system::{pallet_prelude::*, RawOrigin}; - pub mod config_preludes { - use super::*; - use frame_support::derive_impl; + pub mod config_preludes { + use super::*; + use frame_support::derive_impl; - pub struct TestDefaultConfig; + pub struct TestDefaultConfig; - #[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)] - impl frame_system::DefaultConfig for TestDefaultConfig {} + #[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)] + impl frame_system::DefaultConfig for TestDefaultConfig {} - #[frame_support::register_default_impl(TestDefaultConfig)] - impl DefaultConfig for TestDefaultConfig { - type WeightInfo = (); - #[inject_runtime_type] - type RuntimeEvent = (); - #[inject_runtime_type] - type RuntimeCall = (); - } - } - #[pallet::config(with_default)] - pub trait Config: frame_system::Config { - #[pallet::no_default_bounds] - type RuntimeEvent: From> + IsType<::RuntimeEvent>; + #[frame_support::register_default_impl(TestDefaultConfig)] + impl DefaultConfig for TestDefaultConfig { + type WeightInfo = (); + #[inject_runtime_type] + type RuntimeEvent = (); + #[inject_runtime_type] + type RuntimeCall = (); + } + } + #[pallet::config(with_default)] + pub trait Config: frame_system::Config { + #[pallet::no_default_bounds] + type RuntimeEvent: From> + IsType<::RuntimeEvent>; - #[pallet::no_default_bounds] - type RuntimeCall: Parameter - + UnfilteredDispatchable - + GetDispatchInfo; + #[pallet::no_default_bounds] + type RuntimeCall: Parameter + + UnfilteredDispatchable + + GetDispatchInfo; - type WeightInfo: WeightInfo; - } + type WeightInfo: WeightInfo; + } - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); - #[pallet::call] - impl Pallet { - #[pallet::call_index(0)] - #[pallet::weight({ + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight({ let dispatch_info = call.get_dispatch_info(); ( T::WeightInfo::sudo().saturating_add(dispatch_info.weight), dispatch_info.class ) })] - pub fn sudo( - origin: OriginFor, - call: Box<::RuntimeCall>, - ) -> DispatchResultWithPostInfo { - Self::ensure_sudo(origin)?; + pub fn sudo( + origin: OriginFor, + call: Box<::RuntimeCall>, + ) -> DispatchResultWithPostInfo { + Self::ensure_sudo(origin)?; - let res = call.dispatch_bypass_filter(RawOrigin::Root.into()); - Self::deposit_event(Event::Sudid { sudo_result: res.map(|_| ()).map_err(|e| e.error) }); + let res = call.dispatch_bypass_filter(RawOrigin::Root.into()); + 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::weight((*weight, call.get_dispatch_info().class))] - pub fn sudo_unchecked_weight( - origin: OriginFor, - call: Box<::RuntimeCall>, - weight: Weight, - ) -> DispatchResultWithPostInfo { - Self::ensure_sudo(origin)?; - let _ = weight; + #[pallet::call_index(1)] + #[pallet::weight((*weight, call.get_dispatch_info().class))] + pub fn sudo_unchecked_weight( + origin: OriginFor, + call: Box<::RuntimeCall>, + weight: Weight, + ) -> DispatchResultWithPostInfo { + Self::ensure_sudo(origin)?; + let _ = weight; - let res = call.dispatch_bypass_filter(RawOrigin::Root.into()); - Self::deposit_event(Event::Sudid { sudo_result: res.map(|_| ()).map_err(|e| e.error) }); + let res = call.dispatch_bypass_filter(RawOrigin::Root.into()); + 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::weight(T::WeightInfo::set_key())] - pub fn set_key( - origin: OriginFor, - new: AccountIdLookupOf, - ) -> DispatchResultWithPostInfo { - Self::ensure_sudo(origin)?; + #[pallet::call_index(2)] + #[pallet::weight(T::WeightInfo::set_key())] + pub fn set_key( + origin: OriginFor, + new: AccountIdLookupOf, + ) -> DispatchResultWithPostInfo { + Self::ensure_sudo(origin)?; - let new = T::Lookup::lookup(new)?; - Self::deposit_event(Event::KeyChanged { old: Key::::get(), new: new.clone() }); - Key::::put(new); + let new = T::Lookup::lookup(new)?; + Self::deposit_event(Event::KeyChanged { + old: Key::::get(), + new: new.clone(), + }); + Key::::put(new); - Ok(Pays::No.into()) - } + Ok(Pays::No.into()) + } - #[pallet::call_index(3)] - #[pallet::weight({ + #[pallet::call_index(3)] + #[pallet::weight({ let dispatch_info = call.get_dispatch_info(); ( T::WeightInfo::sudo_as().saturating_add(dispatch_info.weight), dispatch_info.class, ) })] - pub fn sudo_as( - origin: OriginFor, - who: AccountIdLookupOf, - call: Box<::RuntimeCall>, - ) -> DispatchResultWithPostInfo { - Self::ensure_sudo(origin)?; + pub fn sudo_as( + origin: OriginFor, + who: AccountIdLookupOf, + call: Box<::RuntimeCall>, + ) -> DispatchResultWithPostInfo { + Self::ensure_sudo(origin)?; - let who = T::Lookup::lookup(who)?; - let res = call.dispatch_bypass_filter(RawOrigin::Signed(who).into()); - Self::deposit_event(Event::SudoAsDone { - sudo_result: res.map(|_| ()).map_err(|e| e.error), - }); + let who = T::Lookup::lookup(who)?; + let res = call.dispatch_bypass_filter(RawOrigin::Signed(who).into()); + Self::deposit_event(Event::SudoAsDone { + sudo_result: res.map(|_| ()).map_err(|e| e.error), + }); - Ok(Pays::No.into()) - } + Ok(Pays::No.into()) + } - #[pallet::call_index(4)] - #[pallet::weight(T::WeightInfo::remove_key())] - pub fn remove_key(origin: OriginFor) -> DispatchResultWithPostInfo { - Self::ensure_sudo(origin)?; + #[pallet::call_index(4)] + #[pallet::weight(T::WeightInfo::remove_key())] + pub fn remove_key(origin: OriginFor) -> DispatchResultWithPostInfo { + Self::ensure_sudo(origin)?; - Self::deposit_event(Event::KeyRemoved {}); - Key::::kill(); + Self::deposit_event(Event::KeyRemoved {}); + Key::::kill(); - Ok(Pays::No.into()) - } - } + Ok(Pays::No.into()) + } + } - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - Sudid { - sudo_result: DispatchResult, - }, - KeyChanged { - old: Option, - new: T::AccountId, - }, - KeyRemoved, - SudoAsDone { sudo_result: DispatchResult }, - } + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + Sudid { + sudo_result: DispatchResult, + }, + KeyChanged { + old: Option, + new: T::AccountId, + }, + KeyRemoved, + SudoAsDone { + sudo_result: DispatchResult, + }, + } - #[pallet::error] - pub enum Error { - RequireSudo, - } + #[pallet::error] + pub enum Error { + RequireSudo, + } - #[pallet::storage] - pub(super) type Key = StorageValue<_, T::AccountId, OptionQuery>; + #[pallet::storage] + pub(super) type Key = StorageValue<_, T::AccountId, OptionQuery>; - #[pallet::genesis_config] - #[derive(frame_support::DefaultNoBound)] - pub struct GenesisConfig { - pub key: Option, - } + #[pallet::genesis_config] + #[derive(frame_support::DefaultNoBound)] + pub struct GenesisConfig { + pub key: Option, + } - #[pallet::genesis_build] - impl BuildGenesisConfig for GenesisConfig { - fn build(&self) { - Key::::set(self.key.clone()); - } - } + #[pallet::genesis_build] + impl BuildGenesisConfig for GenesisConfig { + fn build(&self) { + Key::::set(self.key.clone()); + } + } - impl Pallet { - pub(crate) fn ensure_sudo(origin: OriginFor) -> DispatchResult { - let sender = ensure_signed_or_root(origin)?; + impl Pallet { + pub(crate) fn ensure_sudo(origin: OriginFor) -> DispatchResult { + let sender = ensure_signed_or_root(origin)?; - if let Some(sender) = sender { - if Key::::get().map_or(false, |k| k == sender) { - Ok(()) - } else { - Err(Error::::RequireSudo.into()) - } - } else { - Ok(()) - } - } - } + if let Some(sender) = sender { + if Key::::get().map_or(false, |k| k == sender) { + Ok(()) + } else { + Err(Error::::RequireSudo.into()) + } + } else { + Ok(()) + } + } + } } diff --git a/pallets/sudo/src/mock.rs b/pallets/sudo/src/mock.rs index 0fb4f4e..e924012 100644 --- a/pallets/sudo/src/mock.rs +++ b/pallets/sudo/src/mock.rs @@ -6,104 +6,122 @@ use sp_runtime::BuildStorage; #[frame_support::pallet] pub mod logger { - use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; - #[pallet::config] - pub trait Config: frame_system::Config { - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - } + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + } - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); - #[pallet::call] - impl Pallet { - #[pallet::call_index(0)] - #[pallet::weight(*weight)] - pub fn privileged_i32_log( - origin: OriginFor, - i: i32, - weight: Weight, - ) -> DispatchResultWithPostInfo { - ensure_root(origin)?; - >::try_append(i).map_err(|_| "could not append")?; - Self::deposit_event(Event::AppendI32 { value: i, weight }); - Ok(().into()) - } + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight(*weight)] + pub fn privileged_i32_log( + origin: OriginFor, + i: i32, + weight: Weight, + ) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + >::try_append(i).map_err(|_| "could not append")?; + Self::deposit_event(Event::AppendI32 { value: i, weight }); + Ok(().into()) + } - #[pallet::call_index(1)] - #[pallet::weight(*weight)] - pub fn non_privileged_log( - origin: OriginFor, - i: i32, - weight: Weight, - ) -> DispatchResultWithPostInfo { - let sender = ensure_signed(origin)?; - >::try_append(i).map_err(|_| "could not append")?; - >::try_append(sender.clone()).map_err(|_| "could not append")?; - Self::deposit_event(Event::AppendI32AndAccount { sender, value: i, weight }); - Ok(().into()) - } - } + #[pallet::call_index(1)] + #[pallet::weight(*weight)] + pub fn non_privileged_log( + origin: OriginFor, + i: i32, + weight: Weight, + ) -> DispatchResultWithPostInfo { + let sender = ensure_signed(origin)?; + >::try_append(i).map_err(|_| "could not append")?; + >::try_append(sender.clone()).map_err(|_| "could not append")?; + Self::deposit_event(Event::AppendI32AndAccount { + sender, + value: i, + weight, + }); + Ok(().into()) + } + } - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - AppendI32 { value: i32, weight: Weight }, - AppendI32AndAccount { sender: T::AccountId, value: i32, weight: Weight }, - } + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + AppendI32 { + value: i32, + weight: Weight, + }, + AppendI32AndAccount { + sender: T::AccountId, + value: i32, + weight: Weight, + }, + } - #[pallet::storage] - #[pallet::getter(fn account_log)] - pub(super) type AccountLog = - StorageValue<_, BoundedVec>, ValueQuery>; + #[pallet::storage] + #[pallet::getter(fn account_log)] + pub(super) type AccountLog = + StorageValue<_, BoundedVec>, ValueQuery>; - #[pallet::storage] - #[pallet::getter(fn i32_log)] - pub(super) type I32Log = StorageValue<_, BoundedVec>, ValueQuery>; + #[pallet::storage] + #[pallet::getter(fn i32_log)] + pub(super) type I32Log = StorageValue<_, BoundedVec>, ValueQuery>; } type Block = frame_system::mocking::MockBlock; frame_support::construct_runtime!( - pub enum Test - { - System: frame_system, - Sudo: sudo, - Logger: logger, - } + pub enum Test + { + System: frame_system, + Sudo: sudo, + Logger: logger, + } ); #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for Test { - type Block = Block; + type Block = Block; } impl logger::Config for Test { - type RuntimeEvent = RuntimeEvent; + type RuntimeEvent = RuntimeEvent; } impl Config for Test { - type RuntimeEvent = RuntimeEvent; - type RuntimeCall = RuntimeCall; - type WeightInfo = (); + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type WeightInfo = (); } pub type SudoCall = sudo::Call; pub type LoggerCall = logger::Call; pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); - sudo::GenesisConfig:: { key: Some(root_key) } - .assimilate_storage(&mut t) - .unwrap(); - let mut ext: sp_io::TestExternalities = t.into(); - ext.execute_with(|| System::set_block_number(1)); - ext + let mut t = frame_system::GenesisConfig::::default() + .build_storage() + .unwrap(); + sudo::GenesisConfig:: { + key: Some(root_key), + } + .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")] pub fn new_bench_ext() -> sp_io::TestExternalities { - frame_system::GenesisConfig::::default().build_storage().unwrap().into() + frame_system::GenesisConfig::::default() + .build_storage() + .unwrap() + .into() } diff --git a/pallets/sudo/src/tests.rs b/pallets/sudo/src/tests.rs index af191d3..7caaf62 100644 --- a/pallets/sudo/src/tests.rs +++ b/pallets/sudo/src/tests.rs @@ -1,188 +1,217 @@ use super::*; use frame_support::{assert_noop, assert_ok, weights::Weight}; use mock::{ - new_test_ext, Logger, LoggerCall, RuntimeCall, RuntimeEvent as TestEvent, RuntimeOrigin, Sudo, - SudoCall, System, Test, + new_test_ext, Logger, LoggerCall, RuntimeCall, RuntimeEvent as TestEvent, RuntimeOrigin, Sudo, + SudoCall, System, Test, }; #[test] fn test_setup_works() { - new_test_ext(1).execute_with(|| { - assert_eq!(Key::::get(), Some(1u64)); - assert!(Logger::i32_log().is_empty()); - assert!(Logger::account_log().is_empty()); - }); + new_test_ext(1).execute_with(|| { + assert_eq!(Key::::get(), Some(1u64)); + assert!(Logger::i32_log().is_empty()); + assert!(Logger::account_log().is_empty()); + }); } #[docify::export] #[test] fn sudo_basics() { - new_test_ext(1).execute_with(|| { - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1_000, 0), - })); - assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call)); - assert_eq!(Logger::i32_log(), vec![42i32]); + new_test_ext(1).execute_with(|| { + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1_000, 0), + })); + assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call)); + assert_eq!(Logger::i32_log(), vec![42i32]); - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1_000, 0), - })); - assert_noop!(Sudo::sudo(RuntimeOrigin::signed(2), call), Error::::RequireSudo); - }); + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1_000, 0), + })); + assert_noop!( + Sudo::sudo(RuntimeOrigin::signed(2), call), + Error::::RequireSudo + ); + }); } #[test] fn sudo_emits_events_correctly() { - new_test_ext(1).execute_with(|| { - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1, 0), - })); - assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call)); - System::assert_has_event(TestEvent::Sudo(Event::Sudid { sudo_result: Ok(()) })); - }) + new_test_ext(1).execute_with(|| { + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1, 0), + })); + assert_ok!(Sudo::sudo(RuntimeOrigin::signed(1), call)); + System::assert_has_event(TestEvent::Sudo(Event::Sudid { + sudo_result: Ok(()), + })); + }) } #[test] fn sudo_unchecked_weight_basics() { - new_test_ext(1).execute_with(|| { - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1_000, 0), - })); - assert_ok!(Sudo::sudo_unchecked_weight( - RuntimeOrigin::signed(1), - call, - Weight::from_parts(1_000, 0) - )); - assert_eq!(Logger::i32_log(), vec![42i32]); + new_test_ext(1).execute_with(|| { + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1_000, 0), + })); + assert_ok!(Sudo::sudo_unchecked_weight( + RuntimeOrigin::signed(1), + call, + Weight::from_parts(1_000, 0) + )); + assert_eq!(Logger::i32_log(), vec![42i32]); - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1_000, 0), - })); - assert_noop!( - Sudo::sudo_unchecked_weight( - RuntimeOrigin::signed(2), - call, - Weight::from_parts(1_000, 0) - ), - Error::::RequireSudo, - ); - assert_eq!(Logger::i32_log(), vec![42i32]); + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1_000, 0), + })); + assert_noop!( + Sudo::sudo_unchecked_weight( + RuntimeOrigin::signed(2), + call, + Weight::from_parts(1_000, 0) + ), + Error::::RequireSudo, + ); + assert_eq!(Logger::i32_log(), vec![42i32]); - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1, 0), - })); - let sudo_unchecked_weight_call = - SudoCall::sudo_unchecked_weight { call, 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)); - }); + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1, 0), + })); + let sudo_unchecked_weight_call = SudoCall::sudo_unchecked_weight { + call, + 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] fn sudo_unchecked_weight_emits_events_correctly() { - new_test_ext(1).execute_with(|| { - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1, 0), - })); - assert_ok!(Sudo::sudo_unchecked_weight( - RuntimeOrigin::signed(1), - call, - Weight::from_parts(1_000, 0) - )); - System::assert_has_event(TestEvent::Sudo(Event::Sudid { sudo_result: Ok(()) })); - }) + new_test_ext(1).execute_with(|| { + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1, 0), + })); + assert_ok!(Sudo::sudo_unchecked_weight( + RuntimeOrigin::signed(1), + call, + Weight::from_parts(1_000, 0) + )); + System::assert_has_event(TestEvent::Sudo(Event::Sudid { + sudo_result: Ok(()), + })); + }) } #[docify::export] #[test] fn set_key_basics() { - new_test_ext(1).execute_with(|| { - assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2)); - assert_eq!(Key::::get(), Some(2u64)); - }); + new_test_ext(1).execute_with(|| { + assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2)); + assert_eq!(Key::::get(), Some(2u64)); + }); - new_test_ext(1).execute_with(|| { - assert_noop!(Sudo::set_key(RuntimeOrigin::signed(2), 3), Error::::RequireSudo); - }); + new_test_ext(1).execute_with(|| { + assert_noop!( + Sudo::set_key(RuntimeOrigin::signed(2), 3), + Error::::RequireSudo + ); + }); } #[test] fn set_key_emits_events_correctly() { - new_test_ext(1).execute_with(|| { - assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2)); - System::assert_has_event(TestEvent::Sudo(Event::KeyChanged { old: Some(1), new: 2 })); - assert_ok!(Sudo::set_key(RuntimeOrigin::signed(2), 4)); - System::assert_has_event(TestEvent::Sudo(Event::KeyChanged { old: Some(2), new: 4 })); - }); + new_test_ext(1).execute_with(|| { + assert_ok!(Sudo::set_key(RuntimeOrigin::signed(1), 2)); + System::assert_has_event(TestEvent::Sudo(Event::KeyChanged { + old: Some(1), + 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] fn remove_key_works() { - new_test_ext(1).execute_with(|| { - assert_ok!(Sudo::remove_key(RuntimeOrigin::signed(1))); - assert!(Key::::get().is_none()); - System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {})); + new_test_ext(1).execute_with(|| { + assert_ok!(Sudo::remove_key(RuntimeOrigin::signed(1))); + assert!(Key::::get().is_none()); + System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {})); - assert_noop!(Sudo::remove_key(RuntimeOrigin::signed(1)), Error::::RequireSudo); - assert_noop!(Sudo::set_key(RuntimeOrigin::signed(1), 1), Error::::RequireSudo); - }); + assert_noop!( + Sudo::remove_key(RuntimeOrigin::signed(1)), + Error::::RequireSudo + ); + assert_noop!( + Sudo::set_key(RuntimeOrigin::signed(1), 1), + Error::::RequireSudo + ); + }); } #[test] fn using_root_origin_works() { - new_test_ext(1).execute_with(|| { - assert_ok!(Sudo::remove_key(RuntimeOrigin::root())); - assert!(Key::::get().is_none()); - System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {})); + new_test_ext(1).execute_with(|| { + assert_ok!(Sudo::remove_key(RuntimeOrigin::root())); + assert!(Key::::get().is_none()); + System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {})); - assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1)); - assert_eq!(Some(1), Key::::get()); - }); + assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1)); + assert_eq!(Some(1), Key::::get()); + }); } #[test] fn sudo_as_basics() { - new_test_ext(1).execute_with(|| { - let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { - i: 42, - weight: Weight::from_parts(1_000, 0), - })); - assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call)); - assert!(Logger::i32_log().is_empty()); - assert!(Logger::account_log().is_empty()); + new_test_ext(1).execute_with(|| { + let call = Box::new(RuntimeCall::Logger(LoggerCall::privileged_i32_log { + i: 42, + weight: Weight::from_parts(1_000, 0), + })); + assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call)); + assert!(Logger::i32_log().is_empty()); + assert!(Logger::account_log().is_empty()); - let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log { - i: 42, - weight: Weight::from_parts(1, 0), - })); - assert_noop!(Sudo::sudo_as(RuntimeOrigin::signed(3), 2, call), Error::::RequireSudo); + let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log { + i: 42, + weight: Weight::from_parts(1, 0), + })); + assert_noop!( + Sudo::sudo_as(RuntimeOrigin::signed(3), 2, call), + Error::::RequireSudo + ); - let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log { - i: 42, - weight: Weight::from_parts(1, 0), - })); - assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call)); - assert_eq!(Logger::i32_log(), vec![42i32]); - assert_eq!(Logger::account_log(), vec![2]); - }); + let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log { + i: 42, + weight: Weight::from_parts(1, 0), + })); + assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call)); + assert_eq!(Logger::i32_log(), vec![42i32]); + assert_eq!(Logger::account_log(), vec![2]); + }); } #[docify::export] #[test] fn sudo_as_emits_events_correctly() { - new_test_ext(1).execute_with(|| { - let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log { - i: 42, - weight: Weight::from_parts(1, 0), - })); - assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call)); - System::assert_has_event(TestEvent::Sudo(Event::SudoAsDone { sudo_result: Ok(()) })); - }); + new_test_ext(1).execute_with(|| { + let call = Box::new(RuntimeCall::Logger(LoggerCall::non_privileged_log { + i: 42, + weight: Weight::from_parts(1, 0), + })); + assert_ok!(Sudo::sudo_as(RuntimeOrigin::signed(1), 2, call)); + System::assert_has_event(TestEvent::Sudo(Event::SudoAsDone { + sudo_result: Ok(()), + })); + }); }