Skip to content

Commit

Permalink
Partial add of bulk_core_count to HostConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
BradleyOlson64 committed Nov 30, 2023
1 parent b9421f3 commit d4d22e3
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ impl RuntimeApiSubsystemClient for BlockChainRpcClient {
async fn node_features(&self, at: Hash) -> Result<NodeFeatures, ApiError> {
Ok(self.rpc_client.parachain_host_node_features(at).await?)
}

async fn bulk_core_count(&self, at: Hash) -> Result<u16, ApiError> {
Ok(self.rpc_client.parachain_host_bulk_core_count(at).await?)
}
}

#[async_trait::async_trait]
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 @@ -607,6 +607,14 @@ impl RelayChainRpcClient {
.await
}

pub async fn parachain_host_bulk_core_count(
&self,
at: RelayHash
) -> Result<u16, RelayChainError> {
self.call_remote_runtime_function("ParachainHost_bulk_core_count", at, None::<()>)
.await
}

pub async fn parachain_host_disabled_validators(
&self,
at: RelayHash,
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 @@ -68,6 +68,7 @@ pub(crate) struct RequestResultCache {
para_backing_state: LruMap<(Hash, ParaId), Option<async_backing::BackingState>>,
async_backing_params: LruMap<Hash, async_backing::AsyncBackingParams>,
node_features: LruMap<SessionIndex, vstaging::NodeFeatures>,
bulk_core_count: LruMap<SessionIndex, u16>,
}

impl Default for RequestResultCache {
Expand Down Expand Up @@ -102,6 +103,7 @@ impl Default for RequestResultCache {
para_backing_state: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
async_backing_params: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
node_features: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
bulk_core_count: LruMap::new(ByLength::new(DEFAULT_CACHE_CAP)),
}
}
}
Expand Down Expand Up @@ -507,6 +509,21 @@ impl RequestResultCache {
) {
self.async_backing_params.insert(key, value);
}

pub(crate) fn bulk_core_count(
&mut self,
session_index: SessionIndex,
) -> Option<&u16> {
self.bulk_core_count.get(&session_index).map(|v| &*v)
}

pub(crate) fn cache_bulk_core_count(
&mut self,
session_index: SessionIndex,
value: u16,
) {
self.bulk_core_count.insert(session_index, value);
}
}

pub(crate) enum RequestResult {
Expand Down Expand Up @@ -558,4 +575,5 @@ pub(crate) enum RequestResult {
ParaBackingState(Hash, ParaId, Option<async_backing::BackingState>),
AsyncBackingParams(Hash, async_backing::AsyncBackingParams),
NodeFeatures(SessionIndex, vstaging::NodeFeatures),
BulkCoreCount(SessionIndex, u16),
}
17 changes: 17 additions & 0 deletions polkadot/node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ where
self.requests_cache.cache_async_backing_params(relay_parent, params),
NodeFeatures(session_index, params) =>
self.requests_cache.cache_node_features(session_index, params),
BulkCoreCount(session_index, count) =>
self.requests_cache.cache_bulk_core_count(session_index, count)
}
}

Expand Down Expand Up @@ -324,6 +326,14 @@ where
Some(Request::NodeFeatures(index, sender))
}
},
Request::BulkCoreCount(index, sender) => {
if let Some(value) = self.requests_cache.bulk_core_count(index) {
self.metrics.on_cached_request();
let _ = sender.send(Ok(value.clone()));
None
} else {
Some(Request::BulkCoreCount(index, sender))
}
}
}

