sc_service/chain_ops/
check_block.rs1use 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
28pub 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}