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

Disabled validators runtime API #1257

Merged
merged 13 commits into from
Oct 12, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ impl RuntimeApiSubsystemClient for BlockChainRpcClient {
Ok(self.rpc_client.parachain_host_minimum_backing_votes(at, session_index).await?)
}

async fn disabled_validators(
&self,
at: Hash,
) -> Result<Vec<polkadot_primitives::ValidatorIndex>, ApiError> {
Ok(self.rpc_client.parachain_host_disabled_validators(at).await?)
}

async fn staging_async_backing_params(&self, at: Hash) -> Result<AsyncBackingParams, ApiError> {
Ok(self.rpc_client.parachain_host_staging_async_backing_params(at).await?)
}
Expand Down
8 changes: 8 additions & 0 deletions cumulus/client/relay-chain-rpc-interface/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,14 @@ impl RelayChainRpcClient {
.await
}

pub async fn parachain_host_disabled_validators(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not sure why we need to expose everything via RPC. I personally would limit the public facing APIs as much as possible. Anything that is exposed, we need to assume we can not change it without breaking things.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The very least we should communicate clearly that these RPC calls are an unstable feature, which might change anytime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paritytech/cumulus#2160 (comment)

Well, I agree we should limit the scope for collators. But this is out of scope (hehe) of this PR.

&self,
at: RelayHash,
) -> Result<Vec<ValidatorIndex>, RelayChainError> {
self.call_remote_runtime_function("ParachainHost_disabled_validators", at, None::<()>)
.await
}

