BlockId removal: refactor: BlockBackend::block|block_status (#13014)

* BlockId removal: refactor: BlockBackend::block|block_status

It changes the arguments of:
-  `BlockBackend::block`
-  `BlockBackend::block_status`

method from: `BlockId<Block>` to: `Block::Hash`

This PR is part of BlockId::Number refactoring analysis (paritytech/substrate#11292)

* non-obvious reworks

* doc fix

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: parity-processbot <>
This commit is contained in:
Michal Kucharczyk
2023-01-02 10:42:05 +01:00
committed by GitHub
parent 66cfd01f17
commit 9c69fc1b32
12 changed files with 75 additions and 101 deletions
@@ -18,34 +18,37 @@
use crate::error::Error;
use codec::Encode;
use futures::{future, prelude::*};
use sc_client_api::{BlockBackend, HeaderBackend};
use sc_consensus::import_queue::ImportQueue;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use crate::chain_ops::import_blocks;
use std::{pin::Pin, sync::Arc};
use std::sync::Arc;
/// Re-validate known block.
pub fn check_block<B, IQ, C>(
pub async fn check_block<B, IQ, C>(
client: Arc<C>,
import_queue: IQ,
block_id: BlockId<B>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>
) -> Result<(), Error>
where
C: BlockBackend<B> + HeaderBackend<B> + Send + Sync + 'static,
B: BlockT + for<'de> serde::Deserialize<'de>,
IQ: ImportQueue<B> + 'static,
{
match client.block(&block_id) {
Ok(Some(block)) => {
let maybe_block = client
.block_hash_from_id(&block_id)?
.map(|hash| client.block(hash))
.transpose()?
.flatten();
match maybe_block {
Some(block) => {
let mut buf = Vec::new();
1u64.encode_to(&mut buf);
block.encode_to(&mut buf);
let reader = std::io::Cursor::new(buf);
import_blocks(client, import_queue, reader, true, true)
import_blocks(client, import_queue, reader, true, true).await
},
Ok(None) => Box::pin(future::err("Unknown block".into())),
Err(e) => Box::pin(future::err(format!("Error reading block: {}", e).into())),
None => Err("Unknown block")?,
}
}
@@ -25,7 +25,7 @@ use sp_runtime::{
traits::{Block as BlockT, NumberFor, One, SaturatedConversion, Zero},
};
use sc_client_api::{BlockBackend, UsageProvider};
use sc_client_api::{BlockBackend, HeaderBackend, UsageProvider};
use std::{io::Write, pin::Pin, sync::Arc, task::Poll};
/// Performs the blocks export.
@@ -37,7 +37,7 @@ pub fn export_blocks<B, C>(
binary: bool,
) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>
where
C: BlockBackend<B> + UsageProvider<B> + 'static,
C: HeaderBackend<B> + BlockBackend<B> + UsageProvider<B> + 'static,
B: BlockT,
{
let mut block = from;
@@ -75,7 +75,12 @@ where
wrote_header = true;
}
match client.block(&BlockId::number(block))? {
match client
.block_hash_from_id(&BlockId::number(block))?
.map(|hash| client.block(hash))
.transpose()?
.flatten()
{
Some(block) =>
if binary {
output.write_all(&block.encode())?;
@@ -83,7 +88,6 @@ where
serde_json::to_writer(&mut output, &block)
.map_err(|e| format!("Error writing JSON: {}", e))?;
},
// Reached end of the chain.
None => return Poll::Ready(Ok(())),
}
if (block % 10000u32.into()).is_zero() {