diff --git a/substrate/frame/babe/src/mock.rs b/substrate/frame/babe/src/mock.rs index 0a7576aa0778afbe8aa824d31602157ed4becb30..3198930ea6ccb87a9ef29cdbce55cdf1a63dfdee 100644 --- a/substrate/frame/babe/src/mock.rs +++ b/substrate/frame/babe/src/mock.rs @@ -18,7 +18,7 @@ //! Test utilities use codec::Encode; -use super::{Config, Module, CurrentSlot}; +use crate::{self as pallet_babe, Config, CurrentSlot}; use sp_runtime::{ Perbill, impl_opaque_keys, curve::PiecewiseLinear, @@ -27,7 +27,7 @@ use sp_runtime::{ }; use frame_system::InitKind; use frame_support::{ - impl_outer_dispatch, impl_outer_origin, parameter_types, StorageValue, + parameter_types, StorageValue, traits::{KeyOwnerProofSystem, OnInitialize}, weights::Weight, }; @@ -37,23 +37,29 @@ use sp_consensus_babe::{AuthorityId, AuthorityPair, SlotNumber}; use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof}; use sp_staking::SessionIndex; use pallet_staking::EraIndex; - -impl_outer_origin!{ - pub enum Origin for Test where system = frame_system {} -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - babe::Babe, - staking::Staking, - } -} +use pallet_session::historical as pallet_session_historical; type DummyValidatorId = u64; -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Test; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; +type Block = frame_system::mocking::MockBlock<Test>; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event<T>}, + Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>}, + Historical: pallet_session_historical::{Module}, + Offences: pallet_offences::{Module, Call, Storage, Event}, + Babe: pallet_babe::{Module, Call, Storage, Config, Inherent, ValidateUnsigned}, + Staking: pallet_staking::{Module, Call, Storage, Config<T>, Event<T>}, + Session: pallet_session::{Module, Call, Storage, Event, Config<T>}, + Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, + } +); parameter_types! { pub const BlockHashCount: u64 = 250; @@ -79,9 +85,9 @@ impl frame_system::Config for Test { type AccountId = DummyValidatorId; type Lookup = IdentityLookup<Self::AccountId>; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData<u128>; type OnNewAccount = (); type OnKilledAccount = (); @@ -104,7 +110,7 @@ impl_opaque_keys! { } impl pallet_session::Config for Test { - type Event = (); + type Event = Event; type ValidatorId = <Self as frame_system::Config>::AccountId; type ValidatorIdOf = pallet_staking::StashOf<Self>; type ShouldEndSession = Babe; @@ -151,7 +157,7 @@ impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u128; type DustRemoval = (); - type Event = (); + type Event = Event; type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); @@ -182,7 +188,7 @@ parameter_types! { impl pallet_staking::Config for Test { type RewardRemainder = (); type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote; - type Event = (); + type Event = Event; type Currency = Balances; type Slash = (); type Reward = (); @@ -210,7 +216,7 @@ parameter_types! { } impl pallet_offences::Config for Test { - type Event = (); + type Event = Event; type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>; type OnOffenceHandler = Staking; type WeightSoftLimit = OffencesWeightSoftLimit; @@ -235,15 +241,6 @@ impl Config for Test { type WeightInfo = (); } -pub type Balances = pallet_balances::Module<Test>; -pub type Historical = pallet_session::historical::Module<Test>; -pub type Offences = pallet_offences::Module<Test>; -pub type Session = pallet_session::Module<Test>; -pub type Staking = pallet_staking::Module<Test>; -pub type System = frame_system::Module<Test>; -pub type Timestamp = pallet_timestamp::Module<Test>; -pub type Babe = Module<Test>; - pub fn go_to_block(n: u64, s: u64) { use frame_support::traits::OnFinalize; diff --git a/substrate/frame/balances/src/tests.rs b/substrate/frame/balances/src/tests.rs index 7a1b57a7b4db1e8892c30699bf4483367cce4978..de7ccc6d239f953a9cdc89cdd6b08b1409e3666c 100644 --- a/substrate/frame/balances/src/tests.rs +++ b/substrate/frame/balances/src/tests.rs @@ -19,20 +19,6 @@ #![cfg(test)] -#[derive(Debug)] -pub struct CallWithDispatchInfo; -impl sp_runtime::traits::Dispatchable for CallWithDispatchInfo { - type Origin = (); - type Config = (); - type Info = frame_support::weights::DispatchInfo; - type PostInfo = frame_support::weights::PostDispatchInfo; - - fn dispatch(self, _origin: Self::Origin) - -> sp_runtime::DispatchResultWithInfo<Self::PostInfo> { - panic!("Do not use dummy implementation for dispatch."); - } -} - #[macro_export] macro_rules! decl_tests { ($test:ty, $ext_builder:ty, $existential_deposit:expr) => { @@ -52,10 +38,8 @@ macro_rules! decl_tests { const ID_1: LockIdentifier = *b"1 "; const ID_2: LockIdentifier = *b"2 "; - pub type System = frame_system::Module<$test>; - pub type Balances = Module<$test>; - - pub const CALL: &<$test as frame_system::Config>::Call = &$crate::tests::CallWithDispatchInfo; + pub const CALL: &<$test as frame_system::Config>::Call = + &Call::Balances(pallet_balances::Call::transfer(0, 0)); /// create a transaction info struct from weight. Handy to avoid building the whole struct. pub fn info_from_weight(w: Weight) -> DispatchInfo { @@ -485,7 +469,7 @@ macro_rules! decl_tests { assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0); assert_eq!( last_event(), - Event::balances(RawEvent::ReserveRepatriated(1, 2, 41, Status::Free)), + Event::pallet_balances(RawEvent::ReserveRepatriated(1, 2, 41, Status::Free)), ); assert_eq!(Balances::reserved_balance(1), 69); assert_eq!(Balances::free_balance(1), 0); @@ -626,7 +610,7 @@ macro_rules! decl_tests { fn cannot_set_genesis_value_below_ed() { ($existential_deposit).with(|v| *v.borrow_mut() = 11); let mut t = frame_system::GenesisConfig::default().build_storage::<$test>().unwrap(); - let _ = GenesisConfig::<$test> { + let _ = pallet_balances::GenesisConfig::<$test> { balances: vec![(1, 10)], }.assimilate_storage(&mut t).unwrap(); } @@ -635,7 +619,7 @@ macro_rules! decl_tests { #[should_panic = "duplicate balances in genesis."] fn cannot_set_genesis_value_twice() { let mut t = frame_system::GenesisConfig::default().build_storage::<$test>().unwrap(); - let _ = GenesisConfig::<$test> { + let _ = pallet_balances::GenesisConfig::<$test> { balances: vec![(1, 10), (2, 20), (1, 15)], }.assimilate_storage(&mut t).unwrap(); } @@ -704,7 +688,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::balances(RawEvent::Reserved(1, 10)), + Event::pallet_balances(RawEvent::Reserved(1, 10)), ); System::set_block_number(3); @@ -712,7 +696,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::balances(RawEvent::Unreserved(1, 5)), + Event::pallet_balances(RawEvent::Unreserved(1, 5)), ); System::set_block_number(4); @@ -721,7 +705,7 @@ macro_rules! decl_tests { // should only unreserve 5 assert_eq!( last_event(), - Event::balances(RawEvent::Unreserved(1, 5)), + Event::pallet_balances(RawEvent::Unreserved(1, 5)), ); }); } @@ -737,9 +721,9 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::system(system::Event::NewAccount(1)), - Event::balances(RawEvent::Endowed(1, 100)), - Event::balances(RawEvent::BalanceSet(1, 100, 0)), + Event::frame_system(system::Event::NewAccount(1)), + Event::pallet_balances(RawEvent::Endowed(1, 100)), + Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)), ] ); @@ -748,8 +732,8 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::balances(RawEvent::DustLost(1, 99)), - Event::system(system::Event::KilledAccount(1)) + Event::pallet_balances(RawEvent::DustLost(1, 99)), + Event::frame_system(system::Event::KilledAccount(1)) ] ); }); @@ -766,9 +750,9 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::system(system::Event::NewAccount(1)), - Event::balances(RawEvent::Endowed(1, 100)), - Event::balances(RawEvent::BalanceSet(1, 100, 0)), + Event::frame_system(system::Event::NewAccount(1)), + Event::pallet_balances(RawEvent::Endowed(1, 100)), + Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)), ] ); @@ -777,7 +761,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::system(system::Event::KilledAccount(1)) + Event::frame_system(system::Event::KilledAccount(1)) ] ); }); diff --git a/substrate/frame/balances/src/tests_composite.rs b/substrate/frame/balances/src/tests_composite.rs index 7cb9b9d502ba5e807269edd000d8ad6e1ce93567..14dfd0c4b33d6f1391e58213f01b535d994cb773 100644 --- a/substrate/frame/balances/src/tests_composite.rs +++ b/substrate/frame/balances/src/tests_composite.rs @@ -25,30 +25,27 @@ use sp_runtime::{ }; use sp_core::H256; use sp_io; -use frame_support::{impl_outer_origin, impl_outer_event, parameter_types}; +use frame_support::parameter_types; use frame_support::weights::{Weight, DispatchInfo, IdentityFee}; use pallet_transaction_payment::CurrencyAdapter; -use crate::{GenesisConfig, Module, Config, decl_tests, tests::CallWithDispatchInfo}; - -use frame_system as system; -impl_outer_origin!{ - pub enum Origin for Test {} -} - -mod balances { - pub use crate::Event; -} +use crate::{ + self as pallet_balances, + Module, Config, decl_tests, +}; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; +type Block = frame_system::mocking::MockBlock<Test>; -impl_outer_event! { - pub enum Event for Test { - system<T>, - balances<T>, +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event<T>}, + Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>}, } -} +); -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -63,7 +60,7 @@ impl frame_system::Config for Test { type Origin = Origin; type Index = u64; type BlockNumber = u64; - type Call = CallWithDispatchInfo; + type Call = Call; type Hash = H256; type Hashing = ::sp_runtime::traits::BlakeTwo256; type AccountId = u64; @@ -72,7 +69,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = super::AccountData<u64>; type OnNewAccount = (); type OnKilledAccount = (); @@ -94,7 +91,7 @@ impl Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = system::Module<Test>; + type AccountStore = frame_system::Pallet<Test>; type MaxLocks = (); type WeightInfo = (); } @@ -126,7 +123,7 @@ impl ExtBuilder { pub fn build(self) -> sp_io::TestExternalities { self.set_associated_consts(); let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap(); - GenesisConfig::<Test> { + pallet_balances::GenesisConfig::<Test> { balances: if self.monied { vec![ (1, 10 * self.existential_deposit), diff --git a/substrate/frame/balances/src/tests_local.rs b/substrate/frame/balances/src/tests_local.rs index 762ebe871b3e8294009b73c9b9c9fe1a51e5af64..a072d2954bec14692b8338f161a4c58481297e7e 100644 --- a/substrate/frame/balances/src/tests_local.rs +++ b/substrate/frame/balances/src/tests_local.rs @@ -25,31 +25,29 @@ use sp_runtime::{ }; use sp_core::H256; use sp_io; -use frame_support::{impl_outer_origin, impl_outer_event, parameter_types}; +use frame_support::parameter_types; use frame_support::traits::StorageMapShim; use frame_support::weights::{Weight, DispatchInfo, IdentityFee}; -use crate::{GenesisConfig, Module, Config, decl_tests, tests::CallWithDispatchInfo}; +use crate::{ + self as pallet_balances, + Module, Config, decl_tests, +}; use pallet_transaction_payment::CurrencyAdapter; -use frame_system as system; -impl_outer_origin!{ - pub enum Origin for Test {} -} - -mod balances { - pub use crate::Event; -} - -impl_outer_event! { - pub enum Event for Test { - system<T>, - balances<T>, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; +type Block = frame_system::mocking::MockBlock<Test>; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event<T>}, + Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>}, } -} +); -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -64,7 +62,7 @@ impl frame_system::Config for Test { type Origin = Origin; type Index = u64; type BlockNumber = u64; - type Call = CallWithDispatchInfo; + type Call = Call; type Hash = H256; type Hashing = ::sp_runtime::traits::BlakeTwo256; type AccountId = u64; @@ -73,7 +71,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -137,7 +135,7 @@ impl ExtBuilder { pub fn build(self) -> sp_io::TestExternalities { self.set_associated_consts(); let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap(); - GenesisConfig::<Test> { + pallet_balances::GenesisConfig::<Test> { balances: if self.monied { vec![ (1, 10 * self.existential_deposit), @@ -170,9 +168,9 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::system(system::Event::NewAccount(1)), - Event::balances(RawEvent::Endowed(1, 100)), - Event::balances(RawEvent::BalanceSet(1, 100, 0)), + Event::frame_system(frame_system::Event::NewAccount(1)), + Event::pallet_balances(RawEvent::Endowed(1, 100)), + Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)), ] ); @@ -186,8 +184,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::balances(RawEvent::DustLost(1, 1)), - Event::system(system::Event::KilledAccount(1)) + Event::pallet_balances(RawEvent::DustLost(1, 1)), + Event::frame_system(frame_system::Event::KilledAccount(1)) ] ); }); diff --git a/substrate/frame/indices/src/mock.rs b/substrate/frame/indices/src/mock.rs index 77797213cb56c93a7339f69f4b24e3d926cb2543..06c73b1a9bc27fc43d0b53ffcf3fda5d3f48d5e7 100644 --- a/substrate/frame/indices/src/mock.rs +++ b/substrate/frame/indices/src/mock.rs @@ -21,25 +21,23 @@ use sp_runtime::testing::Header; use sp_core::H256; -use frame_support::{impl_outer_origin, impl_outer_event, parameter_types}; -use crate::{self as indices, Module, Config}; -use frame_system as system; -use pallet_balances as balances; +use frame_support::parameter_types; +use crate::{self as pallet_indices, Config}; -impl_outer_origin!{ - pub enum Origin for Test where system = frame_system {} -} -impl_outer_event!{ - pub enum MetaEvent for Test { - system<T>, - balances<T>, - indices<T>, - } -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; +type Block = frame_system::mocking::MockBlock<Test>; -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Test; +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event<T>}, + Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>}, + Indices: pallet_indices::{Module, Call, Storage, Config<T>, Event<T>}, + } +); parameter_types! { pub const BlockHashCount: u64 = 250; @@ -53,7 +51,7 @@ impl frame_system::Config for Test { type BlockLength = (); type DbWeight = (); type Origin = Origin; - type Call = (); + type Call = Call; type Index = u64; type BlockNumber = u64; type Hash = H256; @@ -61,10 +59,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = Indices; type Header = Header; - type Event = MetaEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData<u64>; type OnNewAccount = (); type OnKilledAccount = (); @@ -80,7 +78,7 @@ impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; type DustRemoval = (); - type Event = MetaEvent; + type Event = Event; type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); @@ -94,7 +92,7 @@ impl Config for Test { type AccountIndex = u64; type Currency = Balances; type Deposit = Deposit; - type Event = MetaEvent; + type Event = Event; type WeightInfo = (); } @@ -105,7 +103,3 @@ pub fn new_test_ext() -> sp_io::TestExternalities { }.assimilate_storage(&mut t).unwrap(); t.into() } - -pub type System = frame_system::Module<Test>; -pub type Balances = pallet_balances::Module<Test>; -pub type Indices = Module<Test>;