Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

BlockId removal: refactor: HeaderBackend::status #12981

Merged
merged 2 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ impl<Block: BlockT> HeaderBackend<Block> for Blockchain<Block> {
}
}

fn status(&self, id: BlockId<Block>) -> sp_blockchain::Result<BlockStatus> {
match self.id(id).map_or(false, |hash| self.storage.read().blocks.contains_key(&hash)) {
fn status(&self, hash: Block::Hash) -> sp_blockchain::Result<BlockStatus> {
match self.storage.read().blocks.contains_key(&hash) {
true => Ok(BlockStatus::InChain),
false => Ok(BlockStatus::Unknown),
}
Expand Down
2 changes: 1 addition & 1 deletion client/authority-discovery/src/worker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<Block: BlockT> HeaderBackend<Block> for TestApi {

fn status(
&self,
_id: BlockId<Block>,
_hash: Block::Hash,
) -> std::result::Result<sc_client_api::blockchain::BlockStatus, sp_blockchain::Error> {
Ok(sc_client_api::blockchain::BlockStatus::Unknown)
}
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ where

// early exit if block already in chain, otherwise the check for
// epoch changes will error when trying to re-import an epoch change
match self.client.status(BlockId::Hash(hash)) {
match self.client.status(hash) {
Ok(sp_blockchain::BlockStatus::InChain) => {
// When re-importing existing block strip away intermediates.
let _ = block.remove_intermediate::<BabeIntermediate<Block>>(INTERMEDIATE_KEY);
Expand Down
10 changes: 3 additions & 7 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,8 @@ impl<Block: BlockT> sc_client_api::blockchain::HeaderBackend<Block> for Blockcha
}
}

fn status(&self, id: BlockId<Block>) -> ClientResult<sc_client_api::blockchain::BlockStatus> {
let exists = match id {
BlockId::Hash(hash) => self.header(hash)?.is_some(),
BlockId::Number(n) => n <= self.meta.read().best_number,
};
match exists {
fn status(&self, hash: Block::Hash) -> ClientResult<sc_client_api::blockchain::BlockStatus> {
match self.header(hash)?.is_some() {
true => Ok(sc_client_api::blockchain::BlockStatus::InChain),
false => Ok(sc_client_api::blockchain::BlockStatus::Unknown),
}
Expand Down Expand Up @@ -1223,7 +1219,7 @@ impl<Block: BlockT> Backend<Block> {
}

let parent_exists =
self.blockchain.status(BlockId::Hash(route_to))? == sp_blockchain::BlockStatus::InChain;
self.blockchain.status(route_to)? == sp_blockchain::BlockStatus::InChain;

// Cannot find tree route with empty DB or when imported a detached block.
if meta.best_hash != Default::default() && parent_exists {
Expand Down
2 changes: 1 addition & 1 deletion client/finality-grandpa/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ where

// early exit if block already in chain, otherwise the check for
// authority changes will error when trying to re-import a change block
match self.inner.status(BlockId::Hash(hash)) {
match self.inner.status(hash) {
Ok(BlockStatus::InChain) => {
// Strip justifications when re-importing an existing block.
let _justifications = block.justifications.take();
Expand Down
4 changes: 2 additions & 2 deletions client/merkle-mountain-range/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ impl HeaderBackend<Block> for MockClient {
self.client.lock().info()
}

fn status(&self, id: BlockId<Block>) -> sc_client_api::blockchain::Result<BlockStatus> {
self.client.lock().status(id)
fn status(&self, hash: Hash) -> sc_client_api::blockchain::Result<BlockStatus> {
self.client.lock().status(hash)
}

fn number(&self, hash: Hash) -> sc_client_api::blockchain::Result<Option<BlockNumber>> {
Expand Down
14 changes: 7 additions & 7 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,9 @@ where
CoreApi<Block> + ApiExt<Block, StateBackend = B::State>,
{
let parent_hash = *import_headers.post().parent_hash();
let status = self.backend.blockchain().status(BlockId::Hash(hash))?;
let parent_exists = self.backend.blockchain().status(BlockId::Hash(parent_hash))? ==
blockchain::BlockStatus::InChain;
let status = self.backend.blockchain().status(hash)?;
let parent_exists =
self.backend.blockchain().status(parent_hash)? == blockchain::BlockStatus::InChain;
match (import_existing, status) {
(false, blockchain::BlockStatus::InChain) => return Ok(ImportResult::AlreadyInChain),
(false, blockchain::BlockStatus::Unknown) => {},
Expand Down Expand Up @@ -1572,8 +1572,8 @@ where
self.backend.blockchain().info()
}

fn status(&self, id: BlockId<Block>) -> sp_blockchain::Result<blockchain::BlockStatus> {
self.backend.blockchain().status(id)
fn status(&self, hash: Block::Hash) -> sp_blockchain::Result<blockchain::BlockStatus> {
self.backend.blockchain().status(hash)
}

fn number(
Expand Down Expand Up @@ -1624,8 +1624,8 @@ where
self.backend.blockchain().info()
}

fn status(&self, id: BlockId<Block>) -> sp_blockchain::Result<blockchain::BlockStatus> {
(**self).status(id)
fn status(&self, hash: Block::Hash) -> sp_blockchain::Result<blockchain::BlockStatus> {
(**self).status(hash)
}

fn number(
Expand Down
2 changes: 1 addition & 1 deletion primitives/blockchain/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait HeaderBackend<Block: BlockT>: Send + Sync {
/// Get blockchain info.
fn info(&self) -> Info<Block>;
/// Get block status.
fn status(&self, id: BlockId<Block>) -> Result<BlockStatus>;
fn status(&self, hash: Block::Hash) -> Result<BlockStatus>;
/// Get block number by hash. Returns `None` if the header is not in the chain.
fn number(
&self,
Expand Down