Expand Down Expand Up @@ -612,5 +622,12 @@ where
sender,
result = (index)
),
Request::BulkCoreCount(index, sender) => query!(
BulkCoreCount,
bulk_core_count(),
ver = Request::BULK_CORE_COUNT_RUNTIME_REQUIREMENT,
sender,
result = (index)
),
}
}
4 changes: 4 additions & 0 deletions polkadot/node/core/runtime-api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ impl RuntimeApiSubsystemClient for MockSubsystemClient {
async fn disabled_validators(&self, _: Hash) -> Result<Vec<ValidatorIndex>, ApiError> {
todo!("Not required for tests")
}

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

#[test]
Expand Down
5 changes: 5 additions & 0 deletions polkadot/node/subsystem-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ pub enum RuntimeApiRequest {
AsyncBackingParams(RuntimeApiSender<async_backing::AsyncBackingParams>),
/// Get the node features.
NodeFeatures(SessionIndex, RuntimeApiSender<NodeFeatures>),
/// Get the bulk core count
BulkCoreCount(SessionIndex, RuntimeApiSender<u16>),
}

impl RuntimeApiRequest {
Expand Down Expand Up @@ -751,6 +753,9 @@ impl RuntimeApiRequest {

/// `Node features`
pub const NODE_FEATURES_RUNTIME_REQUIREMENT: u32 = 9;

/// `BulkCoreCount`
pub const BULK_CORE_COUNT_RUNTIME_REQUIREMENT: u32 = 10;
}

/// A message to the Runtime API subsystem.
Expand Down
12 changes: 12 additions & 0 deletions polkadot/node/subsystem-types/src/runtime_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ pub trait RuntimeApiSubsystemClient {

/// Get the node features.
async fn node_features(&self, at: Hash) -> Result<vstaging::NodeFeatures, ApiError>;

// === v10 ===

/// Get the bulk core count
async fn bulk_core_count(&self, at: Hash) -> Result<u16, ApiError>;
}

/// Default implementation of [`RuntimeApiSubsystemClient`] using the client.
Expand Down Expand Up @@ -521,4 +526,11 @@ where
async fn disabled_validators(&self, at: Hash) -> Result<Vec<ValidatorIndex>, ApiError> {
self.client.runtime_api().disabled_validators(at)
}

async fn bulk_core_count(
&self,
at: Hash,
) -> Result<u16, ApiError> {
self.client.runtime_api().bulk_core_count(at)
}
}
29 changes: 29 additions & 0 deletions polkadot/node/subsystem-util/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,32 @@ pub async fn request_node_features(
res.map(Some)
}
}

/// Request current bulk core count
/// Pass in the session index for caching purposes, as it should only change on session boundaries.
/// Prior to runtime API version 10, just return 0.
pub async fn request_bulk_core_count(
parent: Hash,
session_index: SessionIndex,
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
) -> Result<u16> {
let res = recv_runtime(
request_from_runtime(parent, sender, |tx| {
RuntimeApiRequest::BulkCoreCount(session_index, tx)
})
.await,
)
.await;

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

Ok(0u16)
} else {
res.map(Some)
}
}
6 changes: 6 additions & 0 deletions polkadot/primitives/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,11 @@ sp_api::decl_runtime_apis! {
/// This is a staging method! Do not use on production runtimes!
#[api_version(9)]
fn node_features() -> vstaging::NodeFeatures;

/***** Added in v10 *****/

/// Get bulk core count
#[api_version(10)]
fn bulk_core_count() -> u16
}
}
4 changes: 4 additions & 0 deletions polkadot/runtime/parachains/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub struct HostConfiguration<BlockNumber> {
pub minimum_backing_votes: u32,
/// Node features enablement.
pub node_features: NodeFeatures,
/// Bulk core count
pub bulk_core_count: u16,
}

impl<BlockNumber: Default + From<u32>> Default for HostConfiguration<BlockNumber> {
Expand Down Expand Up @@ -315,6 +317,7 @@ impl<BlockNumber: Default + From<u32>> Default for HostConfiguration<BlockNumber
on_demand_ttl: 5u32.into(),
minimum_backing_votes: LEGACY_MIN_BACKING_VOTES,
node_features: NodeFeatures::EMPTY,
bulk_core_count: Default::default(),
}
}
}
Expand Down Expand Up @@ -515,6 +518,7 @@ pub mod pallet {
/// v7-v8: <https://github.com/paritytech/polkadot/pull/6969>
/// v8-v9: <https://github.com/paritytech/polkadot/pull/7577>
/// v9-v10: <https://github.com/paritytech/polkadot-sdk/pull/2177>

const STORAGE_VERSION: StorageVersion = StorageVersion::new(10);

#[pallet::pallet]
Expand Down

0 comments on commit d4d22e3

Please sign in to comment.