mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 07:51:03 +00:00
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:
committed by
GitHub
parent
66cfd01f17
commit
9c69fc1b32
@@ -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() {
|
||||
|
||||
@@ -784,7 +784,8 @@ where
|
||||
let parent_hash = import_block.header.parent_hash();
|
||||
let at = BlockId::Hash(*parent_hash);
|
||||
let state_action = std::mem::replace(&mut import_block.state_action, StateAction::Skip);
|
||||
let (enact_state, storage_changes) = match (self.block_status(&at)?, state_action) {
|
||||
let (enact_state, storage_changes) = match (self.block_status(*parent_hash)?, state_action)
|
||||
{
|
||||
(BlockStatus::KnownBad, _) =>
|
||||
return Ok(PrepareStorageChangesResult::Discard(ImportResult::KnownBad)),
|
||||
(
|
||||
@@ -1025,17 +1026,18 @@ where
|
||||
}
|
||||
|
||||
/// Get block status.
|
||||
pub fn block_status(&self, id: &BlockId<Block>) -> sp_blockchain::Result<BlockStatus> {
|
||||
pub fn block_status(&self, hash: Block::Hash) -> sp_blockchain::Result<BlockStatus> {
|
||||
// this can probably be implemented more efficiently
|
||||
if let BlockId::Hash(ref h) = id {
|
||||
if self.importing_block.read().as_ref().map_or(false, |importing| h == importing) {
|
||||
return Ok(BlockStatus::Queued)
|
||||
}
|
||||
if self
|
||||
.importing_block
|
||||
.read()
|
||||
.as_ref()
|
||||
.map_or(false, |importing| &hash == importing)
|
||||
{
|
||||
return Ok(BlockStatus::Queued)
|
||||
}
|
||||
let hash_and_number = match *id {
|
||||
BlockId::Hash(hash) => self.backend.blockchain().number(hash)?.map(|n| (hash, n)),
|
||||
BlockId::Number(n) => self.backend.blockchain().hash(n)?.map(|hash| (hash, n)),
|
||||
};
|
||||
|
||||
let hash_and_number = self.backend.blockchain().number(hash)?.map(|n| (hash, n));
|
||||
match hash_and_number {
|
||||
Some((hash, number)) =>
|
||||
if self.backend.have_state_at(hash, number) {
|
||||
@@ -1779,7 +1781,7 @@ where
|
||||
// Own status must be checked first. If the block and ancestry is pruned
|
||||
// this function must return `AlreadyInChain` rather than `MissingState`
|
||||
match self
|
||||
.block_status(&BlockId::Hash(hash))
|
||||
.block_status(hash)
|
||||
.map_err(|e| ConsensusError::ClientImport(e.to_string()))?
|
||||
{
|
||||
BlockStatus::InChainWithState | BlockStatus::Queued =>
|
||||
@@ -1792,7 +1794,7 @@ where
|
||||
}
|
||||
|
||||
match self
|
||||
.block_status(&BlockId::Hash(parent_hash))
|
||||
.block_status(parent_hash)
|
||||
.map_err(|e| ConsensusError::ClientImport(e.to_string()))?
|
||||
{
|
||||
BlockStatus::InChainWithState | BlockStatus::Queued => {},
|
||||
@@ -1947,20 +1949,16 @@ where
|
||||
self.body(hash)
|
||||
}
|
||||
|
||||
fn block(&self, id: &BlockId<Block>) -> sp_blockchain::Result<Option<SignedBlock<Block>>> {
|
||||
Ok(match self.backend.blockchain().block_hash_from_id(id)? {
|
||||
Some(hash) =>
|
||||
match (self.header(hash)?, self.body(hash)?, self.justifications(hash)?) {
|
||||
(Some(header), Some(extrinsics), justifications) =>
|
||||
Some(SignedBlock { block: Block::new(header, extrinsics), justifications }),
|
||||
_ => None,
|
||||
},
|
||||
None => None,
|
||||
fn block(&self, hash: Block::Hash) -> sp_blockchain::Result<Option<SignedBlock<Block>>> {
|
||||
Ok(match (self.header(hash)?, self.body(hash)?, self.justifications(hash)?) {
|
||||
(Some(header), Some(extrinsics), justifications) =>
|
||||
Some(SignedBlock { block: Block::new(header, extrinsics), justifications }),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn block_status(&self, id: &BlockId<Block>) -> sp_blockchain::Result<BlockStatus> {
|
||||
Client::block_status(self, id)
|
||||
fn block_status(&self, hash: Block::Hash) -> sp_blockchain::Result<BlockStatus> {
|
||||
Client::block_status(self, hash)
|
||||
}
|
||||
|
||||
fn justifications(&self, hash: Block::Hash) -> sp_blockchain::Result<Option<Justifications>> {
|
||||
@@ -2055,9 +2053,9 @@ where
|
||||
{
|
||||
fn block_status(
|
||||
&self,
|
||||
id: &BlockId<B>,
|
||||
hash: B::Hash,
|
||||
) -> Result<BlockStatus, Box<dyn std::error::Error + Send>> {
|
||||
Client::block_status(self, id).map_err(|e| Box::new(e) as Box<_>)
|
||||
Client::block_status(self, hash).map_err(|e| Box::new(e) as Box<_>)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user