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
@@ -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 async_backing_params(&self, at: Hash) -> Result<AsyncBackingParams, ApiError> {
Ok(self.rpc_client.parachain_host_async_backing_params(at).await?)
}
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
@@ -597,6 +597,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_async_backing_params(
&self,
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ decl_test_relay_chains! {
Hrmp: kusama_runtime::Hrmp,
}
},
#[api_version(7)]
#[api_version(8)]
pub struct Westend {
genesis = westend::genesis(),
on_init = (),
@@ -79,7 +79,7 @@ decl_test_relay_chains! {
Balances: westend_runtime::Balances,
}
},
#[api_version(7)]
#[api_version(8)]
pub struct Rococo {
genesis = rococo::genesis(),
on_init = (),
@@ -94,7 +94,7 @@ decl_test_relay_chains! {
Balances: rococo_runtime::Balances,
}
},
#[api_version(7)]
#[api_version(8)]
pub struct Wococo {
genesis = rococo::genesis(),
on_init = (),
18 changes: 18 additions & 0 deletions polkadot/node/core/runtime-api/src/cache.rs
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ pub(crate) struct RequestResultCache {
unapplied_slashes: LruMap<Hash, Vec<(SessionIndex, CandidateHash, slashing::PendingSlashes)>>,
key_ownership_proof: LruMap<(Hash, ValidatorId), Option<slashing::OpaqueKeyOwnershipProof>>,
minimum_backing_votes: LruMap<SessionIndex, u32>,
disabled_validators: LruMap<Hash, Vec<ValidatorIndex>>,
para_backing_state: LruMap<(Hash, ParaId), Option<async_backing::BackingState>>,
async_backing_params: LruMap<Hash, async_backing::AsyncBackingParams>,
}
@@ -96,6 +97,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)),
para_backing_state: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
async_backing_params: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
}
@@ -444,6 +446,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 para_backing_state(
&mut self,
key: (Hash, ParaId),
@@ -520,6 +537,7 @@ pub(crate) enum RequestResult {
slashing::OpaqueKeyOwnershipProof,
Option<()>,
),
DisabledValidators(Hash, Vec<ValidatorIndex>),
ParaBackingState(Hash, ParaId, Option<async_backing::BackingState>),
AsyncBackingParams(Hash, async_backing::AsyncBackingParams),
}
10 changes: 10 additions & 0 deletions polkadot/node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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),
ParaBackingState(relay_parent, para_id, constraints) => self
.requests_cache
.cache_para_backing_state((relay_parent, para_id), constraints),
@@ -296,6 +298,8 @@ where
Request::SubmitReportDisputeLost(dispute_proof, key_ownership_proof, sender)
},
),
Request::DisabledValidators(sender) => query!(disabled_validators(), sender)
.map(|sender| Request::DisabledValidators(sender)),
Request::ParaBackingState(para, sender) => query!(para_backing_state(para), sender)
.map(|sender| Request::ParaBackingState(para, sender)),
Request::AsyncBackingParams(sender) => query!(async_backing_params(), sender)
@@ -565,6 +569,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::ParaBackingState(para, sender) => {
query!(
ParaBackingState,
4 changes: 4 additions & 0 deletions polkadot/node/core/runtime-api/src/tests.rs
Original file line number Diff line number Diff line change
@@ -268,6 +268,10 @@ impl RuntimeApiSubsystemClient for MockSubsystemClient {
async fn minimum_backing_votes(&self, _: Hash, _: SessionIndex) -> Result<u32, ApiError> {
todo!("Not required for tests")
}

async fn disabled_validators(&self, _: Hash) -> Result<Vec<ValidatorIndex>, ApiError> {
todo!("Not required for tests")
}
}

#[test]
5 changes: 5 additions & 0 deletions polkadot/node/subsystem-types/src/messages.rs
Original file line number Diff line number Diff line change
@@ -695,6 +695,8 @@ pub enum RuntimeApiRequest {
),
/// Get the minimum required backing votes.
MinimumBackingVotes(SessionIndex, RuntimeApiSender<u32>),
/// Returns all disabled validators at a given block height.
DisabledValidators(RuntimeApiSender<Vec<ValidatorIndex>>),
/// Get the backing state of the given para.
ParaBackingState(ParaId, RuntimeApiSender<Option<async_backing::BackingState>>),
/// Get candidate's acceptance limitations for asynchronous backing for a relay parent.
@@ -726,6 +728,9 @@ impl RuntimeApiRequest {

/// Minimum version to enable asynchronous backing: `AsyncBackingParams` and `ParaBackingState`.
pub const ASYNC_BACKING_STATE_RUNTIME_REQUIREMENT: u32 = 7;

/// `DisabledValidators`
pub const DISABLED_VALIDATORS_RUNTIME_REQUIREMENT: u32 = 8;
}

/// A message to the Runtime API subsystem.
9 changes: 8 additions & 1 deletion polkadot/node/subsystem-types/src/runtime_client.rs
Original file line number Diff line number Diff line change
@@ -255,6 +255,10 @@ pub trait RuntimeApiSubsystemClient {
at: Hash,
para_id: Id,
) -> Result<Option<async_backing::BackingState>, ApiError>;

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

/// Default implementation of [`RuntimeApiSubsystemClient`] using the client.
@@ -497,11 +501,14 @@ where
self.client.runtime_api().para_backing_state(at, para_id)
}

/// Returns candidate's acceptance limitations for asynchronous backing for a relay parent.
async fn async_backing_params(
&self,
at: Hash,
) -> Result<async_backing::AsyncBackingParams, ApiError> {
self.client.runtime_api().async_backing_params(at)
}

async fn disabled_validators(&self, at: Hash) -> Result<Vec<ValidatorIndex>, ApiError> {
self.client.runtime_api().disabled_validators(at)
}
}
1 change: 1 addition & 0 deletions polkadot/node/subsystem-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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_async_backing_params() -> AsyncBackingParams; AsyncBackingParams;
}

