BlockId removal: refactor: HeaderBackend::header (#12874)

* BlockId removal: refactor: HeaderBackend::header

It changes the arguments of:
- `HeaderBackend::header`,
- `Client::header`,
- `PeersClient::header`
- `ChainApi::block_header`

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

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

* non-trivial usages of haeder(block_id) refactored

This may required introduction of dedicated function:
header_for_block_num

* fmt

* fix

* doc fixed

* ".git/.scripts/fmt.sh"

* BlockId removal: refactor: HeaderBackend::expect_header

It changes the arguments of `HeaderBackend::expect_header` method from: `BlockId<Block>` to: `Block::Hash`

* ".git/.scripts/fmt.sh"

* readme updated

* ".git/.scripts/fmt.sh"

* fix

Co-authored-by: parity-processbot <>
This commit is contained in:
Michal Kucharczyk
2022-12-20 10:43:31 +01:00
committed by GitHub
parent 74da30c8a2
commit 548955a73f
55 changed files with 307 additions and 360 deletions
+16 -10
View File
@@ -163,17 +163,23 @@ impl PeersClient {
pub fn header(
&self,
block: &BlockId<Block>,
hash: <Block as BlockT>::Hash,
) -> ClientResult<Option<<Block as BlockT>::Header>> {
self.client.header(block)
self.client.header(hash)
}
pub fn has_state_at(&self, block: &BlockId<Block>) -> bool {
let header = match self.header(block).unwrap() {
Some(header) => header,
None => return false,
let (number, hash) = match *block {
BlockId::Hash(h) => match self.as_client().number(h) {
Ok(Some(n)) => (n, h),
_ => return false,
},
BlockId::Number(n) => match self.as_client().hash(n) {
Ok(Some(h)) => (n, h),
_ => return false,
},
};
self.backend.have_state_at(header.hash(), *header.number())
self.backend.have_state_at(hash, number)
}
pub fn justifications(
@@ -367,7 +373,7 @@ where
{
let mut hashes = Vec::with_capacity(count);
let full_client = self.client.as_client();
let mut at = full_client.header(&at).unwrap().unwrap().hash();
let mut at = full_client.block_hash_from_id(&at).unwrap().unwrap();
for _ in 0..count {
let builder =
full_client.new_block_at(&BlockId::Hash(at), Default::default(), false).unwrap();
@@ -404,7 +410,7 @@ where
if inform_sync_about_new_best_block {
self.network.new_best_block_imported(
at,
*full_client.header(&BlockId::Hash(at)).ok().flatten().unwrap().number(),
*full_client.header(at).ok().flatten().unwrap().number(),
);
}
hashes
@@ -549,7 +555,7 @@ where
pub fn has_block(&self, hash: H256) -> bool {
self.backend
.as_ref()
.map(|backend| backend.blockchain().header(BlockId::hash(hash)).unwrap().is_some())
.map(|backend| backend.blockchain().header(hash).unwrap().is_some())
.unwrap_or(false)
}
@@ -673,7 +679,7 @@ impl<B: BlockT> WarpSyncProvider<B> for TestWarpSyncProvider<B> {
_start: B::Hash,
) -> Result<EncodedProof, Box<dyn std::error::Error + Send + Sync>> {
let info = self.0.info();
let best_header = self.0.header(BlockId::hash(info.best_hash)).unwrap().unwrap();
let best_header = self.0.header(info.best_hash).unwrap().unwrap();
Ok(EncodedProof(best_header.encode()))
}
fn verify(