Skip to content

Commit

Permalink
refactor(runtime-api): remove redundant version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sw10pa committed Feb 18, 2025
1 parent fd72d58 commit 29dc4a0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 78 deletions.
2 changes: 1 addition & 1 deletion polkadot/node/network/statement-distribution/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub enum Error {
FetchSessionInfo(RuntimeApiError),

#[error("Fetching disabled validators failed {0:?}")]
FetchDisabledValidators(runtime::Error),
FetchDisabledValidators(RuntimeApiError),

#[error("Fetching validator groups failed {0:?}")]
FetchValidatorGroups(RuntimeApiError),
Expand Down
6 changes: 4 additions & 2 deletions polkadot/node/network/statement-distribution/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,13 @@ 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::runtime::get_disabled_validators_with_fallback(
ctx.sender(),
polkadot_node_subsystem_util::request_disabled_validators(
new_relay_parent,
ctx.sender(),
)
.await
.await
.map_err(JfyiError::RuntimeApiUnavailable)?
.map_err(JfyiError::FetchDisabledValidators)?
.into_iter()
.collect();
Expand Down
8 changes: 1 addition & 7 deletions polkadot/node/subsystem-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use polkadot_primitives::{
ValidationCodeHash, ValidatorId, ValidatorIndex, 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 Down Expand Up @@ -488,12 +487,7 @@ impl Validator {

let validators = validators?;

// TODO: https://github.com/paritytech/polkadot-sdk/issues/1940
// When `DisabledValidators` is released remove this and add a
// `request_disabled_validators` call here
let disabled_validators = get_disabled_validators_with_fallback(sender, parent)
.await
.map_err(|e| Error::try_from(e).expect("the conversion is infallible; qed"))?;
let disabled_validators = request_disabled_validators(parent, sender).await.await??;

Self::construct(&validators, &disabled_validators, signing_context, keystore)
}
Expand Down
82 changes: 14 additions & 68 deletions polkadot/node/subsystem-util/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ use polkadot_primitives::{
CandidateHash, CoreIndex, EncodeAs, ExecutorParams, GroupIndex, GroupRotationInfo, Hash,
Id as ParaId, IndexedVec, NodeFeatures, SessionIndex, SessionInfo, Signed, SigningContext,
UncheckedSigned, ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex,
DEFAULT_SCHEDULING_LOOKAHEAD, LEGACY_MIN_BACKING_VOTES,
DEFAULT_SCHEDULING_LOOKAHEAD,
};

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

use crate::{
has_required_runtime, 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,
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,
};

/// Errors that can happen on runtime fetches.
Expand Down Expand Up @@ -202,7 +202,7 @@ impl RuntimeInfo {
Some(result) => Ok(result),
None => {
let disabled_validators =
get_disabled_validators_with_fallback(sender, relay_parent).await?;
request_disabled_validators(relay_parent, sender).await.await??;
self.disabled_validators_cache.insert(relay_parent, disabled_validators.clone());
Ok(disabled_validators)
},
Expand Down Expand Up @@ -469,61 +469,35 @@ where
}

/// Request the min backing votes value.
/// Prior to runtime API version 6, just return a hardcoded constant.
pub async fn request_min_backing_votes(
parent: Hash,
session_index: SessionIndex,
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
) -> Result<u32> {
let min_backing_votes_res = recv_runtime(
recv_runtime(
request_from_runtime(parent, sender, |tx| {
RuntimeApiRequest::MinimumBackingVotes(session_index, tx)
})
.await,
)
.await;

if let Err(Error::RuntimeRequest(RuntimeApiError::NotSupported { .. })) = min_backing_votes_res
{
gum::trace!(
target: LOG_TARGET,
?parent,
"Querying the backing threshold from the runtime is not supported by the current Runtime API",
);

Ok(LEGACY_MIN_BACKING_VOTES)
} else {
min_backing_votes_res
}
.await
}

/// Request the node features enabled in the runtime.
/// Pass in the session index for caching purposes, as it should only change on session boundaries.
/// Prior to runtime API version 9, just return `None`.
pub async fn request_node_features(
parent: Hash,
session_index: SessionIndex,
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
) -> Result<Option<NodeFeatures>> {
let res = recv_runtime(
recv_runtime(
request_from_runtime(parent, sender, |tx| {
RuntimeApiRequest::NodeFeatures(session_index, tx)
})
.await,
)
.await;

if let Err(Error::RuntimeRequest(RuntimeApiError::NotSupported { .. })) = res {
gum::trace!(
target: LOG_TARGET,
?parent,
"Querying the node features from the runtime is not supported by the current Runtime API",
);

Ok(None)
} else {
res.map(Some)
}
.await
.map(Some)
}

/// A snapshot of the runtime claim queue at an arbitrary relay chain block.
Expand Down Expand Up @@ -568,34 +542,6 @@ impl ClaimQueueSnapshot {
}
}

// 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)
}

/// Fetch the claim queue and wrap it into a helpful `ClaimQueueSnapshot`
pub async fn fetch_claim_queue(
sender: &mut impl SubsystemSender<RuntimeApiMessage>,
Expand All @@ -609,8 +555,8 @@ pub async fn fetch_claim_queue(
Ok(cq.into())
}

/// 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.
/// Returns the lookahead from the scheduler params if the runtime supports it,
/// or default value if scheduling lookahead API is not supported by runtime.
pub async fn fetch_scheduling_lookahead(
parent: Hash,
session_index: SessionIndex,
Expand Down

0 comments on commit 29dc4a0

Please sign in to comment.