#[allow(missing_docs)]
pub async fn parachain_host_staging_async_backing_params(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use constants::{
bridge_hub_polkadot, bridge_hub_rococo, collectives, kusama, penpal, polkadot, rococo, westend,
};
use impls::{RococoWococoMessageHandler, WococoRococoMessageHandler};
use polkadot_primitives::runtime_api::runtime_decl_for_parachain_host::ParachainHostV6;

// Substrate
use frame_support::traits::OnInitialize;
Expand Down
18 changes: 18 additions & 0 deletions polkadot/node/core/runtime-api/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(crate) struct RequestResultCache {
key_ownership_proof:
LruMap<(Hash, ValidatorId), Option<vstaging::slashing::OpaqueKeyOwnershipProof>>,
minimum_backing_votes: LruMap<SessionIndex, u32>,
disabled_validators: LruMap<Hash, Vec<ValidatorIndex>>,

staging_para_backing_state: LruMap<(Hash, ParaId), Option<vstaging::BackingState>>,
staging_async_backing_params: LruMap<Hash, vstaging::AsyncBackingParams>,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl Default for RequestResultCache {
unapplied_slashes: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
key_ownership_proof: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
minimum_backing_votes: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
disabled_validators: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),

staging_para_backing_state: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
staging_async_backing_params: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
Expand Down Expand Up @@ -448,6 +450,21 @@ impl RequestResultCache {
self.minimum_backing_votes.insert(session_index, minimum_backing_votes);
}

pub(crate) fn disabled_validators(
&mut self,
relay_parent: &Hash,
) -> Option<&Vec<ValidatorIndex>> {
self.disabled_validators.get(relay_parent).map(|v| &*v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what that .map(|v| &*v) does?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It converts &mut to &

}

pub(crate) fn cache_disabled_validators(
&mut self,
relay_parent: Hash,
disabled_validators: Vec<ValidatorIndex>,
) {
self.disabled_validators.insert(relay_parent, disabled_validators);
}

pub(crate) fn staging_para_backing_state(
&mut self,
key: (Hash, ParaId),
Expand Down Expand Up @@ -524,6 +541,7 @@ pub(crate) enum RequestResult {
vstaging::slashing::OpaqueKeyOwnershipProof,
Option<()>,
),
DisabledValidators(Hash, Vec<ValidatorIndex>),

StagingParaBackingState(Hash, ParaId, Option<vstaging::BackingState>),
StagingAsyncBackingParams(Hash, vstaging::AsyncBackingParams),
Expand Down
10 changes: 10 additions & 0 deletions polkadot/node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ where
.requests_cache
.cache_key_ownership_proof((relay_parent, validator_id), key_ownership_proof),
SubmitReportDisputeLost(_, _, _, _) => {},
DisabledValidators(relay_parent, disabled_validators) =>
self.requests_cache.cache_disabled_validators(relay_parent, disabled_validators),

StagingParaBackingState(relay_parent, para_id, constraints) => self
.requests_cache
Expand Down Expand Up @@ -297,6 +299,8 @@ where
Request::SubmitReportDisputeLost(dispute_proof, key_ownership_proof, sender)
},
),
Request::DisabledValidators(sender) => query!(disabled_validators(), sender)
.map(|sender| Request::DisabledValidators(sender)),

Request::StagingParaBackingState(para, sender) =>
query!(staging_para_backing_state(para), sender)
Expand Down Expand Up @@ -569,6 +573,12 @@ where
ver = Request::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT,
sender
),
Request::DisabledValidators(sender) => query!(
DisabledValidators,
disabled_validators(),
ver = Request::DISABLED_VALIDATORS_RUNTIME_REQUIREMENT,
sender
),

Request::StagingParaBackingState(para, sender) => {
query!(
Expand Down
6 changes: 6 additions & 0 deletions polkadot/node/subsystem-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,9 @@ pub enum RuntimeApiRequest {
),
/// Get the minimum required backing votes.
MinimumBackingVotes(SessionIndex, RuntimeApiSender<u32>),
/// Returns all disabled validators at a given block height.
/// `V6`
DisabledValidators(RuntimeApiSender<Vec<ValidatorIndex>>),

/// Get the backing state of the given para.
/// This is a staging API that will not be available on production runtimes.
Expand Down Expand Up @@ -726,6 +729,9 @@ impl RuntimeApiRequest {
/// `MinimumBackingVotes`
pub const MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT: u32 = 6;

/// `DisabledValidators`
pub const DISABLED_VALIDATORS_RUNTIME_REQUIREMENT: u32 = 6;

/// Minimum version for backing state, required for async backing.
///
/// 99 for now, should be adjusted to VSTAGING/actual runtime version once released.
Expand Down
7 changes: 7 additions & 0 deletions polkadot/node/subsystem-types/src/runtime_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ pub trait RuntimeApiSubsystemClient {
session_index: SessionIndex,
) -> Result<u32, ApiError>;

/// Gets the disabled validators at a specific block height
async fn disabled_validators(&self, at: Hash) -> Result<Vec<ValidatorIndex>, ApiError>;

// === Asynchronous backing API ===

/// Returns candidate's acceptance limitations for asynchronous backing for a relay parent.
Expand Down Expand Up @@ -489,6 +492,10 @@ where
self.client.runtime_api().minimum_backing_votes(at)
}

async fn disabled_validators(&self, at: Hash) -> Result<Vec<ValidatorIndex>, ApiError> {
self.client.runtime_api().disabled_validators(at)
}

async fn staging_para_backing_state(
&self,
at: Hash,
Expand Down
1 change: 1 addition & 0 deletions polkadot/node/subsystem-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ specialize_requests! {
fn request_unapplied_slashes() -> Vec<(SessionIndex, CandidateHash, slashing::PendingSlashes)>; UnappliedSlashes;
fn request_key_ownership_proof(validator_id: ValidatorId) -> Option<slashing::OpaqueKeyOwnershipProof>; KeyOwnershipProof;
fn request_submit_report_dispute_lost(dp: slashing::DisputeProof, okop: slashing::OpaqueKeyOwnershipProof) -> Option<()>; SubmitReportDisputeLost;
fn request_disabled_validators() -> Vec<ValidatorIndex>; DisabledValidators;

fn request_staging_async_backing_params() -> vstaging_primitives::AsyncBackingParams; StagingAsyncBackingParams;
}
Expand Down
4 changes: 4 additions & 0 deletions polkadot/primitives/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ sp_api::decl_runtime_apis! {
#[api_version(6)]
fn minimum_backing_votes() -> u32;

/// Returns a sorted Vec with the `ValidatorIndex` of all disabled validators.
#[api_version(6)]
fn disabled_validators() -> Vec<ValidatorIndex>;

/***** Asynchronous backing *****/

/// Returns the state of parachain backing for a given para.
Expand Down
11 changes: 10 additions & 1 deletion polkadot/runtime/parachains/src/runtime_api_impl/vstaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use primitives::{
AsyncBackingParams, BackingState, CandidatePendingAvailability, Constraints,
InboundHrmpLimitations, OutboundHrmpChannelLimitations,
},
Id as ParaId,
Id as ParaId, ValidatorIndex,
};
use sp_std::prelude::*;

Expand Down Expand Up @@ -123,3 +123,12 @@ pub fn async_backing_params<T: configuration::Config>() -> AsyncBackingParams {
pub fn minimum_backing_votes<T: initializer::Config>() -> u32 {
<configuration::Pallet<T>>::config().minimum_backing_votes
}

/// Implementation for `DisabledValidators`
pub fn disabled_validators<T: pallet_session::Config>() -> Vec<ValidatorIndex> {
<pallet_session::Pallet<T>>::disabled_validators()
Overkillus marked this conversation as resolved.
Show resolved Hide resolved
.iter()
.cloned()
.map(|v| ValidatorIndex(v))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's correct since the type ValidatorIndex implies shuffling (and currently truncation to max_validators) done in the shared pallet. Session indices are raw.

.collect()
}
13 changes: 12 additions & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ use runtime_parachains::{
inclusion::{AggregateMessageOrigin, UmpQueueId},
initializer as parachains_initializer, origin as parachains_origin, paras as parachains_paras,
paras_inherent as parachains_paras_inherent,
runtime_api_impl::v5 as parachains_runtime_api_impl,
runtime_api_impl::{
v5 as parachains_runtime_api_impl, vstaging as parachains_staging_runtime_api_impl,
},
scheduler as parachains_scheduler, session_info as parachains_session_info,
shared as parachains_shared,
};
Expand Down Expand Up @@ -1713,6 +1715,7 @@ sp_api::impl_runtime_apis! {
}
}

#[api_version(6)]
impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
fn validators() -> Vec<ValidatorId> {
parachains_runtime_api_impl::validators::<Runtime>()
Expand Down Expand Up @@ -1843,6 +1846,14 @@ sp_api::impl_runtime_apis! {
key_ownership_proof,
)
}

fn minimum_backing_votes() -> u32 {
parachains_staging_runtime_api_impl::minimum_backing_votes::<Runtime>()
}

fn disabled_validators() -> Vec<ValidatorIndex> {
parachains_staging_runtime_api_impl::disabled_validators::<Runtime>()
}
}

#[api_version(3)]
Expand Down
4 changes: 4 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,10 @@ sp_api::impl_runtime_apis! {
fn minimum_backing_votes() -> u32 {
parachains_staging_runtime_api_impl::minimum_backing_votes::<Runtime>()
}

fn disabled_validators() -> Vec<ValidatorIndex> {
parachains_staging_runtime_api_impl::disabled_validators::<Runtime>()
}
}

impl beefy_primitives::BeefyApi<Block, BeefyId> for Runtime {
Expand Down