referrerpolicy=no-referrer-when-downgrade

cumulus_client_consensus_relay_chain/
import_queue.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5// Cumulus is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9
10// Cumulus is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with Cumulus. If not, see <https://www.gnu.org/licenses/>.
17
18use std::{marker::PhantomData, sync::Arc};
19
20use sc_consensus::{import_queue::Verifier as VerifierT, BlockImportParams};
21use sp_api::ProvideRuntimeApi;
22use sp_block_builder::BlockBuilder as BlockBuilderApi;
23use sp_inherents::CreateInherentDataProviders;
24use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
25
26/// A verifier that just checks the inherents.
27pub struct Verifier<Client, Block, CIDP> {
28	client: Arc<Client>,
29	create_inherent_data_providers: CIDP,
30	_marker: PhantomData<Block>,
31}
32
33impl<Client, Block, CIDP> Verifier<Client, Block, CIDP> {
34	/// Create a new instance.
35	pub fn new(client: Arc<Client>, create_inherent_data_providers: CIDP) -> Self {
36		Self { client, create_inherent_data_providers, _marker: PhantomData }
37	}
38}
39
40#[async_trait::async_trait]
41impl<Client, Block, CIDP> VerifierT<Block> for Verifier<Client, Block, CIDP>
42where
43	Block: BlockT,
44	Client: ProvideRuntimeApi<Block> + Send + Sync,
45	<Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>,
46	CIDP: CreateInherentDataProviders<Block, ()>,
47{
48	async fn verify(
49		&self,
50		mut block_params: BlockImportParams<Block>,
51	) -> Result<BlockImportParams<Block>, String> {
52		block_params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom(
53			block_params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
54		));
55
56		// Skip checks that include execution, if being told so, or when importing only state.
57		//
58		// This is done for example when gap syncing and it is expected that the block after the gap
59		// was checked/chosen properly, e.g. by warp syncing to this block using a finality proof.
60		if block_params.state_action.skip_execution_checks() || block_params.with_state() {
61			return Ok(block_params)
62		}
63
64		if let Some(inner_body) = block_params.body.take() {
65			let inherent_data_providers = self
66				.create_inherent_data_providers
67				.create_inherent_data_providers(*block_params.header.parent_hash(), ())
68				.await
69				.map_err(|e| e.to_string())?;
70
71			let block = Block::new(block_params.header.clone(), inner_body);
72			sp_block_builder::check_inherents(
73				self.client.clone(),
74				*block.header().parent_hash(),
75				block.clone(),
76				&inherent_data_providers,
77			)
78			.await
79			.map_err(|e| format!("Error checking block inherents {:?}", e))?;
80
81			let (_, inner_body) = block.deconstruct();
82			block_params.body = Some(inner_body);
83		}
84
85		block_params.post_hash = Some(block_params.header.hash());
86		Ok(block_params)
87	}
88}