referrerpolicy=no-referrer-when-downgrade

sc_service/chain_ops/
check_block.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::error::Error;
20use codec::Encode;
21use sc_client_api::{BlockBackend, HeaderBackend};
22use sc_consensus::import_queue::ImportQueue;
23use sp_runtime::{generic::BlockId, traits::Block as BlockT};
24
25use crate::chain_ops::import_blocks;
26use std::sync::Arc;
27
28/// Re-validate known block.
29pub async fn check_block<B, IQ, C>(
30	client: Arc<C>,
31	import_queue: IQ,
32	block_id: BlockId<B>,
33) -> Result<(), Error>
34where
35	C: BlockBackend<B> + HeaderBackend<B> + Send + Sync + 'static,
36	B: BlockT + for<'de> serde::Deserialize<'de>,
37	IQ: ImportQueue<B> + 'static,
38{
39	let maybe_block = client
40		.block_hash_from_id(&block_id)?
41		.map(|hash| client.block(hash))
42		.transpose()?
43		.flatten();
44	match maybe_block {
45		Some(block) => {
46			let mut buf = Vec::new();
47			1u64.encode_to(&mut buf);
48			block.encode_to(&mut buf);
49			let reader = std::io::Cursor::new(buf);
50			import_blocks(client, import_queue, reader, true, true).await
51		},
52		None => Err("Unknown block")?,
53	}
54}