Rename ModuleToIndex to PalletRuntimeSetup (#7148)

* Rename `ModuleToIndex` to `PalletRuntimeSetup`

Besides the renaming it also adds support getting the name of a pallet
as configured in the runtime.

* Rename it to `PalletInfo`

* Remove accidentally added files
This commit is contained in:
Bastian Köcher
2020-09-22 15:39:56 +02:00
committed by GitHub
parent 22632efc5f
commit 86594727d9
62 changed files with 119 additions and 88 deletions
@@ -52,6 +52,8 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
..
} = definition;
let modules = modules.into_pairs().map(|v| v.into_value()).collect::<Vec<_>>();
// Assert we have system module declared
let system_module = match find_system_module(modules.iter()) {
Some(sm) => sm,
@@ -82,7 +84,7 @@ fn construct_runtime_parsed(definition: RuntimeDefinition) -> Result<TokenStream
&scrate,
)?;
let all_modules = decl_all_modules(&name, modules.iter());
let module_to_index = decl_module_to_index(modules.iter(), modules.len(), &scrate);
let module_to_index = decl_pallet_runtime_setup(&modules, &scrate);
let dispatch = decl_outer_dispatch(&name, modules.iter(), &scrate);
let metadata = decl_runtime_metadata(&name, modules.iter(), &scrate, &unchecked_extrinsic);
@@ -376,22 +378,23 @@ fn decl_all_modules<'a>(
)
}
fn decl_module_to_index<'a>(
module_declarations: impl Iterator<Item = &'a ModuleDeclaration>,
num_modules: usize,
fn decl_pallet_runtime_setup(
module_declarations: &[ModuleDeclaration],
scrate: &TokenStream2,
) -> TokenStream2 {
let names = module_declarations.map(|d| &d.name);
let indices = 0..num_modules;
let names = module_declarations.iter().map(|d| &d.name);
let names2 = module_declarations.iter().map(|d| &d.name);
let name_strings = module_declarations.iter().map(|d| d.name.to_string());
let indices = 0..module_declarations.len();
quote!(
/// Provides an implementation of `ModuleToIndex` to map a module
/// to its index in the runtime.
pub struct ModuleToIndex;
/// Provides an implementation of `PalletInfo` to provide information
/// about the pallet setup in the runtime.
pub struct PalletInfo;
impl #scrate::traits::ModuleToIndex for ModuleToIndex {
fn module_to_index<M: 'static>() -> Option<usize> {
let type_id = #scrate::sp_std::any::TypeId::of::<M>();
impl #scrate::traits::PalletInfo for PalletInfo {
fn index<P: 'static>() -> Option<usize> {
let type_id = #scrate::sp_std::any::TypeId::of::<P>();
#(
if type_id == #scrate::sp_std::any::TypeId::of::<#names>() {
return Some(#indices)
@@ -400,6 +403,17 @@ fn decl_module_to_index<'a>(
None
}
fn name<P: 'static>() -> Option<&'static str> {
let type_id = #scrate::sp_std::any::TypeId::of::<P>();
#(
if type_id == #scrate::sp_std::any::TypeId::of::<#names2>() {
return Some(#name_strings)
}
)*
None
}
}
)
}
+2 -2
View File
@@ -140,8 +140,8 @@ macro_rules! decl_error {
for $crate::sp_runtime::DispatchError
{
fn from(err: $error<$generic $(, $inst_generic)?>) -> Self {
let index = <$generic::ModuleToIndex as $crate::traits::ModuleToIndex>
::module_to_index::<$module<$generic $(, $inst_generic)?>>()
let index = <$generic::PalletInfo as $crate::traits::PalletInfo>
::index::<$module<$generic $(, $inst_generic)?>>()
.expect("Every active module has an index in the runtime; qed") as u8;
$crate::sp_runtime::DispatchError::Module {
+2 -2
View File
@@ -297,7 +297,7 @@ mod tests {
type AccountId: From<u32> + Encode;
type BlockNumber: From<u32> + Encode;
type SomeValue: Get<u32>;
type ModuleToIndex: crate::traits::ModuleToIndex;
type PalletInfo: crate::traits::PalletInfo;
type Call;
}
@@ -443,7 +443,7 @@ mod tests {
type AccountId = u32;
type BlockNumber = u32;
type SomeValue = SystemValue;
type ModuleToIndex = ();
type PalletInfo = ();
type Call = Call;
}
+11 -9
View File
@@ -1313,8 +1313,6 @@ impl<T: Clone + Ord> ChangeMembers<T> for () {
fn set_prime(_: Option<T>) {}
}
/// Trait for type that can handle the initialization of account IDs at genesis.
pub trait InitializeMembers<AccountId> {
/// Initialize the members to the given `members`.
@@ -1380,16 +1378,20 @@ pub trait ValidatorRegistration<ValidatorId> {
fn is_registered(id: &ValidatorId) -> bool;
}
/// Something that can convert a given module into the index of the module in the runtime.
/// Provides information about the pallet setup in the runtime.
///
/// The index of a module is determined by the position it appears in `construct_runtime!`.
pub trait ModuleToIndex {
/// Convert the given module `M` into an index.
fn module_to_index<M: 'static>() -> Option<usize>;
/// An implementor should be able to provide information about each pallet that
/// is configured in `construct_runtime!`.
pub trait PalletInfo {
/// Convert the given pallet `P` into its index as configured in the runtime.
fn index<P: 'static>() -> Option<usize>;
/// Convert the given pallet `P` into its name as configured in the runtime.
fn name<P: 'static>() -> Option<&'static str>;
}
impl ModuleToIndex for () {
fn module_to_index<M: 'static>() -> Option<usize> { Some(0) }
impl PalletInfo for () {
fn index<P: 'static>() -> Option<usize> { Some(0) }
fn name<P: 'static>() -> Option<&'static str> { Some("test") }
}
/// The function and pallet name of the Call.
@@ -24,13 +24,14 @@
use sp_runtime::{generic, traits::{BlakeTwo256, Block as _, Verify}, DispatchError};
use sp_core::{H256, sr25519};
use sp_std::cell::RefCell;
use frame_support::traits::PalletInfo as _;
mod system;
pub trait Currency {}
thread_local! {
pub static INTEGRITY_TEST_EXEC: RefCell<u32> = RefCell::new(0);
pub static INTEGRITY_TEST_EXEC: RefCell<u32> = RefCell::new(0);
}
mod module1 {
@@ -107,7 +108,7 @@ impl system::Trait for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type ModuleToIndex = ModuleToIndex;
type PalletInfo = PalletInfo;
type Call = Call;
}
@@ -157,3 +158,15 @@ fn integrity_test_works() {
__construct_runtime_integrity_test::runtime_integrity_tests();
assert_eq!(INTEGRITY_TEST_EXEC.with(|i| *i.borrow()), 1);
}
#[test]
fn pallet_in_runtime_is_correct() {
assert_eq!(PalletInfo::index::<System>().unwrap(), 0);
assert_eq!(PalletInfo::name::<System>().unwrap(), "System");
assert_eq!(PalletInfo::index::<Module1_2>().unwrap(), 3);
assert_eq!(PalletInfo::name::<Module1_2>().unwrap(), "Module1_2");
assert_eq!(PalletInfo::index::<Module2>().unwrap(), 2);
assert_eq!(PalletInfo::name::<Module2>().unwrap(), "Module2");
}
@@ -241,7 +241,7 @@ impl system::Trait for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type ModuleToIndex = ();
type PalletInfo = ();
type Call = Call;
}
@@ -164,7 +164,7 @@ impl system::Trait for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type ModuleToIndex = ();
type PalletInfo = ();
type Call = Call;
}
+1 -1
View File
@@ -27,7 +27,7 @@ pub trait Trait: 'static + Eq + Clone {
type AccountId: Encode + EncodeLike + Decode;
type Call;
type Event: From<Event<Self>>;
type ModuleToIndex: frame_support::traits::ModuleToIndex;
type PalletInfo: frame_support::traits::PalletInfo;
}
frame_support::decl_module! {