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 para_id = ParaId::from(100);
let announce_block = |_, _| (); let announce_block = |_, _| ();
let client = Arc::new(TestClientBuilder::new().build()); 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); 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 { impl HeaderBackend<Block> for BlockChainRpcClient {
fn header( fn header(
&self, &self,
id: BlockId, hash: <Block as polkadot_service::BlockT>::Hash,
) -> sp_blockchain::Result<Option<<Block as polkadot_service::BlockT>::Header>> { ) -> sp_blockchain::Result<Option<<Block as polkadot_service::BlockT>::Header>> {
let fetch_header = |hash| block_local(self.rpc_client.chain_get_header(Some(hash))); Ok(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)
}
},
}
} }
fn info(&self) -> Info<Block> { fn info(&self) -> Info<Block> {
@@ -425,7 +414,7 @@ impl HeaderBackend<Block> for BlockChainRpcClient {
id: sp_api::BlockId<Block>, id: sp_api::BlockId<Block>,
) -> sp_blockchain::Result<sp_blockchain::BlockStatus> { ) -> sp_blockchain::Result<sp_blockchain::BlockStatus> {
let exists = match id { let exists = match id {
BlockId::Hash(_) => self.header(id)?.is_some(), BlockId::Hash(hash) => self.header(hash)?.is_some(),
BlockId::Number(n) => { BlockId::Number(n) => {
let best_header = block_local(self.rpc_client.chain_get_header(None))?; let best_header = block_local(self.rpc_client.chain_get_header(None))?;
if let Some(best) = best_header { if let Some(best) = best_header {
@@ -24,7 +24,7 @@ use cumulus_test_client::{
}; };
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use sp_keyring::AccountKeyring::*; 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}; use std::{env, process::Command};
fn call_validate_block_encoded_header( fn call_validate_block_encoded_header(
@@ -60,7 +60,7 @@ fn create_test_client() -> (Client, Header) {
.build(); .build();
let genesis_header = client let genesis_header = client
.header(&BlockId::number(0)) .header(client.chain_info().genesis_hash)
.ok() .ok()
.flatten() .flatten()
.expect("Genesis header exists; qed"); .expect("Genesis header exists; qed");
+16 -12
View File
@@ -82,7 +82,10 @@ mod tests {
ValidationParams, ValidationParams,
}; };
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; 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}; use std::{env, process::Command, str::FromStr};
const SLOT_DURATION: u64 = 6000; const SLOT_DURATION: u64 = 6000;
@@ -106,14 +109,14 @@ mod tests {
fn build_block( fn build_block(
client: &Client, client: &Client,
at: BlockId<Block>, hash: <Block as BlockT>::Hash,
timestamp: u64, timestamp: u64,
relay_chain_slot: Slot, relay_chain_slot: Slot,
) -> (ParachainBlockData, PHash) { ) -> (ParachainBlockData, PHash) {
let sproof_builder = let sproof_builder =
RelayStateSproofBuilder { current_slot: relay_chain_slot, ..Default::default() }; 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; let relay_parent_storage_root = sproof_builder.clone().into_state_root_and_proof().0;
@@ -125,7 +128,7 @@ mod tests {
let block = client let block = client
.init_block_builder_with_timestamp( .init_block_builder_with_timestamp(
&at, &BlockId::Hash(hash),
Some(validation_data), Some(validation_data),
sproof_builder, sproof_builder,
timestamp, timestamp,
@@ -146,19 +149,20 @@ mod tests {
.expect("TIMESTAMP is a valid `u64`"); .expect("TIMESTAMP is a valid `u64`");
let block = let block =
build_block(&client, BlockId::number(0), SLOT_DURATION, 1.into()).0.into_block(); build_block(&client, client.chain_info().genesis_hash, SLOT_DURATION, 1.into())
futures::executor::block_on(client.import(sp_consensus::BlockOrigin::Own, block)) .0
.into_block();
futures::executor::block_on(
client.import(sp_consensus::BlockOrigin::Own, block.clone()),
)
.unwrap(); .unwrap();
let hashof1 = block.hash();
let (block, relay_chain_root) = 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( let header = call_validate_block(
client client.header(hashof1).ok().flatten().expect("Genesis header exists"),
.header(&BlockId::number(1))
.ok()
.flatten()
.expect("Genesis header exists"),
block.clone(), block.clone(),
relay_chain_root, relay_chain_root,
) )