From 29dc4a00e814c5407ad5aa6082eea90551284d32 Mon Sep 17 00:00:00 2001 From: Stephane Gurgenidze Date: Tue, 18 Feb 2025 18:49:22 +0400 Subject: [PATCH] refactor(runtime-api): remove redundant version checks --- .../statement-distribution/src/error.rs | 2 +- .../statement-distribution/src/v2/mod.rs | 6 +- polkadot/node/subsystem-util/src/lib.rs | 8 +- .../node/subsystem-util/src/runtime/mod.rs | 82 ++++--------------- 4 files changed, 20 insertions(+), 78 deletions(-) diff --git a/polkadot/node/network/statement-distribution/src/error.rs b/polkadot/node/network/statement-distribution/src/error.rs index cff9afbf86675..d48eb9f013e4b 100644 --- a/polkadot/node/network/statement-distribution/src/error.rs +++ b/polkadot/node/network/statement-distribution/src/error.rs @@ -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), diff --git a/polkadot/node/network/statement-distribution/src/v2/mod.rs b/polkadot/node/network/statement-distribution/src/v2/mod.rs index 3034ca7caf851..c1206d90c6cd8 100644 --- a/polkadot/node/network/statement-distribution/src/v2/mod.rs +++ b/polkadot/node/network/statement-distribution/src/v2/mod.rs @@ -586,11 +586,13 @@ pub(crate) async fn handle_active_leaves_update( 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(); diff --git a/polkadot/node/subsystem-util/src/lib.rs b/polkadot/node/subsystem-util/src/lib.rs index 6b069ee86113f..274b76a3e4cbf 100644 --- a/polkadot/node/subsystem-util/src/lib.rs +++ b/polkadot/node/subsystem-util/src/lib.rs @@ -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}; @@ -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) } diff --git a/polkadot/node/subsystem-util/src/runtime/mod.rs b/polkadot/node/subsystem-util/src/runtime/mod.rs index dd843cfb01fa9..57ee9b33af1f0 100644 --- a/polkadot/node/subsystem-util/src/runtime/mod.rs +++ b/polkadot/node/subsystem-util/src/runtime/mod.rs @@ -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. @@ -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) }, @@ -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, ) -> Result { - 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, ) -> Result> { - 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. @@ -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: &mut Sender, - relay_parent: Hash, -) -> Result> { - 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, @@ -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,