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

Commit

Permalink
Make pallet macro generate accessor to PalletInfo information on pall…
Browse files Browse the repository at this point in the history
…et placeholder (#8630)

* generate accessor to PalletInfo information on pallet placeholder

* remove unused

* use trait, and add tests

* less verbose doc

* add PalletInfoAccess to prelude for ease usage
  • Loading branch information
gui1117 authored Apr 19, 2021
1 parent 4b9a6bb commit 3a19eb6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
23 changes: 23 additions & 0 deletions frame/support/procedural/src/pallet/expand/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::pallet::{Def, parse::helper::get_doc_literals};
/// * Implement ModuleErrorMetadata on Pallet
/// * declare Module type alias for construct_runtime
/// * replace the first field type of `struct Pallet` with `PhantomData` if it is `_`
/// * implementation of `PalletInfoAccess` information
pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
let frame_support = &def.frame_support;
let frame_system = &def.frame_system;
Expand Down Expand Up @@ -134,5 +135,27 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
.put_into_storage::<<T as #frame_system::Config>::PalletInfo, Self>();
}
}

// Implement `PalletInfoAccess` for `Pallet`
impl<#type_impl_gen> #frame_support::traits::PalletInfoAccess
for #pallet_ident<#type_use_gen>
#config_where_clause
{
fn index() -> usize {
<
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
>::index::<Self>()
.expect("Pallet is part of the runtime because pallet `Config` trait is \
implemented by the runtime")
}

fn name() -> &'static str {
<
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
>::name::<Self>()
.expect("Pallet is part of the runtime because pallet `Config` trait is \
implemented by the runtime")
}
}
)
}
6 changes: 5 additions & 1 deletion frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ pub mod pallet_prelude {
EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, DebugNoBound, CloneNoBound, Twox256,
Twox128, Blake2_256, Blake2_128, Identity, Twox64Concat, Blake2_128Concat, ensure,
RuntimeDebug, storage,
traits::{Get, Hooks, IsType, GetPalletVersion, EnsureOrigin},
traits::{Get, Hooks, IsType, GetPalletVersion, EnsureOrigin, PalletInfoAccess},
dispatch::{DispatchResultWithPostInfo, Parameter, DispatchError, DispatchResult},
weights::{DispatchClass, Pays, Weight},
storage::types::{StorageValue, StorageMap, StorageDoubleMap, ValueQuery, OptionQuery},
Expand Down Expand Up @@ -1362,6 +1362,10 @@ pub mod pallet_prelude {
///
/// It declare `type Module` type alias for `Pallet`, used by [`construct_runtime`].
///
/// It implements [`traits::PalletInfoAccess`] on `Pallet` to ease access to pallet informations
/// given by [`frame_support::traits::PalletInfo`].
/// (The implementation use the associated type `frame_system::Config::PalletInfo`).
///
/// If attribute generate_store then macro create the trait `Store` and implement it on `Pallet`.
///
/// # Hooks: `#[pallet::hooks]` mandatory
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use randomness::Randomness;
mod metadata;
pub use metadata::{
CallMetadata, GetCallMetadata, GetCallName, PalletInfo, PalletVersion, GetPalletVersion,
PALLET_VERSION_STORAGE_KEY_POSTFIX,
PALLET_VERSION_STORAGE_KEY_POSTFIX, PalletInfoAccess,
};

mod hooks;
Expand Down
10 changes: 10 additions & 0 deletions frame/support/src/traits/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ pub trait PalletInfo {
fn name<P: 'static>() -> Option<&'static str>;
}

/// Provides information about the pallet setup in the runtime.
///
/// Access the information provided by [`PalletInfo`] for a specific pallet.
pub trait PalletInfoAccess {
/// Index of the pallet as configured in the runtime.
fn index() -> usize;
/// Name of the pallet as configured in the runtime.
fn name() -> &'static str;
}

/// The function and pallet name of the Call.
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug)]
pub struct CallMetadata {
Expand Down
11 changes: 11 additions & 0 deletions frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,14 @@ fn metadata() {

pretty_assertions::assert_eq!(pallet_metadata, expected_pallet_metadata);
}

#[test]
fn test_pallet_info_access() {
assert_eq!(<System as frame_support::traits::PalletInfoAccess>::name(), "System");
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::name(), "Example");
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::name(), "Example2");

assert_eq!(<System as frame_support::traits::PalletInfoAccess>::index(), 0);
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::index(), 1);
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::index(), 2);
}
15 changes: 15 additions & 0 deletions frame/support/test/tests/pallet_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,18 @@ fn metadata() {
pretty_assertions::assert_eq!(pallet_metadata, expected_pallet_metadata);
pretty_assertions::assert_eq!(pallet_instance1_metadata, expected_pallet_instance1_metadata);
}

#[test]
fn test_pallet_info_access() {
assert_eq!(<System as frame_support::traits::PalletInfoAccess>::name(), "System");
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::name(), "Example");
assert_eq!(<Instance1Example as frame_support::traits::PalletInfoAccess>::name(), "Instance1Example");
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::name(), "Example2");
assert_eq!(<Instance1Example2 as frame_support::traits::PalletInfoAccess>::name(), "Instance1Example2");

assert_eq!(<System as frame_support::traits::PalletInfoAccess>::index(), 0);
assert_eq!(<Example as frame_support::traits::PalletInfoAccess>::index(), 1);
assert_eq!(<Instance1Example as frame_support::traits::PalletInfoAccess>::index(), 2);
assert_eq!(<Example2 as frame_support::traits::PalletInfoAccess>::index(), 3);
assert_eq!(<Instance1Example2 as frame_support::traits::PalletInfoAccess>::index(), 4);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ frame_support::decl_module! {
const Foo: u32 = u32::max_value();

#[weight = 0]
fn accumulate_dummy(origin, increase_by: T::Balance) {
fn accumulate_dummy(_origin, _increase_by: T::Balance) {
unimplemented!();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait Config: 'static + Eq + Clone {
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin, system=self {
#[weight = 0]
fn noop(origin) {}
fn noop(_origin) {}
}
}

Expand Down

0 comments on commit 3a19eb6

Please sign in to comment.