Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit c147734

Browse files
committed
Add PalletInfo impl to avid storage collision, fixes tests
1 parent 6fe2313 commit c147734

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

frame/balances/src/tests_local.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use frame_support::traits::StorageMapShim;
3030
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
3131
use crate::{GenesisConfig, Module, Config, decl_tests, tests::CallWithDispatchInfo};
3232
use pallet_transaction_payment::CurrencyAdapter;
33+
use std::any::TypeId;
3334

3435
use frame_system as system;
3536
impl_outer_origin!{
@@ -47,6 +48,38 @@ impl_outer_event! {
4748
}
4849
}
4950

51+
/// Provides an implementation of `PalletInfo`.
52+
///
53+
/// Usually this is generated by the `construct_runtime!` macro, but tests typically use `()`.
54+
/// However the impl for `()` returns the same prefix "test" for all modules, which causes
55+
/// collisions between pallets with storage items with the same name. For instance in this case
56+
/// System and Balances both have a storage item called `Account`.
57+
pub struct PalletInfo;
58+
59+
impl frame_support::traits::PalletInfo for PalletInfo {
60+
fn index<P: 'static>() -> Option<usize> {
61+
let type_id = TypeId::of::<P>();
62+
if type_id == TypeId::of::<system::Pallet<Test>>() {
63+
return Some(0)
64+
}
65+
if type_id == TypeId::of::<crate::Pallet<Test>>() {
66+
return Some(1)
67+
}
68+
None
69+
}
70+
71+
fn name<P: 'static>() -> Option<&'static str> {
72+
let type_id = TypeId::of::<P>();
73+
if type_id == TypeId::of::<system::Pallet<Test>>() {
74+
return Some("System")
75+
}
76+
if type_id == TypeId::of::<crate::Pallet<Test>>() {
77+
return Some("Balances")
78+
}
79+
None
80+
}
81+
}
82+
5083
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
5184
#[derive(Clone, PartialEq, Eq, Debug)]
5285
pub struct Test;
@@ -73,7 +106,7 @@ impl frame_system::Config for Test {
73106
type Event = Event;
74107
type BlockHashCount = BlockHashCount;
75108
type Version = ();
76-
type PalletInfo = ();
109+
type PalletInfo = PalletInfo;
77110
type AccountData = ();
78111
type OnNewAccount = ();
79112
type OnKilledAccount = ();

0 commit comments

Comments
 (0)