Construct Runtime v2 (#1378)

Moved from https://github.com/paritytech/substrate/pull/14788

----

Fixes https://github.com/paritytech/polkadot-sdk/issues/232

This PR introduces outer-macro approach for `construct_runtime` as
discussed in the linked issue. It looks like the following:
```rust
#[frame_support::runtime]
mod runtime {
	#[runtime::runtime]
        #[runtime::derive(
		RuntimeCall,
		RuntimeEvent,
		RuntimeError,
		RuntimeOrigin,
		RuntimeFreezeReason,
		RuntimeHoldReason,
		RuntimeSlashReason,
		RuntimeLockId,
                RuntimeTask,
	)]
	pub struct Runtime;

	#[runtime::pallet_index(0)]
	pub type System = frame_system;

	#[runtime::pallet_index(1)]
	pub type Timestamp = pallet_timestamp;

	#[runtime::pallet_index(2)]
	pub type Aura = pallet_aura;

	#[runtime::pallet_index(3)]
	pub type Grandpa = pallet_grandpa;

	#[runtime::pallet_index(4)]
	pub type Balances = pallet_balances;

	#[runtime::pallet_index(5)]
	pub type TransactionPayment = pallet_transaction_payment;

	#[runtime::pallet_index(6)]
	pub type Sudo = pallet_sudo;

	// Include the custom logic from the pallet-template in the runtime.
	#[runtime::pallet_index(7)]
	pub type TemplateModule = pallet_template;
}
```

## Features
- `#[runtime::runtime]` attached to a struct defines the main runtime
- `#[runtime::derive]` attached to this struct defines the types
generated by runtime
- `#[runtime::pallet_index]` must be attached to a pallet to define its
index
- `#[runtime::disable_call]` can be optionally attached to a pallet to
disable its calls
- `#[runtime::disable_unsigned]` can be optionally attached to a pallet
to disable unsigned calls
- A pallet instance can be defined as `TemplateModule:
pallet_template<Instance>`
- An optional attribute can be defined as
`#[frame_support::runtime(legacy_ordering)]` to ensure that the order of
hooks is same as the order of pallets (and not based on the
pallet_index). This is to support legacy runtimes and should be avoided
for new ones.

## Todo
- [x] Update the latest syntax in kitchensink and tests
- [x] Update UI tests
- [x] Docs

## Extension
- Abstract away the Executive similar to
https://github.com/paritytech/substrate/pull/14742
- Optionally avoid the need to specify all runtime types (TBD)

---------

Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: Nikhil Gupta <>
This commit is contained in:
gupnik
2024-03-13 12:31:01 +05:30
committed by GitHub
parent a756baf3b2
commit 82f3c3e2e8
45 changed files with 3211 additions and 204 deletions
@@ -208,8 +208,8 @@
//! This macro returns the ` :: expanded { Error }` list of additional parts we would like to
//! expose.
mod expand;
mod parse;
pub(crate) mod expand;
pub(crate) mod parse;
use crate::pallet::parse::helper::two128_str;
use cfg_expr::Predicate;
@@ -515,7 +515,7 @@ fn construct_runtime_final_expansion(
Ok(res)
}
fn decl_all_pallets<'a>(
pub(crate) fn decl_all_pallets<'a>(
runtime: &'a Ident,
pallet_declarations: impl Iterator<Item = &'a Pallet>,
features: &HashSet<&str>,
@@ -624,7 +624,8 @@ fn decl_all_pallets<'a>(
#( #all_pallets_without_system )*
)
}
fn decl_pallet_runtime_setup(
pub(crate) fn decl_pallet_runtime_setup(
runtime: &Ident,
pallet_declarations: &[Pallet],
scrate: &TokenStream2,
@@ -730,7 +731,7 @@ fn decl_pallet_runtime_setup(
)
}
fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 {
pub(crate) fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 {
quote!(
#[cfg(test)]
mod __construct_runtime_integrity_test {
@@ -745,7 +746,7 @@ fn decl_integrity_test(scrate: &TokenStream2) -> TokenStream2 {
)
}
fn decl_static_assertions(
pub(crate) fn decl_static_assertions(
runtime: &Ident,
pallet_decls: &[Pallet],
scrate: &TokenStream2,
@@ -776,7 +777,7 @@ fn decl_static_assertions(
}
}
fn check_pallet_number(input: TokenStream2, pallet_num: usize) -> Result<()> {
pub(crate) fn check_pallet_number(input: TokenStream2, pallet_num: usize) -> Result<()> {
let max_pallet_num = {
if cfg!(feature = "tuples-96") {
96