Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ mod tests {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ mod tests {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = ();
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/society/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type OnNewAccount = ();
type OnKilledAccount = ();
type AccountData = pallet_balances::AccountData<u64>;
Expand Down
23 changes: 21 additions & 2 deletions frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,7 +2409,7 @@ mod tests {
use crate::weights::{DispatchInfo, DispatchClass, Pays, RuntimeDbWeight};
use crate::traits::{
CallMetadata, GetCallMetadata, GetCallName, OnInitialize, OnFinalize, OnRuntimeUpgrade,
IntegrityTest, Get,
IntegrityTest, Get, PalletInfo,
};

pub trait Config: system::Config + Sized where Self::AccountId: From<u32> { }
Expand Down Expand Up @@ -2562,13 +2562,32 @@ mod tests {
}
}

impl PalletInfo for TraitImpl {
fn index<P: 'static>() -> Option<usize> {
let type_id = sp_std::any::TypeId::of::<P>();
if type_id == sp_std::any::TypeId::of::<Test>() {
return Some(0)
}

None
}
fn name<P: 'static>() -> Option<&'static str> {
let type_id = sp_std::any::TypeId::of::<P>();
if type_id == sp_std::any::TypeId::of::<Test>() {
return Some("Test")
}

None
}
}

impl system::Config for TraitImpl {
type Origin = OuterOrigin;
type AccountId = u32;
type Call = OuterCall;
type BaseCallFilter = ();
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = Self;
type DbWeight = ();
}

Expand Down
6 changes: 3 additions & 3 deletions frame/support/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ mod tests {
impl system::Config for TestRuntime {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = crate::tests::PanicPalletInfo;
type DbWeight = ();
}

Expand All @@ -744,14 +744,14 @@ mod tests {
impl system_renamed::Config for TestRuntime2 {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = crate::tests::PanicPalletInfo;
type DbWeight = ();
}

impl system::Config for TestRuntime2 {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = crate::tests::PanicPalletInfo;
type DbWeight = ();
}

Expand Down
16 changes: 14 additions & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ macro_rules! assert_ok {
pub use serde::{Serialize, Deserialize};

#[cfg(test)]
mod tests {
pub mod tests {
use super::*;
use codec::{Codec, EncodeLike};
use frame_metadata::{
Expand All @@ -581,6 +581,18 @@ mod tests {
use sp_std::{marker::PhantomData, result};
use sp_io::TestExternalities;

/// A PalletInfo implementation which just panics.
pub struct PanicPalletInfo;

impl crate::traits::PalletInfo for PanicPalletInfo {
fn index<P: 'static>() -> Option<usize> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
}
fn name<P: 'static>() -> Option<&'static str> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
}
}

pub trait Config: 'static {
type BlockNumber: Codec + EncodeLike + Default;
type Origin;
Expand Down Expand Up @@ -625,7 +637,7 @@ mod tests {
impl Config for Test {
type BlockNumber = u32;
type Origin = u32;
type PalletInfo = ();
type PalletInfo = PanicPalletInfo;
type DbWeight = ();
}

Expand Down
39 changes: 37 additions & 2 deletions frame/support/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ pub use frame_metadata::{
///# }
///# use module0 as module1;
///# use module0 as module2;
///# impl frame_support::traits::PalletInfo for Runtime {
///# fn index<P: 'static>() -> Option<usize> { unimplemented!() }
///# fn name<P: 'static>() -> Option<&'static str> { unimplemented!() }
///# }
///# impl module0::Config for Runtime {
///# type Origin = u32;
///# type BlockNumber = u32;
///# type PalletInfo = ();
///# type PalletInfo = Self;
///# type DbWeight = ();
///# }
///#
Expand Down Expand Up @@ -414,6 +418,37 @@ mod tests {
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub struct TestRuntime;

impl crate::traits::PalletInfo for TestRuntime {
fn index<P: 'static>() -> Option<usize> {
let type_id = sp_std::any::TypeId::of::<P>();
if type_id == sp_std::any::TypeId::of::<system::Module<TestRuntime>>() {
return Some(0)
}
if type_id == sp_std::any::TypeId::of::<EventModule>() {
return Some(1)
}
if type_id == sp_std::any::TypeId::of::<EventModule2>() {
return Some(2)
}

None
}
fn name<P: 'static>() -> Option<&'static str> {
let type_id = sp_std::any::TypeId::of::<P>();
if type_id == sp_std::any::TypeId::of::<system::Module<TestRuntime>>() {
return Some("System")
}
if type_id == sp_std::any::TypeId::of::<EventModule>() {
return Some("EventModule")
}
if type_id == sp_std::any::TypeId::of::<EventModule2>() {
return Some("EventModule2")
}

None
}
}
Copy link
Contributor Author

@gui1117 gui1117 Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I didn't use construct_runtime here because I don't think it is really needed for the test, and the easiest is a manual implementation.


impl_outer_event! {
pub enum TestEvent for TestRuntime {
system,
Expand Down Expand Up @@ -451,7 +486,7 @@ mod tests {
type AccountId = u32;
type BlockNumber = u32;
type SomeValue = SystemValue;
type PalletInfo = ();
type PalletInfo = Self;
type DbWeight = ();
type Call = Call;
}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/storage/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod tests {
impl Config for Runtime {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = crate::tests::PanicPalletInfo;
type DbWeight = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ mod tests {
type BlockNumber = u32;
type Balance = u32;
type DbWeight = DbWeight;
type PalletInfo = ();
type PalletInfo = crate::tests::PanicPalletInfo;
}

decl_module! {
Expand Down
12 changes: 12 additions & 0 deletions frame/support/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ frame_support::decl_module! {
/// Some test module
pub struct Module<T: Config> for enum Call where origin: T::Origin, system=self {}
}

/// A PalletInfo implementation which just panics.
pub struct PanicPalletInfo;

impl frame_support::traits::PalletInfo for PanicPalletInfo {
fn index<P: 'static>() -> Option<usize> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
}
fn name<P: 'static>() -> Option<&'static str> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
}
}
8 changes: 4 additions & 4 deletions frame/support/test/tests/decl_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod tests {
impl frame_support_test::Config for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

Expand Down Expand Up @@ -441,7 +441,7 @@ mod test2 {
impl frame_support_test::Config for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

Expand Down Expand Up @@ -469,7 +469,7 @@ mod test3 {
impl frame_support_test::Config for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

Expand Down Expand Up @@ -514,7 +514,7 @@ mod test_append_and_len {
impl frame_support_test::Config for Test {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/genesisconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Test;
impl frame_support_test::Config for Test {
type BlockNumber = u32;
type Origin = ();
type PalletInfo = ();
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl system::Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type PalletInfo = ();
type PalletInfo = PalletInfo;
type Call = Call;
type DbWeight = ();
}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/issue2219.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl system::Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type PalletInfo = ();
type PalletInfo = PalletInfo;
type Call = Call;
type DbWeight = ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod tests {
type BlockWeights = ();
type BlockLength = ();
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Runtime;
impl frame_support_test::Config for Runtime {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ mod tests {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down
33 changes: 32 additions & 1 deletion test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,37 @@ impl From<frame_system::Event<Runtime>> for Event {
}
}

impl frame_support::traits::PalletInfo for Runtime {
fn index<P: 'static>() -> Option<usize> {
let type_id = sp_std::any::TypeId::of::<P>();
if type_id == sp_std::any::TypeId::of::<system::Module<Runtime>>() {
return Some(0)
}
if type_id == sp_std::any::TypeId::of::<pallet_timestamp::Module<Runtime>>() {
return Some(1)
}
if type_id == sp_std::any::TypeId::of::<pallet_babe::Module<Runtime>>() {
return Some(2)
}

None
}
fn name<P: 'static>() -> Option<&'static str> {
let type_id = sp_std::any::TypeId::of::<P>();
if type_id == sp_std::any::TypeId::of::<system::Module<Runtime>>() {
return Some("System")
}
if type_id == sp_std::any::TypeId::of::<pallet_timestamp::Module<Runtime>>() {
return Some("Timestamp")
}
if type_id == sp_std::any::TypeId::of::<pallet_babe::Module<Runtime>>() {
return Some("Babe")
}

None
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I didn't use construct_runtime here because runtime declare a very specific implementation of Call which is not compatible with the code generated by construct_runtime


parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
pub const MinimumPeriod: u64 = 5;
Expand Down Expand Up @@ -463,7 +494,7 @@ impl frame_system::Config for Runtime {
type BlockHashCount = BlockHashCount;
type DbWeight = ();
type Version = ();
type PalletInfo = ();
type PalletInfo = Self;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
Expand Down