Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
4 changes: 2 additions & 2 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
PalletInstancesRevExSystem,
>;

impl_runtime_apis! {
Expand Down Expand Up @@ -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 = PalletInstances::storage_info();

return (list, storage_info)
}
Expand Down
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
PalletInstancesRevExSystem,
(),
>;

Expand Down Expand Up @@ -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 = PalletInstances::storage_info();

return (list, storage_info)
}
Expand Down
17 changes: 15 additions & 2 deletions frame/executive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime, Block, Context, Runtime, AllPallets>;
pub type Executive = executive::Executive<
Runtime,
Block,
Context,
Runtime,
PalletInstancesRevExSystem,
>;
```

### Custom `OnRuntimeUpgrade` logic
Expand All @@ -54,7 +60,14 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}
}

pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPallets, CustomOnRuntimeUpgrade>;
pub type Executive = executive::Executive<
Runtime,
Block,
Context,
Runtime,
PalletInstancesRevExSystem,
CustomOnRuntimeUpgrade,
>;
```

License: Apache-2.0
70 changes: 44 additions & 26 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
//! # type Context = frame_system::ChainContext<Runtime>;
//! # pub type Block = generic::Block<Header, UncheckedExtrinsic>;
//! # pub type Balances = u64;
//! # pub type AllPallets = u64;
//! # pub type Pallets = u64;
//! # pub enum Runtime {};
//! # use sp_runtime::transaction_validity::{
//! # TransactionValidity, UnknownTransaction, TransactionSource,
Expand All @@ -73,7 +73,7 @@
//! # }
//! # }
//! /// Executive: handles dispatch to the various modules.
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPallets>;
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, Pallets>;
//! ```
//!
//! ### Custom `OnRuntimeUpgrade` logic
Expand All @@ -90,7 +90,7 @@
//! # type Context = frame_system::ChainContext<Runtime>;
//! # pub type Block = generic::Block<Header, UncheckedExtrinsic>;
//! # pub type Balances = u64;
//! # pub type AllPallets = u64;
//! # pub type Pallets = u64;
//! # pub enum Runtime {};
//! # use sp_runtime::transaction_validity::{
//! # TransactionValidity, UnknownTransaction, TransactionSource,
Expand All @@ -111,7 +111,14 @@
//! }
//! }
//!
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPallets, CustomOnRuntimeUpgrade>;
//! pub type Executive = executive::Executive<
//! Runtime,
//! Block,
//! Context,
//! Runtime,
//! Pallets,
//! CustomOnRuntimeUpgrade,
//! >;
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
Expand Down Expand Up @@ -148,26 +155,33 @@ pub type OriginOf<E, C> = <CallOf<E, C> 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`.
/// - `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 `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)>,
);
/// already called by `PalletInstanceOrder`. It will be called before all modules will be called.
pub struct Executive<
System,
Block,
Context,
UnsignedValidator,
PalletInstanceOrder,
OnRuntimeUpgrade = (),
>(PhantomData<(System, Block, Context, UnsignedValidator, PalletInstanceOrder, OnRuntimeUpgrade)>);

impl<
System: frame_system::Config + EnsureInherentsAreFirst<Block>,
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllPallets: OnRuntimeUpgrade
PalletInstanceOrder: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> ExecuteBlock<Block>
for Executive<System, Block, Context, UnsignedValidator, AllPallets, COnRuntimeUpgrade>
for Executive<System, Block, Context, UnsignedValidator, PalletInstanceOrder, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
Expand All @@ -182,7 +196,7 @@ where
Block,
Context,
UnsignedValidator,
AllPallets,
PalletInstanceOrder,
COnRuntimeUpgrade,
>::execute_block(block);
}
Expand All @@ -193,13 +207,13 @@ impl<
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllPallets: OnRuntimeUpgrade
PalletInstanceOrder: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> Executive<System, Block, Context, UnsignedValidator, AllPallets, COnRuntimeUpgrade>
> Executive<System, Block, Context, UnsignedValidator, PalletInstanceOrder, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
Expand All @@ -215,7 +229,8 @@ where
weight = weight.saturating_add(
<frame_system::Pallet<System> as OnRuntimeUpgrade>::on_runtime_upgrade(),
);
weight = weight.saturating_add(<AllPallets as OnRuntimeUpgrade>::on_runtime_upgrade());
weight =
weight.saturating_add(<PalletInstanceOrder as OnRuntimeUpgrade>::on_runtime_upgrade());

weight
}
Expand Down Expand Up @@ -257,15 +272,15 @@ where
#[cfg(feature = "try-runtime")]
pub fn try_runtime_upgrade() -> Result<frame_support::weights::Weight, &'static str> {
<
(frame_system::Pallet::<System>, COnRuntimeUpgrade, AllPallets)
(frame_system::Pallet::<System>, COnRuntimeUpgrade, PalletInstanceOrder)
as
OnRuntimeUpgrade
>::pre_upgrade().unwrap();

let weight = Self::execute_on_runtime_upgrade();

<
(frame_system::Pallet::<System>, COnRuntimeUpgrade, AllPallets)
(frame_system::Pallet::<System>, COnRuntimeUpgrade, PalletInstanceOrder)
as
OnRuntimeUpgrade
>::post_upgrade().unwrap();
Expand Down Expand Up @@ -309,9 +324,9 @@ where
weight = weight.saturating_add(<frame_system::Pallet<System> as OnInitialize<
System::BlockNumber,
>>::on_initialize(*block_number));
weight = weight.saturating_add(
<AllPallets as OnInitialize<System::BlockNumber>>::on_initialize(*block_number),
);
weight = weight.saturating_add(<PalletInstanceOrder as OnInitialize<
System::BlockNumber,
>>::on_initialize(*block_number));
weight = weight.saturating_add(
<System::BlockWeights as frame_support::traits::Get<_>>::get().base_block,
);
Expand Down Expand Up @@ -425,7 +440,7 @@ where
remaining_weight,
);
remaining_weight = remaining_weight.saturating_sub(used_weight);
used_weight = <AllPallets as OnIdle<System::BlockNumber>>::on_idle(
used_weight = <PalletInstanceOrder as OnIdle<System::BlockNumber>>::on_idle(
block_number,
remaining_weight,
)
Expand All @@ -439,7 +454,7 @@ where
<frame_system::Pallet<System> as OnFinalize<System::BlockNumber>>::on_finalize(
block_number,
);
<AllPallets as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<PalletInstanceOrder as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
}

/// Apply extrinsic outside of the block execution function.
Expand Down Expand Up @@ -568,7 +583,9 @@ where
// as well.
frame_system::BlockHash::<System>::insert(header.number(), header.hash());

<AllPallets as OffchainWorker<System::BlockNumber>>::offchain_worker(*header.number())
<PalletInstanceOrder as OffchainWorker<System::BlockNumber>>::offchain_worker(
*header.number(),
)
}
}

Expand Down Expand Up @@ -850,7 +867,7 @@ mod tests {
Block<TestXt>,
ChainContext<Runtime>,
Runtime,
AllPallets,
PalletInstancesRevExSystem,
CustomOnRuntimeUpgrade,
>;

Expand Down Expand Up @@ -1363,11 +1380,12 @@ mod tests {
// All weights that show up in the `initialize_block_impl`
let frame_system_upgrade_weight = frame_system::Pallet::<Runtime>::on_runtime_upgrade();
let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade();
let runtime_upgrade_weight = <AllPallets as OnRuntimeUpgrade>::on_runtime_upgrade();
let runtime_upgrade_weight =
<PalletInstanceOrder as OnRuntimeUpgrade>::on_runtime_upgrade();
let frame_system_on_initialize_weight =
frame_system::Pallet::<Runtime>::on_initialize(block_number);
let on_initialize_weight =
<AllPallets as OnInitialize<u64>>::on_initialize(block_number);
<PalletInstanceOrder as OnInitialize<u64>>::on_initialize(block_number);
let base_block_weight =
<Runtime as frame_system::Config>::BlockWeights::get().base_block;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn expand_outer_config(
#build_storage_calls

#scrate::BasicExternalities::execute_with_storage(storage, || {
<AllPalletsWithSystem as #scrate::traits::OnGenesis>::on_genesis();
<PalletInstancesRev as #scrate::traits::OnGenesis>::on_genesis();
});

Ok(())
Expand Down
24 changes: 23 additions & 1 deletion frame/support/procedural/src/construct_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,34 @@ 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.
Expand Down Expand Up @@ -318,7 +340,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 {

#[test]
pub fn runtime_integrity_tests() {
<AllPalletsWithSystem as #scrate::traits::IntegrityTest>::integrity_test();
<PalletInstances as #scrate::traits::IntegrityTest>::integrity_test();
}
}
)
Expand Down
15 changes: 15 additions & 0 deletions frame/support/procedural/src/pallet/expand/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
14 changes: 14 additions & 0 deletions frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,20 @@ macro_rules! decl_module {
}
}

impl<$trait_instance: $trait_name $(<I>, $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 $(<I>, $instance: $instantiable)?> $crate::dispatch::GetCallName
for $call_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )*
Expand Down
4 changes: 2 additions & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading