Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Abstract away the executive #14742

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
26 changes: 9 additions & 17 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl pallet_template::Config for Runtime {

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub struct Runtime {
pub struct Runtime<_> {
System: frame_system,
Timestamp: pallet_timestamp,
Aura: pallet_aura,
Expand Down Expand Up @@ -316,14 +316,6 @@ pub type UncheckedExtrinsic =
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
/// The payload being signed in transactions.
pub type SignedPayload = generic::SignedPayload<RuntimeCall, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
>;

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
Expand All @@ -348,11 +340,11 @@ impl_runtime_apis! {
}

fn execute_block(block: Block) {
Executive::execute_block(block);
Runtime::api_impl_core_execute_block(block);
}

fn initialize_block(header: &<Block as BlockT>::Header) {
Executive::initialize_block(header)
Runtime::api_impl_core_initialize_block(header)
}
}

Expand All @@ -372,11 +364,11 @@ impl_runtime_apis! {

impl sp_block_builder::BlockBuilder<Block> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
Executive::apply_extrinsic(extrinsic)
Runtime::api_impl_builder_apply_extrinsic(extrinsic)
}

fn finalize_block() -> <Block as BlockT>::Header {
Executive::finalize_block()
Runtime::api_impl_builder_finalize_block()
}

fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
Expand All @@ -397,13 +389,13 @@ impl_runtime_apis! {
tx: <Block as BlockT>::Extrinsic,
block_hash: <Block as BlockT>::Hash,
) -> TransactionValidity {
Executive::validate_transaction(source, tx, block_hash)
Runtime::api_impl_validate_transaction(source, tx, block_hash)
}
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(header: &<Block as BlockT>::Header) {
Executive::offchain_worker(header)
Runtime::api_impl_offchain_worker(header)
}
}

Expand Down Expand Up @@ -556,7 +548,7 @@ impl_runtime_apis! {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here. If any of the pre/post migration checks fail, we shall stop
// right here and right now.
let weight = Executive::try_runtime_upgrade(checks).unwrap();
let weight = Runtime::api_impl_try_runtime_upgrade(checks).unwrap();
(weight, BlockWeights::get().max_block)
}

Expand All @@ -568,7 +560,7 @@ impl_runtime_apis! {
) -> Weight {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here.
Executive::try_execute_block(block, state_root_check, signature_check, select).expect("execute-block failed")
Runtime::api_impl_try_execute_block(block, state_root_check, signature_check, select).expect("execute-block failed")
}
}
}
27 changes: 9 additions & 18 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ impl pallet_statement::Config for Runtime {
}

construct_runtime!(
pub struct Runtime
pub struct Runtime<Migrations>
{
System: frame_system,
Utility: pallet_utility,
Expand Down Expand Up @@ -1982,15 +1982,6 @@ pub type UncheckedExtrinsic =
pub type SignedPayload = generic::SignedPayload<RuntimeCall, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
Migrations,
>;

// All migrations executed on runtime upgrade as a nested tuple of types implementing
// `OnRuntimeUpgrade`.
Expand Down Expand Up @@ -2085,11 +2076,11 @@ impl_runtime_apis! {
}

fn execute_block(block: Block) {
Executive::execute_block(block);
Runtime::api_impl_core_execute_block(block);
}

fn initialize_block(header: &<Block as BlockT>::Header) {
Executive::initialize_block(header)
Runtime::api_impl_core_initialize_block(header)
}
}

Expand All @@ -2109,11 +2100,11 @@ impl_runtime_apis! {

impl sp_block_builder::BlockBuilder<Block> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
Executive::apply_extrinsic(extrinsic)
Runtime::api_impl_builder_apply_extrinsic(extrinsic)
}

fn finalize_block() -> <Block as BlockT>::Header {
Executive::finalize_block()
Runtime::api_impl_builder_finalize_block()
}

fn inherent_extrinsics(data: InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
Expand All @@ -2131,7 +2122,7 @@ impl_runtime_apis! {
tx: <Block as BlockT>::Extrinsic,
block_hash: <Block as BlockT>::Hash,
) -> TransactionValidity {
Executive::validate_transaction(source, tx, block_hash)
Runtime::api_impl_validate_transaction(source, tx, block_hash)
}
}

Expand All @@ -2146,7 +2137,7 @@ impl_runtime_apis! {

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(header: &<Block as BlockT>::Header) {
Executive::offchain_worker(header)
Runtime::api_impl_offchain_worker(header)
}
}

Expand Down Expand Up @@ -2520,7 +2511,7 @@ impl_runtime_apis! {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here. If any of the pre/post migration checks fail, we shall stop
// right here and right now.
let weight = Executive::try_runtime_upgrade(checks).unwrap();
let weight = Runtime::api_impl_try_runtime_upgrade(checks).unwrap();
(weight, RuntimeBlockWeights::get().max_block)
}

Expand All @@ -2532,7 +2523,7 @@ impl_runtime_apis! {
) -> Weight {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here.
Executive::try_execute_block(block, state_root_check, signature_check, select).unwrap()
Runtime::api_impl_try_execute_block(block, state_root_check, signature_check, select).unwrap()
}
}

