From 7418770e6ad08c62306a325106ac396b7fd83fef Mon Sep 17 00:00:00 2001 From: Tsvetomir Dimitrov Date: Thu, 10 Aug 2023 11:55:32 +0300 Subject: [PATCH] Add `disabled_validators` in staging runtime api --- node/core/runtime-api/src/cache.rs | 18 ++++++++++++++++++ node/core/runtime-api/src/lib.rs | 10 ++++++++++ node/subsystem-types/src/messages.rs | 6 ++++++ node/subsystem-types/src/runtime_client.rs | 7 +++++++ primitives/src/runtime_api.rs | 6 ++++++ .../src/runtime_api_impl/vstaging.rs | 11 ++++++++++- runtime/rococo/src/lib.rs | 9 ++++++++- 7 files changed, 65 insertions(+), 2 deletions(-) diff --git a/node/core/runtime-api/src/cache.rs b/node/core/runtime-api/src/cache.rs index 26aaf3fb6ec8..5cd1b9e26c03 100644 --- a/node/core/runtime-api/src/cache.rs +++ b/node/core/runtime-api/src/cache.rs @@ -68,6 +68,7 @@ pub(crate) struct RequestResultCache { LruCache>, key_ownership_proof: LruCache<(Hash, ValidatorId), Option>, + disabled_validators: LruCache>, staging_para_backing_state: LruCache<(Hash, ParaId), Option>, staging_async_backing_params: LruCache, @@ -100,6 +101,7 @@ impl Default for RequestResultCache { disputes: LruCache::new(DEFAULT_CACHE_CAP), unapplied_slashes: LruCache::new(DEFAULT_CACHE_CAP), key_ownership_proof: LruCache::new(DEFAULT_CACHE_CAP), + disabled_validators: LruCache::new(DEFAULT_CACHE_CAP), staging_para_backing_state: LruCache::new(DEFAULT_CACHE_CAP), staging_async_backing_params: LruCache::new(DEFAULT_CACHE_CAP), @@ -437,6 +439,21 @@ impl RequestResultCache { None } + pub(crate) fn disabled_validators( + &mut self, + relay_parent: &Hash, + ) -> Option<&Vec> { + self.disabled_validators.get(relay_parent) + } + + pub(crate) fn cache_disabled_validators( + &mut self, + relay_parent: Hash, + disabled_validators: Vec, + ) { + self.disabled_validators.put(relay_parent, disabled_validators); + } + pub(crate) fn staging_para_backing_state( &mut self, key: (Hash, ParaId), @@ -512,6 +529,7 @@ pub(crate) enum RequestResult { vstaging::slashing::OpaqueKeyOwnershipProof, Option<()>, ), + DisabledValidators(Hash, Vec), StagingParaBackingState(Hash, ParaId, Option), StagingAsyncBackingParams(Hash, vstaging::AsyncBackingParams), diff --git a/node/core/runtime-api/src/lib.rs b/node/core/runtime-api/src/lib.rs index 78531d41272b..04f5bb3fd706 100644 --- a/node/core/runtime-api/src/lib.rs +++ b/node/core/runtime-api/src/lib.rs @@ -163,6 +163,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 @@ -294,6 +296,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) @@ -551,6 +555,12 @@ where ver = Request::SUBMIT_REPORT_DISPUTE_LOST_RUNTIME_REQUIREMENT, sender ), + Request::DisabledValidators(sender) => query!( + DisabledValidators, + disabled_validators(), + ver = Request::DISABLED_VALIDATORS_RUNTIME_REQUIREMENT, + sender + ), Request::StagingParaBackingState(para, sender) => { query!( diff --git a/node/subsystem-types/src/messages.rs b/node/subsystem-types/src/messages.rs index 8adc39eed56d..0d85034103d0 100644 --- a/node/subsystem-types/src/messages.rs +++ b/node/subsystem-types/src/messages.rs @@ -691,6 +691,9 @@ pub enum RuntimeApiRequest { slashing::OpaqueKeyOwnershipProof, RuntimeApiSender>, ), + /// Returns all disabled validators at a given block height. + /// `V6` + DisabledValidators(RuntimeApiSender>), /// Get the backing state of the given para. /// This is a staging API that will not be available on production runtimes. @@ -719,6 +722,9 @@ impl RuntimeApiRequest { /// `SubmitReportDisputeLost` pub const SUBMIT_REPORT_DISPUTE_LOST_RUNTIME_REQUIREMENT: u32 = 5; + /// `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. diff --git a/node/subsystem-types/src/runtime_client.rs b/node/subsystem-types/src/runtime_client.rs index 312cc4eec6ce..77ef358b9b0e 100644 --- a/node/subsystem-types/src/runtime_client.rs +++ b/node/subsystem-types/src/runtime_client.rs @@ -232,6 +232,9 @@ pub trait RuntimeApiSubsystemClient { session_index: SessionIndex, ) -> Result, ApiError>; + /// Gets the disabled validators at a specific block height + async fn disabled_validators(&self, at: Hash) -> Result, ApiError>; + // === Asynchronous backing API === /// Returns candidate's acceptance limitations for asynchronous backing for a relay parent. @@ -473,6 +476,10 @@ where runtime_api.submit_report_dispute_lost(at, dispute_proof, key_ownership_proof) } + async fn disabled_validators(&self, at: Hash) -> Result, ApiError> { + self.client.runtime_api().disabled_validators(at) + } + async fn staging_para_backing_state( &self, at: Hash, diff --git a/primitives/src/runtime_api.rs b/primitives/src/runtime_api.rs index 483256fe20f3..463bb789b1ed 100644 --- a/primitives/src/runtime_api.rs +++ b/primitives/src/runtime_api.rs @@ -240,6 +240,12 @@ sp_api::decl_runtime_apis! { key_ownership_proof: vstaging::slashing::OpaqueKeyOwnershipProof, ) -> Option<()>; + /* Staging APIs */ + + /// Returns a sorted Vec with the `ValidatorIndex` of all disabled validators. + #[api_version(6)] + fn disabled_validators() -> Vec; + /***** Asynchronous backing *****/ /// Returns the state of parachain backing for a given para. diff --git a/runtime/parachains/src/runtime_api_impl/vstaging.rs b/runtime/parachains/src/runtime_api_impl/vstaging.rs index 5406428377d0..af1f3b17b7c5 100644 --- a/runtime/parachains/src/runtime_api_impl/vstaging.rs +++ b/runtime/parachains/src/runtime_api_impl/vstaging.rs @@ -23,7 +23,7 @@ use primitives::{ AsyncBackingParams, BackingState, CandidatePendingAvailability, Constraints, InboundHrmpLimitations, OutboundHrmpChannelLimitations, }, - Id as ParaId, + Id as ParaId, ValidatorIndex, }; use sp_std::prelude::*; @@ -118,3 +118,12 @@ pub fn backing_state( pub fn async_backing_params() -> AsyncBackingParams { >::config().async_backing_params } + +/// Implementation for `DisabledValidators` +pub fn disabled_validators() -> Vec { + >::disabled_validators() + .iter() + .cloned() + .map(|v| ValidatorIndex(v)) + .collect() +} diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index fb2a56c8100c..59a04a45f767 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -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, }; @@ -1715,6 +1717,7 @@ sp_api::impl_runtime_apis! { } } + #[api_version(6)] impl primitives::runtime_api::ParachainHost for Runtime { fn validators() -> Vec { parachains_runtime_api_impl::validators::() @@ -1845,6 +1848,10 @@ sp_api::impl_runtime_apis! { key_ownership_proof, ) } + + fn disabled_validators() -> Vec { + parachains_staging_runtime_api_impl::disabled_validators::() + } } #[api_version(3)]