Skip to content

Commit

Permalink
ParachainHost: No need to be generic over the block or hash type (#2537)
Browse files Browse the repository at this point in the history
The `BlockNumber` and `Hash` are fixed types any way.
  • Loading branch information
bkchr authored Nov 29, 2023
1 parent 8f03570 commit d3d301f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion polkadot/node/service/src/fake_runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ sp_api::impl_runtime_apis! {
}
}

impl runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
impl runtime_api::ParachainHost<Block> for Runtime {
fn validators() -> Vec<ValidatorId> {
unimplemented!()
}
Expand Down
35 changes: 19 additions & 16 deletions polkadot/primitives/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,46 +116,45 @@
use crate::{
async_backing, slashing, vstaging, AsyncBackingParams, BlockNumber, CandidateCommitments,
CandidateEvent, CandidateHash, CommittedCandidateReceipt, CoreState, DisputeState,
ExecutorParams, GroupRotationInfo, OccupiedCoreAssumption, PersistedValidationData,
ExecutorParams, GroupRotationInfo, Hash, OccupiedCoreAssumption, PersistedValidationData,
PvfCheckStatement, ScrapedOnChainVotes, SessionIndex, SessionInfo, ValidatorId, ValidatorIndex,
ValidatorSignature,
};
use parity_scale_codec::{Decode, Encode};
use polkadot_core_primitives as pcp;
use polkadot_parachain_primitives::primitives as ppp;
use sp_std::{collections::btree_map::BTreeMap, prelude::*};

sp_api::decl_runtime_apis! {
/// The API for querying the state of parachains on-chain.
#[api_version(5)]
pub trait ParachainHost<H: Encode + Decode = pcp::v2::Hash, N: Encode + Decode = pcp::v2::BlockNumber> {
pub trait ParachainHost {
/// Get the current validators.
fn validators() -> Vec<ValidatorId>;

/// Returns the validator groups and rotation info localized based on the hypothetical child
/// of a block whose state this is invoked on. Note that `now` in the `GroupRotationInfo`
/// should be the successor of the number of the block.
fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<N>);
fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>);

/// Yields information on all availability cores as relevant to the child block.
/// Cores are either free or occupied. Free cores can have paras assigned to them.
fn availability_cores() -> Vec<CoreState<H, N>>;
fn availability_cores() -> Vec<CoreState<Hash, BlockNumber>>;

/// Yields the persisted validation data for the given `ParaId` along with an assumption that
/// should be used if the para currently occupies a core.
///
/// Returns `None` if either the para is not registered or the assumption is `Freed`
/// and the para already occupies a core.
fn persisted_validation_data(para_id: ppp::Id, assumption: OccupiedCoreAssumption)
-> Option<PersistedValidationData<H, N>>;
-> Option<PersistedValidationData<Hash, BlockNumber>>;

/// Returns the persisted validation data for the given `ParaId` along with the corresponding
/// validation code hash. Instead of accepting assumption about the para, matches the validation
/// data hash against an expected one and yields `None` if they're not equal.
fn assumed_validation_data(
para_id: ppp::Id,
expected_persisted_validation_data_hash: pcp::v2::Hash,
) -> Option<(PersistedValidationData<H, N>, ppp::ValidationCodeHash)>;
expected_persisted_validation_data_hash: Hash,
) -> Option<(PersistedValidationData<Hash, BlockNumber>, ppp::ValidationCodeHash)>;

/// Checks if the given validation outputs pass the acceptance criteria.
fn check_validation_outputs(para_id: ppp::Id, outputs: CandidateCommitments) -> bool;
Expand All @@ -169,30 +168,34 @@ sp_api::decl_runtime_apis! {
///
/// Returns `None` if either the para is not registered or the assumption is `Freed`
/// and the para already occupies a core.
fn validation_code(para_id: ppp::Id, assumption: OccupiedCoreAssumption)
-> Option<ppp::ValidationCode>;
fn validation_code(
para_id: ppp::Id,
assumption: OccupiedCoreAssumption,
) -> Option<ppp::ValidationCode>;

/// Get the receipt of a candidate pending availability. This returns `Some` for any paras
/// assigned to occupied cores in `availability_cores` and `None` otherwise.
fn candidate_pending_availability(para_id: ppp::Id) -> Option<CommittedCandidateReceipt<H>>;
fn candidate_pending_availability(para_id: ppp::Id) -> Option<CommittedCandidateReceipt<Hash>>;

/// Get a vector of events concerning candidates that occurred within a block.
fn candidate_events() -> Vec<CandidateEvent<H>>;
fn candidate_events() -> Vec<CandidateEvent<Hash>>;

/// Get all the pending inbound messages in the downward message queue for a para.
fn dmq_contents(
recipient: ppp::Id,
) -> Vec<pcp::v2::InboundDownwardMessage<N>>;
) -> Vec<pcp::v2::InboundDownwardMessage<BlockNumber>>;

/// Get the contents of all channels addressed to the given recipient. Channels that have no
/// messages in them are also included.
fn inbound_hrmp_channels_contents(recipient: ppp::Id) -> BTreeMap<ppp::Id, Vec<pcp::v2::InboundHrmpMessage<N>>>;
fn inbound_hrmp_channels_contents(
recipient: ppp::Id,
) -> BTreeMap<ppp::Id, Vec<pcp::v2::InboundHrmpMessage<BlockNumber>>>;

/// Get the validation code from its hash.
fn validation_code_by_hash(hash: ppp::ValidationCodeHash) -> Option<ppp::ValidationCode>;

/// Scrape dispute relevant from on-chain, backing votes and resolved disputes.
fn on_chain_votes() -> Option<ScrapedOnChainVotes<H>>;
fn on_chain_votes() -> Option<ScrapedOnChainVotes<Hash>>;

/***** Added in v2 *****/

Expand Down Expand Up @@ -253,7 +256,7 @@ sp_api::decl_runtime_apis! {

/// Returns the state of parachain backing for a given para.
#[api_version(7)]
fn para_backing_state(_: ppp::Id) -> Option<async_backing::BackingState<H, N>>;
fn para_backing_state(_: ppp::Id) -> Option<async_backing::BackingState<Hash, BlockNumber>>;

/// Returns candidate's acceptance limitations for asynchronous backing for a relay parent.
#[api_version(7)]
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ sp_api::impl_runtime_apis! {
}

#[api_version(9)]
impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
impl primitives::runtime_api::ParachainHost<Block> for Runtime {
fn validators() -> Vec<ValidatorId> {
parachains_runtime_api_impl::validators::<Runtime>()
}
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ sp_api::impl_runtime_apis! {
}
}

impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
impl primitives::runtime_api::ParachainHost<Block> for Runtime {
fn validators() -> Vec<ValidatorId> {
runtime_impl::validators::<Runtime>()
}
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ sp_api::impl_runtime_apis! {
}

#[api_version(9)]
impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
impl primitives::runtime_api::ParachainHost<Block> for Runtime {
fn validators() -> Vec<ValidatorId> {
parachains_runtime_api_impl::validators::<Runtime>()
}
Expand Down

0 comments on commit d3d301f

Please sign in to comment.