diff --git a/substrate/frame/assets/Cargo.toml b/substrate/frame/assets/Cargo.toml index fe7b30eaac..4dddd1c59c 100644 --- a/substrate/frame/assets/Cargo.toml +++ b/substrate/frame/assets/Cargo.toml @@ -28,7 +28,7 @@ frame-benchmarking = { version = "2.0.0", default-features = false, path = "../b sp-core = { version = "2.0.0", path = "../../primitives/core" } sp-std = { version = "2.0.0", path = "../../primitives/std" } sp-io = { version = "2.0.0", path = "../../primitives/io" } -pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" } +pallet-balances = { version = "2.0.0", path = "../balances" } [features] default = ["std"] diff --git a/substrate/frame/assets/src/lib.rs b/substrate/frame/assets/src/lib.rs index 8f1ad02c08..099361eceb 100644 --- a/substrate/frame/assets/src/lib.rs +++ b/substrate/frame/assets/src/lib.rs @@ -1084,30 +1084,28 @@ impl Module { #[cfg(test)] mod tests { use super::*; + use crate as pallet_assets; - use frame_support::{impl_outer_origin, assert_ok, assert_noop, parameter_types, impl_outer_event}; + use frame_support::{assert_ok, assert_noop, parameter_types}; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; use pallet_balances::Error as BalancesError; - mod pallet_assets { - pub use crate::Event; - } + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; - impl_outer_event! { - pub enum Event for Test { - frame_system, - pallet_balances, - pallet_assets, + 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}, + Assets: pallet_assets::{Module, Call, Storage, Event}, } - } + ); - impl_outer_origin! { - pub enum Origin for Test where system = frame_system {} - } - - #[derive(Clone, Eq, PartialEq)] - pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; } @@ -1118,7 +1116,7 @@ mod tests { type DbWeight = (); type Origin = Origin; type Index = u64; - type Call = (); + type Call = Call; type BlockNumber = u64; type Hash = H256; type Hashing = BlakeTwo256; @@ -1128,7 +1126,7 @@ mod tests { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); @@ -1171,9 +1169,6 @@ mod tests { type MetadataDepositPerByte = MetadataDepositPerByte; type WeightInfo = (); } - type System = frame_system::Module; - type Balances = pallet_balances::Module; - type Assets = Module; pub(crate) fn new_test_ext() -> sp_io::TestExternalities { frame_system::GenesisConfig::default().build_storage::().unwrap().into() diff --git a/substrate/frame/collective/src/lib.rs b/substrate/frame/collective/src/lib.rs index b2993fd45e..ead9135aaa 100644 --- a/substrate/frame/collective/src/lib.rs +++ b/substrate/frame/collective/src/lib.rs @@ -965,7 +965,7 @@ mod tests { use hex_literal::hex; use sp_core::H256; use sp_runtime::{ - traits::{BlakeTwo256, IdentityLookup, Block as BlockT}, testing::Header, + traits::{BlakeTwo256, IdentityLookup}, testing::Header, BuildStorage, }; use crate as collective; diff --git a/substrate/frame/elections-phragmen/src/lib.rs b/substrate/frame/elections-phragmen/src/lib.rs index 1bef73831e..d566975e2e 100644 --- a/substrate/frame/elections-phragmen/src/lib.rs +++ b/substrate/frame/elections-phragmen/src/lib.rs @@ -1047,7 +1047,7 @@ mod tests { use sp_core::H256; use sp_runtime::{ testing::Header, BuildStorage, DispatchResult, - traits::{BlakeTwo256, IdentityLookup, Block as BlockT}, + traits::{BlakeTwo256, IdentityLookup}, }; use crate as elections_phragmen; diff --git a/substrate/frame/elections/src/mock.rs b/substrate/frame/elections/src/mock.rs index bf3d355b6d..b386542b2b 100644 --- a/substrate/frame/elections/src/mock.rs +++ b/substrate/frame/elections/src/mock.rs @@ -25,7 +25,7 @@ use frame_support::{ }; use sp_core::H256; use sp_runtime::{ - BuildStorage, testing::Header, traits::{BlakeTwo256, IdentityLookup, Block as BlockT}, + BuildStorage, testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; use crate as elections; diff --git a/substrate/frame/offences/benchmarking/src/mock.rs b/substrate/frame/offences/benchmarking/src/mock.rs index 20fd3ba9b0..6ebb9f19e6 100644 --- a/substrate/frame/offences/benchmarking/src/mock.rs +++ b/substrate/frame/offences/benchmarking/src/mock.rs @@ -26,7 +26,7 @@ use frame_support::{ }; use frame_system as system; use sp_runtime::{ - traits::{IdentityLookup, Block as BlockT}, + traits::IdentityLookup, testing::{Header, UintAuthorityId}, }; diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 3f6afd3ff5..2c2cdf00a0 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -302,6 +302,11 @@ pub fn decl_storage(input: TokenStream) -> TokenStream { /// The population of the genesis storage depends on the order of modules. So, if one of your /// modules depends on another module, the module that is depended upon needs to come before /// the module depending on it. +/// +/// # Type definitions +/// +/// * The macro generates a type alias for each pallet to their `Module` (or `Pallet`). +/// E.g. `type System = frame_system::Module` #[proc_macro] pub fn construct_runtime(input: TokenStream) -> TokenStream { construct_runtime::construct_runtime(input) diff --git a/substrate/frame/support/src/inherent.rs b/substrate/frame/support/src/inherent.rs index feb200dae5..430075d603 100644 --- a/substrate/frame/support/src/inherent.rs +++ b/substrate/frame/support/src/inherent.rs @@ -75,6 +75,7 @@ macro_rules! impl_outer_inherent { fn check_extrinsics(&self, block: &$block) -> $crate::inherent::CheckInherentsResult { use $crate::inherent::{ProvideInherent, IsFatalError}; use $crate::traits::IsSubType; + use $crate::sp_runtime::traits::Block as _; let mut result = $crate::inherent::CheckInherentsResult::new(); for xt in block.extrinsics() { diff --git a/substrate/frame/support/test/tests/construct_runtime.rs b/substrate/frame/support/test/tests/construct_runtime.rs index 2b9f026487..8dc44c2024 100644 --- a/substrate/frame/support/test/tests/construct_runtime.rs +++ b/substrate/frame/support/test/tests/construct_runtime.rs @@ -21,7 +21,7 @@ #![recursion_limit="128"] -use sp_runtime::{generic, traits::{BlakeTwo256, Block as _, Verify}, DispatchError}; +use sp_runtime::{generic, traits::{BlakeTwo256, Verify}, DispatchError}; use sp_core::{H256, sr25519}; use sp_std::cell::RefCell; use frame_support::traits::PalletInfo as _; diff --git a/substrate/frame/support/test/tests/instance.rs b/substrate/frame/support/test/tests/instance.rs index a734363b01..dc6c41564a 100644 --- a/substrate/frame/support/test/tests/instance.rs +++ b/substrate/frame/support/test/tests/instance.rs @@ -18,7 +18,7 @@ #![recursion_limit="128"] use codec::{Codec, EncodeLike, Encode, Decode}; -use sp_runtime::{generic, BuildStorage, traits::{BlakeTwo256, Block as _, Verify}}; +use sp_runtime::{generic, BuildStorage, traits::{BlakeTwo256, Verify}}; use frame_support::{ Parameter, traits::Get, parameter_types, metadata::{ diff --git a/substrate/frame/support/test/tests/issue2219.rs b/substrate/frame/support/test/tests/issue2219.rs index 59410c6db2..adabb2d597 100644 --- a/substrate/frame/support/test/tests/issue2219.rs +++ b/substrate/frame/support/test/tests/issue2219.rs @@ -16,7 +16,7 @@ // limitations under the License. use frame_support::sp_runtime::generic; -use frame_support::sp_runtime::traits::{BlakeTwo256, Block as _, Verify}; +use frame_support::sp_runtime::traits::{BlakeTwo256, Verify}; use frame_support::codec::{Encode, Decode}; use sp_core::{H256, sr25519}; use serde::{Serialize, Deserialize}; diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index 1e4bfa7474..974b901480 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -23,7 +23,7 @@ use frame_support::{ dispatch::{UnfilteredDispatchable, Parameter}, storage::unhashed, }; -use sp_runtime::{traits::Block as _, DispatchError}; +use sp_runtime::DispatchError; use sp_io::{TestExternalities, hashing::{twox_64, twox_128, blake2_128}}; pub struct SomeType1; diff --git a/substrate/frame/support/test/tests/pallet_compatibility.rs b/substrate/frame/support/test/tests/pallet_compatibility.rs index 7cc3392ef0..66d0134413 100644 --- a/substrate/frame/support/test/tests/pallet_compatibility.rs +++ b/substrate/frame/support/test/tests/pallet_compatibility.rs @@ -15,8 +15,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use sp_runtime::traits::Block as _; - pub trait SomeAssociation { type A: frame_support::dispatch::Parameter + Default; } diff --git a/substrate/frame/support/test/tests/pallet_compatibility_instance.rs b/substrate/frame/support/test/tests/pallet_compatibility_instance.rs index 05ad44e7a7..d7de03ea46 100644 --- a/substrate/frame/support/test/tests/pallet_compatibility_instance.rs +++ b/substrate/frame/support/test/tests/pallet_compatibility_instance.rs @@ -15,8 +15,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use sp_runtime::traits::Block as _; - mod pallet_old { use frame_support::{ decl_storage, decl_error, decl_event, decl_module, weights::Weight, traits::Get, Parameter diff --git a/substrate/frame/support/test/tests/pallet_instance.rs b/substrate/frame/support/test/tests/pallet_instance.rs index 2317fb05a2..62654d53e1 100644 --- a/substrate/frame/support/test/tests/pallet_instance.rs +++ b/substrate/frame/support/test/tests/pallet_instance.rs @@ -23,7 +23,7 @@ use frame_support::{ dispatch::UnfilteredDispatchable, storage::unhashed, }; -use sp_runtime::{traits::Block as _, DispatchError}; +use sp_runtime::DispatchError; use sp_io::{TestExternalities, hashing::{twox_64, twox_128, blake2_128}}; #[frame_support::pallet] diff --git a/substrate/frame/support/test/tests/pallet_version.rs b/substrate/frame/support/test/tests/pallet_version.rs index ca36ee7fc4..a86a876b48 100644 --- a/substrate/frame/support/test/tests/pallet_version.rs +++ b/substrate/frame/support/test/tests/pallet_version.rs @@ -20,7 +20,7 @@ #![recursion_limit="128"] use codec::{Decode, Encode}; -use sp_runtime::{generic, traits::{BlakeTwo256, Block as _, Verify}, BuildStorage}; +use sp_runtime::{generic, traits::{BlakeTwo256, Verify}, BuildStorage}; use frame_support::{ traits::{PALLET_VERSION_STORAGE_KEY_POSTFIX, PalletVersion, OnRuntimeUpgrade, GetPalletVersion}, crate_to_pallet_version, weights::Weight, diff --git a/substrate/frame/support/test/tests/pallet_with_name_trait_is_valid.rs b/substrate/frame/support/test/tests/pallet_with_name_trait_is_valid.rs index 42b0ebc6e9..6247e46c85 100644 --- a/substrate/frame/support/test/tests/pallet_with_name_trait_is_valid.rs +++ b/substrate/frame/support/test/tests/pallet_with_name_trait_is_valid.rs @@ -88,7 +88,6 @@ mod tests { use crate as pallet_test; use frame_support::parameter_types; - use sp_runtime::traits::Block; type SignedExtra = ( frame_system::CheckEra, diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index cdb2662373..87a636b37f 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -112,6 +112,8 @@ mod extensions; pub mod weights; #[cfg(test)] mod tests; +#[cfg(feature = "std")] +pub mod mocking; pub use extensions::{ diff --git a/substrate/frame/system/src/mocking.rs b/substrate/frame/system/src/mocking.rs new file mode 100644 index 0000000000..9f80c59a9c --- /dev/null +++ b/substrate/frame/system/src/mocking.rs @@ -0,0 +1,31 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Provide types to help defining a mock environment when testing pallets. + +use sp_runtime::generic; + +/// An unchecked extrinsic type to be used in tests. +pub type MockUncheckedExtrinsic = generic::UncheckedExtrinsic< + ::AccountId, ::Call, Signature, Extra, +>; + +/// An implementation of `sp_runtime::traits::Block` to be used in tests. +pub type MockBlock = generic::Block< + generic::Header<::BlockNumber, sp_runtime::traits::BlakeTwo256>, + MockUncheckedExtrinsic, +>;