From 1a045f3a128b0f782f05c934604bfcc6d914becf Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Mon, 7 Aug 2023 18:36:22 +0400 Subject: [PATCH 1/4] bring back timestamp --- Cargo.lock | 17 +++ Cargo.toml | 1 + primitives/timestamp/Cargo.toml | 39 +++++++ primitives/timestamp/src/lib.rs | 194 ++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+) create mode 100644 primitives/timestamp/Cargo.toml create mode 100644 primitives/timestamp/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 0d953383686..fa16fc9a0db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2877,6 +2877,23 @@ dependencies = [ "tracing", ] +[[package]] +name = "cumulus-primitives-timestamp" +version = "0.1.0" +dependencies = [ + "cumulus-primitives-core", + "cumulus-test-client", + "cumulus-test-relay-sproof-builder", + "futures", + "parity-scale-codec 3.6.4", + "sp-consensus", + "sp-inherents", + "sp-runtime", + "sp-std", + "sp-timestamp", + "sp-tracing", +] + [[package]] name = "cumulus-primitives-utility" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 7981dc816cd..b0899a03bc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ members = [ "parachain-template/runtime", "primitives/core", "primitives/parachain-inherent", + "primitives/timestamp", "primitives/utility", "polkadot-parachain", "parachains/common", diff --git a/primitives/timestamp/Cargo.toml b/primitives/timestamp/Cargo.toml new file mode 100644 index 00000000000..254ab578b95 --- /dev/null +++ b/primitives/timestamp/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "cumulus-primitives-timestamp" +version = "0.1.0" +authors = ["Parity Technologies "] +edition = "2021" +description = "Provides timestamp related functionality for parachains." + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ "derive" ] } +futures = "0.3.28" + +# Substrate +sp-inherents = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } +sp-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } + +# Cumulus +cumulus-primitives-core = { path = "../core", default-features = false } + +[dev-dependencies] + +# Substrate +sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } + +# Cumulus +cumulus-test-client = { path = "../../test/client" } +cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder" } + + +[features] +default = [ "std" ] +std = [ + "sp-inherents/std", + "sp-std/std", + "sp-timestamp/std", + "cumulus-primitives-core/std", +] diff --git a/primitives/timestamp/src/lib.rs b/primitives/timestamp/src/lib.rs new file mode 100644 index 00000000000..932656d9b0f --- /dev/null +++ b/primitives/timestamp/src/lib.rs @@ -0,0 +1,194 @@ +// Copyright 2021 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +//! Cumulus timestamp related primitives. +//! +//! Provides a [`InherentDataProvider`] that should be used in the validation phase of the parachain. +//! It will be used to create the inherent data and that will be used to check the inherents inside +//! the parachain block (in this case the timestamp inherent). As we don't have access to any clock +//! from the runtime the timestamp is always passed as an inherent into the runtime. To check this +//! inherent when validating the block, we will use the relay chain slot. As the relay chain slot +//! is derived from a timestamp, we can easily convert it back to a timestamp by muliplying it with +//! the slot duration. By comparing the relay chain slot derived timestamp with the timestamp we can +//! ensure that the parachain timestamp is reasonable. + +#![cfg_attr(not(feature = "std"), no_std)] + +use cumulus_primitives_core::relay_chain::Slot; +use sp_inherents::{Error, InherentData}; +use sp_std::time::Duration; + +pub use sp_timestamp::{InherentType, INHERENT_IDENTIFIER}; + +/// The inherent data provider for the timestamp. +/// +/// This should be used in the runtime when checking the inherents in the validation phase of the +/// parachain. +pub struct InherentDataProvider { + relay_chain_slot: Slot, + relay_chain_slot_duration: Duration, +} + +impl InherentDataProvider { + /// Create `Self` from the given relay chain slot and slot duration. + pub fn from_relay_chain_slot_and_duration( + relay_chain_slot: Slot, + relay_chain_slot_duration: Duration, + ) -> Self { + Self { relay_chain_slot, relay_chain_slot_duration } + } + + /// Create the inherent data. + pub fn create_inherent_data(&self) -> Result { + let mut inherent_data = InherentData::new(); + self.provide_inherent_data(&mut inherent_data).map(|_| inherent_data) + } + + /// Provide the inherent data into the given `inherent_data`. + pub fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> { + // As the parachain starts building at around `relay_chain_slot + 1` we use that slot to + // calculate the timestamp. + let data: InherentType = ((*self.relay_chain_slot + 1) * + self.relay_chain_slot_duration.as_millis() as u64) + .into(); + + inherent_data.put_data(INHERENT_IDENTIFIER, &data) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use codec::{Decode, Encode}; + use cumulus_primitives_core::{relay_chain::Hash as PHash, PersistedValidationData}; + use cumulus_test_client::{ + runtime::{Block, Header, WASM_BINARY}, + BlockData, BuildParachainBlockData, Client, ClientBlockImportExt, ExecutorResult, HeadData, + InitBlockBuilder, ParachainBlockData, TestClientBuilder, TestClientBuilderExt, + ValidationParams, + }; + use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; + use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; + use std::{env, process::Command, str::FromStr}; + + const SLOT_DURATION: u64 = 6000; + + fn call_validate_block( + parent_head: Header, + block_data: ParachainBlockData, + relay_parent_storage_root: PHash, + ) -> ExecutorResult
{ + cumulus_test_client::validate_block( + ValidationParams { + block_data: BlockData(block_data.encode()), + parent_head: HeadData(parent_head.encode()), + relay_parent_number: 1, + relay_parent_storage_root, + }, + WASM_BINARY.expect("You need to build the WASM binaries to run the tests!"), + ) + .map(|v| Header::decode(&mut &v.head_data.0[..]).expect("Decodes `Header`.")) + } + + fn build_block( + client: &Client, + hash: ::Hash, + timestamp: u64, + relay_chain_slot: Slot, + ) -> (ParachainBlockData, PHash) { + let sproof_builder = + RelayStateSproofBuilder { current_slot: relay_chain_slot, ..Default::default() }; + + let parent_header = client.header(hash).ok().flatten().expect("Genesis header exists"); + + let relay_parent_storage_root = sproof_builder.clone().into_state_root_and_proof().0; + + let validation_data = PersistedValidationData { + relay_parent_number: 1, + parent_head: parent_header.encode().into(), + ..Default::default() + }; + + let block = client + .init_block_builder_with_timestamp( + hash, + Some(validation_data), + sproof_builder, + timestamp, + ) + .build_parachain_block(*parent_header.state_root()); + + (block, relay_parent_storage_root) + } + + #[test] + fn check_timestamp_inherent_works() { + sp_tracing::try_init_simple(); + let relay_chain_slot = 2; + + if env::var("RUN_TEST").is_ok() { + let mut client = TestClientBuilder::default().build(); + let timestamp = u64::from_str(&env::var("TIMESTAMP").expect("TIMESTAMP is set")) + .expect("TIMESTAMP is a valid `u64`"); + + let block = + build_block(&client, client.chain_info().genesis_hash, SLOT_DURATION, 1.into()) + .0 + .into_block(); + futures::executor::block_on( + client.import(sp_consensus::BlockOrigin::Own, block.clone()), + ) + .unwrap(); + + let hashof1 = block.hash(); + let (block, relay_chain_root) = + build_block(&client, hashof1, timestamp, relay_chain_slot.into()); + + let header = call_validate_block( + client.header(hashof1).ok().flatten().expect("Genesis header exists"), + block.clone(), + relay_chain_root, + ) + .expect("Calls validate block"); + assert_eq!(block.header(), &header); + } else { + let slot_timestamp = relay_chain_slot * SLOT_DURATION; + + for (timestamp, res) in &[ + (slot_timestamp, true), + (slot_timestamp - 500, true), + (slot_timestamp + 500, true), + (slot_timestamp * 10, false), + ] { + let output = Command::new(env::current_exe().unwrap()) + .args(["check_timestamp_inherent_works", "--", "--nocapture"]) + .env("RUN_TEST", "1") + .env("TIMESTAMP", timestamp.to_string()) + .output() + .expect("Runs the test"); + + if !res { + assert!(String::from_utf8(output.stderr) + .unwrap() + .contains("Checking inherents failed")); + } + + assert!(dbg!(output.status.success()) == *res); + } + } + } +} From 451fc55bc1fe455710abe3e6b5c518951387d0b4 Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Mon, 7 Aug 2023 20:19:51 +0400 Subject: [PATCH 2/4] Restore CheckInherents --- pallets/parachain-system/Cargo.toml | 4 +- .../parachain-system/proc-macro/Cargo.toml | 1 + .../parachain-system/proc-macro/src/lib.rs | 40 +++++++++++++++---- pallets/parachain-system/src/lib.rs | 29 +++++++++++++- .../src/validate_block/implementation.rs | 29 +++++++++++++- 5 files changed, 92 insertions(+), 11 deletions(-) diff --git a/pallets/parachain-system/Cargo.toml b/pallets/parachain-system/Cargo.toml index 8990a848ad0..03f52a022fc 100644 --- a/pallets/parachain-system/Cargo.toml +++ b/pallets/parachain-system/Cargo.toml @@ -80,4 +80,6 @@ runtime-benchmarks = [ try-runtime = ["frame-support/try-runtime"] -parameterized-consensus-hook = [] +parameterized-consensus-hook = [ + "cumulus-pallet-parachain-system-proc-macro/parameterized-consensus-hook", +] diff --git a/pallets/parachain-system/proc-macro/Cargo.toml b/pallets/parachain-system/proc-macro/Cargo.toml index 8ff664b0284..7719b075817 100644 --- a/pallets/parachain-system/proc-macro/Cargo.toml +++ b/pallets/parachain-system/proc-macro/Cargo.toml @@ -17,3 +17,4 @@ proc-macro-crate = "1.3.1" [features] default = [ "std" ] std = [] +parameterized-consensus-hook = [] diff --git a/pallets/parachain-system/proc-macro/src/lib.rs b/pallets/parachain-system/proc-macro/src/lib.rs index 3c6fb21e5f3..9d5ac12a898 100644 --- a/pallets/parachain-system/proc-macro/src/lib.rs +++ b/pallets/parachain-system/proc-macro/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use proc_macro2::{Span, TokenStream}; +use proc_macro2::Span; use proc_macro_crate::{crate_name, FoundCrate}; use syn::{ parse::{Parse, ParseStream}, @@ -25,17 +25,20 @@ use syn::{ mod keywords { syn::custom_keyword!(Runtime); syn::custom_keyword!(BlockExecutor); + syn::custom_keyword!(CheckInherents); } struct Input { runtime: Path, block_executor: Path, + check_inherents: Option, } impl Parse for Input { fn parse(input: ParseStream) -> Result { let mut runtime = None; let mut block_executor = None; + let mut check_inherents = None; fn parse_inner( input: ParseStream, @@ -56,26 +59,24 @@ impl Parse for Input { } } - while runtime.is_none() || block_executor.is_none() { + while !input.is_empty() || runtime.is_none() || block_executor.is_none() { let lookahead = input.lookahead1(); if lookahead.peek(keywords::Runtime) { parse_inner::(input, &mut runtime)?; } else if lookahead.peek(keywords::BlockExecutor) { parse_inner::(input, &mut block_executor)?; + } else if lookahead.peek(keywords::CheckInherents) { + parse_inner::(input, &mut check_inherents)?; } else { return Err(lookahead.error()) } } - let rest = input.parse::()?; - if !rest.is_empty() { - return Err(Error::new(rest.span(), "Unexpected input data")) - } - Ok(Self { runtime: runtime.expect("Everything is parsed before; qed"), block_executor: block_executor.expect("Everything is parsed before; qed"), + check_inherents, }) } } @@ -91,7 +92,7 @@ fn crate_() -> Result { #[proc_macro] pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let Input { runtime, block_executor } = match syn::parse(input) { + let Input { runtime, block_executor, check_inherents } = match syn::parse(input) { Ok(t) => t, Err(e) => return e.into_compile_error().into(), }; @@ -101,6 +102,28 @@ pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::To Err(e) => return e.into_compile_error().into(), }; + let check_inherents = match check_inherents { + Some(_check_inherents) => { + #[cfg(not(feature = "parameterized-consensus-hook"))] + quote::quote! { #_check_inherents } + #[cfg(feature = "parameterized-consensus-hook")] + return Error::new( + Span::call_site(), + "CheckInherents is incompatible with consensus hook feature", + ) + .into_compile_error() + .into() + }, + None => { + #[cfg(feature = "parameterized-consensus-hook")] + quote::quote! { #crate_::DummyCheckInherents<<#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock> } + #[cfg(not(feature = "parameterized-consensus-hook"))] + return Error::new(Span::call_site(), "missing CheckInherents input") + .into_compile_error() + .into() + }, + }; + if cfg!(not(feature = "std")) { quote::quote! { #[doc(hidden)] @@ -127,6 +150,7 @@ pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::To <#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock, #block_executor, #runtime, + #check_inherents, >(params); #crate_::validate_block::polkadot_parachain::write_result(&res) diff --git a/pallets/parachain-system/src/lib.rs b/pallets/parachain-system/src/lib.rs index 83954995678..cf63aee64da 100644 --- a/pallets/parachain-system/src/lib.rs +++ b/pallets/parachain-system/src/lib.rs @@ -48,7 +48,7 @@ use frame_system::{ensure_none, ensure_root, pallet_prelude::HeaderFor}; use polkadot_parachain::primitives::RelayChainBlockNumber; use scale_info::TypeInfo; use sp_runtime::{ - traits::{BlockNumberProvider, Hash}, + traits::{Block as BlockT, BlockNumberProvider, Hash}, transaction_validity::{ InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity, ValidTransaction, @@ -1497,6 +1497,33 @@ impl UpwardMessageSender for Pallet { } } +/// Something that can check the inherents of a block. +#[deprecated = "use `cumulus-pallet-aura-ext::FixedVelocityConsensusHook` instead"] +pub trait CheckInherents { + /// Check all inherents of the block. + /// + /// This function gets passed all the extrinsics of the block, so it is up to the callee to + /// identify the inherents. The `validation_data` can be used to access the + fn check_inherents( + block: &Block, + validation_data: &RelayChainStateProof, + ) -> frame_support::inherent::CheckInherentsResult; +} + +/// Struct that always returns `Ok` on inherents check, needed for backwards-compatibility. +#[doc(hidden)] +pub struct DummyCheckInherents(sp_std::marker::PhantomData); + +#[allow(deprecated)] +impl CheckInherents for DummyCheckInherents { + fn check_inherents( + _: &Block, + _: &RelayChainStateProof, + ) -> frame_support::inherent::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + /// Something that should be informed about system related events. /// /// This includes events like [`on_validation_data`](Self::on_validation_data) that is being diff --git a/pallets/parachain-system/src/validate_block/implementation.rs b/pallets/parachain-system/src/validate_block/implementation.rs index 6945362394f..9f08819c5ac 100644 --- a/pallets/parachain-system/src/validate_block/implementation.rs +++ b/pallets/parachain-system/src/validate_block/implementation.rs @@ -70,7 +70,12 @@ fn with_externalities R, R>(f: F) -> R { /// ensuring that the final storage root matches the storage root in the header of the block. In the /// end we return back the [`ValidationResult`] with all the required information for the validator. #[doc(hidden)] -pub fn validate_block, PSC: crate::Config>( +pub fn validate_block< + B: BlockT, + E: ExecuteBlock, + PSC: crate::Config, + CI: crate::CheckInherents, +>( MemoryOptimizedValidationParams { block_data, parent_head, @@ -159,6 +164,28 @@ where sp_io::offchain_index::host_clear.replace_implementation(host_offchain_index_clear), ); + #[cfg(not(feature = "parameterized-consensus-hook"))] + run_with_externalities::(&backend, || { + let relay_chain_proof = crate::RelayChainStateProof::new( + PSC::SelfParaId::get(), + inherent_data.validation_data.relay_parent_storage_root, + inherent_data.relay_chain_state.clone(), + ) + .expect("Invalid relay chain state proof"); + + let res = CI::check_inherents(&block, &relay_chain_proof); + + if !res.ok() { + if log::log_enabled!(log::Level::Error) { + res.into_errors().for_each(|e| { + log::error!("Checking inherent with identifier `{:?}` failed", e.0) + }); + } + + panic!("Checking inherents failed"); + } + }); + run_with_externalities::(&backend, || { let head_data = HeadData(block.header().encode()); From 4387fdc29183da89ccf21deee81c03a0ab75fe2c Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Mon, 7 Aug 2023 23:44:43 +0400 Subject: [PATCH 3/4] revert to empty CheckInherents --- Cargo.lock | 5 - pallets/parachain-system/Cargo.toml | 4 +- .../parachain-system/proc-macro/Cargo.toml | 1 - .../parachain-system/proc-macro/src/lib.rs | 32 +---- pallets/parachain-system/src/lib.rs | 21 +-- .../src/validate_block/implementation.rs | 6 +- parachain-template/runtime/src/lib.rs | 13 ++ .../assets/asset-hub-kusama/src/lib.rs | 13 ++ .../assets/asset-hub-polkadot/src/lib.rs | 13 ++ .../assets/asset-hub-westend/src/lib.rs | 13 ++ .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 13 ++ .../bridge-hub-polkadot/src/lib.rs | 13 ++ .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 13 ++ .../collectives-polkadot/src/lib.rs | 13 ++ .../contracts/contracts-rococo/src/lib.rs | 13 ++ .../glutton/glutton-kusama/src/lib.rs | 13 ++ .../runtimes/starters/seedling/src/lib.rs | 13 ++ parachains/runtimes/starters/shell/src/lib.rs | 13 ++ parachains/runtimes/testing/penpal/src/lib.rs | 13 ++ .../testing/rococo-parachain/src/lib.rs | 13 ++ primitives/timestamp/Cargo.toml | 12 -- primitives/timestamp/src/lib.rs | 124 ------------------ test/runtime/src/lib.rs | 13 ++ 23 files changed, 213 insertions(+), 187 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fa16fc9a0db..e5ab529a67a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2882,16 +2882,11 @@ name = "cumulus-primitives-timestamp" version = "0.1.0" dependencies = [ "cumulus-primitives-core", - "cumulus-test-client", - "cumulus-test-relay-sproof-builder", "futures", "parity-scale-codec 3.6.4", - "sp-consensus", "sp-inherents", - "sp-runtime", "sp-std", "sp-timestamp", - "sp-tracing", ] [[package]] diff --git a/pallets/parachain-system/Cargo.toml b/pallets/parachain-system/Cargo.toml index 03f52a022fc..8990a848ad0 100644 --- a/pallets/parachain-system/Cargo.toml +++ b/pallets/parachain-system/Cargo.toml @@ -80,6 +80,4 @@ runtime-benchmarks = [ try-runtime = ["frame-support/try-runtime"] -parameterized-consensus-hook = [ - "cumulus-pallet-parachain-system-proc-macro/parameterized-consensus-hook", -] +parameterized-consensus-hook = [] diff --git a/pallets/parachain-system/proc-macro/Cargo.toml b/pallets/parachain-system/proc-macro/Cargo.toml index 7719b075817..8ff664b0284 100644 --- a/pallets/parachain-system/proc-macro/Cargo.toml +++ b/pallets/parachain-system/proc-macro/Cargo.toml @@ -17,4 +17,3 @@ proc-macro-crate = "1.3.1" [features] default = [ "std" ] std = [] -parameterized-consensus-hook = [] diff --git a/pallets/parachain-system/proc-macro/src/lib.rs b/pallets/parachain-system/proc-macro/src/lib.rs index 9d5ac12a898..d9d12a3f711 100644 --- a/pallets/parachain-system/proc-macro/src/lib.rs +++ b/pallets/parachain-system/proc-macro/src/lib.rs @@ -31,7 +31,7 @@ mod keywords { struct Input { runtime: Path, block_executor: Path, - check_inherents: Option, + check_inherents: Path, } impl Parse for Input { @@ -59,7 +59,11 @@ impl Parse for Input { } } - while !input.is_empty() || runtime.is_none() || block_executor.is_none() { + while !input.is_empty() || + runtime.is_none() || + block_executor.is_none() || + check_inherents.is_none() + { let lookahead = input.lookahead1(); if lookahead.peek(keywords::Runtime) { @@ -76,7 +80,7 @@ impl Parse for Input { Ok(Self { runtime: runtime.expect("Everything is parsed before; qed"), block_executor: block_executor.expect("Everything is parsed before; qed"), - check_inherents, + check_inherents: check_inherents.expect("Everything is parsed before; qed"), }) } } @@ -102,28 +106,6 @@ pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::To Err(e) => return e.into_compile_error().into(), }; - let check_inherents = match check_inherents { - Some(_check_inherents) => { - #[cfg(not(feature = "parameterized-consensus-hook"))] - quote::quote! { #_check_inherents } - #[cfg(feature = "parameterized-consensus-hook")] - return Error::new( - Span::call_site(), - "CheckInherents is incompatible with consensus hook feature", - ) - .into_compile_error() - .into() - }, - None => { - #[cfg(feature = "parameterized-consensus-hook")] - quote::quote! { #crate_::DummyCheckInherents<<#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock> } - #[cfg(not(feature = "parameterized-consensus-hook"))] - return Error::new(Span::call_site(), "missing CheckInherents input") - .into_compile_error() - .into() - }, - }; - if cfg!(not(feature = "std")) { quote::quote! { #[doc(hidden)] diff --git a/pallets/parachain-system/src/lib.rs b/pallets/parachain-system/src/lib.rs index cf63aee64da..1687ee091c2 100644 --- a/pallets/parachain-system/src/lib.rs +++ b/pallets/parachain-system/src/lib.rs @@ -86,10 +86,12 @@ pub use consensus_hook::{ConsensusHook, ExpectParentIncluded}; /// ``` /// struct BlockExecutor; /// struct Runtime; +/// struct CheckInherents; /// /// cumulus_pallet_parachain_system::register_validate_block! { /// Runtime = Runtime, /// BlockExecutor = Executive, +/// CheckInherents = CheckInherents, /// } /// /// # fn main() {} @@ -1498,7 +1500,10 @@ impl UpwardMessageSender for Pallet { } /// Something that can check the inherents of a block. -#[deprecated = "use `cumulus-pallet-aura-ext::FixedVelocityConsensusHook` instead"] +#[cfg_attr( + feature = "parameterized-consensus-hook", + deprecated = "consider switching to `cumulus-pallet-parachain-system::ConsensusHook`" +)] pub trait CheckInherents { /// Check all inherents of the block. /// @@ -1510,20 +1515,6 @@ pub trait CheckInherents { ) -> frame_support::inherent::CheckInherentsResult; } -/// Struct that always returns `Ok` on inherents check, needed for backwards-compatibility. -#[doc(hidden)] -pub struct DummyCheckInherents(sp_std::marker::PhantomData); - -#[allow(deprecated)] -impl CheckInherents for DummyCheckInherents { - fn check_inherents( - _: &Block, - _: &RelayChainStateProof, - ) -> frame_support::inherent::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - /// Something that should be informed about system related events. /// /// This includes events like [`on_validation_data`](Self::on_validation_data) that is being diff --git a/pallets/parachain-system/src/validate_block/implementation.rs b/pallets/parachain-system/src/validate_block/implementation.rs index 9f08819c5ac..4f07f2021c3 100644 --- a/pallets/parachain-system/src/validate_block/implementation.rs +++ b/pallets/parachain-system/src/validate_block/implementation.rs @@ -65,7 +65,10 @@ fn with_externalities R, R>(f: F) -> R { /// we have the in-memory database that contains all the values from the state of the parachain /// that we require to verify the block. /// -/// 5. The last step is to execute the entire block in the machinery we just have setup. Executing +/// 5. We are going to run `check_inherents`. This is important to check stuff like the timestamp +/// matching the real world time. +/// +/// 6. The last step is to execute the entire block in the machinery we just have setup. Executing /// the blocks include running all transactions in the block against our in-memory database and /// ensuring that the final storage root matches the storage root in the header of the block. In the /// end we return back the [`ValidationResult`] with all the required information for the validator. @@ -164,7 +167,6 @@ where sp_io::offchain_index::host_clear.replace_implementation(host_offchain_index_clear), ); - #[cfg(not(feature = "parameterized-consensus-hook"))] run_with_externalities::(&backend, || { let relay_chain_proof = crate::RelayChainStateProof::new( PSC::SelfParaId::get(), diff --git a/parachain-template/runtime/src/lib.rs b/parachain-template/runtime/src/lib.rs index e65bd25b7c5..92b2c5b5667 100644 --- a/parachain-template/runtime/src/lib.rs +++ b/parachain-template/runtime/src/lib.rs @@ -745,7 +745,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index 8334a91230d..e08f449fdbf 100644 --- a/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -1237,9 +1237,22 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } #[cfg(feature = "state-trie-version-1")] diff --git a/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index f422826ec91..d4c57a0447a 100644 --- a/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -1219,9 +1219,22 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } #[cfg(test)] diff --git a/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 0144ec479e1..0e0a28bc123 100644 --- a/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -1355,9 +1355,22 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } pub mod migrations { diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs index 773e972f2b5..48cea58a4d0 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -781,7 +781,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs index 2d231d0379d..76d07f0d3c0 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -781,7 +781,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 125af87b25d..5e7d0c05ebb 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -1219,9 +1219,22 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } #[cfg(test)] diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs b/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs index 39dc0bc269a..90eb99e2035 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs @@ -903,7 +903,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/contracts/contracts-rococo/src/lib.rs b/parachains/runtimes/contracts/contracts-rococo/src/lib.rs index e0aff91d54f..244404471f6 100644 --- a/parachains/runtimes/contracts/contracts-rococo/src/lib.rs +++ b/parachains/runtimes/contracts/contracts-rococo/src/lib.rs @@ -694,7 +694,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/glutton/glutton-kusama/src/lib.rs b/parachains/runtimes/glutton/glutton-kusama/src/lib.rs index 5e295dba468..9885b517dfc 100644 --- a/parachains/runtimes/glutton/glutton-kusama/src/lib.rs +++ b/parachains/runtimes/glutton/glutton-kusama/src/lib.rs @@ -400,7 +400,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/starters/seedling/src/lib.rs b/parachains/runtimes/starters/seedling/src/lib.rs index dff355195ed..a46f861b442 100644 --- a/parachains/runtimes/starters/seedling/src/lib.rs +++ b/parachains/runtimes/starters/seedling/src/lib.rs @@ -314,7 +314,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/starters/shell/src/lib.rs b/parachains/runtimes/starters/shell/src/lib.rs index 205462fac39..a56f252a581 100644 --- a/parachains/runtimes/starters/shell/src/lib.rs +++ b/parachains/runtimes/starters/shell/src/lib.rs @@ -344,7 +344,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/testing/penpal/src/lib.rs b/parachains/runtimes/testing/penpal/src/lib.rs index 8c5fc7f3f2f..f232cc2de00 100644 --- a/parachains/runtimes/testing/penpal/src/lib.rs +++ b/parachains/runtimes/testing/penpal/src/lib.rs @@ -831,7 +831,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/testing/rococo-parachain/src/lib.rs b/parachains/runtimes/testing/rococo-parachain/src/lib.rs index 71cb9a36f8c..9083430d82e 100644 --- a/parachains/runtimes/testing/rococo-parachain/src/lib.rs +++ b/parachains/runtimes/testing/rococo-parachain/src/lib.rs @@ -796,7 +796,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, + CheckInherents = CheckInherents, } diff --git a/primitives/timestamp/Cargo.toml b/primitives/timestamp/Cargo.toml index 254ab578b95..176f22fe5d3 100644 --- a/primitives/timestamp/Cargo.toml +++ b/primitives/timestamp/Cargo.toml @@ -17,18 +17,6 @@ sp-timestamp = { git = "https://github.com/paritytech/substrate", default-featur # Cumulus cumulus-primitives-core = { path = "../core", default-features = false } -[dev-dependencies] - -# Substrate -sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } -sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } -sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } - -# Cumulus -cumulus-test-client = { path = "../../test/client" } -cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder" } - - [features] default = [ "std" ] std = [ diff --git a/primitives/timestamp/src/lib.rs b/primitives/timestamp/src/lib.rs index 932656d9b0f..5af3ddf21dc 100644 --- a/primitives/timestamp/src/lib.rs +++ b/primitives/timestamp/src/lib.rs @@ -68,127 +68,3 @@ impl InherentDataProvider { inherent_data.put_data(INHERENT_IDENTIFIER, &data) } } - -#[cfg(test)] -mod tests { - use super::*; - - use codec::{Decode, Encode}; - use cumulus_primitives_core::{relay_chain::Hash as PHash, PersistedValidationData}; - use cumulus_test_client::{ - runtime::{Block, Header, WASM_BINARY}, - BlockData, BuildParachainBlockData, Client, ClientBlockImportExt, ExecutorResult, HeadData, - InitBlockBuilder, ParachainBlockData, TestClientBuilder, TestClientBuilderExt, - ValidationParams, - }; - use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; - use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; - use std::{env, process::Command, str::FromStr}; - - const SLOT_DURATION: u64 = 6000; - - fn call_validate_block( - parent_head: Header, - block_data: ParachainBlockData, - relay_parent_storage_root: PHash, - ) -> ExecutorResult
{ - cumulus_test_client::validate_block( - ValidationParams { - block_data: BlockData(block_data.encode()), - parent_head: HeadData(parent_head.encode()), - relay_parent_number: 1, - relay_parent_storage_root, - }, - WASM_BINARY.expect("You need to build the WASM binaries to run the tests!"), - ) - .map(|v| Header::decode(&mut &v.head_data.0[..]).expect("Decodes `Header`.")) - } - - fn build_block( - client: &Client, - hash: ::Hash, - timestamp: u64, - relay_chain_slot: Slot, - ) -> (ParachainBlockData, PHash) { - let sproof_builder = - RelayStateSproofBuilder { current_slot: relay_chain_slot, ..Default::default() }; - - let parent_header = client.header(hash).ok().flatten().expect("Genesis header exists"); - - let relay_parent_storage_root = sproof_builder.clone().into_state_root_and_proof().0; - - let validation_data = PersistedValidationData { - relay_parent_number: 1, - parent_head: parent_header.encode().into(), - ..Default::default() - }; - - let block = client - .init_block_builder_with_timestamp( - hash, - Some(validation_data), - sproof_builder, - timestamp, - ) - .build_parachain_block(*parent_header.state_root()); - - (block, relay_parent_storage_root) - } - - #[test] - fn check_timestamp_inherent_works() { - sp_tracing::try_init_simple(); - let relay_chain_slot = 2; - - if env::var("RUN_TEST").is_ok() { - let mut client = TestClientBuilder::default().build(); - let timestamp = u64::from_str(&env::var("TIMESTAMP").expect("TIMESTAMP is set")) - .expect("TIMESTAMP is a valid `u64`"); - - let block = - build_block(&client, client.chain_info().genesis_hash, SLOT_DURATION, 1.into()) - .0 - .into_block(); - futures::executor::block_on( - client.import(sp_consensus::BlockOrigin::Own, block.clone()), - ) - .unwrap(); - - let hashof1 = block.hash(); - let (block, relay_chain_root) = - build_block(&client, hashof1, timestamp, relay_chain_slot.into()); - - let header = call_validate_block( - client.header(hashof1).ok().flatten().expect("Genesis header exists"), - block.clone(), - relay_chain_root, - ) - .expect("Calls validate block"); - assert_eq!(block.header(), &header); - } else { - let slot_timestamp = relay_chain_slot * SLOT_DURATION; - - for (timestamp, res) in &[ - (slot_timestamp, true), - (slot_timestamp - 500, true), - (slot_timestamp + 500, true), - (slot_timestamp * 10, false), - ] { - let output = Command::new(env::current_exe().unwrap()) - .args(["check_timestamp_inherent_works", "--", "--nocapture"]) - .env("RUN_TEST", "1") - .env("TIMESTAMP", timestamp.to_string()) - .output() - .expect("Runs the test"); - - if !res { - assert!(String::from_utf8(output.stderr) - .unwrap() - .contains("Checking inherents failed")); - } - - assert!(dbg!(output.status.success()) == *res); - } - } - } -} diff --git a/test/runtime/src/lib.rs b/test/runtime/src/lib.rs index 28684db67a7..92bbf1fcbdd 100644 --- a/test/runtime/src/lib.rs +++ b/test/runtime/src/lib.rs @@ -474,7 +474,20 @@ impl_runtime_apis! { } } +struct CheckInherents; + +#[allow(deprecated)] +impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { + fn check_inherents( + _: &Block, + _: &cumulus_pallet_parachain_system::RelayChainStateProof, + ) -> sp_inherents::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, + CheckInherents = CheckInherents, } From 5ce60e52253c41857fa673be76ab25cab0a66daf Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Tue, 8 Aug 2023 00:23:22 +0400 Subject: [PATCH 4/4] make CheckInherents optional --- .../parachain-system/proc-macro/src/lib.rs | 21 ++++++++++++------- pallets/parachain-system/src/lib.rs | 14 +++++++++++++ parachain-template/runtime/src/lib.rs | 13 ------------ .../assets/asset-hub-kusama/src/lib.rs | 13 ------------ .../assets/asset-hub-polkadot/src/lib.rs | 13 ------------ .../assets/asset-hub-westend/src/lib.rs | 13 ------------ .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 13 ------------ .../bridge-hub-polkadot/src/lib.rs | 13 ------------ .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 13 ------------ .../collectives-polkadot/src/lib.rs | 13 ------------ .../contracts/contracts-rococo/src/lib.rs | 13 ------------ .../glutton/glutton-kusama/src/lib.rs | 13 ------------ .../runtimes/starters/seedling/src/lib.rs | 13 ------------ parachains/runtimes/starters/shell/src/lib.rs | 13 ------------ parachains/runtimes/testing/penpal/src/lib.rs | 13 ------------ .../testing/rococo-parachain/src/lib.rs | 13 ------------ test/runtime/src/lib.rs | 13 ------------ 17 files changed, 28 insertions(+), 202 deletions(-) diff --git a/pallets/parachain-system/proc-macro/src/lib.rs b/pallets/parachain-system/proc-macro/src/lib.rs index d9d12a3f711..70c6857120c 100644 --- a/pallets/parachain-system/proc-macro/src/lib.rs +++ b/pallets/parachain-system/proc-macro/src/lib.rs @@ -31,7 +31,7 @@ mod keywords { struct Input { runtime: Path, block_executor: Path, - check_inherents: Path, + check_inherents: Option, } impl Parse for Input { @@ -59,11 +59,7 @@ impl Parse for Input { } } - while !input.is_empty() || - runtime.is_none() || - block_executor.is_none() || - check_inherents.is_none() - { + while !input.is_empty() || runtime.is_none() || block_executor.is_none() { let lookahead = input.lookahead1(); if lookahead.peek(keywords::Runtime) { @@ -80,7 +76,7 @@ impl Parse for Input { Ok(Self { runtime: runtime.expect("Everything is parsed before; qed"), block_executor: block_executor.expect("Everything is parsed before; qed"), - check_inherents: check_inherents.expect("Everything is parsed before; qed"), + check_inherents, }) } } @@ -106,6 +102,17 @@ pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::To Err(e) => return e.into_compile_error().into(), }; + let check_inherents = match check_inherents { + Some(_check_inherents) => { + quote::quote! { #_check_inherents } + }, + None => { + quote::quote! { + #crate_::DummyCheckInherents<<#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock> + } + }, + }; + if cfg!(not(feature = "std")) { quote::quote! { #[doc(hidden)] diff --git a/pallets/parachain-system/src/lib.rs b/pallets/parachain-system/src/lib.rs index 1687ee091c2..648e6e90cd4 100644 --- a/pallets/parachain-system/src/lib.rs +++ b/pallets/parachain-system/src/lib.rs @@ -1515,6 +1515,20 @@ pub trait CheckInherents { ) -> frame_support::inherent::CheckInherentsResult; } +/// Struct that always returns `Ok` on inherents check, needed for backwards-compatibility. +#[doc(hidden)] +pub struct DummyCheckInherents(sp_std::marker::PhantomData); + +#[allow(deprecated)] +impl CheckInherents for DummyCheckInherents { + fn check_inherents( + _: &Block, + _: &RelayChainStateProof, + ) -> frame_support::inherent::CheckInherentsResult { + sp_inherents::CheckInherentsResult::new() + } +} + /// Something that should be informed about system related events. /// /// This includes events like [`on_validation_data`](Self::on_validation_data) that is being diff --git a/parachain-template/runtime/src/lib.rs b/parachain-template/runtime/src/lib.rs index 92b2c5b5667..e65bd25b7c5 100644 --- a/parachain-template/runtime/src/lib.rs +++ b/parachain-template/runtime/src/lib.rs @@ -745,20 +745,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index e08f449fdbf..8334a91230d 100644 --- a/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -1237,22 +1237,9 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } #[cfg(feature = "state-trie-version-1")] diff --git a/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index d4c57a0447a..f422826ec91 100644 --- a/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -1219,22 +1219,9 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } #[cfg(test)] diff --git a/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 0e0a28bc123..0144ec479e1 100644 --- a/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -1355,22 +1355,9 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } pub mod migrations { diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs index 48cea58a4d0..773e972f2b5 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -781,20 +781,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs index 76d07f0d3c0..2d231d0379d 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -781,20 +781,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 5e7d0c05ebb..125af87b25d 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -1219,22 +1219,9 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } #[cfg(test)] diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs b/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs index 90eb99e2035..39dc0bc269a 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs @@ -903,20 +903,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/contracts/contracts-rococo/src/lib.rs b/parachains/runtimes/contracts/contracts-rococo/src/lib.rs index 244404471f6..e0aff91d54f 100644 --- a/parachains/runtimes/contracts/contracts-rococo/src/lib.rs +++ b/parachains/runtimes/contracts/contracts-rococo/src/lib.rs @@ -694,20 +694,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/glutton/glutton-kusama/src/lib.rs b/parachains/runtimes/glutton/glutton-kusama/src/lib.rs index 9885b517dfc..5e295dba468 100644 --- a/parachains/runtimes/glutton/glutton-kusama/src/lib.rs +++ b/parachains/runtimes/glutton/glutton-kusama/src/lib.rs @@ -400,20 +400,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/starters/seedling/src/lib.rs b/parachains/runtimes/starters/seedling/src/lib.rs index a46f861b442..dff355195ed 100644 --- a/parachains/runtimes/starters/seedling/src/lib.rs +++ b/parachains/runtimes/starters/seedling/src/lib.rs @@ -314,20 +314,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/starters/shell/src/lib.rs b/parachains/runtimes/starters/shell/src/lib.rs index a56f252a581..205462fac39 100644 --- a/parachains/runtimes/starters/shell/src/lib.rs +++ b/parachains/runtimes/starters/shell/src/lib.rs @@ -344,20 +344,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/testing/penpal/src/lib.rs b/parachains/runtimes/testing/penpal/src/lib.rs index f232cc2de00..8c5fc7f3f2f 100644 --- a/parachains/runtimes/testing/penpal/src/lib.rs +++ b/parachains/runtimes/testing/penpal/src/lib.rs @@ -831,20 +831,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/parachains/runtimes/testing/rococo-parachain/src/lib.rs b/parachains/runtimes/testing/rococo-parachain/src/lib.rs index 9083430d82e..71cb9a36f8c 100644 --- a/parachains/runtimes/testing/rococo-parachain/src/lib.rs +++ b/parachains/runtimes/testing/rococo-parachain/src/lib.rs @@ -796,20 +796,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, - CheckInherents = CheckInherents, } diff --git a/test/runtime/src/lib.rs b/test/runtime/src/lib.rs index 92bbf1fcbdd..28684db67a7 100644 --- a/test/runtime/src/lib.rs +++ b/test/runtime/src/lib.rs @@ -474,20 +474,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -#[allow(deprecated)] -impl cumulus_pallet_parachain_system::CheckInherents for CheckInherents { - fn check_inherents( - _: &Block, - _: &cumulus_pallet_parachain_system::RelayChainStateProof, - ) -> sp_inherents::CheckInherentsResult { - sp_inherents::CheckInherentsResult::new() - } -} - cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = Executive, - CheckInherents = CheckInherents, }