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

BlockId removal: refactor: HeaderBackend::header #6418

Merged
merged 10 commits into from
Dec 20, 2022
2 changes: 1 addition & 1 deletion cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ pub fn run() -> Result<()> {
ensure_dev(chain_spec).map_err(Error::Other)?;
runner.sync_run(|mut config| {
let (client, _, _, _) = service::new_chain_ops(&mut config, None)?;
let header = client.header(BlockId::Number(0_u32.into())).unwrap().unwrap();
let header = client.header(client.info().genesis_hash).unwrap().unwrap();
let inherent_data = benchmark_inherent_data(header)
.map_err(|e| format!("generating inherent data: {:?}", e))?;
let remark_builder = RemarkBuilder::new(client.clone());
Expand Down
4 changes: 2 additions & 2 deletions node/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,12 @@ impl sc_client_api::StorageProvider<Block, crate::FullBackend> for Client {
}

impl sp_blockchain::HeaderBackend<Block> for Client {
fn header(&self, id: BlockId<Block>) -> sp_blockchain::Result<Option<Header>> {
fn header(&self, hash: Hash) -> sp_blockchain::Result<Option<Header>> {
with_client! {
self,
client,
{
client.header(&id)
client.header(hash)
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions node/core/chain-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use polkadot_node_subsystem::{
messages::ChainApiMessage, overseer, FromOrchestra, OverseerSignal, SpawnedSubsystem,
SubsystemError, SubsystemResult,
};
use polkadot_primitives::v2::{Block, BlockId};
use polkadot_primitives::v2::Block;

mod metrics;
use self::metrics::Metrics;
Expand Down Expand Up @@ -99,10 +99,7 @@ where
},
ChainApiMessage::BlockHeader(hash, response_channel) => {
let _timer = subsystem.metrics.time_block_header();
let result = subsystem
.client
.header(BlockId::Hash(hash))
.map_err(|e| e.to_string().into());
let result = subsystem.client.header(hash).map_err(|e| e.to_string().into());
subsystem.metrics.on_request(result.is_ok());
let _ = response_channel.send(result);
},
Expand Down Expand Up @@ -134,7 +131,7 @@ where
let mut hash = hash;

let next_parent = core::iter::from_fn(|| {
let maybe_header = subsystem.client.header(BlockId::Hash(hash));
let maybe_header = subsystem.client.header(hash);
match maybe_header {
// propagate the error
Err(e) => {
Expand Down
1 change: 0 additions & 1 deletion node/core/parachains-inherent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ polkadot-overseer = { path = "../../overseer" }
polkadot-primitives = { path = "../../../primitives" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
3 changes: 1 addition & 2 deletions node/core/parachains-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use polkadot_node_subsystem::{
errors::SubsystemError, messages::ProvisionerMessage, overseer::Handle,
};
use polkadot_primitives::v2::{Block, Hash, InherentData as ParachainsInherentData};
use sp_runtime::generic::BlockId;
use std::{sync::Arc, time};

pub(crate) const LOG_TARGET: &str = "parachain::parachains-inherent";
Expand Down Expand Up @@ -87,7 +86,7 @@ impl<C: sp_blockchain::HeaderBackend<Block>> ParachainsInherentDataProvider<C> {

let mut timeout = futures_timer::Delay::new(PROVISIONER_TIMEOUT).fuse();

let parent_header = match client.header(BlockId::Hash(parent)) {
let parent_header = match client.header(parent) {
Ok(Some(h)) => h,
Ok(None) => return Err(Error::ParentHeaderNotFound(parent)),
Err(err) => return Err(Error::Blockchain(err)),
Expand Down
7 changes: 2 additions & 5 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ where
&self,
hash: Block::Hash,
) -> sp_blockchain::Result<Option<<Block as BlockT>::Header>> {
<Self as sp_blockchain::HeaderBackend<Block>>::header(
self,
generic::BlockId::<Block>::Hash(hash),
)
<Self as sp_blockchain::HeaderBackend<Block>>::header(self, hash)
}
fn number(
&self,
Expand Down Expand Up @@ -701,7 +698,7 @@ where
return None
};

let parent_hash = client.header(&BlockId::Hash(hash)).ok()??.parent_hash;
let parent_hash = client.header(hash).ok()??.parent_hash;

Some(BlockInfo { hash, parent_hash, number })
})
Expand Down
13 changes: 7 additions & 6 deletions node/test/client/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_consensus_babe::{
digests::{PreDigest, SecondaryPlainPreDigest},
BABE_ENGINE_ID,
};
use sp_runtime::{generic::BlockId, Digest, DigestItem};
use sp_runtime::{generic::BlockId, traits::Block as BlockT, Digest, DigestItem};
use sp_state_machine::BasicExternalities;

/// An extension for the test client to initialize a Polkadot specific block builder.
Expand All @@ -42,20 +42,21 @@ pub trait InitPolkadotBlockBuilder {
/// which should be the parent block of the block that is being build.
fn init_polkadot_block_builder_at(
&self,
at: &BlockId<Block>,
hash: <Block as BlockT>::Hash,
) -> sc_block_builder::BlockBuilder<Block, Client, FullBackend>;
}

impl InitPolkadotBlockBuilder for Client {
fn init_polkadot_block_builder(&self) -> BlockBuilder<Block, Client, FullBackend> {
let chain_info = self.chain_info();
self.init_polkadot_block_builder_at(&BlockId::Hash(chain_info.best_hash))
self.init_polkadot_block_builder_at(chain_info.best_hash)
}

fn init_polkadot_block_builder_at(
&self,
at: &BlockId<Block>,
hash: <Block as BlockT>::Hash,
) -> BlockBuilder<Block, Client, FullBackend> {
let at = BlockId::Hash(hash);
let last_timestamp =
self.runtime_api().get_last_timestamp(&at).expect("Get last timestamp");

Expand Down Expand Up @@ -87,7 +88,7 @@ impl InitPolkadotBlockBuilder for Client {
};

let mut block_builder = self
.new_block_at(at, digest, false)
.new_block_at(&at, digest, false)
.expect("Creates new block builder for test runtime");

let mut inherent_data = sp_inherents::InherentData::new();
Expand All @@ -97,7 +98,7 @@ impl InitPolkadotBlockBuilder for Client {
.expect("Put timestamp inherent data");

let parent_header = self
.header(at)
.header(hash)
.expect("Get the parent block header")
.expect("The target block header must exist");

Expand Down