Skip to content

Commit 48f94de

Browse files
bkchrgithub-actions[bot]
authored andcommitted
Remove the deprecated CheckInherents logic (#9732)
This removes the `CheckInherents` logic from `register_validate_block`, instead the `ConsensusHook` should be used to verify certain data. Besides that it also changes the how `validate_block` verifies the `ValidationData` passed by the collator. Instead of extracting the `ValidationData` before executing the block, the validation data is now checked as part of the pallet logic. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 02bd53c commit 48f94de

File tree

6 files changed

+43
-170
lines changed

6 files changed

+43
-170
lines changed

cumulus/pallets/aura-ext/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
//! ```
2626
//! # struct Runtime;
2727
//! # struct Executive;
28-
//! # struct CheckInherents;
2928
//! cumulus_pallet_parachain_system::register_validate_block! {
3029
//! Runtime = Runtime,
3130
//! BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,

cumulus/pallets/parachain-system/proc-macro/src/lib.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ mod keywords {
3131
struct Input {
3232
runtime: Path,
3333
block_executor: Path,
34-
check_inherents: Option<Path>,
3534
}
3635

3736
impl Parse for Input {
3837
fn parse(input: ParseStream) -> Result<Self, Error> {
3938
let mut runtime = None;
4039
let mut block_executor = None;
41-
let mut check_inherents = None;
4240

4341
fn parse_inner<KW: Parse + Spanned>(
4442
input: ParseStream,
@@ -67,7 +65,7 @@ impl Parse for Input {
6765
} else if lookahead.peek(keywords::BlockExecutor) {
6866
parse_inner::<keywords::BlockExecutor>(input, &mut block_executor)?;
6967
} else if lookahead.peek(keywords::CheckInherents) {
70-
parse_inner::<keywords::CheckInherents>(input, &mut check_inherents)?;
68+
return Err(Error::new(input.span(), "`CheckInherents` is not supported anymore!"));
7169
} else {
7270
return Err(lookahead.error())
7371
}
@@ -76,7 +74,6 @@ impl Parse for Input {
7674
Ok(Self {
7775
runtime: runtime.expect("Everything is parsed before; qed"),
7876
block_executor: block_executor.expect("Everything is parsed before; qed"),
79-
check_inherents,
8077
})
8178
}
8279
}
@@ -92,7 +89,7 @@ fn crate_() -> Result<Ident, Error> {
9289

9390
#[proc_macro]
9491
pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
95-
let Input { runtime, block_executor, check_inherents } = match syn::parse(input) {
92+
let Input { runtime, block_executor } = match syn::parse(input) {
9693
Ok(t) => t,
9794
Err(e) => return e.into_compile_error().into(),
9895
};
@@ -102,17 +99,6 @@ pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::To
10299
Err(e) => return e.into_compile_error().into(),
103100
};
104101

105-
let check_inherents = match check_inherents {
106-
Some(_check_inherents) => {
107-
quote::quote! { #_check_inherents }
108-
},
109-
None => {
110-
quote::quote! {
111-
#crate_::DummyCheckInherents<<#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock>
112-
}
113-
},
114-
};
115-
116102
if cfg!(not(feature = "std")) {
117103
quote::quote! {
118104
#[doc(hidden)]
@@ -139,7 +125,6 @@ pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::To
139125
<#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock,
140126
#block_executor,
141127
#runtime,
142-
#check_inherents,
143128
>(params);
144129

145130
#crate_::validate_block::polkadot_parachain_primitives::write_result(&res)

cumulus/pallets/parachain-system/src/lib.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use polkadot_parachain_primitives::primitives::RelayChainBlockNumber;
5555
use polkadot_runtime_parachains::{FeeTracker, GetMinFeeFactor};
5656
use scale_info::TypeInfo;
5757
use sp_runtime::{
58-
traits::{Block as BlockT, BlockNumberProvider, Hash},
58+
traits::{BlockNumberProvider, Hash},
5959
FixedU128, RuntimeDebug, SaturatedConversion,
6060
};
6161
use xcm::{latest::XcmHash, VersionedLocation, VersionedXcm, MAX_XCM_DECODE_DEPTH};
@@ -96,12 +96,10 @@ pub use consensus_hook::{ConsensusHook, ExpectParentIncluded};
9696
/// ```
9797
/// struct BlockExecutor;
9898
/// struct Runtime;
99-
/// struct CheckInherents;
10099
///
101100
/// cumulus_pallet_parachain_system::register_validate_block! {
102101
/// Runtime = Runtime,
103102
/// BlockExecutor = Executive,
104-
/// CheckInherents = CheckInherents,
105103
/// }
106104
///
107105
/// # fn main() {}
@@ -798,8 +796,8 @@ pub mod pallet {
798796
pub type NewValidationCode<T: Config> = StorageValue<_, Vec<u8>, OptionQuery>;
799797

800798
/// The [`PersistedValidationData`] set for this block.
801-
/// This value is expected to be set only once per block and it's never stored
802-
/// in the trie.
799+
///
800+
/// This value is expected to be set only once by the [`Pallet::set_validation_data`] inherent.
803801
#[pallet::storage]
804802
pub type ValidationData<T: Config> = StorageValue<_, PersistedValidationData>;
805803

@@ -1752,34 +1750,6 @@ impl<T: Config> polkadot_runtime_parachains::EnsureForParachain for Pallet<T> {
17521750
}
17531751
}
17541752

1755-
/// Something that can check the inherents of a block.
1756-
#[deprecated(note = "This trait is deprecated and will be removed by September 2024. \
1757-
Consider switching to `cumulus-pallet-parachain-system::ConsensusHook`")]
1758-
pub trait CheckInherents<Block: BlockT> {
1759-
/// Check all inherents of the block.
1760-
///
1761-
/// This function gets passed all the extrinsics of the block, so it is up to the callee to
1762-
/// identify the inherents. The `validation_data` can be used to access the
1763-
fn check_inherents(
1764-
block: &Block,
1765-
validation_data: &RelayChainStateProof,
1766-
) -> frame_support::inherent::CheckInherentsResult;
1767-
}
1768-
1769-
/// Struct that always returns `Ok` on inherents check, needed for backwards-compatibility.
1770-
#[doc(hidden)]
1771-
pub struct DummyCheckInherents<Block>(core::marker::PhantomData<Block>);
1772-
1773-
#[allow(deprecated)]
1774-
impl<Block: BlockT> CheckInherents<Block> for DummyCheckInherents<Block> {
1775-
fn check_inherents(
1776-
_: &Block,
1777-
_: &RelayChainStateProof,
1778-
) -> frame_support::inherent::CheckInherentsResult {
1779-
sp_inherents::CheckInherentsResult::new()
1780-
}
1781-
}
1782-
17831753
/// Something that should be informed about system related events.
17841754
///
17851755
/// This includes events like [`on_validation_data`](Self::on_validation_data) that is being

cumulus/pallets/parachain-system/src/validate_block/implementation.rs

Lines changed: 22 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,22 @@
1717
//! The actual implementation of the validate block functionality.
1818
1919
use super::{trie_cache, trie_recorder, MemoryOptimizedValidationParams};
20-
use crate::parachain_inherent::BasicParachainInherentData;
2120
use alloc::vec::Vec;
2221
use codec::{Decode, Encode};
2322
use cumulus_primitives_core::{
24-
relay_chain::{Hash as RHash, UMPSignal, UMP_SEPARATOR},
23+
relay_chain::{BlockNumber as RNumber, Hash as RHash, UMPSignal, UMP_SEPARATOR},
2524
ClaimQueueOffset, CoreSelector, ParachainBlockData, PersistedValidationData,
2625
};
2726
use frame_support::{
2827
traits::{ExecuteBlock, Get, IsSubType},
2928
BoundedVec,
3029
};
31-
use polkadot_parachain_primitives::primitives::{
32-
HeadData, RelayChainBlockNumber, ValidationResult,
33-
};
30+
use polkadot_parachain_primitives::primitives::{HeadData, ValidationResult};
3431
use sp_core::storage::{ChildInfo, StateVersion};
3532
use sp_externalities::{set_and_run_with_externalities, Externalities};
3633
use sp_io::{hashing::blake2_128, KillStorageResult};
3734
use sp_runtime::traits::{
38-
Block as BlockT, ExtrinsicCall, ExtrinsicLike, Hash as HashT, HashingFor, Header as HeaderT,
35+
Block as BlockT, ExtrinsicCall, Hash as HashT, HashingFor, Header as HeaderT,
3936
};
4037
use sp_state_machine::OverlayedChanges;
4138
use sp_trie::{HashDBT, ProofSizeProvider, EMPTY_PREFIX};
@@ -69,21 +66,12 @@ environmental::environmental!(recorder: trait ProofSizeProvider);
6966
/// we have the in-memory database that contains all the values from the state of the parachain
7067
/// that we require to verify the block.
7168
///
72-
/// 5. We are going to run `check_inherents`. This is important to check stuff like the timestamp
73-
/// matching the real world time.
74-
///
75-
/// 6. The last step is to execute the entire block in the machinery we just have setup. Executing
69+
/// 5. The last step is to execute the entire block in the machinery we just have setup. Executing
7670
/// the blocks include running all transactions in the block against our in-memory database and
7771
/// ensuring that the final storage root matches the storage root in the header of the block. In the
7872
/// end we return back the [`ValidationResult`] with all the required information for the validator.
7973
#[doc(hidden)]
80-
#[allow(deprecated)]
81-
pub fn validate_block<
82-
B: BlockT,
83-
E: ExecuteBlock<B>,
84-
PSC: crate::Config,
85-
CI: crate::CheckInherents<B>,
86-
>(
74+
pub fn validate_block<B: BlockT, E: ExecuteBlock<B>, PSC: crate::Config>(
8775
MemoryOptimizedValidationParams {
8876
block_data,
8977
parent_head: parachain_head,
@@ -205,49 +193,13 @@ where
205193
let mut overlay = OverlayedChanges::default();
206194

207195
parent_header = block.header().clone();
208-
let inherent_data = extract_parachain_inherent_data(&block);
209-
210-
validate_validation_data(
211-
&inherent_data.validation_data,
212-
relay_parent_number,
213-
relay_parent_storage_root,
214-
&parachain_head,
215-
);
216-
217-
// We don't need the recorder or the overlay in here.
218-
run_with_externalities_and_recorder::<B, _, _>(
219-
&backend,
220-
&mut Default::default(),
221-
&mut Default::default(),
222-
|| {
223-
let relay_chain_proof = crate::RelayChainStateProof::new(
224-
PSC::SelfParaId::get(),
225-
inherent_data.validation_data.relay_parent_storage_root,
226-
inherent_data.relay_chain_state.clone(),
227-
)
228-
.expect("Invalid relay chain state proof");
229-
230-
#[allow(deprecated)]
231-
let res = CI::check_inherents(&block, &relay_chain_proof);
232-
233-
if !res.ok() {
234-
if log::log_enabled!(log::Level::Error) {
235-
res.into_errors().for_each(|e| {
236-
log::error!("Checking inherent with identifier `{:?}` failed", e.0)
237-
});
238-
}
239-
240-
panic!("Checking inherents failed");
241-
}
242-
},
243-
);
244196

245197
run_with_externalities_and_recorder::<B, _, _>(
246198
&execute_backend,
247199
// Here is the only place where we want to use the recorder.
248-
// We want to ensure that we not accidentally read something from the proof, that was
249-
// not yet read and thus, alter the proof size. Otherwise we end up with mismatches in
250-
// later blocks.
200+
// We want to ensure that we not accidentally read something from the proof, that
201+
// was not yet read and thus, alter the proof size. Otherwise, we end up with
202+
// mismatches in later blocks.
251203
&mut execute_recorder,
252204
&mut overlay,
253205
|| {
@@ -262,6 +214,15 @@ where
262214
// are passing here the overlay.
263215
&mut overlay,
264216
|| {
217+
// Ensure the validation data is correct.
218+
validate_validation_data(
219+
crate::ValidationData::<PSC>::get()
220+
.expect("`ValidationData` must be set after executing a block; qed"),
221+
&parachain_head,
222+
relay_parent_number,
223+
relay_parent_storage_root,
224+
);
225+
265226
new_validation_code =
266227
new_validation_code.take().or(crate::NewValidationCode::<PSC>::get());
267228

@@ -382,35 +343,14 @@ where
382343
}
383344
}
384345

385-
/// Extract the [`BasicParachainInherentData`].
386-
fn extract_parachain_inherent_data<B: BlockT, PSC: crate::Config>(
387-
block: &B,
388-
) -> &BasicParachainInherentData
389-
where
390-
B::Extrinsic: ExtrinsicCall,
391-
<B::Extrinsic as ExtrinsicCall>::Call: IsSubType<crate::Call<PSC>>,
392-
{
393-
block
394-
.extrinsics()
395-
.iter()
396-
// Inherents are at the front of the block and are unsigned.
397-
.take_while(|e| e.is_bare())
398-
.filter_map(|e| e.call().is_sub_type())
399-
.find_map(|c| match c {
400-
crate::Call::set_validation_data { data: validation_data, .. } => Some(validation_data),
401-
_ => None,
402-
})
403-
.expect("Could not find `set_validation_data` inherent")
404-
}
405-
406-
/// Validate the given [`PersistedValidationData`] against the [`MemoryOptimizedValidationParams`].
346+
/// Validates the given [`PersistedValidationData`] against the data from the relay chain.
407347
fn validate_validation_data(
408-
validation_data: &PersistedValidationData,
409-
relay_parent_number: RelayChainBlockNumber,
348+
validation_data: PersistedValidationData,
349+
parent_header: &[u8],
350+
relay_parent_number: RNumber,
410351
relay_parent_storage_root: RHash,
411-
parent_head: &[u8],
412352
) {
413-
assert_eq!(parent_head, validation_data.parent_head.0, "Parent head doesn't match");
353+
assert_eq!(parent_header, &validation_data.parent_head.0, "Parent head doesn't match");
414354
assert_eq!(
415355
relay_parent_number, validation_data.relay_parent_number,
416356
"Relay parent number doesn't match",

cumulus/pallets/parachain-system/src/validate_block/tests.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ fn validate_block_fails_on_invalid_validation_data() {
417417
Default::default(),
418418
);
419419

420+
let block = seal_parachain_block_data(block, &client);
421+
420422
call_validate_block(parent_head, block, Hash::random()).unwrap_err();
421423
} else {
422424
let output = Command::new(env::current_exe().unwrap())
@@ -431,43 +433,6 @@ fn validate_block_fails_on_invalid_validation_data() {
431433
}
432434
}
433435

434-
#[test]
435-
fn check_inherents_are_unsigned_and_before_all_other_extrinsics() {
436-
sp_tracing::try_init_simple();
437-
438-
if env::var("RUN_TEST").is_ok() {
439-
let (client, parent_head) = create_test_client();
440-
441-
let TestBlockData { mut block, validation_data, .. } = build_block_with_witness(
442-
&client,
443-
Vec::new(),
444-
parent_head.clone(),
445-
Default::default(),
446-
Default::default(),
447-
);
448-
449-
block.blocks_mut()[0].extrinsics.insert(0, transfer(&client, Alice, Bob, 69));
450-
451-
call_validate_block(parent_head, block, validation_data.relay_parent_storage_root)
452-
.unwrap_err();
453-
} else {
454-
let output = Command::new(env::current_exe().unwrap())
455-
.args([
456-
"check_inherents_are_unsigned_and_before_all_other_extrinsics",
457-
"--",
458-
"--nocapture",
459-
])
460-
.env("RUN_TEST", "1")
461-
.output()
462-
.expect("Runs the test");
463-
assert!(output.status.success());
464-
465-
assert!(String::from_utf8(output.stderr)
466-
.unwrap()
467-
.contains("Could not find `set_validation_data` inherent"));
468-
}
469-
}
470-
471436
/// Test that ensures that `ValidationParams` and `MemoryOptimizedValidationParams`
472437
/// are encoding/decoding.
473438
#[test]

prdoc/pr_9732.prdoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
title: Remove the deprecated `CheckInherents` logic
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
This removes the `CheckInherents` logic from `register_validate_block`, instead the `ConsensusHook` should be used to verify certain data.
6+
7+
Besides that it also changes the how `validate_block` verifies the `ValidationData` passed by the collator. Instead of extracting the `ValidationData` before executing the block, the validation data is now checked as part of the pallet logic.
8+
crates:
9+
- name: cumulus-pallet-aura-ext
10+
bump: major
11+
- name: cumulus-pallet-parachain-system-proc-macro
12+
bump: major
13+
- name: cumulus-pallet-parachain-system
14+
bump: major

0 commit comments

Comments
 (0)