Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Prev Previous commit
Next Next commit
Flat tuple for getting all pallet instances
  • Loading branch information
gavofyork committed Oct 19, 2021
commit f92db2d4806a329217a842207f216e9b1343edfb
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,
PalletInstances,
>;

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 = PalletInstancesWithSystem::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,
PalletInstances,
(),
>;

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 = PalletInstancesWithSystem::storage_info();

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

### Custom `OnRuntimeUpgrade` logic
Expand All @@ -54,7 +54,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,
PalletInstances,
CustomOnRuntimeUpgrade,
>;
```

License: Apache-2.0
53 changes: 30 additions & 23 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 PalletInstances = 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, PalletInstances>;
//! ```
//!
//! ### 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 PalletInstances = 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,
//! PalletInstances,
//! CustomOnRuntimeUpgrade,
//! >;
//! ```

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

impl<
System: frame_system::Config + EnsureInherentsAreFirst<Block>,
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllPallets: OnRuntimeUpgrade
PalletInstances: 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, PalletInstances, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
Expand All @@ -182,7 +189,7 @@ where
Block,
Context,
UnsignedValidator,
AllPallets,
PalletInstances,
COnRuntimeUpgrade,
>::execute_block(block);
}
Expand All @@ -193,13 +200,13 @@ impl<
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllPallets: OnRuntimeUpgrade
PalletInstances: 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, PalletInstances, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
Expand All @@ -215,7 +222,7 @@ 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(<PalletInstances as OnRuntimeUpgrade>::on_runtime_upgrade());

weight
}
Expand Down Expand Up @@ -257,15 +264,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, PalletInstances)
as
OnRuntimeUpgrade
>::pre_upgrade().unwrap();

let weight = Self::execute_on_runtime_upgrade();

<
(frame_system::Pallet::<System>, COnRuntimeUpgrade, AllPallets)
(frame_system::Pallet::<System>, COnRuntimeUpgrade, PalletInstances)
as
OnRuntimeUpgrade
>::post_upgrade().unwrap();
Expand Down Expand Up @@ -310,7 +317,7 @@ where
System::BlockNumber,
>>::on_initialize(*block_number));
weight = weight.saturating_add(
<AllPallets as OnInitialize<System::BlockNumber>>::on_initialize(*block_number),
<PalletInstances 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 +432,7 @@ where
remaining_weight,
);
remaining_weight = remaining_weight.saturating_sub(used_weight);
used_weight = <AllPallets as OnIdle<System::BlockNumber>>::on_idle(
used_weight = <PalletInstances as OnIdle<System::BlockNumber>>::on_idle(
block_number,
remaining_weight,
)
Expand All @@ -439,7 +446,7 @@ where
<frame_system::Pallet<System> as OnFinalize<System::BlockNumber>>::on_finalize(
block_number,
);
<AllPallets as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<PalletInstances as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
}

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

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

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

Expand Down Expand Up @@ -1363,11 +1370,11 @@ 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 = <PalletInstances 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);
<PalletInstances 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();
<PalletInstancesWithSystem as #scrate::traits::OnGenesis>::on_genesis();
});

Ok(())
Expand Down
17 changes: 16 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,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.
Expand Down Expand Up @@ -318,7 +333,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 {

#[test]
pub fn runtime_integrity_tests() {
<AllPalletsWithSystem as #scrate::traits::IntegrityTest>::integrity_test();
<PalletInstancesWithSystem as #scrate::traits::IntegrityTest>::integrity_test();
}
}
)
Expand Down
6 changes: 3 additions & 3 deletions frame/support/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T: GetStorageVersion + PalletInfoAccess> 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;
Expand All @@ -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)
}
12 changes: 6 additions & 6 deletions frame/support/src/traits/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait OnInitialize<BlockNumber> {
}
}

#[impl_for_tuples(30)]
#[impl_for_tuples(60)]
impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
fn on_initialize(n: BlockNumber) -> crate::weights::Weight {
let mut weight = 0;
Expand All @@ -50,7 +50,7 @@ impl<BlockNumber: Clone> OnInitialize<BlockNumber> 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<BlockNumber> {
/// The block is being finalized. Implement to have something happen.
///
Expand Down Expand Up @@ -79,7 +79,7 @@ pub trait OnIdle<BlockNumber> {
}
}

#[impl_for_tuples(30)]
#[impl_for_tuples(60)]
impl<BlockNumber: Copy + AtLeast32BitUnsigned> OnIdle<BlockNumber> for Tuple {
fn on_idle(n: BlockNumber, remaining_weight: crate::weights::Weight) -> crate::weights::Weight {
let on_idle_functions: &[fn(
Expand All @@ -105,7 +105,7 @@ impl<BlockNumber: Copy + AtLeast32BitUnsigned> OnIdle<BlockNumber> 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() {}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -316,7 +316,7 @@ pub trait GenesisBuild<T, I = ()>: 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<Moment> {
/// Called when the timestamp is set.
fn on_timestamp_set(moment: Moment);
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/traits/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub trait ExecuteBlock<Block: BlockT> {
/// 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<BlockNumber> {
/// This function is being called after every block import (when fully synced).
///
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/traits/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub trait StorageInfoTrait {
fn storage_info() -> Vec<StorageInfo>;
}

#[impl_trait_for_tuples::impl_for_tuples(30)]
#[impl_trait_for_tuples::impl_for_tuples(60)]
impl StorageInfoTrait for Tuple {
fn storage_info() -> Vec<StorageInfo> {
let mut res = vec![];
Expand Down
Loading