From bdc2aeb11b4d51c647f9b7b0d76770f0e73135e7 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 18 Oct 2021 19:25:09 +0200 Subject: [PATCH 01/18] Allow pallet's info to be enumerated --- frame/support/src/lib.rs | 4 +- frame/support/src/traits.rs | 2 +- frame/support/src/traits/metadata.rs | 64 +++++++++++++++++++++ frame/support/test/tests/pallet_instance.rs | 33 +++++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index f3b00c764bb35..1b93b5fb5975e 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -1476,11 +1476,11 @@ pub mod pallet_prelude { /// * [`traits::OnGenesis`]: contains some logic to write pallet version into storage. /// * `PalletErrorTypeInfo`: provides the type information for the pallet error, if defined. /// -/// It declare `type Module` type alias for `Pallet`, used by [`construct_runtime`]. +/// It declares `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`). +/// (The implementation uses the associated type `frame_system::Config::PalletInfo`). /// /// It implements [`traits::StorageInfoTrait`] on `Pallet` which give information about all /// storages. diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 5ac0208dc2033..f0736b9acda42 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -63,7 +63,7 @@ pub use randomness::Randomness; mod metadata; pub use metadata::{ CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo, - PalletInfoAccess, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, + PalletInfoAccess, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, PalletInfoData, }; mod hooks; diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index e60cf8be8a41c..7d3bbde57c94c 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -19,6 +19,7 @@ use codec::{Decode, Encode}; use sp_runtime::RuntimeDebug; +use impl_trait_for_tuples::impl_for_tuples; /// Provides information about the pallet itself and its setup in the runtime. /// @@ -35,6 +36,19 @@ pub trait PalletInfo { fn crate_version() -> Option; } +/// Information regarding an instance of a pallet. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug)] +pub struct PalletInfoData { + /// Index of the pallet as configured in the runtime. + pub index: usize, + /// Name of the pallet as configured in the runtime. + pub name: &'static str, + /// Name of the Rust module containing the pallet. + pub module_name: &'static str, + /// Version of the crate containing the pallet. + pub crate_version: CrateVersion, +} + /// Provides information about the pallet itself and its setup in the runtime. /// /// Declare some information and access the information provided by [`PalletInfo`] for a specific @@ -48,6 +62,35 @@ pub trait PalletInfoAccess { fn module_name() -> &'static str; /// Version of the crate containing the pallet. fn crate_version() -> CrateVersion; + /// Get all information at once. + fn info() -> PalletInfoData { + PalletInfoData { + index: Self::index(), + name: Self::name(), + module_name: Self::module_name(), + crate_version: Self::crate_version(), + } + } + /// All pallet information that this type represents. For tuples, this can be len more than 1. + fn infos() -> Vec { + vec![Self::info()] + } +} + +#[impl_for_tuples(60)] +impl PalletInfoAccess for Tuple { + fn index() -> usize { unreachable!("fields not available on a tuple") } + fn name() -> &'static str { unreachable!("fields not available on a tuple") } + fn module_name() -> &'static str { unreachable!("fields not available on a tuple") } + fn crate_version() -> CrateVersion { unreachable!("fields not available on a tuple") } + fn info() -> PalletInfoData { unreachable!("fields not available on a tuple") } + fn infos() -> Vec { + let mut result = vec![]; + for_tuples!( #( + result.extend(Tuple::infos()); + )* ); + result + } } /// The function and pallet name of the Call. @@ -206,6 +249,27 @@ pub trait GetStorageVersion { mod tests { use super::*; + struct Pallet1; + impl PalletInfoAccess for Pallet1 { + fn index() -> usize { 1 } + fn name() -> &'static str { "Pallat1" } + fn module_name() -> &'static str { "pallet1" } + fn crate_version() -> CrateVersion { CrateVersion::new(1, 0, 0) } + } + struct Pallet2; + impl PalletInfoAccess for Pallet2 { + fn index() -> usize { 2 } + fn name() -> &'static str { "Pallat2" } + fn module_name() -> &'static str { "pallet2" } + fn crate_version() -> CrateVersion { CrateVersion::new(1, 0, 0) } + } + + #[test] + fn check_tuples() { + use PalletInfoAccess; + assert!(<(Pallet1, Pallet2,)>::infos().len() == 2); + } + #[test] fn check_storage_version_ordering() { let version = StorageVersion::new(1); diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 34586e8414216..fece56360244a 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -505,6 +505,39 @@ fn storage_expand() { }); } +#[test] +fn pallet_metadata_expands() { + use frame_support::traits::{PalletInfoAccess, PalletInfoData, CrateVersion}; + let mut infos = AllPallets::infos(); + infos.sort_by_key(|x| x.index); + assert_eq!(infos, vec![ + PalletInfoData { + index: 1, + name: "Example", + module_name: "pallet", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + PalletInfoData { + index: 2, + name: "Instance1Example", + module_name: "pallet", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + PalletInfoData { + index: 3, + name: "Example2", + module_name: "pallet2", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + PalletInfoData { + index: 4, + name: "Instance1Example2", + module_name: "pallet2", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + ]); +} + #[test] fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { From 130bdcfc348b86dc3e0b32a2a760b3410e166975 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 18 Oct 2021 23:58:06 +0200 Subject: [PATCH 02/18] Fixes --- frame/support/src/traits/metadata.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 7d3bbde57c94c..f15fb4098f241 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -17,6 +17,7 @@ //! Traits for managing information attached to pallets and their constituents. +use sp_std::prelude::*; use codec::{Decode, Encode}; use sp_runtime::RuntimeDebug; use impl_trait_for_tuples::impl_for_tuples; From 5d838f962e3be9c8e09b813d8e9711d297219f5c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 19 Oct 2021 10:22:52 +0200 Subject: [PATCH 03/18] Formatting --- frame/support/src/traits.rs | 2 +- frame/support/src/traits/metadata.rs | 56 ++++++++++++++------ frame/support/test/tests/pallet_instance.rs | 57 +++++++++++---------- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index f0736b9acda42..68a8c21c5f58b 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -63,7 +63,7 @@ pub use randomness::Randomness; mod metadata; pub use metadata::{ CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo, - PalletInfoAccess, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, PalletInfoData, + PalletInfoAccess, PalletInfoData, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, }; mod hooks; diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index f15fb4098f241..1bc0ea3200282 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -17,10 +17,10 @@ //! Traits for managing information attached to pallets and their constituents. -use sp_std::prelude::*; use codec::{Decode, Encode}; -use sp_runtime::RuntimeDebug; use impl_trait_for_tuples::impl_for_tuples; +use sp_runtime::RuntimeDebug; +use sp_std::prelude::*; /// Provides information about the pallet itself and its setup in the runtime. /// @@ -80,11 +80,21 @@ pub trait PalletInfoAccess { #[impl_for_tuples(60)] impl PalletInfoAccess for Tuple { - fn index() -> usize { unreachable!("fields not available on a tuple") } - fn name() -> &'static str { unreachable!("fields not available on a tuple") } - fn module_name() -> &'static str { unreachable!("fields not available on a tuple") } - fn crate_version() -> CrateVersion { unreachable!("fields not available on a tuple") } - fn info() -> PalletInfoData { unreachable!("fields not available on a tuple") } + fn index() -> usize { + unreachable!("fields not available on a tuple") + } + fn name() -> &'static str { + unreachable!("fields not available on a tuple") + } + fn module_name() -> &'static str { + unreachable!("fields not available on a tuple") + } + fn crate_version() -> CrateVersion { + unreachable!("fields not available on a tuple") + } + fn info() -> PalletInfoData { + unreachable!("fields not available on a tuple") + } fn infos() -> Vec { let mut result = vec![]; for_tuples!( #( @@ -252,17 +262,33 @@ mod tests { struct Pallet1; impl PalletInfoAccess for Pallet1 { - fn index() -> usize { 1 } - fn name() -> &'static str { "Pallat1" } - fn module_name() -> &'static str { "pallet1" } - fn crate_version() -> CrateVersion { CrateVersion::new(1, 0, 0) } + fn index() -> usize { + 1 + } + fn name() -> &'static str { + "Pallat1" + } + fn module_name() -> &'static str { + "pallet1" + } + fn crate_version() -> CrateVersion { + CrateVersion::new(1, 0, 0) + } } struct Pallet2; impl PalletInfoAccess for Pallet2 { - fn index() -> usize { 2 } - fn name() -> &'static str { "Pallat2" } - fn module_name() -> &'static str { "pallet2" } - fn crate_version() -> CrateVersion { CrateVersion::new(1, 0, 0) } + fn index() -> usize { + 2 + } + fn name() -> &'static str { + "Pallat2" + } + fn module_name() -> &'static str { + "pallet2" + } + fn crate_version() -> CrateVersion { + CrateVersion::new(1, 0, 0) + } } #[test] diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index fece56360244a..72fcdfe2b6108 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -507,35 +507,38 @@ fn storage_expand() { #[test] fn pallet_metadata_expands() { - use frame_support::traits::{PalletInfoAccess, PalletInfoData, CrateVersion}; + use frame_support::traits::{CrateVersion, PalletInfoAccess, PalletInfoData}; let mut infos = AllPallets::infos(); infos.sort_by_key(|x| x.index); - assert_eq!(infos, vec![ - PalletInfoData { - index: 1, - name: "Example", - module_name: "pallet", - crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, - }, - PalletInfoData { - index: 2, - name: "Instance1Example", - module_name: "pallet", - crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, - }, - PalletInfoData { - index: 3, - name: "Example2", - module_name: "pallet2", - crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, - }, - PalletInfoData { - index: 4, - name: "Instance1Example2", - module_name: "pallet2", - crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, - }, - ]); + assert_eq!( + infos, + vec![ + PalletInfoData { + index: 1, + name: "Example", + module_name: "pallet", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + PalletInfoData { + index: 2, + name: "Instance1Example", + module_name: "pallet", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + PalletInfoData { + index: 3, + name: "Example2", + module_name: "pallet2", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + PalletInfoData { + index: 4, + name: "Instance1Example2", + module_name: "pallet2", + crate_version: CrateVersion { major: 3, minor: 0, patch: 0 }, + }, + ] + ); } #[test] From f92db2d4806a329217a842207f216e9b1343edfb Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 19 Oct 2021 10:45:02 +0200 Subject: [PATCH 04/18] Flat tuple for getting all pallet instances --- bin/node-template/runtime/src/lib.rs | 4 +- bin/node/runtime/src/lib.rs | 4 +- frame/executive/README.md | 11 +++- frame/executive/src/lib.rs | 53 +++++++++++-------- .../src/construct_runtime/expand/config.rs | 2 +- .../procedural/src/construct_runtime/mod.rs | 17 +++++- frame/support/src/migrations.rs | 6 +-- frame/support/src/traits/hooks.rs | 12 ++--- frame/support/src/traits/misc.rs | 2 +- frame/support/src/traits/storage.rs | 2 +- frame/support/test/tests/pallet.rs | 8 +-- frame/support/test/tests/pallet_instance.rs | 8 +-- 12 files changed, 79 insertions(+), 50 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 4b49cb48ef352..aa9c20808d879 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -323,7 +323,7 @@ pub type Executive = frame_executive::Executive< Block, frame_system::ChainContext, Runtime, - AllPallets, + PalletInstances, >; impl_runtime_apis! { @@ -474,7 +474,7 @@ impl_runtime_apis! { list_benchmark!(list, extra, pallet_timestamp, Timestamp); list_benchmark!(list, extra, pallet_template, TemplateModule); - let storage_info = AllPalletsWithSystem::storage_info(); + let storage_info = PalletInstancesWithSystem::storage_info(); return (list, storage_info) } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index c7920629bf356..b6e08ac8c9c62 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1327,7 +1327,7 @@ pub type Executive = frame_executive::Executive< Block, frame_system::ChainContext, Runtime, - AllPallets, + PalletInstances, (), >; @@ -1653,7 +1653,7 @@ impl_runtime_apis! { list_benchmark!(list, extra, pallet_utility, Utility); list_benchmark!(list, extra, pallet_vesting, Vesting); - let storage_info = AllPalletsWithSystem::storage_info(); + let storage_info = PalletInstancesWithSystem::storage_info(); return (list, storage_info) } diff --git a/frame/executive/README.md b/frame/executive/README.md index ae3bbf1a9d994..c64fde61aeb98 100644 --- a/frame/executive/README.md +++ b/frame/executive/README.md @@ -35,7 +35,7 @@ The default Substrate node template declares the [`Executive`](https://docs.rs/f ```rust # /// Executive: handles dispatch to the various modules. -pub type Executive = executive::Executive; +pub type Executive = executive::Executive; ``` ### Custom `OnRuntimeUpgrade` logic @@ -54,7 +54,14 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { } } -pub type Executive = executive::Executive; +pub type Executive = executive::Executive< + Runtime, + Block, + Context, + Runtime, + PalletInstances, + CustomOnRuntimeUpgrade, +>; ``` License: Apache-2.0 diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index b1bdf357ec07d..e3e794ac129cc 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -59,7 +59,7 @@ //! # type Context = frame_system::ChainContext; //! # pub type Block = generic::Block; //! # pub type Balances = u64; -//! # pub type AllPallets = u64; +//! # pub type PalletInstances = u64; //! # pub enum Runtime {}; //! # use sp_runtime::transaction_validity::{ //! # TransactionValidity, UnknownTransaction, TransactionSource, @@ -73,7 +73,7 @@ //! # } //! # } //! /// Executive: handles dispatch to the various modules. -//! pub type Executive = executive::Executive; +//! pub type Executive = executive::Executive; //! ``` //! //! ### Custom `OnRuntimeUpgrade` logic @@ -90,7 +90,7 @@ //! # type Context = frame_system::ChainContext; //! # pub type Block = generic::Block; //! # pub type Balances = u64; -//! # pub type AllPallets = u64; +//! # pub type PalletInstances = u64; //! # pub enum Runtime {}; //! # use sp_runtime::transaction_validity::{ //! # TransactionValidity, UnknownTransaction, TransactionSource, @@ -111,7 +111,14 @@ //! } //! } //! -//! pub type Executive = executive::Executive; +//! pub type Executive = executive::Executive< +//! Runtime, +//! Block, +//! Context, +//! Runtime, +//! PalletInstances, +//! CustomOnRuntimeUpgrade, +//! >; //! ``` #![cfg_attr(not(feature = "std"), no_std)] @@ -148,11 +155,11 @@ pub type OriginOf = as Dispatchable>::Origin; /// - `Block`: The block type of the runtime /// - `Context`: The context that is used when checking an extrinsic. /// - `UnsignedValidator`: The unsigned transaction validator of the runtime. -/// - `AllPallets`: Tuple that contains all modules. Will be used to call e.g. `on_initialize`. +/// - `PalletInstances`: Tuple that contains all modules. Will be used to call e.g. `on_initialize`. /// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are -/// already called by `AllPallets`. It will be called before all modules will be called. -pub struct Executive( - PhantomData<(System, Block, Context, UnsignedValidator, AllPallets, OnRuntimeUpgrade)>, +/// already called by `PalletInstances`. It will be called before all modules will be called. +pub struct Executive( + PhantomData<(System, Block, Context, UnsignedValidator, PalletInstances, OnRuntimeUpgrade)>, ); impl< @@ -160,14 +167,14 @@ impl< Block: traits::Block
, Context: Default, UnsignedValidator, - AllPallets: OnRuntimeUpgrade + PalletInstances: OnRuntimeUpgrade + OnInitialize + OnIdle + OnFinalize + OffchainWorker, COnRuntimeUpgrade: OnRuntimeUpgrade, > ExecuteBlock - for Executive + for Executive where Block::Extrinsic: Checkable + Codec, CheckedOf: Applyable + GetDispatchInfo, @@ -182,7 +189,7 @@ where Block, Context, UnsignedValidator, - AllPallets, + PalletInstances, COnRuntimeUpgrade, >::execute_block(block); } @@ -193,13 +200,13 @@ impl< Block: traits::Block
, Context: Default, UnsignedValidator, - AllPallets: OnRuntimeUpgrade + PalletInstances: OnRuntimeUpgrade + OnInitialize + OnIdle + OnFinalize + OffchainWorker, COnRuntimeUpgrade: OnRuntimeUpgrade, - > Executive + > Executive where Block::Extrinsic: Checkable + Codec, CheckedOf: Applyable + GetDispatchInfo, @@ -215,7 +222,7 @@ where weight = weight.saturating_add( as OnRuntimeUpgrade>::on_runtime_upgrade(), ); - weight = weight.saturating_add(::on_runtime_upgrade()); + weight = weight.saturating_add(::on_runtime_upgrade()); weight } @@ -257,7 +264,7 @@ where #[cfg(feature = "try-runtime")] pub fn try_runtime_upgrade() -> Result { < - (frame_system::Pallet::, COnRuntimeUpgrade, AllPallets) + (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstances) as OnRuntimeUpgrade >::pre_upgrade().unwrap(); @@ -265,7 +272,7 @@ where let weight = Self::execute_on_runtime_upgrade(); < - (frame_system::Pallet::, COnRuntimeUpgrade, AllPallets) + (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstances) as OnRuntimeUpgrade >::post_upgrade().unwrap(); @@ -310,7 +317,7 @@ where System::BlockNumber, >>::on_initialize(*block_number)); weight = weight.saturating_add( - >::on_initialize(*block_number), + >::on_initialize(*block_number), ); weight = weight.saturating_add( >::get().base_block, @@ -425,7 +432,7 @@ where remaining_weight, ); remaining_weight = remaining_weight.saturating_sub(used_weight); - used_weight = >::on_idle( + used_weight = >::on_idle( block_number, remaining_weight, ) @@ -439,7 +446,7 @@ where as OnFinalize>::on_finalize( block_number, ); - >::on_finalize(block_number); + >::on_finalize(block_number); } /// Apply extrinsic outside of the block execution function. @@ -568,7 +575,7 @@ where // as well. frame_system::BlockHash::::insert(header.number(), header.hash()); - >::offchain_worker(*header.number()) + >::offchain_worker(*header.number()) } } @@ -850,7 +857,7 @@ mod tests { Block, ChainContext, Runtime, - AllPallets, + PalletInstances, CustomOnRuntimeUpgrade, >; @@ -1363,11 +1370,11 @@ mod tests { // All weights that show up in the `initialize_block_impl` let frame_system_upgrade_weight = frame_system::Pallet::::on_runtime_upgrade(); let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade(); - let runtime_upgrade_weight = ::on_runtime_upgrade(); + let runtime_upgrade_weight = ::on_runtime_upgrade(); let frame_system_on_initialize_weight = frame_system::Pallet::::on_initialize(block_number); let on_initialize_weight = - >::on_initialize(block_number); + >::on_initialize(block_number); let base_block_weight = ::BlockWeights::get().base_block; diff --git a/frame/support/procedural/src/construct_runtime/expand/config.rs b/frame/support/procedural/src/construct_runtime/expand/config.rs index 5e1b9d94700e6..b290fe292cb8e 100644 --- a/frame/support/procedural/src/construct_runtime/expand/config.rs +++ b/frame/support/procedural/src/construct_runtime/expand/config.rs @@ -82,7 +82,7 @@ pub fn expand_outer_config( #build_storage_calls #scrate::BasicExternalities::execute_with_storage(storage, || { - ::on_genesis(); + ::on_genesis(); }); Ok(()) diff --git a/frame/support/procedural/src/construct_runtime/mod.rs b/frame/support/procedural/src/construct_runtime/mod.rs index 04bb2ead645d2..a1afa31d86154 100644 --- a/frame/support/procedural/src/construct_runtime/mod.rs +++ b/frame/support/procedural/src/construct_runtime/mod.rs @@ -215,12 +215,27 @@ fn decl_all_pallets<'a>( .iter() .fold(TokenStream2::default(), |combined, name| quote!((#name, #combined))); + let flat_pallets_with_system = quote!( (#( #names ),*) ); + let names_without_system = names + .iter() + .filter(|n| **n != SYSTEM_PALLET_NAME); + let flat_pallets = quote!( (#( #names_without_system ),*) ); + quote!( #types + + /// All pallets included in the runtime as a flat tuple of types. + /// Excludes the System pallet. + pub type PalletInstances = ( #flat_pallets ); + /// All pallets included in the runtime as a flat tuple of types. + pub type PalletInstancesWithSystem = ( #flat_pallets_with_system ); + /// All pallets included in the runtime as a nested tuple of types. /// Excludes the System pallet. + #[deprecated(note = "use `PalletInstances` instead")] pub type AllPallets = ( #all_pallets ); /// All pallets included in the runtime as a nested tuple of types. + #[deprecated(note = "use `PalletInstancesWithSystem` instead")] pub type AllPalletsWithSystem = ( #all_pallets_with_system ); /// All modules included in the runtime as a nested tuple of types. @@ -318,7 +333,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 { #[test] pub fn runtime_integrity_tests() { - ::integrity_test(); + ::integrity_test(); } } ) diff --git a/frame/support/src/migrations.rs b/frame/support/src/migrations.rs index dc3402440fdd4..216464829818a 100644 --- a/frame/support/src/migrations.rs +++ b/frame/support/src/migrations.rs @@ -42,7 +42,7 @@ impl PalletVersionToStorageVersionHelpe } } -#[impl_trait_for_tuples::impl_for_tuples(30)] +#[impl_trait_for_tuples::impl_for_tuples(60)] impl PalletVersionToStorageVersionHelper for T { fn migrate(db_weight: &RuntimeDbWeight) -> Weight { let mut weight: Weight = 0; @@ -58,9 +58,9 @@ impl PalletVersionToStorageVersionHelper for T { /// /// This will remove all `PalletVersion's` from the state and insert the current storage version. pub fn migrate_from_pallet_version_to_storage_version< - AllPallets: PalletVersionToStorageVersionHelper, + PalletInstances: PalletVersionToStorageVersionHelper, >( db_weight: &RuntimeDbWeight, ) -> Weight { - AllPallets::migrate(db_weight) + PalletInstances::migrate(db_weight) } diff --git a/frame/support/src/traits/hooks.rs b/frame/support/src/traits/hooks.rs index 2a8b0a156247a..726274727b5ce 100644 --- a/frame/support/src/traits/hooks.rs +++ b/frame/support/src/traits/hooks.rs @@ -38,7 +38,7 @@ pub trait OnInitialize { } } -#[impl_for_tuples(30)] +#[impl_for_tuples(60)] impl OnInitialize for Tuple { fn on_initialize(n: BlockNumber) -> crate::weights::Weight { let mut weight = 0; @@ -50,7 +50,7 @@ impl OnInitialize for Tuple { /// The block finalization trait. /// /// Implementing this lets you express what should happen for your pallet when the block is ending. -#[impl_for_tuples(30)] +#[impl_for_tuples(60)] pub trait OnFinalize { /// The block is being finalized. Implement to have something happen. /// @@ -79,7 +79,7 @@ pub trait OnIdle { } } -#[impl_for_tuples(30)] +#[impl_for_tuples(60)] impl OnIdle for Tuple { fn on_idle(n: BlockNumber, remaining_weight: crate::weights::Weight) -> crate::weights::Weight { let on_idle_functions: &[fn( @@ -105,7 +105,7 @@ impl OnIdle for Tuple { /// Implementing this trait for a pallet let's you express operations that should /// happen at genesis. It will be called in an externalities provided environment and /// will see the genesis state after all pallets have written their genesis state. -#[impl_for_tuples(30)] +#[impl_for_tuples(60)] pub trait OnGenesis { /// Something that should happen at genesis. fn on_genesis() {} @@ -187,7 +187,7 @@ pub trait OnRuntimeUpgrade { } } -#[impl_for_tuples(30)] +#[impl_for_tuples(60)] impl OnRuntimeUpgrade for Tuple { fn on_runtime_upgrade() -> crate::weights::Weight { let mut weight = 0; @@ -316,7 +316,7 @@ pub trait GenesisBuild: Default + sp_runtime::traits::MaybeSerializeD } /// A trait which is called when the timestamp is set in the runtime. -#[impl_for_tuples(30)] +#[impl_for_tuples(60)] pub trait OnTimestampSet { /// Called when the timestamp is set. fn on_timestamp_set(moment: Moment); diff --git a/frame/support/src/traits/misc.rs b/frame/support/src/traits/misc.rs index 9109bfeeae722..e429ee39112b8 100644 --- a/frame/support/src/traits/misc.rs +++ b/frame/support/src/traits/misc.rs @@ -299,7 +299,7 @@ pub trait ExecuteBlock { /// but cannot preform any alterations. More specifically alterations are /// not forbidden, but they are not persisted in any way after the worker /// has finished. -#[impl_trait_for_tuples::impl_for_tuples(30)] +#[impl_trait_for_tuples::impl_for_tuples(60)] pub trait OffchainWorker { /// This function is being called after every block import (when fully synced). /// diff --git a/frame/support/src/traits/storage.rs b/frame/support/src/traits/storage.rs index 9a88a3ed44046..e990278c237e4 100644 --- a/frame/support/src/traits/storage.rs +++ b/frame/support/src/traits/storage.rs @@ -71,7 +71,7 @@ pub trait StorageInfoTrait { fn storage_info() -> Vec; } -#[impl_trait_for_tuples::impl_for_tuples(30)] +#[impl_trait_for_tuples::impl_for_tuples(60)] impl StorageInfoTrait for Tuple { fn storage_info() -> Vec { let mut res = vec![]; diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index dc72be3ebdd49..3e4934f49f0ff 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -939,10 +939,10 @@ fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); - assert_eq!(AllPallets::on_initialize(1), 10); - AllPallets::on_finalize(1); + assert_eq!(PalletInstances::on_initialize(1), 10); + PalletInstances::on_finalize(1); - assert_eq!(AllPallets::on_runtime_upgrade(), 30); + assert_eq!(PalletInstances::on_runtime_upgrade(), 30); assert_eq!( frame_system::Pallet::::events()[0].event, @@ -992,7 +992,7 @@ fn migrate_from_pallet_version_to_storage_version() { let db_weight = RuntimeDbWeight { read: 0, write: 5 }; let weight = frame_support::migrations::migrate_from_pallet_version_to_storage_version::< - AllPalletsWithSystem, + PalletInstancesWithSystem, >(&db_weight); // 3 pallets, 2 writes and every write costs 5 weight. diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 72fcdfe2b6108..dfa1da7ecec47 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -508,7 +508,7 @@ fn storage_expand() { #[test] fn pallet_metadata_expands() { use frame_support::traits::{CrateVersion, PalletInfoAccess, PalletInfoData}; - let mut infos = AllPallets::infos(); + let mut infos = PalletInstancesWithSystem::infos(); infos.sort_by_key(|x| x.index); assert_eq!( infos, @@ -546,10 +546,10 @@ fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); - assert_eq!(AllPallets::on_initialize(1), 21); - AllPallets::on_finalize(1); + assert_eq!(PalletInstances::on_initialize(1), 21); + PalletInstances::on_finalize(1); - assert_eq!(AllPallets::on_runtime_upgrade(), 61); + assert_eq!(PalletInstances::on_runtime_upgrade(), 61); // The order is indeed reversed due to https://github.com/paritytech/substrate/issues/6280 assert_eq!( From 9dcb776f1682f0ee1ffffdd38e00e97adfbbe2f2 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 19 Oct 2021 13:43:47 +0200 Subject: [PATCH 05/18] Renaming and fixing reversedness --- bin/node-template/runtime/src/lib.rs | 4 +- bin/node/runtime/src/lib.rs | 4 +- frame/executive/README.md | 10 +++- frame/executive/src/lib.rs | 48 ++++++++++--------- .../src/construct_runtime/expand/config.rs | 2 +- .../procedural/src/construct_runtime/mod.rs | 29 ++++++----- .../src/pallet/expand/pallet_struct.rs | 15 ++++++ frame/support/src/dispatch.rs | 14 ++++++ frame/support/src/migrations.rs | 4 +- frame/support/src/traits.rs | 1 + frame/support/src/traits/metadata.rs | 48 +++++-------------- frame/support/test/tests/pallet.rs | 8 ++-- frame/support/test/tests/pallet_instance.rs | 8 ++-- 13 files changed, 108 insertions(+), 87 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index aa9c20808d879..8ee169d7231e2 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -323,7 +323,7 @@ pub type Executive = frame_executive::Executive< Block, frame_system::ChainContext, Runtime, - PalletInstances, + PalletInstancesRevExSystem, >; impl_runtime_apis! { @@ -474,7 +474,7 @@ impl_runtime_apis! { list_benchmark!(list, extra, pallet_timestamp, Timestamp); list_benchmark!(list, extra, pallet_template, TemplateModule); - let storage_info = PalletInstancesWithSystem::storage_info(); + let storage_info = PalletInstances::storage_info(); return (list, storage_info) } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b6e08ac8c9c62..edc8754e12917 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1327,7 +1327,7 @@ pub type Executive = frame_executive::Executive< Block, frame_system::ChainContext, Runtime, - PalletInstances, + PalletInstancesRevExSystem, (), >; @@ -1653,7 +1653,7 @@ impl_runtime_apis! { list_benchmark!(list, extra, pallet_utility, Utility); list_benchmark!(list, extra, pallet_vesting, Vesting); - let storage_info = PalletInstancesWithSystem::storage_info(); + let storage_info = PalletInstances::storage_info(); return (list, storage_info) } diff --git a/frame/executive/README.md b/frame/executive/README.md index c64fde61aeb98..cc75617defe8a 100644 --- a/frame/executive/README.md +++ b/frame/executive/README.md @@ -35,7 +35,13 @@ The default Substrate node template declares the [`Executive`](https://docs.rs/f ```rust # /// Executive: handles dispatch to the various modules. -pub type Executive = executive::Executive; +pub type Executive = executive::Executive< + Runtime, + Block, + Context, + Runtime, + PalletInstancesRevExSystem, +>; ``` ### Custom `OnRuntimeUpgrade` logic @@ -59,7 +65,7 @@ pub type Executive = executive::Executive< Block, Context, Runtime, - PalletInstances, + PalletInstancesRevExSystem, CustomOnRuntimeUpgrade, >; ``` diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index e3e794ac129cc..f0f3855e75cc0 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -59,7 +59,7 @@ //! # type Context = frame_system::ChainContext; //! # pub type Block = generic::Block; //! # pub type Balances = u64; -//! # pub type PalletInstances = u64; +//! # pub type Pallets = u64; //! # pub enum Runtime {}; //! # use sp_runtime::transaction_validity::{ //! # TransactionValidity, UnknownTransaction, TransactionSource, @@ -73,7 +73,7 @@ //! # } //! # } //! /// Executive: handles dispatch to the various modules. -//! pub type Executive = executive::Executive; +//! pub type Executive = executive::Executive; //! ``` //! //! ### Custom `OnRuntimeUpgrade` logic @@ -90,7 +90,7 @@ //! # type Context = frame_system::ChainContext; //! # pub type Block = generic::Block; //! # pub type Balances = u64; -//! # pub type PalletInstances = u64; +//! # pub type Pallets = u64; //! # pub enum Runtime {}; //! # use sp_runtime::transaction_validity::{ //! # TransactionValidity, UnknownTransaction, TransactionSource, @@ -116,7 +116,7 @@ //! Block, //! Context, //! Runtime, -//! PalletInstances, +//! Pallets, //! CustomOnRuntimeUpgrade, //! >; //! ``` @@ -155,11 +155,13 @@ pub type OriginOf = as Dispatchable>::Origin; /// - `Block`: The block type of the runtime /// - `Context`: The context that is used when checking an extrinsic. /// - `UnsignedValidator`: The unsigned transaction validator of the runtime. -/// - `PalletInstances`: Tuple that contains all modules. Will be used to call e.g. `on_initialize`. +/// - `PalletInstanceOrder`: Tuple that contains all modules in order used to call e.g. +/// `on_initialize`. Generally this should be the `PalletInstancesRevExSystem` type coming from +/// `construct_runtime` macro. This should exclude the System pallet. /// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are -/// already called by `PalletInstances`. It will be called before all modules will be called. -pub struct Executive( - PhantomData<(System, Block, Context, UnsignedValidator, PalletInstances, OnRuntimeUpgrade)>, +/// already called by `PalletInstanceOrder`. It will be called before all modules will be called. +pub struct Executive( + PhantomData<(System, Block, Context, UnsignedValidator, PalletInstanceOrder, OnRuntimeUpgrade)>, ); impl< @@ -167,14 +169,14 @@ impl< Block: traits::Block
, Context: Default, UnsignedValidator, - PalletInstances: OnRuntimeUpgrade + PalletInstanceOrder: OnRuntimeUpgrade + OnInitialize + OnIdle + OnFinalize + OffchainWorker, COnRuntimeUpgrade: OnRuntimeUpgrade, > ExecuteBlock - for Executive + for Executive where Block::Extrinsic: Checkable + Codec, CheckedOf: Applyable + GetDispatchInfo, @@ -189,7 +191,7 @@ where Block, Context, UnsignedValidator, - PalletInstances, + PalletInstanceOrder, COnRuntimeUpgrade, >::execute_block(block); } @@ -200,13 +202,13 @@ impl< Block: traits::Block
, Context: Default, UnsignedValidator, - PalletInstances: OnRuntimeUpgrade + PalletInstanceOrder: OnRuntimeUpgrade + OnInitialize + OnIdle + OnFinalize + OffchainWorker, COnRuntimeUpgrade: OnRuntimeUpgrade, - > Executive + > Executive where Block::Extrinsic: Checkable + Codec, CheckedOf: Applyable + GetDispatchInfo, @@ -222,7 +224,7 @@ where weight = weight.saturating_add( as OnRuntimeUpgrade>::on_runtime_upgrade(), ); - weight = weight.saturating_add(::on_runtime_upgrade()); + weight = weight.saturating_add(::on_runtime_upgrade()); weight } @@ -264,7 +266,7 @@ where #[cfg(feature = "try-runtime")] pub fn try_runtime_upgrade() -> Result { < - (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstances) + (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstanceOrder) as OnRuntimeUpgrade >::pre_upgrade().unwrap(); @@ -272,7 +274,7 @@ where let weight = Self::execute_on_runtime_upgrade(); < - (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstances) + (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstanceOrder) as OnRuntimeUpgrade >::post_upgrade().unwrap(); @@ -317,7 +319,7 @@ where System::BlockNumber, >>::on_initialize(*block_number)); weight = weight.saturating_add( - >::on_initialize(*block_number), + >::on_initialize(*block_number), ); weight = weight.saturating_add( >::get().base_block, @@ -432,7 +434,7 @@ where remaining_weight, ); remaining_weight = remaining_weight.saturating_sub(used_weight); - used_weight = >::on_idle( + used_weight = >::on_idle( block_number, remaining_weight, ) @@ -446,7 +448,7 @@ where as OnFinalize>::on_finalize( block_number, ); - >::on_finalize(block_number); + >::on_finalize(block_number); } /// Apply extrinsic outside of the block execution function. @@ -575,7 +577,7 @@ where // as well. frame_system::BlockHash::::insert(header.number(), header.hash()); - >::offchain_worker(*header.number()) + >::offchain_worker(*header.number()) } } @@ -857,7 +859,7 @@ mod tests { Block, ChainContext, Runtime, - PalletInstances, + PalletInstancesRevExSystem, CustomOnRuntimeUpgrade, >; @@ -1370,11 +1372,11 @@ mod tests { // All weights that show up in the `initialize_block_impl` let frame_system_upgrade_weight = frame_system::Pallet::::on_runtime_upgrade(); let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade(); - let runtime_upgrade_weight = ::on_runtime_upgrade(); + let runtime_upgrade_weight = ::on_runtime_upgrade(); let frame_system_on_initialize_weight = frame_system::Pallet::::on_initialize(block_number); let on_initialize_weight = - >::on_initialize(block_number); + >::on_initialize(block_number); let base_block_weight = ::BlockWeights::get().base_block; diff --git a/frame/support/procedural/src/construct_runtime/expand/config.rs b/frame/support/procedural/src/construct_runtime/expand/config.rs index b290fe292cb8e..8f98ba876aa2f 100644 --- a/frame/support/procedural/src/construct_runtime/expand/config.rs +++ b/frame/support/procedural/src/construct_runtime/expand/config.rs @@ -82,7 +82,7 @@ pub fn expand_outer_config( #build_storage_calls #scrate::BasicExternalities::execute_with_storage(storage, || { - ::on_genesis(); + ::on_genesis(); }); Ok(()) diff --git a/frame/support/procedural/src/construct_runtime/mod.rs b/frame/support/procedural/src/construct_runtime/mod.rs index a1afa31d86154..7e327bec89f3c 100644 --- a/frame/support/procedural/src/construct_runtime/mod.rs +++ b/frame/support/procedural/src/construct_runtime/mod.rs @@ -215,27 +215,34 @@ fn decl_all_pallets<'a>( .iter() .fold(TokenStream2::default(), |combined, name| quote!((#name, #combined))); - let flat_pallets_with_system = quote!( (#( #names ),*) ); - let names_without_system = names - .iter() - .filter(|n| **n != SYSTEM_PALLET_NAME); - let flat_pallets = quote!( (#( #names_without_system ),*) ); + let pallet_instances = quote!( (#( #names ),*) ); + let names_rev = names.iter().rev(); + let pallet_instances_rev = quote!( (#( #names_rev ),*) ); + let names_ex_sys = names.iter().filter(|n| **n != SYSTEM_PALLET_NAME); + let pallet_instances_ex_sys = quote!( (#( #names_ex_sys ),*) ); + let names_rev_ex_sys = names.iter().rev().filter(|n| **n != SYSTEM_PALLET_NAME); + let pallet_instances_rev_ex_sys = quote!( (#( #names_rev_ex_sys ),*) ); quote!( #types /// All pallets included in the runtime as a flat tuple of types. - /// Excludes the System pallet. - pub type PalletInstances = ( #flat_pallets ); + pub type PalletInstances = ( #pallet_instances ); + + // TODO: Remove these once executive is not sensitive to ordering. + /// All pallet instances excluding System which are in the runtime as a flat tuple of types. + pub type PalletInstancesExSystem = ( #pallet_instances_ex_sys ); + /// All pallet instances excluding System which are in the runtime as a flat tuple of types. + pub type PalletInstancesRev = ( #pallet_instances_rev ); /// All pallets included in the runtime as a flat tuple of types. - pub type PalletInstancesWithSystem = ( #flat_pallets_with_system ); + pub type PalletInstancesRevExSystem = ( #pallet_instances_rev_ex_sys ); /// All pallets included in the runtime as a nested tuple of types. /// Excludes the System pallet. - #[deprecated(note = "use `PalletInstances` instead")] + #[deprecated(note = "use `PalletInstancesRev` (or possibly `PalletInstances`) instead")] pub type AllPallets = ( #all_pallets ); /// All pallets included in the runtime as a nested tuple of types. - #[deprecated(note = "use `PalletInstancesWithSystem` instead")] + #[deprecated(note = "use `PalletInstancesRevWithSystem` (or possibly `PalletInstancesWithSystem`) instead")] pub type AllPalletsWithSystem = ( #all_pallets_with_system ); /// All modules included in the runtime as a nested tuple of types. @@ -333,7 +340,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 { #[test] pub fn runtime_integrity_tests() { - ::integrity_test(); + ::integrity_test(); } } ) diff --git a/frame/support/procedural/src/pallet/expand/pallet_struct.rs b/frame/support/procedural/src/pallet/expand/pallet_struct.rs index 57e814b6b8438..4e58cf56aaaf2 100644 --- a/frame/support/procedural/src/pallet/expand/pallet_struct.rs +++ b/frame/support/procedural/src/pallet/expand/pallet_struct.rs @@ -233,6 +233,21 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { } } + impl<#type_impl_gen> #frame_support::traits::PalletsInfoAccess + for #pallet_ident<#type_use_gen> + #config_where_clause + { + fn info() -> Option<#frame_support::traits::PalletInfoData> { + use #frame_support::traits::PalletInfoAccess; + Some(#frame_support::traits::PalletInfoData { + index: Self::index(), + name: Self::name(), + module_name: Self::module_name(), + crate_version: Self::crate_version(), + }) + } + } + #storage_info ) } diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 6dc7fb8a94cae..f45ea0f89607a 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2165,6 +2165,20 @@ macro_rules! decl_module { } } + impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::traits::PalletsInfoAccess + for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* + { + fn info() -> Option<$crate::traits::PalletInfoData> { + use $crate::traits::PalletInfoAccess; + Some($crate::traits::PalletInfoData { + index: Self::index(), + name: Self::name(), + module_name: Self::module_name(), + crate_version: Self::crate_version(), + }) + } + } + // Implement GetCallName for the Call. impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::dispatch::GetCallName for $call_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* diff --git a/frame/support/src/migrations.rs b/frame/support/src/migrations.rs index 216464829818a..18966376403c7 100644 --- a/frame/support/src/migrations.rs +++ b/frame/support/src/migrations.rs @@ -58,9 +58,9 @@ impl PalletVersionToStorageVersionHelper for T { /// /// This will remove all `PalletVersion's` from the state and insert the current storage version. pub fn migrate_from_pallet_version_to_storage_version< - PalletInstances: PalletVersionToStorageVersionHelper, + Pallets: PalletVersionToStorageVersionHelper, >( db_weight: &RuntimeDbWeight, ) -> Weight { - PalletInstances::migrate(db_weight) + Pallets::migrate(db_weight) } diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 68a8c21c5f58b..6b9a176476820 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -64,6 +64,7 @@ mod metadata; pub use metadata::{ CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo, PalletInfoAccess, PalletInfoData, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, + PalletsInfoAccess, }; mod hooks; diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 1bc0ea3200282..f83d02078b2d5 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -63,42 +63,24 @@ pub trait PalletInfoAccess { fn module_name() -> &'static str; /// Version of the crate containing the pallet. fn crate_version() -> CrateVersion; - /// Get all information at once. - fn info() -> PalletInfoData { - PalletInfoData { - index: Self::index(), - name: Self::name(), - module_name: Self::module_name(), - crate_version: Self::crate_version(), - } - } - /// All pallet information that this type represents. For tuples, this can be len more than 1. - fn infos() -> Vec { - vec![Self::info()] - } +} + +/// Provide information about a bunch of pallets. +pub trait PalletsInfoAccess { + /// Get information about a single pallet. Will give a result of `None` if called on a tuple. + fn info() -> Option { None } + /// All of the pallets' information that this type represents. Relevant for tuples. + fn infos() -> Vec { Self::info().into_iter().collect() } } #[impl_for_tuples(60)] -impl PalletInfoAccess for Tuple { - fn index() -> usize { - unreachable!("fields not available on a tuple") - } - fn name() -> &'static str { - unreachable!("fields not available on a tuple") - } - fn module_name() -> &'static str { - unreachable!("fields not available on a tuple") - } - fn crate_version() -> CrateVersion { - unreachable!("fields not available on a tuple") - } - fn info() -> PalletInfoData { - unreachable!("fields not available on a tuple") - } +impl PalletsInfoAccess for Tuple { fn infos() -> Vec { let mut result = vec![]; for_tuples!( #( - result.extend(Tuple::infos()); + if let Some(info) = Tuple::info() { + result.push(info); + } )* ); result } @@ -291,12 +273,6 @@ mod tests { } } - #[test] - fn check_tuples() { - use PalletInfoAccess; - assert!(<(Pallet1, Pallet2,)>::infos().len() == 2); - } - #[test] fn check_storage_version_ordering() { let version = StorageVersion::new(1); diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 3e4934f49f0ff..36fc811e7a9cc 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -939,10 +939,10 @@ fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); - assert_eq!(PalletInstances::on_initialize(1), 10); - PalletInstances::on_finalize(1); + assert_eq!(PalletInstancesRevExSystem::on_initialize(1), 10); + PalletInstancesRevExSystem::on_finalize(1); - assert_eq!(PalletInstances::on_runtime_upgrade(), 30); + assert_eq!(PalletInstancesRevExSystem::on_runtime_upgrade(), 30); assert_eq!( frame_system::Pallet::::events()[0].event, @@ -992,7 +992,7 @@ fn migrate_from_pallet_version_to_storage_version() { let db_weight = RuntimeDbWeight { read: 0, write: 5 }; let weight = frame_support::migrations::migrate_from_pallet_version_to_storage_version::< - PalletInstancesWithSystem, + PalletInstancesRev, >(&db_weight); // 3 pallets, 2 writes and every write costs 5 weight. diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index dfa1da7ecec47..78a633965e5cf 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -508,7 +508,7 @@ fn storage_expand() { #[test] fn pallet_metadata_expands() { use frame_support::traits::{CrateVersion, PalletInfoAccess, PalletInfoData}; - let mut infos = PalletInstancesWithSystem::infos(); + let mut infos = PalletInstances::infos(); infos.sort_by_key(|x| x.index); assert_eq!( infos, @@ -546,10 +546,10 @@ fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); - assert_eq!(PalletInstances::on_initialize(1), 21); - PalletInstances::on_finalize(1); + assert_eq!(PalletInstancesRev::on_initialize(1), 21); + PalletInstancesRev::on_finalize(1); - assert_eq!(PalletInstances::on_runtime_upgrade(), 61); + assert_eq!(PalletInstancesRev::on_runtime_upgrade(), 61); // The order is indeed reversed due to https://github.com/paritytech/substrate/issues/6280 assert_eq!( From 7395d26738288668ceabd28dce2445a5a6692dbe Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 19 Oct 2021 13:47:10 +0200 Subject: [PATCH 06/18] Formatting --- frame/executive/src/lib.rs | 27 ++++++++++++------- .../procedural/src/construct_runtime/mod.rs | 4 +-- frame/support/src/traits.rs | 4 +-- frame/support/src/traits/metadata.rs | 8 ++++-- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index f0f3855e75cc0..dca83b6d0a980 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -160,9 +160,14 @@ pub type OriginOf = as Dispatchable>::Origin; /// `construct_runtime` macro. This should exclude the System pallet. /// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are /// already called by `PalletInstanceOrder`. It will be called before all modules will be called. -pub struct Executive( - PhantomData<(System, Block, Context, UnsignedValidator, PalletInstanceOrder, OnRuntimeUpgrade)>, -); +pub struct Executive< + System, + Block, + Context, + UnsignedValidator, + PalletInstanceOrder, + OnRuntimeUpgrade = (), +>(PhantomData<(System, Block, Context, UnsignedValidator, PalletInstanceOrder, OnRuntimeUpgrade)>); impl< System: frame_system::Config + EnsureInherentsAreFirst, @@ -224,7 +229,8 @@ where weight = weight.saturating_add( as OnRuntimeUpgrade>::on_runtime_upgrade(), ); - weight = weight.saturating_add(::on_runtime_upgrade()); + weight = + weight.saturating_add(::on_runtime_upgrade()); weight } @@ -318,9 +324,9 @@ where weight = weight.saturating_add( as OnInitialize< System::BlockNumber, >>::on_initialize(*block_number)); - weight = weight.saturating_add( - >::on_initialize(*block_number), - ); + weight = weight.saturating_add(>::on_initialize(*block_number)); weight = weight.saturating_add( >::get().base_block, ); @@ -577,7 +583,9 @@ where // as well. frame_system::BlockHash::::insert(header.number(), header.hash()); - >::offchain_worker(*header.number()) + >::offchain_worker( + *header.number(), + ) } } @@ -1372,7 +1380,8 @@ mod tests { // All weights that show up in the `initialize_block_impl` let frame_system_upgrade_weight = frame_system::Pallet::::on_runtime_upgrade(); let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade(); - let runtime_upgrade_weight = ::on_runtime_upgrade(); + let runtime_upgrade_weight = + ::on_runtime_upgrade(); let frame_system_on_initialize_weight = frame_system::Pallet::::on_initialize(block_number); let on_initialize_weight = diff --git a/frame/support/procedural/src/construct_runtime/mod.rs b/frame/support/procedural/src/construct_runtime/mod.rs index 7e327bec89f3c..ba764ffdc282c 100644 --- a/frame/support/procedural/src/construct_runtime/mod.rs +++ b/frame/support/procedural/src/construct_runtime/mod.rs @@ -220,7 +220,7 @@ fn decl_all_pallets<'a>( let pallet_instances_rev = quote!( (#( #names_rev ),*) ); let names_ex_sys = names.iter().filter(|n| **n != SYSTEM_PALLET_NAME); let pallet_instances_ex_sys = quote!( (#( #names_ex_sys ),*) ); - let names_rev_ex_sys = names.iter().rev().filter(|n| **n != SYSTEM_PALLET_NAME); + let names_rev_ex_sys = names.iter().rev().filter(|n| **n != SYSTEM_PALLET_NAME); let pallet_instances_rev_ex_sys = quote!( (#( #names_rev_ex_sys ),*) ); quote!( @@ -228,7 +228,7 @@ fn decl_all_pallets<'a>( /// All pallets included in the runtime as a flat tuple of types. pub type PalletInstances = ( #pallet_instances ); - + // TODO: Remove these once executive is not sensitive to ordering. /// All pallet instances excluding System which are in the runtime as a flat tuple of types. pub type PalletInstancesExSystem = ( #pallet_instances_ex_sys ); diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 6b9a176476820..513267c5c8ba6 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -63,8 +63,8 @@ pub use randomness::Randomness; mod metadata; pub use metadata::{ CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo, - PalletInfoAccess, PalletInfoData, StorageVersion, STORAGE_VERSION_STORAGE_KEY_POSTFIX, - PalletsInfoAccess, + PalletInfoAccess, PalletInfoData, PalletsInfoAccess, StorageVersion, + STORAGE_VERSION_STORAGE_KEY_POSTFIX, }; mod hooks; diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index f83d02078b2d5..670b4495b8950 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -68,9 +68,13 @@ pub trait PalletInfoAccess { /// Provide information about a bunch of pallets. pub trait PalletsInfoAccess { /// Get information about a single pallet. Will give a result of `None` if called on a tuple. - fn info() -> Option { None } + fn info() -> Option { + None + } /// All of the pallets' information that this type represents. Relevant for tuples. - fn infos() -> Vec { Self::info().into_iter().collect() } + fn infos() -> Vec { + Self::info().into_iter().collect() + } } #[impl_for_tuples(60)] From 7012b0ffb96c9e224ba997ef9855e02babd2c2b8 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 19 Oct 2021 15:13:23 +0200 Subject: [PATCH 07/18] Fixes --- frame/executive/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index dca83b6d0a980..46ac32eeb134b 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -1381,11 +1381,11 @@ mod tests { let frame_system_upgrade_weight = frame_system::Pallet::::on_runtime_upgrade(); let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade(); let runtime_upgrade_weight = - ::on_runtime_upgrade(); + ::on_runtime_upgrade(); let frame_system_on_initialize_weight = frame_system::Pallet::::on_initialize(block_number); let on_initialize_weight = - >::on_initialize(block_number); + >::on_initialize(block_number); let base_block_weight = ::BlockWeights::get().base_block; From 19205f6183c9a52ae8a6f6e229cb948eaa3d36ff Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 15:53:31 +0200 Subject: [PATCH 08/18] Back to nesting --- bin/node-template/runtime/src/lib.rs | 4 +- bin/node/runtime/src/lib.rs | 4 +- frame/executive/README.md | 4 +- frame/executive/src/lib.rs | 8 ++-- .../src/construct_runtime/expand/config.rs | 2 +- .../procedural/src/construct_runtime/mod.rs | 23 +---------- .../src/pallet/expand/pallet_struct.rs | 9 +++-- frame/support/src/dispatch.rs | 9 +++-- frame/support/src/traits/metadata.rs | 40 ++++++++++++------- frame/support/test/tests/pallet.rs | 8 ++-- frame/support/test/tests/pallet_instance.rs | 8 ++-- 11 files changed, 55 insertions(+), 64 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 8ee169d7231e2..4b49cb48ef352 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -323,7 +323,7 @@ pub type Executive = frame_executive::Executive< Block, frame_system::ChainContext, Runtime, - PalletInstancesRevExSystem, + AllPallets, >; impl_runtime_apis! { @@ -474,7 +474,7 @@ impl_runtime_apis! { list_benchmark!(list, extra, pallet_timestamp, Timestamp); list_benchmark!(list, extra, pallet_template, TemplateModule); - let storage_info = PalletInstances::storage_info(); + let storage_info = AllPalletsWithSystem::storage_info(); return (list, storage_info) } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index edc8754e12917..c7920629bf356 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1327,7 +1327,7 @@ pub type Executive = frame_executive::Executive< Block, frame_system::ChainContext, Runtime, - PalletInstancesRevExSystem, + AllPallets, (), >; @@ -1653,7 +1653,7 @@ impl_runtime_apis! { list_benchmark!(list, extra, pallet_utility, Utility); list_benchmark!(list, extra, pallet_vesting, Vesting); - let storage_info = PalletInstances::storage_info(); + let storage_info = AllPalletsWithSystem::storage_info(); return (list, storage_info) } diff --git a/frame/executive/README.md b/frame/executive/README.md index cc75617defe8a..e96d07b0843f2 100644 --- a/frame/executive/README.md +++ b/frame/executive/README.md @@ -40,7 +40,7 @@ pub type Executive = executive::Executive< Block, Context, Runtime, - PalletInstancesRevExSystem, + AllPallets, >; ``` @@ -65,7 +65,7 @@ pub type Executive = executive::Executive< Block, Context, Runtime, - PalletInstancesRevExSystem, + AllPallets, CustomOnRuntimeUpgrade, >; ``` diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 46ac32eeb134b..b0624572e0e79 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -156,7 +156,7 @@ pub type OriginOf = as Dispatchable>::Origin; /// - `Context`: The context that is used when checking an extrinsic. /// - `UnsignedValidator`: The unsigned transaction validator of the runtime. /// - `PalletInstanceOrder`: Tuple that contains all modules in order used to call e.g. -/// `on_initialize`. Generally this should be the `PalletInstancesRevExSystem` type coming from +/// `on_initialize`. Generally this should be the `AllPallets` type coming from /// `construct_runtime` macro. This should exclude the System pallet. /// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are /// already called by `PalletInstanceOrder`. It will be called before all modules will be called. @@ -867,7 +867,7 @@ mod tests { Block, ChainContext, Runtime, - PalletInstancesRevExSystem, + AllPallets, CustomOnRuntimeUpgrade, >; @@ -1381,11 +1381,11 @@ mod tests { let frame_system_upgrade_weight = frame_system::Pallet::::on_runtime_upgrade(); let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade(); let runtime_upgrade_weight = - ::on_runtime_upgrade(); + ::on_runtime_upgrade(); let frame_system_on_initialize_weight = frame_system::Pallet::::on_initialize(block_number); let on_initialize_weight = - >::on_initialize(block_number); + >::on_initialize(block_number); let base_block_weight = ::BlockWeights::get().base_block; diff --git a/frame/support/procedural/src/construct_runtime/expand/config.rs b/frame/support/procedural/src/construct_runtime/expand/config.rs index 8f98ba876aa2f..38c8a7d9f9474 100644 --- a/frame/support/procedural/src/construct_runtime/expand/config.rs +++ b/frame/support/procedural/src/construct_runtime/expand/config.rs @@ -82,7 +82,7 @@ pub fn expand_outer_config( #build_storage_calls #scrate::BasicExternalities::execute_with_storage(storage, || { - ::on_genesis(); + ::on_genesis(); }); Ok(()) diff --git a/frame/support/procedural/src/construct_runtime/mod.rs b/frame/support/procedural/src/construct_runtime/mod.rs index ba764ffdc282c..a81aa0e312ada 100644 --- a/frame/support/procedural/src/construct_runtime/mod.rs +++ b/frame/support/procedural/src/construct_runtime/mod.rs @@ -215,34 +215,13 @@ fn decl_all_pallets<'a>( .iter() .fold(TokenStream2::default(), |combined, name| quote!((#name, #combined))); - let pallet_instances = quote!( (#( #names ),*) ); - let names_rev = names.iter().rev(); - let pallet_instances_rev = quote!( (#( #names_rev ),*) ); - let names_ex_sys = names.iter().filter(|n| **n != SYSTEM_PALLET_NAME); - let pallet_instances_ex_sys = quote!( (#( #names_ex_sys ),*) ); - let names_rev_ex_sys = names.iter().rev().filter(|n| **n != SYSTEM_PALLET_NAME); - let pallet_instances_rev_ex_sys = quote!( (#( #names_rev_ex_sys ),*) ); - quote!( #types - /// All pallets included in the runtime as a flat tuple of types. - pub type PalletInstances = ( #pallet_instances ); - - // TODO: Remove these once executive is not sensitive to ordering. - /// All pallet instances excluding System which are in the runtime as a flat tuple of types. - pub type PalletInstancesExSystem = ( #pallet_instances_ex_sys ); - /// All pallet instances excluding System which are in the runtime as a flat tuple of types. - pub type PalletInstancesRev = ( #pallet_instances_rev ); - /// All pallets included in the runtime as a flat tuple of types. - pub type PalletInstancesRevExSystem = ( #pallet_instances_rev_ex_sys ); - /// All pallets included in the runtime as a nested tuple of types. /// Excludes the System pallet. - #[deprecated(note = "use `PalletInstancesRev` (or possibly `PalletInstances`) instead")] pub type AllPallets = ( #all_pallets ); /// All pallets included in the runtime as a nested tuple of types. - #[deprecated(note = "use `PalletInstancesRevWithSystem` (or possibly `PalletInstancesWithSystem`) instead")] pub type AllPalletsWithSystem = ( #all_pallets_with_system ); /// All modules included in the runtime as a nested tuple of types. @@ -340,7 +319,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 { #[test] pub fn runtime_integrity_tests() { - ::integrity_test(); + ::integrity_test(); } } ) diff --git a/frame/support/procedural/src/pallet/expand/pallet_struct.rs b/frame/support/procedural/src/pallet/expand/pallet_struct.rs index 4e58cf56aaaf2..a1f05a222e3d6 100644 --- a/frame/support/procedural/src/pallet/expand/pallet_struct.rs +++ b/frame/support/procedural/src/pallet/expand/pallet_struct.rs @@ -237,14 +237,15 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { for #pallet_ident<#type_use_gen> #config_where_clause { - fn info() -> Option<#frame_support::traits::PalletInfoData> { - use #frame_support::traits::PalletInfoAccess; - Some(#frame_support::traits::PalletInfoData { + fn count() -> usize { 1 } + fn accumulate(acc: &mut Vec<#frame_support::traits::PalletInfoData>) { + let item = #frame_support::traits::PalletInfoData { index: Self::index(), name: Self::name(), module_name: Self::module_name(), crate_version: Self::crate_version(), - }) + }; + acc.push(item); } } diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index f45ea0f89607a..cb61556e277f0 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2168,14 +2168,15 @@ macro_rules! decl_module { impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::traits::PalletsInfoAccess for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* { - fn info() -> Option<$crate::traits::PalletInfoData> { - use $crate::traits::PalletInfoAccess; - Some($crate::traits::PalletInfoData { + fn count() -> usize { 1 } + fn accumulate(acc: &mut $crate::traits::PalletInfoData) { + let item = $crate::traits::PalletInfoData { index: Self::index(), name: Self::name(), module_name: Self::module_name(), crate_version: Self::crate_version(), - }) + }; + acc.push(item); } } diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 670b4495b8950..32d21437b6fbd 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -67,26 +67,36 @@ pub trait PalletInfoAccess { /// Provide information about a bunch of pallets. pub trait PalletsInfoAccess { - /// Get information about a single pallet. Will give a result of `None` if called on a tuple. - fn info() -> Option { - None - } + /// The number of pallets' information that this type represents. Relevant for nested tuples. + /// + /// You probably don't want this function but `infos()` instead. + fn count() -> usize { 0 } + + /// Extend the given vector by all of the pallets' information that this type represents. + /// Relevant for tuples. + /// + /// You probably don't want this function but `infos()` instead. + fn accumulate(_accumulator: &mut Vec) {} + /// All of the pallets' information that this type represents. Relevant for tuples. fn infos() -> Vec { - Self::info().into_iter().collect() + let mut result = Vec::with_capacity(Self::infos_len()); + Self::accumulate_infos(&mut result); + result } } -#[impl_for_tuples(60)] -impl PalletsInfoAccess for Tuple { - fn infos() -> Vec { - let mut result = vec![]; - for_tuples!( #( - if let Some(info) = Tuple::info() { - result.push(info); - } - )* ); - result +impl PalletsInfoAccess for () {} +impl PalletsInfoAccess for (T,) { + fn count() -> usize { T::count() } + fn accumulate(acc: &mut Vec) { T::accumulate(acc) } +} + +impl PalletsInfoAccess for (T1, T2) { + fn count() -> usize { T1::count() + T2::count() } + fn accumulate(acc: &mut Vec) { + T1::accumulate(acc); + T2::accumulate(acc); } } diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 36fc811e7a9cc..15fb2c128672a 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -939,10 +939,10 @@ fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); - assert_eq!(PalletInstancesRevExSystem::on_initialize(1), 10); - PalletInstancesRevExSystem::on_finalize(1); + assert_eq!(AllPallets::on_initialize(1), 10); + AllPallets::on_finalize(1); - assert_eq!(PalletInstancesRevExSystem::on_runtime_upgrade(), 30); + assert_eq!(AllPallets::on_runtime_upgrade(), 30); assert_eq!( frame_system::Pallet::::events()[0].event, @@ -992,7 +992,7 @@ fn migrate_from_pallet_version_to_storage_version() { let db_weight = RuntimeDbWeight { read: 0, write: 5 }; let weight = frame_support::migrations::migrate_from_pallet_version_to_storage_version::< - PalletInstancesRev, + AllPallets, >(&db_weight); // 3 pallets, 2 writes and every write costs 5 weight. diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 78a633965e5cf..66c47bfd76903 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -508,7 +508,7 @@ fn storage_expand() { #[test] fn pallet_metadata_expands() { use frame_support::traits::{CrateVersion, PalletInfoAccess, PalletInfoData}; - let mut infos = PalletInstances::infos(); + let mut infos = AllPalletsWithSystem::infos(); infos.sort_by_key(|x| x.index); assert_eq!( infos, @@ -546,10 +546,10 @@ fn pallet_hooks_expand() { TestExternalities::default().execute_with(|| { frame_system::Pallet::::set_block_number(1); - assert_eq!(PalletInstancesRev::on_initialize(1), 21); - PalletInstancesRev::on_finalize(1); + assert_eq!(AllPallets::on_initialize(1), 21); + AllPallets::on_finalize(1); - assert_eq!(PalletInstancesRev::on_runtime_upgrade(), 61); + assert_eq!(AllPallets::on_runtime_upgrade(), 61); // The order is indeed reversed due to https://github.com/paritytech/substrate/issues/6280 assert_eq!( From 14d538921978133afeadcea9767e859c1ca231f2 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 15:55:23 +0200 Subject: [PATCH 09/18] Back to nestingx --- frame/executive/src/lib.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index b0624572e0e79..843898d2678d6 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -155,33 +155,33 @@ pub type OriginOf = as Dispatchable>::Origin; /// - `Block`: The block type of the runtime /// - `Context`: The context that is used when checking an extrinsic. /// - `UnsignedValidator`: The unsigned transaction validator of the runtime. -/// - `PalletInstanceOrder`: Tuple that contains all modules in order used to call e.g. +/// - `AllPallets`: Tuple that contains all modules in order used to call e.g. /// `on_initialize`. Generally this should be the `AllPallets` type coming from /// `construct_runtime` macro. This should exclude the System pallet. /// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are -/// already called by `PalletInstanceOrder`. It will be called before all modules will be called. +/// already called by `AllPallets`. It will be called before all modules will be called. pub struct Executive< System, Block, Context, UnsignedValidator, - PalletInstanceOrder, + AllPallets, OnRuntimeUpgrade = (), ->(PhantomData<(System, Block, Context, UnsignedValidator, PalletInstanceOrder, OnRuntimeUpgrade)>); +>(PhantomData<(System, Block, Context, UnsignedValidator, AllPallets, OnRuntimeUpgrade)>); impl< System: frame_system::Config + EnsureInherentsAreFirst, Block: traits::Block
, Context: Default, UnsignedValidator, - PalletInstanceOrder: OnRuntimeUpgrade + AllPallets: OnRuntimeUpgrade + OnInitialize + OnIdle + OnFinalize + OffchainWorker, COnRuntimeUpgrade: OnRuntimeUpgrade, > ExecuteBlock - for Executive + for Executive where Block::Extrinsic: Checkable + Codec, CheckedOf: Applyable + GetDispatchInfo, @@ -196,7 +196,7 @@ where Block, Context, UnsignedValidator, - PalletInstanceOrder, + AllPallets, COnRuntimeUpgrade, >::execute_block(block); } @@ -207,13 +207,13 @@ impl< Block: traits::Block
, Context: Default, UnsignedValidator, - PalletInstanceOrder: OnRuntimeUpgrade + AllPallets: OnRuntimeUpgrade + OnInitialize + OnIdle + OnFinalize + OffchainWorker, COnRuntimeUpgrade: OnRuntimeUpgrade, - > Executive + > Executive where Block::Extrinsic: Checkable + Codec, CheckedOf: Applyable + GetDispatchInfo, @@ -230,7 +230,7 @@ where as OnRuntimeUpgrade>::on_runtime_upgrade(), ); weight = - weight.saturating_add(::on_runtime_upgrade()); + weight.saturating_add(::on_runtime_upgrade()); weight } @@ -272,7 +272,7 @@ where #[cfg(feature = "try-runtime")] pub fn try_runtime_upgrade() -> Result { < - (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstanceOrder) + (frame_system::Pallet::, COnRuntimeUpgrade, AllPallets) as OnRuntimeUpgrade >::pre_upgrade().unwrap(); @@ -280,7 +280,7 @@ where let weight = Self::execute_on_runtime_upgrade(); < - (frame_system::Pallet::, COnRuntimeUpgrade, PalletInstanceOrder) + (frame_system::Pallet::, COnRuntimeUpgrade, AllPallets) as OnRuntimeUpgrade >::post_upgrade().unwrap(); @@ -324,7 +324,7 @@ where weight = weight.saturating_add( as OnInitialize< System::BlockNumber, >>::on_initialize(*block_number)); - weight = weight.saturating_add(>::on_initialize(*block_number)); weight = weight.saturating_add( @@ -440,7 +440,7 @@ where remaining_weight, ); remaining_weight = remaining_weight.saturating_sub(used_weight); - used_weight = >::on_idle( + used_weight = >::on_idle( block_number, remaining_weight, ) @@ -454,7 +454,7 @@ where as OnFinalize>::on_finalize( block_number, ); - >::on_finalize(block_number); + >::on_finalize(block_number); } /// Apply extrinsic outside of the block execution function. @@ -583,7 +583,7 @@ where // as well. frame_system::BlockHash::::insert(header.number(), header.hash()); - >::offchain_worker( + >::offchain_worker( *header.number(), ) } From 4b1a6855aae69cc13f7ded49c4267433aedc2000 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 16:01:18 +0200 Subject: [PATCH 10/18] Revert executive lib --- frame/executive/src/lib.rs | 46 ++++++++++++-------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 843898d2678d6..b1bdf357ec07d 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -59,7 +59,7 @@ //! # type Context = frame_system::ChainContext; //! # pub type Block = generic::Block; //! # pub type Balances = u64; -//! # pub type Pallets = u64; +//! # pub type AllPallets = u64; //! # pub enum Runtime {}; //! # use sp_runtime::transaction_validity::{ //! # TransactionValidity, UnknownTransaction, TransactionSource, @@ -73,7 +73,7 @@ //! # } //! # } //! /// Executive: handles dispatch to the various modules. -//! pub type Executive = executive::Executive; +//! pub type Executive = executive::Executive; //! ``` //! //! ### Custom `OnRuntimeUpgrade` logic @@ -90,7 +90,7 @@ //! # type Context = frame_system::ChainContext; //! # pub type Block = generic::Block; //! # pub type Balances = u64; -//! # pub type Pallets = u64; +//! # pub type AllPallets = u64; //! # pub enum Runtime {}; //! # use sp_runtime::transaction_validity::{ //! # TransactionValidity, UnknownTransaction, TransactionSource, @@ -111,14 +111,7 @@ //! } //! } //! -//! pub type Executive = executive::Executive< -//! Runtime, -//! Block, -//! Context, -//! Runtime, -//! Pallets, -//! CustomOnRuntimeUpgrade, -//! >; +//! pub type Executive = executive::Executive; //! ``` #![cfg_attr(not(feature = "std"), no_std)] @@ -155,19 +148,12 @@ pub type OriginOf = as Dispatchable>::Origin; /// - `Block`: The block type of the runtime /// - `Context`: The context that is used when checking an extrinsic. /// - `UnsignedValidator`: The unsigned transaction validator of the runtime. -/// - `AllPallets`: Tuple that contains all modules in order used to call e.g. -/// `on_initialize`. Generally this should be the `AllPallets` type coming from -/// `construct_runtime` macro. This should exclude the System pallet. +/// - `AllPallets`: Tuple that contains all modules. Will be used to call e.g. `on_initialize`. /// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are /// already called by `AllPallets`. It will be called before all modules will be called. -pub struct Executive< - System, - Block, - Context, - UnsignedValidator, - AllPallets, - OnRuntimeUpgrade = (), ->(PhantomData<(System, Block, Context, UnsignedValidator, AllPallets, OnRuntimeUpgrade)>); +pub struct Executive( + PhantomData<(System, Block, Context, UnsignedValidator, AllPallets, OnRuntimeUpgrade)>, +); impl< System: frame_system::Config + EnsureInherentsAreFirst, @@ -229,8 +215,7 @@ where weight = weight.saturating_add( as OnRuntimeUpgrade>::on_runtime_upgrade(), ); - weight = - weight.saturating_add(::on_runtime_upgrade()); + weight = weight.saturating_add(::on_runtime_upgrade()); weight } @@ -324,9 +309,9 @@ where weight = weight.saturating_add( as OnInitialize< System::BlockNumber, >>::on_initialize(*block_number)); - weight = weight.saturating_add(>::on_initialize(*block_number)); + weight = weight.saturating_add( + >::on_initialize(*block_number), + ); weight = weight.saturating_add( >::get().base_block, ); @@ -583,9 +568,7 @@ where // as well. frame_system::BlockHash::::insert(header.number(), header.hash()); - >::offchain_worker( - *header.number(), - ) + >::offchain_worker(*header.number()) } } @@ -1380,8 +1363,7 @@ mod tests { // All weights that show up in the `initialize_block_impl` let frame_system_upgrade_weight = frame_system::Pallet::::on_runtime_upgrade(); let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade(); - let runtime_upgrade_weight = - ::on_runtime_upgrade(); + let runtime_upgrade_weight = ::on_runtime_upgrade(); let frame_system_on_initialize_weight = frame_system::Pallet::::on_initialize(block_number); let on_initialize_weight = From 51d256187d94ab036145e398cac7815a6241126c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 16:03:05 +0200 Subject: [PATCH 11/18] Reversions --- frame/support/procedural/src/construct_runtime/expand/config.rs | 2 +- frame/support/procedural/src/construct_runtime/mod.rs | 2 +- frame/support/src/migrations.rs | 2 +- frame/support/src/traits/misc.rs | 2 +- frame/support/src/traits/storage.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frame/support/procedural/src/construct_runtime/expand/config.rs b/frame/support/procedural/src/construct_runtime/expand/config.rs index 38c8a7d9f9474..5e1b9d94700e6 100644 --- a/frame/support/procedural/src/construct_runtime/expand/config.rs +++ b/frame/support/procedural/src/construct_runtime/expand/config.rs @@ -82,7 +82,7 @@ pub fn expand_outer_config( #build_storage_calls #scrate::BasicExternalities::execute_with_storage(storage, || { - ::on_genesis(); + ::on_genesis(); }); Ok(()) diff --git a/frame/support/procedural/src/construct_runtime/mod.rs b/frame/support/procedural/src/construct_runtime/mod.rs index a81aa0e312ada..863df34266591 100644 --- a/frame/support/procedural/src/construct_runtime/mod.rs +++ b/frame/support/procedural/src/construct_runtime/mod.rs @@ -319,7 +319,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 { #[test] pub fn runtime_integrity_tests() { - ::integrity_test(); + ::integrity_test(); } } ) diff --git a/frame/support/src/migrations.rs b/frame/support/src/migrations.rs index 18966376403c7..c61cbac62a16b 100644 --- a/frame/support/src/migrations.rs +++ b/frame/support/src/migrations.rs @@ -42,7 +42,7 @@ impl PalletVersionToStorageVersionHelpe } } -#[impl_trait_for_tuples::impl_for_tuples(60)] +#[impl_trait_for_tuples::impl_for_tuples(30)] impl PalletVersionToStorageVersionHelper for T { fn migrate(db_weight: &RuntimeDbWeight) -> Weight { let mut weight: Weight = 0; diff --git a/frame/support/src/traits/misc.rs b/frame/support/src/traits/misc.rs index e429ee39112b8..9109bfeeae722 100644 --- a/frame/support/src/traits/misc.rs +++ b/frame/support/src/traits/misc.rs @@ -299,7 +299,7 @@ pub trait ExecuteBlock { /// but cannot preform any alterations. More specifically alterations are /// not forbidden, but they are not persisted in any way after the worker /// has finished. -#[impl_trait_for_tuples::impl_for_tuples(60)] +#[impl_trait_for_tuples::impl_for_tuples(30)] pub trait OffchainWorker { /// This function is being called after every block import (when fully synced). /// diff --git a/frame/support/src/traits/storage.rs b/frame/support/src/traits/storage.rs index e990278c237e4..9a88a3ed44046 100644 --- a/frame/support/src/traits/storage.rs +++ b/frame/support/src/traits/storage.rs @@ -71,7 +71,7 @@ pub trait StorageInfoTrait { fn storage_info() -> Vec; } -#[impl_trait_for_tuples::impl_for_tuples(60)] +#[impl_trait_for_tuples::impl_for_tuples(30)] impl StorageInfoTrait for Tuple { fn storage_info() -> Vec { let mut res = vec![]; From b26a8c56c339a7760e32456bdd3ccaf66c5296b2 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 16:04:01 +0200 Subject: [PATCH 12/18] Reversions --- frame/support/src/traits/hooks.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frame/support/src/traits/hooks.rs b/frame/support/src/traits/hooks.rs index 726274727b5ce..2a8b0a156247a 100644 --- a/frame/support/src/traits/hooks.rs +++ b/frame/support/src/traits/hooks.rs @@ -38,7 +38,7 @@ pub trait OnInitialize { } } -#[impl_for_tuples(60)] +#[impl_for_tuples(30)] impl OnInitialize for Tuple { fn on_initialize(n: BlockNumber) -> crate::weights::Weight { let mut weight = 0; @@ -50,7 +50,7 @@ impl OnInitialize for Tuple { /// The block finalization trait. /// /// Implementing this lets you express what should happen for your pallet when the block is ending. -#[impl_for_tuples(60)] +#[impl_for_tuples(30)] pub trait OnFinalize { /// The block is being finalized. Implement to have something happen. /// @@ -79,7 +79,7 @@ pub trait OnIdle { } } -#[impl_for_tuples(60)] +#[impl_for_tuples(30)] impl OnIdle for Tuple { fn on_idle(n: BlockNumber, remaining_weight: crate::weights::Weight) -> crate::weights::Weight { let on_idle_functions: &[fn( @@ -105,7 +105,7 @@ impl OnIdle for Tuple { /// Implementing this trait for a pallet let's you express operations that should /// happen at genesis. It will be called in an externalities provided environment and /// will see the genesis state after all pallets have written their genesis state. -#[impl_for_tuples(60)] +#[impl_for_tuples(30)] pub trait OnGenesis { /// Something that should happen at genesis. fn on_genesis() {} @@ -187,7 +187,7 @@ pub trait OnRuntimeUpgrade { } } -#[impl_for_tuples(60)] +#[impl_for_tuples(30)] impl OnRuntimeUpgrade for Tuple { fn on_runtime_upgrade() -> crate::weights::Weight { let mut weight = 0; @@ -316,7 +316,7 @@ pub trait GenesisBuild: Default + sp_runtime::traits::MaybeSerializeD } /// A trait which is called when the timestamp is set in the runtime. -#[impl_for_tuples(60)] +#[impl_for_tuples(30)] pub trait OnTimestampSet { /// Called when the timestamp is set. fn on_timestamp_set(moment: Moment); From f690e43e5d3ff7bda6d002ea272ee6432186b12d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 16:45:41 +0200 Subject: [PATCH 13/18] Fixes --- frame/support/src/dispatch.rs | 3 ++- frame/support/src/traits/metadata.rs | 7 +++---- frame/support/test/tests/pallet_instance.rs | 8 +++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index cb61556e277f0..b69f3b16b1f83 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2169,7 +2169,8 @@ macro_rules! decl_module { for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* { fn count() -> usize { 1 } - fn accumulate(acc: &mut $crate::traits::PalletInfoData) { + fn accumulate(acc: &mut Vec<$crate::traits::PalletInfoData>) { + use $crate::traits::PalletInfoAccess; let item = $crate::traits::PalletInfoData { index: Self::index(), name: Self::name(), diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 32d21437b6fbd..0cb5e2314de4b 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -18,7 +18,6 @@ //! Traits for managing information attached to pallets and their constituents. use codec::{Decode, Encode}; -use impl_trait_for_tuples::impl_for_tuples; use sp_runtime::RuntimeDebug; use sp_std::prelude::*; @@ -80,8 +79,8 @@ pub trait PalletsInfoAccess { /// All of the pallets' information that this type represents. Relevant for tuples. fn infos() -> Vec { - let mut result = Vec::with_capacity(Self::infos_len()); - Self::accumulate_infos(&mut result); + let mut result = Vec::with_capacity(Self::count()); + Self::accumulate(&mut result); result } } @@ -95,8 +94,8 @@ impl PalletsInfoAccess for (T,) { impl PalletsInfoAccess for (T1, T2) { fn count() -> usize { T1::count() + T2::count() } fn accumulate(acc: &mut Vec) { - T1::accumulate(acc); T2::accumulate(acc); + T1::accumulate(acc); } } diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 66c47bfd76903..3c8d45ffe4a8e 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -507,12 +507,18 @@ fn storage_expand() { #[test] fn pallet_metadata_expands() { - use frame_support::traits::{CrateVersion, PalletInfoAccess, PalletInfoData}; + use frame_support::traits::{CrateVersion, PalletsInfoAccess, PalletInfoData}; let mut infos = AllPalletsWithSystem::infos(); infos.sort_by_key(|x| x.index); assert_eq!( infos, vec![ + PalletInfoData { + index: 0, + name: "System", + module_name: "frame_system", + crate_version: CrateVersion { major: 4, minor: 0, patch: 0 }, + }, PalletInfoData { index: 1, name: "Example", From dddc075f81814d6f4f4b3fbbd892f00116020891 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 16:48:21 +0200 Subject: [PATCH 14/18] Fixes --- frame/support/test/tests/pallet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 15fb2c128672a..dc72be3ebdd49 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -992,7 +992,7 @@ fn migrate_from_pallet_version_to_storage_version() { let db_weight = RuntimeDbWeight { read: 0, write: 5 }; let weight = frame_support::migrations::migrate_from_pallet_version_to_storage_version::< - AllPallets, + AllPalletsWithSystem, >(&db_weight); // 3 pallets, 2 writes and every write costs 5 weight. From b497b08d24b123c063d289b7e65567690ba3950b Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 16:53:50 +0200 Subject: [PATCH 15/18] Formatting --- .../src/pallet/expand/pallet_struct.rs | 4 +++- frame/support/src/dispatch.rs | 2 +- frame/support/src/traits/metadata.rs | 16 ++++++++++++---- frame/support/test/tests/pallet_instance.rs | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/frame/support/procedural/src/pallet/expand/pallet_struct.rs b/frame/support/procedural/src/pallet/expand/pallet_struct.rs index a1f05a222e3d6..61f70c495d39c 100644 --- a/frame/support/procedural/src/pallet/expand/pallet_struct.rs +++ b/frame/support/procedural/src/pallet/expand/pallet_struct.rs @@ -238,7 +238,9 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { #config_where_clause { fn count() -> usize { 1 } - fn accumulate(acc: &mut Vec<#frame_support::traits::PalletInfoData>) { + fn accumulate( + acc: &mut #frame_support::sp_std::vec::Vec<#frame_support::traits::PalletInfoData> + ) { let item = #frame_support::traits::PalletInfoData { index: Self::index(), name: Self::name(), diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index b69f3b16b1f83..a492bc12f6a38 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2169,7 +2169,7 @@ macro_rules! decl_module { for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* { fn count() -> usize { 1 } - fn accumulate(acc: &mut Vec<$crate::traits::PalletInfoData>) { + fn accumulate(acc: &mut $crate::sp_std::vec::Vec<$crate::traits::PalletInfoData>) { use $crate::traits::PalletInfoAccess; let item = $crate::traits::PalletInfoData { index: Self::index(), diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 0cb5e2314de4b..4f7eb02ad6866 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -69,7 +69,9 @@ pub trait PalletsInfoAccess { /// The number of pallets' information that this type represents. Relevant for nested tuples. /// /// You probably don't want this function but `infos()` instead. - fn count() -> usize { 0 } + fn count() -> usize { + 0 + } /// Extend the given vector by all of the pallets' information that this type represents. /// Relevant for tuples. @@ -87,12 +89,18 @@ pub trait PalletsInfoAccess { impl PalletsInfoAccess for () {} impl PalletsInfoAccess for (T,) { - fn count() -> usize { T::count() } - fn accumulate(acc: &mut Vec) { T::accumulate(acc) } + fn count() -> usize { + T::count() + } + fn accumulate(acc: &mut Vec) { + T::accumulate(acc) + } } impl PalletsInfoAccess for (T1, T2) { - fn count() -> usize { T1::count() + T2::count() } + fn count() -> usize { + T1::count() + T2::count() + } fn accumulate(acc: &mut Vec) { T2::accumulate(acc); T1::accumulate(acc); diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 3c8d45ffe4a8e..3a1009402d6f2 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -507,7 +507,7 @@ fn storage_expand() { #[test] fn pallet_metadata_expands() { - use frame_support::traits::{CrateVersion, PalletsInfoAccess, PalletInfoData}; + use frame_support::traits::{CrateVersion, PalletInfoData, PalletsInfoAccess}; let mut infos = AllPalletsWithSystem::infos(); infos.sort_by_key(|x| x.index); assert_eq!( From 8802275c936eea744a284617ac401b9e0335253c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 19:20:04 +0200 Subject: [PATCH 16/18] Fixes --- frame/support/procedural/src/pallet/expand/pallet_struct.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/support/procedural/src/pallet/expand/pallet_struct.rs b/frame/support/procedural/src/pallet/expand/pallet_struct.rs index 61f70c495d39c..96dfdbb4b6f2d 100644 --- a/frame/support/procedural/src/pallet/expand/pallet_struct.rs +++ b/frame/support/procedural/src/pallet/expand/pallet_struct.rs @@ -241,6 +241,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream { fn accumulate( acc: &mut #frame_support::sp_std::vec::Vec<#frame_support::traits::PalletInfoData> ) { + use #frame_support::traits::PalletInfoAccess; let item = #frame_support::traits::PalletInfoData { index: Self::index(), name: Self::name(), From 9882bdae957aa6f7109a3c85f76266c50067a48d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 20 Oct 2021 19:24:40 +0200 Subject: [PATCH 17/18] Spelling --- frame/support/src/traits/metadata.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 4f7eb02ad6866..e7a89faa5a4fc 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -269,7 +269,7 @@ mod tests { 1 } fn name() -> &'static str { - "Pallat1" + "Pallet1" } fn module_name() -> &'static str { "pallet1" @@ -284,7 +284,7 @@ mod tests { 2 } fn name() -> &'static str { - "Pallat2" + "Pallet2" } fn module_name() -> &'static str { "pallet2" From 206e95e93a1ba7646c58ca10398754ca1d2ff0a9 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 21 Oct 2021 10:28:39 +0200 Subject: [PATCH 18/18] Comments --- 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 e7a89faa5a4fc..0da76f7585aca 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -66,7 +66,7 @@ pub trait PalletInfoAccess { /// Provide information about a bunch of pallets. pub trait PalletsInfoAccess { - /// The number of pallets' information that this type represents. Relevant for nested tuples. + /// The number of pallets' information that this type represents. /// /// You probably don't want this function but `infos()` instead. fn count() -> usize { @@ -74,12 +74,11 @@ pub trait PalletsInfoAccess { } /// Extend the given vector by all of the pallets' information that this type represents. - /// Relevant for tuples. /// /// You probably don't want this function but `infos()` instead. fn accumulate(_accumulator: &mut Vec) {} - /// All of the pallets' information that this type represents. Relevant for tuples. + /// All of the pallets' information that this type represents. fn infos() -> Vec { let mut result = Vec::with_capacity(Self::count()); Self::accumulate(&mut result); @@ -102,6 +101,7 @@ impl PalletsInfoAccess for (T1, T2 T1::count() + T2::count() } fn accumulate(acc: &mut Vec) { + // The AllPallets type tuplises the pallets in reverse order, so we unreverse them here. T2::accumulate(acc); T1::accumulate(acc); }