From 71c67a608a6459c499bf96d943a036653e75a096 Mon Sep 17 00:00:00 2001 From: Kasper Ziemianek Date: Tue, 7 Mar 2023 16:05:36 +0100 Subject: [PATCH 1/6] GetCallIndex trait --- .../src/construct_runtime/expand/call.rs | 24 +++++++++++++++++++ frame/support/procedural/src/lib.rs | 2 +- .../procedural/src/pallet/expand/call.rs | 15 ++++++++++++ frame/support/src/dispatch.rs | 18 +++++++++++++- frame/support/src/traits.rs | 2 +- frame/support/src/traits/metadata.rs | 10 ++++++++ frame/support/test/tests/pallet.rs | 2 +- frame/support/test/tests/pallet_instance.rs | 2 +- 8 files changed, 70 insertions(+), 5 deletions(-) diff --git a/frame/support/procedural/src/construct_runtime/expand/call.rs b/frame/support/procedural/src/construct_runtime/expand/call.rs index 5ec665682ddaa..44c845380a15a 100644 --- a/frame/support/procedural/src/construct_runtime/expand/call.rs +++ b/frame/support/procedural/src/construct_runtime/expand/call.rs @@ -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; @@ -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); @@ -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 @@ -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; diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index fec824107f123..53080c9af919a 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -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. diff --git a/frame/support/procedural/src/pallet/expand/call.rs b/frame/support/procedural/src/pallet/expand/call.rs index 3db454eb6211b..1ad99587728ea 100644 --- a/frame/support/procedural/src/pallet/expand/call.rs +++ b/frame/support/procedural/src/pallet/expand/call.rs @@ -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 diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index c26a3d8e71a36..3b379fca036c2 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -29,7 +29,7 @@ pub use crate::{ result, }, traits::{ - CallMetadata, GetCallMetadata, GetCallName, GetStorageVersion, UnfilteredDispatchable, + CallMetadata, GetCallMetadata, GetCallName, GetCallIndex, GetStorageVersion, UnfilteredDispatchable, }, }; #[cfg(feature = "std")] @@ -2834,6 +2834,22 @@ macro_rules! decl_module { } } + // Implement GetCallIndex for the Call. + impl<$trait_instance: $trait_name $(, $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 + } + + fn get_call_indices() -> &'static [u8] { + &[ + 0 + ] + } + } + // Implement `OnGenesis` for `Module` impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::traits::OnGenesis for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index da8efe6afc483..a209655b95728 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -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, }; diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index f3e4b955da4a9..0e874818ed995 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -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; } diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 8ce85fa506348..a448a44406b8b 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -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}, diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index ea1e27fe9f348..e5327cfede8e3 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -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}, From 7e4468c8440ec4936db83be59236b197fa011f33 Mon Sep 17 00:00:00 2001 From: Kasper Ziemianek Date: Fri, 17 Mar 2023 19:45:13 +0100 Subject: [PATCH 2/6] final impl --- .../src/construct_runtime/expand/call.rs | 24 ------------------- frame/support/src/dispatch.rs | 16 ------------- frame/support/src/traits/metadata.rs | 4 ---- frame/support/test/tests/pallet.rs | 3 +++ frame/support/test/tests/pallet_instance.rs | 2 +- 5 files changed, 4 insertions(+), 45 deletions(-) diff --git a/frame/support/procedural/src/construct_runtime/expand/call.rs b/frame/support/procedural/src/construct_runtime/expand/call.rs index 44c845380a15a..5ec665682ddaa 100644 --- a/frame/support/procedural/src/construct_runtime/expand/call.rs +++ b/frame/support/procedural/src/construct_runtime/expand/call.rs @@ -31,7 +31,6 @@ 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; @@ -58,7 +57,6 @@ 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); @@ -131,7 +129,6 @@ 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 @@ -163,27 +160,6 @@ 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; diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 3b379fca036c2..40776a5e65e23 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2834,22 +2834,6 @@ macro_rules! decl_module { } } - // Implement GetCallIndex for the Call. - impl<$trait_instance: $trait_name $(, $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 - } - - fn get_call_indices() -> &'static [u8] { - &[ - 0 - ] - } - } - // Implement `OnGenesis` for `Module` impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::traits::OnGenesis for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 0e874818ed995..8ee88adc768fc 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -117,12 +117,8 @@ pub trait GetCallIndex { 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; } diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index a448a44406b8b..a900e81206853 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -725,6 +725,9 @@ fn call_expand() { pallet::Call::::get_call_names(), &["foo", "foo_storage_layer", "foo_no_post_info", "check_for_dispatch_context"], ); + + assert_eq!(call_foo.get_call_index(), 0u8); + assert_eq!(pallet::Call::::get_call_indices(), &[0u8, 1u8, 2u8, 3u8]) } #[test] diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index e5327cfede8e3..ea1e27fe9f348 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -19,7 +19,7 @@ use frame_support::{ dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays, UnfilteredDispatchable}, pallet_prelude::ValueQuery, storage::unhashed, - traits::{ConstU32, GetCallName, GetCallMetadata, OnFinalize, OnGenesis, OnInitialize, OnRuntimeUpgrade}, + traits::{ConstU32, GetCallName, OnFinalize, OnGenesis, OnInitialize, OnRuntimeUpgrade}, }; use sp_io::{ hashing::{blake2_128, twox_128, twox_64}, From 19ac6f3102f2d8f4b7dc38c379dcf5c373f2a15b Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Sat, 18 Mar 2023 13:32:54 +0000 Subject: [PATCH 3/6] ".git/.scripts/commands/fmt/fmt.sh" --- frame/support/procedural/src/lib.rs | 3 ++- frame/support/src/dispatch.rs | 3 ++- frame/support/src/traits.rs | 4 ++-- frame/support/test/tests/pallet.rs | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 53080c9af919a..e1d806573f617 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -933,7 +933,8 @@ 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`, `GetCallIndex` 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. diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 40776a5e65e23..56816ba6db6e1 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -29,7 +29,8 @@ pub use crate::{ result, }, traits::{ - CallMetadata, GetCallMetadata, GetCallName, GetCallIndex, GetStorageVersion, UnfilteredDispatchable, + CallMetadata, GetCallIndex, GetCallMetadata, GetCallName, GetStorageVersion, + UnfilteredDispatchable, }, }; #[cfg(feature = "std")] diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index a209655b95728..c50702d4435b7 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -74,8 +74,8 @@ pub use randomness::Randomness; mod metadata; pub use metadata::{ - CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetCallIndex, GetStorageVersion, PalletInfo, - PalletInfoAccess, PalletInfoData, PalletsInfoAccess, StorageVersion, + CallMetadata, CrateVersion, GetCallIndex, GetCallMetadata, GetCallName, GetStorageVersion, + PalletInfo, PalletInfoAccess, PalletInfoData, PalletsInfoAccess, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, }; diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index a900e81206853..c4cff63aee185 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -25,8 +25,8 @@ use frame_support::{ pallet_prelude::{StorageInfoTrait, ValueQuery}, storage::unhashed, traits::{ - ConstU32, GetCallName, GetCallIndex, GetStorageVersion, OnFinalize, OnGenesis, OnInitialize, - OnRuntimeUpgrade, PalletError, PalletInfoAccess, StorageVersion, + ConstU32, GetCallIndex, GetCallName, GetStorageVersion, OnFinalize, OnGenesis, + OnInitialize, OnRuntimeUpgrade, PalletError, PalletInfoAccess, StorageVersion, }, weights::{RuntimeDbWeight, Weight}, }; From b9bad2ee0edf8dd93dab367e3719fda11860ddd9 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 21 Mar 2023 16:50:44 +0100 Subject: [PATCH 4/6] Docs Signed-off-by: Oliver Tale-Yazdi --- frame/support/src/traits/metadata.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 8ee88adc768fc..8aa0bf2c6e4a1 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -109,7 +109,9 @@ pub trait GetCallName { /// Gets the function index of the Call pub trait GetCallIndex { + /// Return all call indices. fn get_call_indices() -> &'static [u8]; + /// Return the index of this call. fn get_call_index(&self) -> u8; } From 28c5bbf21698ada8e87e0b0719fc0ca220310a22 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 21 Mar 2023 17:11:24 +0100 Subject: [PATCH 5/6] One more test Signed-off-by: Oliver Tale-Yazdi --- frame/support/src/traits/metadata.rs | 6 +++--- frame/support/test/tests/pallet.rs | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 8aa0bf2c6e4a1..6f76910ad6c0c 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -101,7 +101,7 @@ pub struct CallMetadata { /// Gets the function name of the Call. pub trait GetCallName { - /// Return all function names. + /// Return all function names in an undefined order. fn get_call_names() -> &'static [&'static str]; /// Return the function name of the Call. fn get_call_name(&self) -> &'static str; @@ -109,9 +109,9 @@ pub trait GetCallName { /// Gets the function index of the Call pub trait GetCallIndex { - /// Return all call indices. + /// Return all call indices in an undefined order. fn get_call_indices() -> &'static [u8]; - /// Return the index of this call. + /// Return the index of this Call. fn get_call_index(&self) -> u8; } diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index c4cff63aee185..9eab60667cc5e 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -225,6 +225,12 @@ pub mod pallet { Ok(().into()) } + #[pallet::call_index(4)] + #[pallet::weight(1)] + pub fn foo_index_out_of_order(_origin: OriginFor) -> DispatchResult { + Ok(()) + } + // Test for DispatchResult return type #[pallet::call_index(2)] #[pallet::weight(1)] @@ -723,11 +729,25 @@ fn call_expand() { assert_eq!(call_foo.get_call_name(), "foo"); assert_eq!( pallet::Call::::get_call_names(), - &["foo", "foo_storage_layer", "foo_no_post_info", "check_for_dispatch_context"], + &[ + "foo", + "foo_storage_layer", + "foo_index_out_of_order", + "foo_no_post_info", + "check_for_dispatch_context" + ], ); assert_eq!(call_foo.get_call_index(), 0u8); - assert_eq!(pallet::Call::::get_call_indices(), &[0u8, 1u8, 2u8, 3u8]) + assert_eq!(pallet::Call::::get_call_indices(), &[0u8, 1u8, 4u8, 2u8, 3u8]) +} + +#[test] +fn call_expand_index() { + let call_foo = pallet::Call::::foo_index_out_of_order {}; + + assert_eq!(call_foo.get_call_index(), 4u8); + assert_eq!(pallet::Call::::get_call_indices(), &[0u8, 1u8, 4u8, 2u8, 3u8]) } #[test] From c00f6edeb83f61e3cda2eab2d2eedfbfeb40eb9f Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 21 Mar 2023 17:13:59 +0100 Subject: [PATCH 6/6] Doc Signed-off-by: Oliver Tale-Yazdi --- frame/support/src/traits/metadata.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 6f76910ad6c0c..20ddb1d34ca1a 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -101,15 +101,15 @@ pub struct CallMetadata { /// Gets the function name of the Call. pub trait GetCallName { - /// Return all function names in an undefined order. + /// Return all function names in the same order as [`GetCallIndex`]. fn get_call_names() -> &'static [&'static str]; /// Return the function name of the Call. fn get_call_name(&self) -> &'static str; } -/// Gets the function index of the Call +/// Gets the function index of the Call. pub trait GetCallIndex { - /// Return all call indices in an undefined order. + /// Return all call indices in the same order as [`GetCallName`]. fn get_call_indices() -> &'static [u8]; /// Return the index of this Call. fn get_call_index(&self) -> u8;