diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 1b77246031..58c4da80d3 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -4791,6 +4791,7 @@ dependencies = [ "frame-system", "pallet-balances", "parity-scale-codec", + "serde", "sp-core", "sp-io", "sp-runtime", diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index 0ac3c93a75..230079a21e 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -18,24 +18,31 @@ // Tests for Identity Pallet use super::*; +use crate as pallet_identity; use sp_runtime::traits::BadOrigin; -use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, - ord_parameter_types, -}; +use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use sp_core::H256; use frame_system::{EnsureSignedBy, EnsureOneOf, EnsureRoot}; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Identity: pallet_identity::{Module, Call, Storage, Event}, + } +); -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -49,16 +56,16 @@ impl frame_system::Config for Test { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -70,7 +77,7 @@ parameter_types! { } impl pallet_balances::Config for Test { type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -100,7 +107,7 @@ type EnsureTwoOrRoot = EnsureOneOf< EnsureSignedBy >; impl Config for Test { - type Event = (); + type Event = Event; type Currency = Balances; type Slashed = (); type BasicDeposit = BasicDeposit; @@ -113,9 +120,6 @@ impl Config for Test { type ForceOrigin = EnsureTwoOrRoot; type WeightInfo = (); } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Identity = Module; pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); diff --git a/substrate/frame/im-online/src/mock.rs b/substrate/frame/im-online/src/mock.rs index cf2138e941..1b80f5b12d 100644 --- a/substrate/frame/im-online/src/mock.rs +++ b/substrate/frame/im-online/src/mock.rs @@ -21,23 +21,31 @@ use std::cell::RefCell; -use crate::{Module, Config}; +use crate::Config; use sp_runtime::Perbill; use sp_staking::{SessionIndex, offence::{ReportOffence, OffenceError}}; use sp_runtime::testing::{Header, UintAuthorityId, TestXt}; use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto}; use sp_core::H256; -use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types}; +use frame_support::parameter_types; +use crate as imonline; +use pallet_session::historical as pallet_session_historical; -impl_outer_origin!{ - pub enum Origin for Runtime {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -impl_outer_dispatch! { - pub enum Call for Runtime where origin: Origin { - imonline::ImOnline, +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Session: pallet_session::{Module, Call, Storage, Event, Config}, + ImOnline: imonline::{Module, Call, Storage, Config, Event}, + Historical: pallet_session_historical::{Module}, } -} +); thread_local! { pub static VALIDATORS: RefCell>> = RefCell::new(Some(vec![ @@ -99,9 +107,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { t.into() } -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Runtime; - parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -122,10 +127,10 @@ impl frame_system::Config for Runtime { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -149,7 +154,7 @@ impl pallet_session::Config for Runtime { type ValidatorId = u64; type ValidatorIdOf = ConvertInto; type Keys = UintAuthorityId; - type Event = (); + type Event = Event; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); @@ -177,7 +182,7 @@ parameter_types! { impl Config for Runtime { type AuthorityId = UintAuthorityId; - type Event = (); + type Event = Event; type ReportUnresponsiveness = OffenceHandler; type ValidatorSet = Historical; type SessionDuration = Period; @@ -192,12 +197,6 @@ impl frame_system::offchain::SendTransactionTypes for Runt type Extrinsic = Extrinsic; } -/// Im Online module. -pub type ImOnline = Module; -pub type System = frame_system::Module; -pub type Session = pallet_session::Module; -pub type Historical = pallet_session::historical::Module; - pub fn advance_session() { let now = System::block_number().max(1); System::set_block_number(now + 1); diff --git a/substrate/frame/lottery/Cargo.toml b/substrate/frame/lottery/Cargo.toml index 49ae53ff1d..e571b4f450 100644 --- a/substrate/frame/lottery/Cargo.toml +++ b/substrate/frame/lottery/Cargo.toml @@ -25,6 +25,7 @@ frame-benchmarking = { version = "2.0.0", default-features = false, path = "../b pallet-balances = { version = "2.0.0", path = "../balances" } sp-core = { version = "2.0.0", path = "../../primitives/core" } sp-io = { version = "2.0.0", path = "../../primitives/io" } +serde = { version = "1.0.101" } [features] default = ["std"] diff --git a/substrate/frame/lottery/src/mock.rs b/substrate/frame/lottery/src/mock.rs index 0f25e9fc7f..ea73ee190e 100644 --- a/substrate/frame/lottery/src/mock.rs +++ b/substrate/frame/lottery/src/mock.rs @@ -18,9 +18,10 @@ //! Test utilities use super::*; +use crate as pallet_lottery; use frame_support::{ - impl_outer_origin, impl_outer_dispatch, parameter_types, + parameter_types, traits::{OnInitialize, OnFinalize, TestRandomness}, }; use sp_core::H256; @@ -31,19 +32,21 @@ use sp_runtime::{ }; use frame_system::EnsureRoot; -impl_outer_origin! { - pub enum Origin for Test {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Lottery: pallet_lottery::{Module, Call, Storage, Event}, } -} +); -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub const MaximumBlockWeight: u32 = 1024; @@ -65,10 +68,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -83,7 +86,7 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -101,7 +104,7 @@ impl Config for Test { type Call = Call; type Currency = Balances; type Randomness = TestRandomness; - type Event = (); + type Event = Event; type ManagerOrigin = EnsureRoot; type MaxCalls = MaxCalls; type ValidateCall = Lottery; @@ -109,10 +112,6 @@ impl Config for Test { type WeightInfo = (); } -pub type Lottery = Module; -pub type System = frame_system::Module; -pub type Balances = pallet_balances::Module; - pub type SystemCall = frame_system::Call; pub type BalancesCall = pallet_balances::Call; diff --git a/substrate/frame/membership/src/lib.rs b/substrate/frame/membership/src/lib.rs index a43a5b4089..f080938095 100644 --- a/substrate/frame/membership/src/lib.rs +++ b/substrate/frame/membership/src/lib.rs @@ -277,21 +277,27 @@ impl, I: Instance> Contains for Module { #[cfg(test)] mod tests { use super::*; + use crate as pallet_membership; - use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, - ord_parameter_types - }; + use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, BadOrigin}, testing::Header}; use frame_system::EnsureSignedBy; - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; + + frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Membership: pallet_membership::{Module, Call, Storage, Config, Event}, + } + ); - #[derive(Clone, Eq, PartialEq)] - pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -308,15 +314,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -356,7 +362,7 @@ mod tests { } impl Config for Test { - type Event = (); + type Event = Event; type AddOrigin = EnsureSignedBy; type RemoveOrigin = EnsureSignedBy; type SwapOrigin = EnsureSignedBy; @@ -366,12 +372,10 @@ mod tests { type MembershipChanged = TestChangeMembers; } - type Membership = Module; - fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); // We use default for brevity, but you can configure as desired if needed. - GenesisConfig::{ + pallet_membership::GenesisConfig::{ members: vec![10, 20, 30], .. Default::default() }.assimilate_storage(&mut t).unwrap(); diff --git a/substrate/frame/merkle-mountain-range/src/mock.rs b/substrate/frame/merkle-mountain-range/src/mock.rs index 2cb4e7c4dc..0adb0294d5 100644 --- a/substrate/frame/merkle-mountain-range/src/mock.rs +++ b/substrate/frame/merkle-mountain-range/src/mock.rs @@ -16,11 +16,10 @@ // limitations under the License. use crate::*; +use crate as pallet_mmr; use codec::{Encode, Decode}; -use frame_support::{ - impl_outer_origin, parameter_types, -}; +use frame_support::parameter_types; use pallet_mmr_primitives::{LeafDataProvider, Compact}; use sp_core::H256; use sp_runtime::{ @@ -32,19 +31,27 @@ use sp_runtime::{ use sp_std::cell::RefCell; use sp_std::prelude::*; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + MMR: pallet_mmr::{Module, Call, Storage}, + } +); -#[derive(Clone, Eq, PartialEq, Encode, Decode)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; } impl frame_system::Config for Test { type BaseCallFilter = (); type Origin = Origin; - type Call = (); + type Call = Call; type Index = u64; type BlockNumber = u64; type Hash = H256; @@ -52,13 +59,13 @@ impl frame_system::Config for Test { type AccountId = sp_core::sr25519::Public; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type DbWeight = (); type BlockWeights = (); type BlockLength = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -102,5 +109,3 @@ impl LeafDataProvider for LeafData { LEAF_DATA.with(|r| r.borrow().clone()) } } - -pub(crate) type MMR = Module; diff --git a/substrate/frame/multisig/src/tests.rs b/substrate/frame/multisig/src/tests.rs index d16b0ad495..78301b2b69 100644 --- a/substrate/frame/multisig/src/tests.rs +++ b/substrate/frame/multisig/src/tests.rs @@ -22,37 +22,27 @@ use super::*; use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch, - impl_outer_event, traits::Filter, + assert_ok, assert_noop, parameter_types, traits::Filter, }; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; -use crate as multisig; +use crate as pallet_multisig; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; -impl_outer_event! { - pub enum TestEvent for Test { - system, - pallet_balances, - multisig, +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Multisig: pallet_multisig::{Module, Call, Storage, Event}, } -} -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, - multisig::Multisig, - } -} +); -// For testing the pallet, we construct most of a mock runtime. This means -// first constructing a configuration type (`Test`) which `impl`s each of the -// configuration traits of pallets we want to use. -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -72,10 +62,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -88,7 +78,7 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = TestEvent; + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -111,7 +101,7 @@ impl Filter for TestBaseCallFilter { } } impl Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type Currency = Balances; type DepositBase = DepositBase; @@ -119,9 +109,6 @@ impl Config for Test { type MaxSignatories = MaxSignatories; type WeightInfo = (); } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Multisig = Module; use pallet_balances::Call as BalancesCall; use pallet_balances::Error as BalancesError; @@ -136,11 +123,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ext } -fn last_event() -> TestEvent { +fn last_event() -> Event { system::Module::::events().pop().map(|e| e.event).expect("Event expected") } -fn expect_event>(e: E) { +fn expect_event>(e: E) { assert_eq!(last_event(), e.into()); } diff --git a/substrate/frame/nicks/src/lib.rs b/substrate/frame/nicks/src/lib.rs index 983be4056d..681a45626f 100644 --- a/substrate/frame/nicks/src/lib.rs +++ b/substrate/frame/nicks/src/lib.rs @@ -239,23 +239,30 @@ decl_module! { #[cfg(test)] mod tests { use super::*; + use crate as pallet_nicks; - use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, - ord_parameter_types - }; + use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use sp_core::H256; use frame_system::EnsureSignedBy; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup, BadOrigin}, }; - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; + + frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Nicks: pallet_nicks::{Module, Call, Storage, Event}, + } + ); - #[derive(Clone, Eq, PartialEq)] - pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -270,15 +277,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -291,7 +298,7 @@ mod tests { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -306,7 +313,7 @@ mod tests { pub const One: u64 = 1; } impl Config for Test { - type Event = (); + type Event = Event; type Currency = Balances; type ReservationFee = ReservationFee; type Slashed = (); @@ -314,9 +321,6 @@ mod tests { type MinLength = MinLength; type MaxLength = MaxLength; } - type System = frame_system::Module; - type Balances = pallet_balances::Module; - type Nicks = Module; fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); diff --git a/substrate/frame/node-authorization/src/lib.rs b/substrate/frame/node-authorization/src/lib.rs index 79b1d6e74c..f1f70e9eac 100644 --- a/substrate/frame/node-authorization/src/lib.rs +++ b/substrate/frame/node-authorization/src/lib.rs @@ -431,21 +431,28 @@ impl Module { #[cfg(test)] mod tests { use super::*; + use crate as pallet_node_authorization; - use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, - parameter_types, ord_parameter_types, - }; + use frame_support::{assert_ok, assert_noop, parameter_types, ord_parameter_types}; use frame_system::EnsureSignedBy; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, BadOrigin}, testing::Header}; - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; - #[derive(Clone, Eq, PartialEq)] - 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}, + NodeAuthorization: pallet_node_authorization::{ + Module, Call, Storage, Config, Event, + }, + } + ); parameter_types! { pub const BlockHashCount: u64 = 250; @@ -459,15 +466,15 @@ mod tests { type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -486,7 +493,7 @@ mod tests { pub const MaxPeerIdLength: u32 = 2; } impl Config for Test { - type Event = (); + type Event = Event; type MaxWellKnownNodes = MaxWellKnownNodes; type MaxPeerIdLength = MaxPeerIdLength; type AddOrigin = EnsureSignedBy; @@ -496,15 +503,13 @@ mod tests { type WeightInfo = (); } - type NodeAuthorization = Module; - fn test_node(id: u8) -> PeerId { PeerId(vec![id]) } fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); - GenesisConfig:: { + pallet_node_authorization::GenesisConfig:: { nodes: vec![(test_node(10), 10), (test_node(20), 20), (test_node(30), 30)], }.assimilate_storage(&mut t).unwrap(); t.into() diff --git a/substrate/frame/offences/src/mock.rs b/substrate/frame/offences/src/mock.rs index 042c050109..c47a9cf943 100644 --- a/substrate/frame/offences/src/mock.rs +++ b/substrate/frame/offences/src/mock.rs @@ -20,7 +20,7 @@ #![cfg(test)] use std::cell::RefCell; -use crate::{Module, Config}; +use crate::Config; use codec::Encode; use sp_runtime::Perbill; use sp_staking::{ @@ -31,14 +31,10 @@ use sp_runtime::testing::Header; use sp_runtime::traits::{IdentityLookup, BlakeTwo256}; use sp_core::H256; use frame_support::{ - impl_outer_origin, impl_outer_event, parameter_types, StorageMap, StorageDoubleMap, + parameter_types, StorageMap, StorageDoubleMap, weights::{Weight, constants::{WEIGHT_PER_SECOND, RocksDbWeight}}, }; -use frame_system as system; - -impl_outer_origin!{ - pub enum Origin for Runtime {} -} +use crate as offences; pub struct OnOffenceHandler; @@ -86,9 +82,20 @@ pub fn set_offence_weight(new: Weight) { OFFENCE_WEIGHT.with(|w| *w.borrow_mut() = new); } -// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. -#[derive(Clone, PartialEq, Eq, Debug)] -pub struct Runtime; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Offences: offences::{Module, Call, Storage, Event}, + } +); + parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -102,16 +109,16 @@ impl frame_system::Config for Runtime { type Origin = Origin; type Index = u64; type BlockNumber = u64; - type Call = (); + type Call = Call; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -125,23 +132,12 @@ parameter_types! { } impl Config for Runtime { - type Event = TestEvent; + type Event = Event; type IdentificationTuple = u64; type OnOffenceHandler = OnOffenceHandler; type WeightSoftLimit = OffencesWeightSoftLimit; } -mod offences { - pub use crate::Event; -} - -impl_outer_event! { - pub enum TestEvent for Runtime { - system, - offences, - } -} - pub fn new_test_ext() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::default().build_storage::().unwrap(); let mut ext = sp_io::TestExternalities::new(t); @@ -149,10 +145,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ext } -/// Offences module. -pub type Offences = Module; -pub type System = frame_system::Module; - pub const KIND: [u8; 16] = *b"test_report_1234"; /// Returns all offence details for the specific `kind` happened at the specific time slot. diff --git a/substrate/frame/offences/src/tests.rs b/substrate/frame/offences/src/tests.rs index a33ba96447..2b7c500dfa 100644 --- a/substrate/frame/offences/src/tests.rs +++ b/substrate/frame/offences/src/tests.rs @@ -21,7 +21,7 @@ use super::*; use crate::mock::{ - Offences, System, Offence, TestEvent, KIND, new_test_ext, with_on_offence_fractions, + Offences, System, Offence, Event, KIND, new_test_ext, with_on_offence_fractions, offence_reports, set_can_report, set_offence_weight, }; use sp_runtime::Perbill; @@ -132,7 +132,7 @@ fn should_deposit_event() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: TestEvent::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), + event: Event::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), topics: vec![], }] ); @@ -167,7 +167,7 @@ fn doesnt_deposit_event_for_dups() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: TestEvent::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), + event: Event::offences(crate::Event::Offence(KIND, time_slot.encode(), true)), topics: vec![], }] ); @@ -304,7 +304,7 @@ fn should_queue_and_resubmit_rejected_offence() { System::events(), vec![EventRecord { phase: Phase::Initialization, - event: TestEvent::offences(crate::Event::Offence(KIND, 42u128.encode(), false)), + event: Event::offences(crate::Event::Offence(KIND, 42u128.encode(), false)), topics: vec![], }] ); diff --git a/substrate/frame/proxy/src/tests.rs b/substrate/frame/proxy/src/tests.rs index 3c417647c2..b31ef1dfdb 100644 --- a/substrate/frame/proxy/src/tests.rs +++ b/substrate/frame/proxy/src/tests.rs @@ -22,39 +22,29 @@ use super::*; use frame_support::{ - assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch, - impl_outer_event, RuntimeDebug, dispatch::DispatchError, traits::Filter, + assert_ok, assert_noop, parameter_types, RuntimeDebug, dispatch::DispatchError, traits::Filter, }; use codec::{Encode, Decode}; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; use crate as proxy; -impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} -} -impl_outer_event! { - pub enum TestEvent for Test { - system, - pallet_balances, - proxy, - pallet_utility, - } -} -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - frame_system::System, - pallet_balances::Balances, - proxy::Proxy, - pallet_utility::Utility, - } -} +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config, Event}, + Proxy: proxy::{Module, Call, Storage, Event}, + Utility: pallet_utility::{Module, Call, Event}, + } +); -// For testing the pallet, we construct most of a mock runtime. This means -// first constructing a configuration type (`Test`) which `impl`s each of the -// configuration traits of pallets we want to use. -#[derive(Clone, Eq, PartialEq)] -pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; pub BlockWeights: frame_system::limits::BlockWeights = @@ -74,10 +64,10 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = TestEvent; + type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -90,14 +80,14 @@ parameter_types! { impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; - type Event = TestEvent; + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; type WeightInfo = (); } impl pallet_utility::Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type WeightInfo = (); } @@ -140,7 +130,7 @@ impl Filter for BaseFilter { } } impl Config for Test { - type Event = TestEvent; + type Event = Event; type Call = Call; type Currency = Balances; type ProxyType = ProxyType; @@ -154,11 +144,6 @@ impl Config for Test { type AnnouncementDepositFactor = AnnouncementDepositFactor; } -type System = frame_system::Module; -type Balances = pallet_balances::Module; -type Utility = pallet_utility::Module; -type Proxy = Module; - use frame_system::Call as SystemCall; use pallet_balances::Call as BalancesCall; use pallet_balances::Error as BalancesError; @@ -177,19 +162,19 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ext } -fn last_event() -> TestEvent { +fn last_event() -> Event { system::Module::::events().pop().expect("Event expected").event } -fn expect_event>(e: E) { +fn expect_event>(e: E) { assert_eq!(last_event(), e.into()); } -fn last_events(n: usize) -> Vec { +fn last_events(n: usize) -> Vec { system::Module::::events().into_iter().rev().take(n).rev().map(|e| e.event).collect() } -fn expect_events(e: Vec) { +fn expect_events(e: Vec) { assert_eq!(last_events(e.len()), e); }