7 changes: 7 additions & 0 deletions polkadot/primitives/src/runtime_api.rs
Original file line number Diff line number Diff line change
@@ -248,6 +248,7 @@ sp_api::decl_runtime_apis! {
#[api_version(6)]
fn minimum_backing_votes() -> u32;


/***** Added in v7: Asynchronous backing *****/

/// Returns the state of parachain backing for a given para.
@@ -257,5 +258,11 @@ sp_api::decl_runtime_apis! {
/// Returns candidate's acceptance limitations for asynchronous backing for a relay parent.
#[api_version(7)]
fn async_backing_params() -> AsyncBackingParams;

/***** Added in v8 *****/

/// Returns a list of all disabled validators at the given block.
#[api_version(8)]
fn disabled_validators() -> Vec<ValidatorIndex>;
}
}
27 changes: 27 additions & 0 deletions polkadot/runtime/parachains/src/runtime_api_impl/vstaging.rs
Original file line number Diff line number Diff line change
@@ -15,3 +15,30 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! Put implementations of functions from staging APIs here.

use crate::shared;
use primitives::ValidatorIndex;
use sp_std::{collections::btree_map::BTreeMap, prelude::Vec};

/// Implementation for `DisabledValidators`
// CAVEAT: this should only be called on the node side
// as it might produce incorrect results on session boundaries
pub fn disabled_validators<T>() -> Vec<ValidatorIndex>
where
T: pallet_session::Config + shared::Config,
{
let shuffled_indices = <shared::Pallet<T>>::active_validator_indices();
// mapping from raw validator index to `ValidatorIndex`
Overkillus marked this conversation as resolved.
Show resolved Hide resolved
// this computation is the same within a session, but should be cheap
let reverse_index = shuffled_indices
.iter()
.enumerate()
.map(|(i, v)| (v.0, ValidatorIndex(i as u32)))
.collect::<BTreeMap<u32, ValidatorIndex>>();

// we might have disabled validators who are not parachain validators
<pallet_session::Pallet<T>>::disabled_validators()
Overkillus marked this conversation as resolved.
Show resolved Hide resolved
.iter()
.filter_map(|v| reverse_index.get(v).cloned())
.collect()
}
11 changes: 9 additions & 2 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::v7 as parachains_runtime_api_impl,
runtime_api_impl::{
v7 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,
};
@@ -1539,7 +1541,7 @@ sp_api::impl_runtime_apis! {
}
}

#[api_version(7)]
#[api_version(8)]
impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
fn validators() -> Vec<ValidatorId> {
parachains_runtime_api_impl::validators::<Runtime>()
@@ -1682,6 +1684,11 @@ sp_api::impl_runtime_apis! {
fn async_backing_params() -> primitives::AsyncBackingParams {
parachains_runtime_api_impl::async_backing_params::<Runtime>()
}

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

}

#[api_version(3)]
10 changes: 8 additions & 2 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -65,7 +65,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, reward_points as parachains_reward_points,
runtime_api_impl::v7 as parachains_runtime_api_impl,
runtime_api_impl::{
v7 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,
};
@@ -1645,7 +1647,7 @@ sp_api::impl_runtime_apis! {
}
}

#[api_version(7)]
#[api_version(8)]
impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
fn validators() -> Vec<ValidatorId> {
parachains_runtime_api_impl::validators::<Runtime>()
@@ -1788,6 +1790,10 @@ sp_api::impl_runtime_apis! {
fn async_backing_params() -> primitives::AsyncBackingParams {
parachains_runtime_api_impl::async_backing_params::<Runtime>()
}

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

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