Expand Down
107 changes: 107 additions & 0 deletions frame/support/procedural/src/construct_runtime/expand/executive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

use crate::construct_runtime::parse::ExecutiveSection;
use frame_support_procedural_tools::generate_crate_access_2018;
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::Result;

pub fn expand_executive(
runtime: &Ident,
system: &Ident,
scrate: &TokenStream,
block: &TokenStream,
executive_section: ExecutiveSection,
) -> Result<TokenStream> {
let executive = generate_crate_access_2018("frame-executive")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will play nicely with the new frame crate, it should re-export the frame-executive.


let on_runtime_upgrade = match executive_section.custom_on_runtime_upgrade {
Some(custom) => quote!(#custom),
None => quote!(()),
};

let try_runtime_section = if let Ok(try_runtime) =
generate_crate_access_2018("frame-try-runtime")
{
quote! {
#[cfg(feature = "try-runtime")]
impl #runtime {
fn api_impl_try_runtime_upgrade(checks: #try_runtime::UpgradeCheckSelect) -> Result<Weight, #scrate::sp_runtime::TryRuntimeError> {
Executive::try_runtime_upgrade(checks)
}

fn api_impl_try_execute_block(
block: #block,
state_root_check: bool,
signature_check: bool,
select: #try_runtime::TryStateSelect
) -> Result<Weight, &'static str> {
Executive::try_execute_block(block, state_root_check, signature_check, select)
}
}
}
} else {
quote!()
};

let res = quote! {
/// Executive: handles dispatch to the various modules.
pub type Executive = #executive::Executive<
#runtime,
#block,
#system::ChainContext<#runtime>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda cheating, if you want to be pedantic. There's now no good way to configure this. But I am not sure if this is all that useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will add the support to provide a custom Executive as well, using the syntax you suggested above.

#runtime,
AllPalletsWithSystem,
#on_runtime_upgrade
>;

impl #runtime {
pub fn api_impl_core_execute_block(block: #block) {
Executive::execute_block(block);
}

pub fn api_impl_core_initialize_block(header: &<#block as #scrate::sp_runtime::traits::Block>::Header) {
Executive::initialize_block(header);
}

pub fn api_impl_builder_apply_extrinsic(extrinsic: <#block as #scrate::sp_runtime::traits::Block>::Extrinsic) -> #scrate::sp_runtime::ApplyExtrinsicResult {
Executive::apply_extrinsic(extrinsic)
}

pub fn api_impl_builder_finalize_block() -> <#block as #scrate::sp_runtime::traits::Block>::Header {
Executive::finalize_block()
}

pub fn api_impl_validate_transaction(
source: #scrate::sp_runtime::transaction_validity::TransactionSource,
tx: <Block as #scrate::sp_runtime::traits::Block>::Extrinsic,
block_hash: <Block as #scrate::sp_runtime::traits::Block>::Hash,
) -> #scrate::sp_runtime::transaction_validity::TransactionValidity {
Executive::validate_transaction(source, tx, block_hash)
}

pub fn api_impl_offchain_worker(header: &<Block as #scrate::sp_runtime::traits::Block>::Header) {
Executive::offchain_worker(header)
}
}

#try_runtime_section
};

Ok(res)
}
2 changes: 2 additions & 0 deletions frame/support/procedural/src/construct_runtime/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

mod call;
mod config;
mod executive;
mod freeze_reason;
mod hold_reason;
mod inherent;
Expand All @@ -29,6 +30,7 @@ mod unsigned;

pub use call::expand_outer_dispatch;
pub use config::expand_outer_config;
pub use executive::expand_executive;
pub use freeze_reason::expand_outer_freeze_reason;
pub use hold_reason::expand_outer_hold_reason;
pub use inherent::expand_outer_inherent;
Expand Down
15 changes: 14 additions & 1 deletion frame/support/procedural/src/construct_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,13 @@ fn construct_runtime_explicit_to_explicit_expanded(
fn construct_runtime_final_expansion(
definition: ExplicitRuntimeDeclaration,
) -> Result<TokenStream2> {
let ExplicitRuntimeDeclaration { name, pallets, pallets_token, where_section } = definition;
let ExplicitRuntimeDeclaration {
name,
pallets,
pallets_token,
where_section,
executive_section,
} = definition;

let system_pallet =
pallets.iter().find(|decl| decl.name == SYSTEM_PALLET_NAME).ok_or_else(|| {
Expand Down Expand Up @@ -402,6 +408,11 @@ fn construct_runtime_final_expansion(
let slash_reason = expand::expand_outer_slash_reason(&pallets, &scrate);
let integrity_test = decl_integrity_test(&scrate);
let static_assertions = decl_static_assertions(&name, &pallets, &scrate);
let executive = if let Some(executive_section) = executive_section {
expand::expand_executive(&name, &frame_system, &scrate, &block, executive_section)?
} else {
quote!()
};

let warning =
where_section.map_or(None, |where_section| {
Expand Down Expand Up @@ -491,6 +502,8 @@ fn construct_runtime_final_expansion(
#integrity_test

#static_assertions

#executive
);

Ok(res)
Expand Down
Loading