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 1 commit
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
24 changes: 24 additions & 0 deletions frame/support/procedural/src/construct_runtime/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn expand_outer_dispatch(
let mut variant_patterns = Vec::new();
let mut query_call_part_macros = Vec::new();
let mut pallet_names = Vec::new();
let mut pallet_indices = Vec::new();
let mut pallet_attrs = Vec::new();
let system_path = &system_pallet.path;

Expand All @@ -57,6 +58,7 @@ pub fn expand_outer_dispatch(
});
variant_patterns.push(quote!(RuntimeCall::#name(call)));
pallet_names.push(name);
pallet_indices.push(index);
pallet_attrs.push(attr);
query_call_part_macros.push(quote! {
#path::__substrate_call_check::is_call_part_defined!(#name);
Expand Down Expand Up @@ -129,6 +131,7 @@ pub fn expand_outer_dispatch(
impl #scrate::dispatch::GetCallMetadata for RuntimeCall {
fn get_call_metadata(&self) -> #scrate::dispatch::CallMetadata {
use #scrate::dispatch::GetCallName;
use #scrate::dispatch::GetCallIndex;
match self {
#(
#pallet_attrs
Expand Down Expand Up @@ -160,6 +163,27 @@ pub fn expand_outer_dispatch(
_ => unreachable!(),
}
}

fn get_module_indices() -> &'static [u8] {
&[#(
#pallet_attrs
#pallet_indices,
)*]
}

fn get_call_indices(module: &str) -> &'static [u8] {
use #scrate::dispatch::{Callable, GetCallIndex};
match module {
#(
#pallet_attrs
stringify!(#pallet_names) =>
<<#pallet_names as Callable<#runtime>>::RuntimeCall
as GetCallIndex>::get_call_indices(),
)*
_ => unreachable!(),
}
}

}
impl #scrate::dispatch::Dispatchable for RuntimeCall {
type RuntimeOrigin = RuntimeOrigin;
Expand Down
2 changes: 1 addition & 1 deletion frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ pub fn compact(_: TokenStream, _: TokenStream) -> TokenStream {
///
/// The macro creates an enum `Call` with one variant per dispatchable. This enum implements:
/// [`Clone`], [`Eq`], [`PartialEq`], [`Debug`] (with stripped implementation in `not("std")`),
/// `Encode`, `Decode`, `GetDispatchInfo`, `GetCallName`, and `UnfilteredDispatchable`.
/// `Encode`, `Decode`, `GetDispatchInfo`, `GetCallName`, `GetCallIndex` and `UnfilteredDispatchable`.
///
/// The macro implements the `Callable` trait on `Pallet` and a function `call_functions`
/// which returns the dispatchable metadata.
Expand Down
15 changes: 15 additions & 0 deletions frame/support/procedural/src/pallet/expand/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
}
}

impl<#type_impl_gen> #frame_support::dispatch::GetCallIndex for #call_ident<#type_use_gen>
#where_clause
{
fn get_call_index(&self) -> u8 {
match *self {
#( Self::#fn_name { .. } => #call_index, )*
Self::__Ignore(_, _) => unreachable!("__PhantomItem cannot be used."),
}
}

fn get_call_indices() -> &'static [u8] {
&[ #( #call_index, )* ]
}
}

impl<#type_impl_gen> #frame_support::traits::UnfilteredDispatchable
for #call_ident<#type_use_gen>
#where_clause
Expand Down
18 changes: 17 additions & 1 deletion frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use crate::{
result,
},
traits::{
CallMetadata, GetCallMetadata, GetCallName, GetStorageVersion, UnfilteredDispatchable,
CallMetadata, GetCallMetadata, GetCallName, GetCallIndex, GetStorageVersion, UnfilteredDispatchable,
},
};
#[cfg(feature = "std")]
Expand Down Expand Up @@ -2834,6 +2834,22 @@ macro_rules! decl_module {
}
}

// Implement GetCallIndex for the Call.
impl<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $crate::dispatch::GetCallIndex
for $call_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )*
{
//fake impl
fn get_call_index(&self) -> u8 {
*self as u8
}
Comment thread
ggwpez marked this conversation as resolved.
Outdated

fn get_call_indices() -> &'static [u8] {
&[
0
]
}
}

// Implement `OnGenesis` for `Module`
impl<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $crate::traits::OnGenesis
for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )*
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 @@ -74,7 +74,7 @@ pub use randomness::Randomness;

mod metadata;
pub use metadata::{
CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo,
CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetCallIndex, GetStorageVersion, PalletInfo,
PalletInfoAccess, PalletInfoData, PalletsInfoAccess, StorageVersion,
STORAGE_VERSION_STORAGE_KEY_POSTFIX,
};
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 @@ -107,12 +107,22 @@ pub trait GetCallName {
fn get_call_name(&self) -> &'static str;
}

/// Gets the function index of the Call
pub trait GetCallIndex {
fn get_call_indices() -> &'static [u8];
fn get_call_index(&self) -> u8;
}

/// Gets the metadata for the Call - function name and pallet name.
pub trait GetCallMetadata {
/// Return all module names.
fn get_module_names() -> &'static [&'static str];
/// Return all module idices.
fn get_module_indices() -> &'static [u8];
/// Return all function names for the given `module`.
fn get_call_names(module: &str) -> &'static [&'static str];
/// Return all function indices for the given `module`.
fn get_call_indices(module: &str) -> &'static [u8];
/// Return a [`CallMetadata`], containing function and pallet name of the Call.
fn get_call_metadata(&self) -> CallMetadata;
}
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use frame_support::{
pallet_prelude::{StorageInfoTrait, ValueQuery},
storage::unhashed,
traits::{
ConstU32, GetCallName, GetStorageVersion, OnFinalize, OnGenesis, OnInitialize,
ConstU32, GetCallName, GetCallIndex, GetStorageVersion, OnFinalize, OnGenesis, OnInitialize,
OnRuntimeUpgrade, PalletError, PalletInfoAccess, StorageVersion,
},
weights::{RuntimeDbWeight, Weight},
Expand Down
2 changes: 1 addition & 1 deletion frame/support/test/tests/pallet_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use frame_support::{
dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays, UnfilteredDispatchable},
pallet_prelude::ValueQuery,
storage::unhashed,
traits::{ConstU32, GetCallName, OnFinalize, OnGenesis, OnInitialize, OnRuntimeUpgrade},
traits::{ConstU32, GetCallName, GetCallMetadata, OnFinalize, OnGenesis, OnInitialize, OnRuntimeUpgrade},
};
use sp_io::{
hashing::{blake2_128, twox_128, twox_64},
Expand Down