Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polkadot Primitives v8 #5525

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion polkadot/node/collation-generation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use polkadot_node_subsystem::{
use polkadot_node_subsystem_util::{
request_async_backing_params, request_availability_cores, request_para_backing_state,
request_persisted_validation_data, request_validation_code, request_validation_code_hash,
request_validators, vstaging::fetch_claim_queue,
request_validators, runtime::fetch_claim_queue,
};
use polkadot_primitives::{
collator_signature_payload, CandidateCommitments, CandidateDescriptor, CandidateReceipt,
Expand Down
6 changes: 3 additions & 3 deletions polkadot/node/core/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ use polkadot_node_subsystem_util::{
executor_params_at_relay_parent, request_from_runtime, request_session_index_for_child,
request_validator_groups, request_validators,
runtime::{
self, prospective_parachains_mode, request_min_backing_votes, ProspectiveParachainsMode,
self, fetch_claim_queue, prospective_parachains_mode, request_min_backing_votes,
ClaimQueueSnapshot, ProspectiveParachainsMode,
},
vstaging::{fetch_claim_queue, ClaimQueueSnapshot},
Validator,
};
use polkadot_primitives::{
Expand All @@ -121,7 +121,7 @@ use polkadot_statement_table::{
Config as TableConfig, Context as TableContextTrait, Table,
};
use sp_keystore::KeystorePtr;
use util::{runtime::request_node_features, vstaging::get_disabled_validators_with_fallback};
use util::runtime::{get_disabled_validators_with_fallback, request_node_features};

mod error;

Expand Down
3 changes: 1 addition & 2 deletions polkadot/node/core/prospective-parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ use polkadot_node_subsystem_util::{
backing_implicit_view::{BlockInfoProspectiveParachains as BlockInfo, View as ImplicitView},
inclusion_emulator::{Constraints, RelayChainBlockInfo},
request_session_index_for_child,
runtime::{prospective_parachains_mode, ProspectiveParachainsMode},
vstaging::fetch_claim_queue,
runtime::{fetch_claim_queue, prospective_parachains_mode, ProspectiveParachainsMode},
};
use polkadot_primitives::{
async_backing::CandidatePendingAvailability, BlockNumber, CandidateHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ use polkadot_node_subsystem_util::{
backing_implicit_view::View as ImplicitView,
reputation::{ReputationAggregator, REPUTATION_CHANGE_INTERVAL},
runtime::{
get_availability_cores, get_group_rotation_info, prospective_parachains_mode,
ProspectiveParachainsMode, RuntimeInfo,
fetch_claim_queue, get_availability_cores, get_group_rotation_info,
prospective_parachains_mode, ProspectiveParachainsMode, RuntimeInfo,
},
vstaging::fetch_claim_queue,
TimeoutExt,
};
use polkadot_primitives::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ use polkadot_node_subsystem::{
use polkadot_node_subsystem_util::{
backing_implicit_view::View as ImplicitView,
reputation::{ReputationAggregator, REPUTATION_CHANGE_INTERVAL},
runtime::{prospective_parachains_mode, ProspectiveParachainsMode},
vstaging::fetch_claim_queue,
runtime::{fetch_claim_queue, prospective_parachains_mode, ProspectiveParachainsMode},
};
use polkadot_primitives::{
CandidateHash, CollatorId, CoreState, Hash, HeadData, Id as ParaId, OccupiedCoreAssumption,
Expand Down
7 changes: 4 additions & 3 deletions polkadot/node/network/statement-distribution/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ use polkadot_node_subsystem::{
use polkadot_node_subsystem_util::{
backing_implicit_view::View as ImplicitView,
reputation::ReputationAggregator,
runtime::{request_min_backing_votes, ProspectiveParachainsMode},
vstaging::{fetch_claim_queue, ClaimQueueSnapshot},
runtime::{
fetch_claim_queue, request_min_backing_votes, ClaimQueueSnapshot, ProspectiveParachainsMode,
},
};
use polkadot_primitives::{
AuthorityDiscoveryId, CandidateHash, CompactStatement, CoreIndex, CoreState, GroupIndex,
Expand Down Expand Up @@ -570,7 +571,7 @@ pub(crate) async fn handle_active_leaves_update<Context>(

for new_relay_parent in new_relay_parents.iter().cloned() {
let disabled_validators: HashSet<_> =
polkadot_node_subsystem_util::vstaging::get_disabled_validators_with_fallback(
polkadot_node_subsystem_util::runtime::get_disabled_validators_with_fallback(
ctx.sender(),
new_relay_parent,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl GroupStatements {
mod tests {
use super::*;

use polkadot_primitives::v7::{Hash, SigningContext, ValidatorPair};
use polkadot_primitives::{Hash, SigningContext, ValidatorPair};
use sp_application_crypto::Pair as PairT;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_consensus_babe::AuthorityId as BabeId;
use sp_consensus_beefy::ecdsa_crypto::AuthorityId as BeefyId;

#[cfg(feature = "westend-native")]
use polkadot_primitives::vstaging::SchedulerParams;
use polkadot_primitives::SchedulerParams;
#[cfg(feature = "rococo-native")]
use rococo_runtime as rococo;
use sc_chain_spec::ChainSpecExtension;
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/subsystem-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use polkadot_primitives::{
ValidatorSignature,
};
pub use rand;
use runtime::get_disabled_validators_with_fallback;
use sp_application_crypto::AppCrypto;
use sp_core::ByteArray;
use sp_keystore::{Error as KeystoreError, KeystorePtr};
Expand All @@ -57,7 +58,6 @@ use std::{
time::Duration,
};
use thiserror::Error;
use vstaging::get_disabled_validators_with_fallback;

pub use determine_new_blocks::determine_new_blocks;
pub use metered;
Expand Down
108 changes: 103 additions & 5 deletions polkadot/node/subsystem-util/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ use polkadot_node_subsystem_types::UnpinHandle;
use polkadot_primitives::{
node_features::FeatureIndex, slashing, AsyncBackingParams, CandidateEvent, CandidateHash,
CoreIndex, CoreState, EncodeAs, ExecutorParams, GroupIndex, GroupRotationInfo, Hash,
IndexedVec, NodeFeatures, OccupiedCore, ScrapedOnChainVotes, SessionIndex, SessionInfo, Signed,
SigningContext, UncheckedSigned, ValidationCode, ValidationCodeHash, ValidatorId,
ValidatorIndex, LEGACY_MIN_BACKING_VOTES,
Id as ParaId, IndexedVec, NodeFeatures, OccupiedCore, ScrapedOnChainVotes, SessionIndex,
SessionInfo, Signed, SigningContext, UncheckedSigned, ValidationCode, ValidationCodeHash,
ValidatorId, ValidatorIndex, LEGACY_MIN_BACKING_VOTES,
};

use std::collections::{BTreeMap, VecDeque};

use crate::{
request_async_backing_params, request_availability_cores, request_candidate_events,
has_required_runtime, request_async_backing_params, request_availability_cores,
request_candidate_events, request_claim_queue, request_disabled_validators,
request_from_runtime, request_key_ownership_proof, request_on_chain_votes,
request_session_executor_params, request_session_index_for_child, request_session_info,
request_submit_report_dispute_lost, request_unapplied_slashes, request_validation_code_by_hash,
request_validator_groups, vstaging::get_disabled_validators_with_fallback,
request_validator_groups,
};

/// Errors that can happen on runtime fetches.
Expand Down Expand Up @@ -579,3 +582,98 @@ pub async fn request_node_features(
res.map(Some)
}
}

/// A snapshot of the runtime claim queue at an arbitrary relay chain block.
#[derive(Default)]
pub struct ClaimQueueSnapshot(pub BTreeMap<CoreIndex, VecDeque<ParaId>>);

impl From<BTreeMap<CoreIndex, VecDeque<ParaId>>> for ClaimQueueSnapshot {
fn from(claim_queue_snapshot: BTreeMap<CoreIndex, VecDeque<ParaId>>) -> Self {
ClaimQueueSnapshot(claim_queue_snapshot)
}
}

impl ClaimQueueSnapshot {
/// Returns the `ParaId` that has a claim for `core_index` at the specified `depth` in the
/// claim queue. A depth of `0` means the very next block.
pub fn get_claim_for(&self, core_index: CoreIndex, depth: usize) -> Option<ParaId> {
self.0.get(&core_index)?.get(depth).copied()
}

/// Returns an iterator over all claimed cores and the claiming `ParaId` at the specified
/// `depth` in the claim queue.
pub fn iter_claims_at_depth(
&self,
depth: usize,
) -> impl Iterator<Item = (CoreIndex, ParaId)> + '_ {
self.0
.iter()
.filter_map(move |(core_index, paras)| Some((*core_index, *paras.get(depth)?)))
}

/// Returns an iterator over all claims on the given core.
pub fn iter_claims_for_core(
&self,
core_index: &CoreIndex,
) -> impl Iterator<Item = &ParaId> + '_ {
self.0.get(core_index).map(|c| c.iter()).into_iter().flatten()
}

/// Returns an iterator over the whole claim queue.
pub fn iter_all_claims(&self) -> impl Iterator<Item = (&CoreIndex, &VecDeque<ParaId>)> + '_ {
self.0.iter()
}
}

// TODO: https://github.com/paritytech/polkadot-sdk/issues/1940
/// Returns disabled validators list if the runtime supports it. Otherwise logs a debug messages and
/// returns an empty vec.
/// Once runtime ver `DISABLED_VALIDATORS_RUNTIME_REQUIREMENT` is released remove this function and
/// replace all usages with `request_disabled_validators`
pub async fn get_disabled_validators_with_fallback<Sender: SubsystemSender<RuntimeApiMessage>>(
sender: &mut Sender,
relay_parent: Hash,
) -> Result<Vec<ValidatorIndex>> {
let disabled_validators = if has_required_runtime(
sender,
relay_parent,
RuntimeApiRequest::DISABLED_VALIDATORS_RUNTIME_REQUIREMENT,
)
.await
{
request_disabled_validators(relay_parent, sender)
.await
.await
.map_err(Error::RuntimeRequestCanceled)??
} else {
gum::debug!(target: LOG_TARGET, "Runtime doesn't support `DisabledValidators` - continuing with an empty disabled validators set");
vec![]
};

Ok(disabled_validators)
}

/// Checks if the runtime supports `request_claim_queue` and attempts to fetch the claim queue.
/// Returns `ClaimQueueSnapshot` or `None` if claim queue API is not supported by runtime.
/// Any specific `RuntimeApiError` is bubbled up to the caller.
pub async fn fetch_claim_queue(
sender: &mut impl SubsystemSender<RuntimeApiMessage>,
relay_parent: Hash,
) -> Result<Option<ClaimQueueSnapshot>> {
if has_required_runtime(
sender,
relay_parent,
RuntimeApiRequest::CLAIM_QUEUE_RUNTIME_REQUIREMENT,
)
.await
{
let res = request_claim_queue(relay_parent, sender)
.await
.await
.map_err(Error::RuntimeRequestCanceled)??;
Ok(Some(res.into()))
} else {
gum::trace!(target: LOG_TARGET, "Runtime doesn't support `request_claim_queue`");
Ok(None)
}
}
105 changes: 0 additions & 105 deletions polkadot/node/subsystem-util/src/vstaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,108 +18,3 @@
//!
//! This module is intended to contain common boiler plate code handling unreleased runtime API
//! calls.

use std::collections::{BTreeMap, VecDeque};

use polkadot_node_subsystem_types::messages::{RuntimeApiMessage, RuntimeApiRequest};
use polkadot_overseer::SubsystemSender;
use polkadot_primitives::{CoreIndex, Hash, Id as ParaId, ValidatorIndex};

use crate::{has_required_runtime, request_claim_queue, request_disabled_validators, runtime};

const LOG_TARGET: &'static str = "parachain::subsystem-util-vstaging";

/// A snapshot of the runtime claim queue at an arbitrary relay chain block.
#[derive(Default)]
pub struct ClaimQueueSnapshot(pub BTreeMap<CoreIndex, VecDeque<ParaId>>);

impl From<BTreeMap<CoreIndex, VecDeque<ParaId>>> for ClaimQueueSnapshot {
fn from(claim_queue_snapshot: BTreeMap<CoreIndex, VecDeque<ParaId>>) -> Self {
ClaimQueueSnapshot(claim_queue_snapshot)
}
}

impl ClaimQueueSnapshot {
/// Returns the `ParaId` that has a claim for `core_index` at the specified `depth` in the
/// claim queue. A depth of `0` means the very next block.
pub fn get_claim_for(&self, core_index: CoreIndex, depth: usize) -> Option<ParaId> {
self.0.get(&core_index)?.get(depth).copied()
}

/// Returns an iterator over all claimed cores and the claiming `ParaId` at the specified
/// `depth` in the claim queue.
pub fn iter_claims_at_depth(
&self,
depth: usize,
) -> impl Iterator<Item = (CoreIndex, ParaId)> + '_ {
self.0
.iter()
.filter_map(move |(core_index, paras)| Some((*core_index, *paras.get(depth)?)))
}

/// Returns an iterator over all claims on the given core.
pub fn iter_claims_for_core(
&self,
core_index: &CoreIndex,
) -> impl Iterator<Item = &ParaId> + '_ {
self.0.get(core_index).map(|c| c.iter()).into_iter().flatten()
}

/// Returns an iterator over the whole claim queue.
pub fn iter_all_claims(&self) -> impl Iterator<Item = (&CoreIndex, &VecDeque<ParaId>)> + '_ {
self.0.iter()
}
}

// TODO: https://github.com/paritytech/polkadot-sdk/issues/1940
/// Returns disabled validators list if the runtime supports it. Otherwise logs a debug messages and
/// returns an empty vec.
/// Once runtime ver `DISABLED_VALIDATORS_RUNTIME_REQUIREMENT` is released remove this function and
/// replace all usages with `request_disabled_validators`
pub async fn get_disabled_validators_with_fallback<Sender: SubsystemSender<RuntimeApiMessage>>(
sender: &mut Sender,
relay_parent: Hash,
) -> Result<Vec<ValidatorIndex>, runtime::Error> {
let disabled_validators = if has_required_runtime(
sender,
relay_parent,
RuntimeApiRequest::DISABLED_VALIDATORS_RUNTIME_REQUIREMENT,
)
.await
{
request_disabled_validators(relay_parent, sender)
.await
.await
.map_err(runtime::Error::RuntimeRequestCanceled)??
} else {
gum::debug!(target: LOG_TARGET, "Runtime doesn't support `DisabledValidators` - continuing with an empty disabled validators set");
vec![]
};

Ok(disabled_validators)
}

/// Checks if the runtime supports `request_claim_queue` and attempts to fetch the claim queue.
/// Returns `ClaimQueueSnapshot` or `None` if claim queue API is not supported by runtime.
/// Any specific `RuntimeApiError` is bubbled up to the caller.
pub async fn fetch_claim_queue(
sender: &mut impl SubsystemSender<RuntimeApiMessage>,
relay_parent: Hash,
) -> Result<Option<ClaimQueueSnapshot>, runtime::Error> {
if has_required_runtime(
sender,
relay_parent,
RuntimeApiRequest::CLAIM_QUEUE_RUNTIME_REQUIREMENT,
)
.await
{
let res = request_claim_queue(relay_parent, sender)
.await
.await
.map_err(runtime::Error::RuntimeRequestCanceled)??;
Ok(Some(res.into()))
} else {
gum::trace!(target: LOG_TARGET, "Runtime doesn't support `request_claim_queue`");
Ok(None)
}
}
2 changes: 1 addition & 1 deletion polkadot/node/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use pallet_staking::Forcing;
use polkadot_primitives::{
vstaging::SchedulerParams, AccountId, AssignmentId, ValidatorId, MAX_CODE_SIZE, MAX_POV_SIZE,
AccountId, AssignmentId, SchedulerParams, ValidatorId, MAX_CODE_SIZE, MAX_POV_SIZE,
};
use polkadot_service::chain_spec::{get_account_id_from_seed, get_from_seed, Extensions};
use polkadot_test_runtime::BABE_GENESIS_EPOCH_CONFIG;
Expand Down
Loading
Loading