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

Make pallet macro generate accessor to PalletInfo information on pallet placeholder #8630

Merged
6 commits merged into from
Apr 19, 2021
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
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Is PalletInfo already taken? Access is weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PalletInfo is the trait giving the info for all pallets https://substrate.dev/rustdocs/v3.0.0/frame_support/traits/trait.PalletInfo.html
It is implemented by an associated type on frame-system https://substrate.dev/rustdocs/v3.0.0/frame_system/pallet/trait.Config.html#associatedtype.PalletInfo

Ideally PalletInfo could be renamed PalletsInfo as it contains info for all pallets, and thus we could use PalletInfo here.

Anyway those 2 traits are very related, PalletInfoAccess is implementing accessor to the info of the pallet. info of the pallet are retrieved from PalletInfo which contains info for all pallets (but which is cumbersome to use directly).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally PalletInfo could be renamed PalletsInfo as it contains info for all pallets, and thus we could use PalletInfo here.

Totally agree with this, maybe even in this PR? the naming is weird, but the functionality is great, working with the old PalletInfo was not simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the same time I prefer not to break, as PalletInfoAccess is also an OK name to me.
Maybe we can write it somewhere for when we want to break frame stuff.

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, lets stay backwards compat.

/// 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