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

Commit

Permalink
BlockId removal: refactor of runtime API
Browse files Browse the repository at this point in the history
It changes the first argument of all generated runtime API calls from: `BlockId<Block>` to: `Block::Hash`
  • Loading branch information
michalkucharczyk committed Feb 15, 2023
1 parent be98b57 commit 63cd557
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
3 changes: 1 addition & 2 deletions client/collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,8 @@ mod tests {
_: PHash,
validation_data: &PersistedValidationData,
) -> Option<ParachainCandidate<Block>> {
let block_id = BlockId::Hash(parent.hash());
let builder = self.client.init_block_builder_at(
&block_id,
parent.hash(),
Some(validation_data.clone()),
Default::default(),
);
Expand Down
23 changes: 11 additions & 12 deletions client/consensus/common/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ use cumulus_relay_chain_interface::{
RelayChainInterface, RelayChainResult, SessionIndex, StorageValue, ValidatorId,
};
use cumulus_test_client::{
runtime::{Block, Header},
runtime::{Block, Hash, Header},
Backend, Client, InitBlockBuilder, TestClientBuilder, TestClientBuilderExt,
};
use futures::{channel::mpsc, executor::block_on, select, FutureExt, Stream, StreamExt};
use futures_timer::Delay;
use sc_client_api::{blockchain::Backend as _, Backend as _, UsageProvider};
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy};
use sp_consensus::{BlockOrigin, BlockStatus};
use sp_runtime::generic::BlockId;
use std::{
collections::{BTreeMap, HashMap},
pin::Pin,
Expand Down Expand Up @@ -212,14 +211,14 @@ impl RelayChainInterface for Relaychain {

fn build_block<B: InitBlockBuilder>(
builder: &B,
at: Option<BlockId<Block>>,
at: Option<Hash>,
timestamp: Option<u64>,
) -> Block {
let builder = match at {
Some(at) => match timestamp {
Some(ts) =>
builder.init_block_builder_with_timestamp(&at, None, Default::default(), ts),
None => builder.init_block_builder_at(&at, None, Default::default()),
builder.init_block_builder_with_timestamp(at, None, Default::default(), ts),
None => builder.init_block_builder_at(at, None, Default::default()),
},
None => builder.init_block_builder(None, Default::default()),
};
Expand Down Expand Up @@ -267,7 +266,7 @@ fn build_and_import_block_ext<B: InitBlockBuilder, I: BlockImport<Block>>(
origin: BlockOrigin,
import_as_best: bool,
importer: &mut I,
at: Option<BlockId<Block>>,
at: Option<Hash>,
timestamp: Option<u64>,
) -> Block {
let block = build_block(builder, at, timestamp);
Expand Down Expand Up @@ -426,7 +425,7 @@ fn follow_finalized_does_not_stop_on_unknown_block() {

let unknown_block = {
let block_builder =
client.init_block_builder_at(&BlockId::Hash(block.hash()), None, Default::default());
client.init_block_builder_at(block.hash(), None, Default::default());
block_builder.build().unwrap().block
};

Expand Down Expand Up @@ -476,7 +475,7 @@ fn follow_new_best_sets_best_after_it_is_imported() {

let unknown_block = {
let block_builder =
client.init_block_builder_at(&BlockId::Hash(block.hash()), None, Default::default());
client.init_block_builder_at(block.hash(), None, Default::default());
block_builder.build().unwrap().block
};

Expand Down Expand Up @@ -608,7 +607,7 @@ fn prune_blocks_on_level_overflow() {
None,
None,
);
let id0 = BlockId::Hash(block0.header.hash());
let id0 = block0.header.hash();

let blocks1 = (0..LEVEL_LIMIT)
.into_iter()
Expand All @@ -623,7 +622,7 @@ fn prune_blocks_on_level_overflow() {
)
})
.collect::<Vec<_>>();
let id10 = BlockId::Hash(blocks1[0].header.hash());
let id10 = blocks1[0].header.hash();

let blocks2 = (0..2)
.into_iter()
Expand Down Expand Up @@ -721,7 +720,7 @@ fn restore_limit_monitor() {
None,
None,
);
let id00 = BlockId::Hash(block00.header.hash());
let id00 = block00.header.hash();

let blocks1 = (0..LEVEL_LIMIT + 1)
.into_iter()
Expand All @@ -736,7 +735,7 @@ fn restore_limit_monitor() {
)
})
.collect::<Vec<_>>();
let id10 = BlockId::Hash(blocks1[0].header.hash());
let id10 = blocks1[0].header.hash();

let _ = (0..LEVEL_LIMIT)
.into_iter()
Expand Down
8 changes: 2 additions & 6 deletions polkadot-parachain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use sp_consensus_aura::AuraApi;
use sp_keystore::SyncCryptoStorePtr;
use sp_runtime::{
app_crypto::AppKey,
generic::BlockId,
traits::{BlakeTwo256, Header as HeaderT},
};
use std::{marker::PhantomData, sync::Arc, time::Duration};
Expand Down Expand Up @@ -994,11 +993,10 @@ where
relay_parent: PHash,
validation_data: &PersistedValidationData,
) -> Option<ParachainCandidate<Block>> {
let block_id = BlockId::hash(parent.hash());
if self
.client
.runtime_api()
.has_api::<dyn AuraApi<Block, AuraId>>(&block_id)
.has_api::<dyn AuraApi<Block, AuraId>>(parent.hash())
.unwrap_or(false)
{
self.aura_consensus
Expand Down Expand Up @@ -1035,12 +1033,10 @@ where
&mut self,
block_import: BlockImportParams<Block, ()>,
) -> Result<(BlockImportParams<Block, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
let block_id = BlockId::hash(*block_import.header.parent_hash());

if self
.client
.runtime_api()
.has_api::<dyn AuraApi<Block, AuraId>>(&block_id)
.has_api::<dyn AuraApi<Block, AuraId>>(*block_import.header.parent_hash())
.unwrap_or(false)
{
self.aura_verifier.get_mut().verify(block_import).await
Expand Down
7 changes: 2 additions & 5 deletions primitives/timestamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ mod tests {
ValidationParams,
};
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::{env, process::Command, str::FromStr};

const SLOT_DURATION: u64 = 6000;
Expand Down Expand Up @@ -128,7 +125,7 @@ mod tests {

let block = client
.init_block_builder_with_timestamp(
&BlockId::Hash(hash),
hash,
Some(validation_data),
sproof_builder,
timestamp,
Expand Down
14 changes: 7 additions & 7 deletions test/client/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub trait InitBlockBuilder {
/// [`BlockId`] to say which should be the parent block of the block that is being build.
fn init_block_builder_at(
&self,
at: &BlockId<Block>,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> sc_block_builder::BlockBuilder<Block, Client, Backend>;
Expand All @@ -60,7 +60,7 @@ pub trait InitBlockBuilder {
/// it will use the given `timestamp` as input for the timestamp inherent.
fn init_block_builder_with_timestamp(
&self,
at: &BlockId<Block>,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
timestamp: u64,
Expand All @@ -69,13 +69,13 @@ pub trait InitBlockBuilder {

fn init_block_builder<'a>(
client: &'a Client,
at: &BlockId<Block>,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
timestamp: u64,
) -> BlockBuilder<'a, Block, Client, Backend> {
let mut block_builder = client
.new_block_at(at, Default::default(), true)
.new_block_at(&BlockId::Hash(at), Default::default(), true)
.expect("Creates new block builder for test runtime");

let mut inherent_data = sp_inherents::InherentData::new();
Expand Down Expand Up @@ -124,15 +124,15 @@ impl InitBlockBuilder for Client {
) -> BlockBuilder<Block, Client, Backend> {
let chain_info = self.chain_info();
self.init_block_builder_at(
&BlockId::Hash(chain_info.best_hash),
chain_info.best_hash,
validation_data,
relay_sproof_builder,
)
}

fn init_block_builder_at(
&self,
at: &BlockId<Block>,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilder<Block, Client, Backend> {
Expand All @@ -145,7 +145,7 @@ impl InitBlockBuilder for Client {

fn init_block_builder_with_timestamp(
&self,
at: &BlockId<Block>,
at: Hash,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
timestamp: u64,
Expand Down
2 changes: 1 addition & 1 deletion test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ pub fn fetch_nonce(client: &Client, account: sp_core::sr25519::Public) -> u32 {
let best_hash = client.chain_info().best_hash;
client
.runtime_api()
.account_nonce(&generic::BlockId::Hash(best_hash), account.into())
.account_nonce(best_hash, account.into())
.expect("Fetching account nonce works; qed")
}

Expand Down

0 comments on commit 63cd557

Please sign in to comment.