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

* BlockId removal: refactor: HeaderBackend::header

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

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

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

* update lockfile for {"polkadot", "substrate"}

Co-authored-by: parity-processbot <>
This commit is contained in:
Michal Kucharczyk
2022-12-20 13:46:44 +01:00
committed by GitHub
parent fbce6fe120
commit 19ad8c8b38
5 changed files with 321 additions and 406 deletions
+298 -376
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -422,7 +422,7 @@ mod tests {
let para_id = ParaId::from(100);
let announce_block = |_, _| ();
let client = Arc::new(TestClientBuilder::new().build());
let header = client.header(&BlockId::Number(0)).unwrap().unwrap();
let header = client.header(client.chain_info().genesis_hash).unwrap().unwrap();
let (sub_tx, sub_rx) = mpsc::channel(64);
@@ -381,20 +381,9 @@ fn block_local<T>(fut: impl Future<Output = T>) -> T {
impl HeaderBackend<Block> for BlockChainRpcClient {
fn header(
&self,
id: BlockId,
hash: <Block as polkadot_service::BlockT>::Hash,
) -> sp_blockchain::Result<Option<<Block as polkadot_service::BlockT>::Header>> {
let fetch_header = |hash| block_local(self.rpc_client.chain_get_header(Some(hash)));
match id {
BlockId::Hash(hash) => Ok(fetch_header(hash)?),
BlockId::Number(number) => {
if let Some(hash) = HeaderBackend::<Block>::hash(self, number)? {
Ok(fetch_header(hash)?)
} else {
Ok(None)
}
},
}
Ok(block_local(self.rpc_client.chain_get_header(Some(hash)))?)
}
fn info(&self) -> Info<Block> {
@@ -425,7 +414,7 @@ impl HeaderBackend<Block> for BlockChainRpcClient {
id: sp_api::BlockId<Block>,
) -> sp_blockchain::Result<sp_blockchain::BlockStatus> {
let exists = match id {
BlockId::Hash(_) => self.header(id)?.is_some(),
BlockId::Hash(hash) => self.header(hash)?.is_some(),
BlockId::Number(n) => {
let best_header = block_local(self.rpc_client.chain_get_header(None))?;
if let Some(best) = best_header {
@@ -24,7 +24,7 @@ use cumulus_test_client::{
};
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use sp_keyring::AccountKeyring::*;
use sp_runtime::{generic::BlockId, traits::Header as HeaderT};
use sp_runtime::traits::Header as HeaderT;
use std::{env, process::Command};
fn call_validate_block_encoded_header(
@@ -60,7 +60,7 @@ fn create_test_client() -> (Client, Header) {
.build();
let genesis_header = client
.header(&BlockId::number(0))
.header(client.chain_info().genesis_hash)
.ok()
.flatten()
.expect("Genesis header exists; qed");
+16 -12
View File
@@ -82,7 +82,10 @@ mod tests {
ValidationParams,
};
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use sp_runtime::{generic::BlockId, traits::Header as HeaderT};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use std::{env, process::Command, str::FromStr};
const SLOT_DURATION: u64 = 6000;
@@ -106,14 +109,14 @@ mod tests {
fn build_block(
client: &Client,
at: BlockId<Block>,
hash: <Block as BlockT>::Hash,
timestamp: u64,
relay_chain_slot: Slot,
) -> (ParachainBlockData, PHash) {
let sproof_builder =
RelayStateSproofBuilder { current_slot: relay_chain_slot, ..Default::default() };
let parent_header = client.header(&at).ok().flatten().expect("Genesis header exists");
let parent_header = client.header(hash).ok().flatten().expect("Genesis header exists");
let relay_parent_storage_root = sproof_builder.clone().into_state_root_and_proof().0;
@@ -125,7 +128,7 @@ mod tests {
let block = client
.init_block_builder_with_timestamp(
&at,
&BlockId::Hash(hash),
Some(validation_data),
sproof_builder,
timestamp,
@@ -146,19 +149,20 @@ mod tests {
.expect("TIMESTAMP is a valid `u64`");
let block =
build_block(&client, BlockId::number(0), SLOT_DURATION, 1.into()).0.into_block();
futures::executor::block_on(client.import(sp_consensus::BlockOrigin::Own, block))
build_block(&client, client.chain_info().genesis_hash, SLOT_DURATION, 1.into())
.0
.into_block();
futures::executor::block_on(
client.import(sp_consensus::BlockOrigin::Own, block.clone()),
)
.unwrap();
let hashof1 = block.hash();
let (block, relay_chain_root) =
build_block(&client, BlockId::number(1), timestamp, relay_chain_slot.into());
build_block(&client, hashof1, timestamp, relay_chain_slot.into());
let header = call_validate_block(
client
.header(&BlockId::number(1))
.ok()
.flatten()
.expect("Genesis header exists"),
client.header(hashof1).ok().flatten().expect("Genesis header exists"),
block.clone(),
relay_chain_root,
)