Skip to content

Commit 27cc8ea

Browse files
committed
refactor: replace helper functions with specialized runtime API requests
1 parent ce9fc12 commit 27cc8ea

File tree

12 files changed

+71
-64
lines changed

12 files changed

+71
-64
lines changed

polkadot/node/collation-generation/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ use polkadot_node_subsystem::{
4444
SubsystemContext, SubsystemError, SubsystemResult, SubsystemSender,
4545
};
4646
use polkadot_node_subsystem_util::{
47-
request_claim_queue, request_persisted_validation_data, request_session_index_for_child,
48-
request_validation_code_hash, request_validators,
49-
runtime::{request_node_features, ClaimQueueSnapshot},
47+
request_claim_queue, request_node_features, request_persisted_validation_data,
48+
request_session_index_for_child, request_validation_code_hash, request_validators,
49+
runtime::ClaimQueueSnapshot,
5050
};
5151
use polkadot_primitives::{
5252
collator_signature_payload,
@@ -509,7 +509,8 @@ impl SessionInfoCache {
509509
let n_validators =
510510
request_validators(relay_parent, &mut sender.clone()).await.await??.len();
511511

512-
let node_features = request_node_features(relay_parent, session_index, sender).await?;
512+
let node_features =
513+
request_node_features(relay_parent, session_index, sender).await.await??;
513514

514515
let info = PerSessionInfo {
515516
v2_receipts: node_features

polkadot/node/core/backing/src/lib.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ use polkadot_node_subsystem::{
9999
use polkadot_node_subsystem_util::{
100100
self as util,
101101
backing_implicit_view::View as ImplicitView,
102-
request_claim_queue, request_disabled_validators, request_session_executor_params,
103-
request_session_index_for_child, request_validator_groups, request_validators,
104-
runtime::{self, request_min_backing_votes, ClaimQueueSnapshot},
102+
request_claim_queue, request_disabled_validators, request_min_backing_votes,
103+
request_node_features, request_session_executor_params, request_session_index_for_child,
104+
request_validator_groups, request_validators,
105+
runtime::{self, ClaimQueueSnapshot},
105106
Validator,
106107
};
107108
use polkadot_parachain_primitives::primitives::IsSystem;
@@ -125,7 +126,6 @@ use polkadot_statement_table::{
125126
Context as TableContextTrait, Table,
126127
};
127128
use sp_keystore::KeystorePtr;
128-
use util::runtime::request_node_features;
129129

130130
mod error;
131131

@@ -322,14 +322,20 @@ impl PerSessionCache {
322322
session_index: SessionIndex,
323323
parent: Hash,
324324
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
325-
) -> Result<NodeFeatures, Error> {
325+
) -> Result<NodeFeatures, RuntimeApiError> {
326326
// Try to get the node features from the cache.
327327
if let Some(node_features) = self.node_features_cache.get(&session_index) {
328328
return Ok(node_features.clone());
329329
}
330330

331331
// Fetch the node features from the runtime since it was not in the cache.
332-
let node_features = request_node_features(parent, session_index, sender).await?;
332+
let node_features = request_node_features(parent, session_index, sender)
333+
.await
334+
.await
335+
.map_err(|err| RuntimeApiError::Execution {
336+
runtime_api_name: "NodeFeatures",
337+
source: Arc::new(err),
338+
})??;
333339

334340
// Cache the fetched node features for future use.
335341
self.node_features_cache.insert(session_index, node_features.clone());
@@ -387,11 +393,12 @@ impl PerSessionCache {
387393

388394
// Fetch the value from the runtime since it was not in the cache.
389395
let minimum_backing_votes = request_min_backing_votes(parent, session_index, sender)
396+
.await
390397
.await
391398
.map_err(|err| RuntimeApiError::Execution {
392399
runtime_api_name: "MinimumBackingVotes",
393400
source: Arc::new(err),
394-
})?;
401+
})??;
395402

396403
// Cache the fetched value for future use.
397404
self.minimum_backing_votes_cache.insert(session_index, minimum_backing_votes);
@@ -1151,8 +1158,8 @@ async fn construct_per_relay_parent_state<Context>(
11511158
let validators = per_session_cache.validators(session_index, parent, ctx.sender()).await;
11521159
let validators = try_runtime_api!(validators);
11531160

1154-
let node_features =
1155-
per_session_cache.node_features(session_index, parent, ctx.sender()).await?;
1161+
let node_features = per_session_cache.node_features(session_index, parent, ctx.sender()).await;
1162+
let node_features = try_runtime_api!(node_features);
11561163

11571164
let inject_core_index = node_features
11581165
.get(FeatureIndex::ElasticScalingMVP as usize)

polkadot/node/core/provisioner/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub enum Error {
4747
#[error("failed to get session index")]
4848
CanceledSessionIndex(#[source] oneshot::Canceled),
4949

50+
#[error("failed to get node features")]
51+
CanceledNodeFeatures(#[source] oneshot::Canceled),
52+
5053
#[error("failed to get backed candidates")]
5154
CanceledBackedCandidates(#[source] oneshot::Canceled),
5255

polkadot/node/core/provisioner/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ use polkadot_node_subsystem::{
3535
SubsystemError,
3636
};
3737
use polkadot_node_subsystem_util::{
38-
request_availability_cores, request_session_index_for_child, runtime::request_node_features,
39-
TimeoutExt,
38+
request_availability_cores, request_node_features, request_session_index_for_child, TimeoutExt,
4039
};
4140
use polkadot_primitives::{
4241
node_features::FeatureIndex,
@@ -203,7 +202,9 @@ async fn handle_active_leaves_update(
203202
.map_err(Error::CanceledSessionIndex)??;
204203
if per_session.get(&session_index).is_none() {
205204
let elastic_scaling_mvp = request_node_features(leaf.hash, session_index, sender)
206-
.await?
205+
.await
206+
.await
207+
.map_err(Error::CanceledNodeFeatures)??
207208
.get(FeatureIndex::ElasticScalingMVP as usize)
208209
.map(|b| *b)
209210
.unwrap_or(false);

polkadot/node/network/availability-distribution/src/error.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use polkadot_primitives::SessionIndex;
2323

2424
use futures::channel::oneshot;
2525

26-
use polkadot_node_subsystem::{ChainApiError, SubsystemError};
26+
use polkadot_node_subsystem::{ChainApiError, RuntimeApiError, SubsystemError};
2727
use polkadot_node_subsystem_util::runtime;
2828

2929
use crate::LOG_TARGET;
@@ -55,6 +55,9 @@ pub enum Error {
5555
#[error("Retrieving response from Chain API unexpectedly failed with error: {0}")]
5656
ChainApi(#[from] ChainApiError),
5757

58+
#[error("Failed to get node features from the runtime")]
59+
FailedNodeFeatures(#[source] RuntimeApiError),
60+
5861
// av-store will drop the sender on any error that happens.
5962
#[error("Response channel to obtain chunk failed")]
6063
QueryChunkResponseChannel(#[source] oneshot::Canceled),
@@ -108,6 +111,7 @@ pub fn log_error(
108111
JfyiError::NoSuchCachedSession { .. } |
109112
JfyiError::QueryAvailableDataResponseChannel(_) |
110113
JfyiError::QueryChunkResponseChannel(_) |
114+
JfyiError::FailedNodeFeatures(_) |
111115
JfyiError::ErasureCoding(_) => gum::warn!(target: LOG_TARGET, error = %jfyi, ctx),
112116
JfyiError::FetchPoV(_) |
113117
JfyiError::SendResponse |

polkadot/node/network/availability-distribution/src/requester/session_cache.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rand::{seq::SliceRandom, thread_rng};
2020
use schnellru::{ByLength, LruMap};
2121

2222
use polkadot_node_subsystem::overseer;
23-
use polkadot_node_subsystem_util::runtime::{request_node_features, RuntimeInfo};
23+
use polkadot_node_subsystem_util::{request_node_features, runtime::RuntimeInfo};
2424
use polkadot_primitives::{
2525
AuthorityDiscoveryId, GroupIndex, Hash, NodeFeatures, SessionIndex, ValidatorIndex,
2626
};
@@ -175,8 +175,10 @@ impl SessionCache {
175175
.get_session_info_by_index(ctx.sender(), relay_parent, session_index)
176176
.await?;
177177

178-
let node_features =
179-
request_node_features(relay_parent, session_index, ctx.sender()).await?;
178+
let node_features = request_node_features(relay_parent, session_index, ctx.sender())
179+
.await
180+
.await?
181+
.map_err(Error::FailedNodeFeatures)?;
180182

181183
let discovery_keys = info.session_info.discovery_keys.clone();
182184
let mut validator_groups = info.session_info.validator_groups.clone();

polkadot/node/network/collator-protocol/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub enum Error {
7171
#[error("Response receiver for claim queue request cancelled")]
7272
CancelledClaimQueue(oneshot::Canceled),
7373

74+
#[error("Response receiver for node features request cancelled")]
75+
CancelledNodeFeatures(oneshot::Canceled),
76+
7477
#[error("No state for the relay parent")]
7578
RelayParentStateNotFound,
7679
}

polkadot/node/network/collator-protocol/src/validator_side/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ use polkadot_node_subsystem::{
4949
use polkadot_node_subsystem_util::{
5050
backing_implicit_view::View as ImplicitView,
5151
reputation::{ReputationAggregator, REPUTATION_CHANGE_INTERVAL},
52-
request_claim_queue, request_session_index_for_child,
53-
runtime::request_node_features,
52+
request_claim_queue, request_node_features, request_session_index_for_child,
5453
};
5554
use polkadot_primitives::{
5655
node_features,
@@ -1283,7 +1282,9 @@ where
12831282
.map_err(Error::CancelledSessionIndex)??;
12841283

12851284
let v2_receipts = request_node_features(*leaf, session_index, sender)
1286-
.await?
1285+
.await
1286+
.await
1287+
.map_err(Error::CancelledNodeFeatures)??
12871288
.get(node_features::FeatureIndex::CandidateReceiptV2 as usize)
12881289
.map(|b| *b)
12891290
.unwrap_or(false);

polkadot/node/network/statement-distribution/src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ pub enum Error {
8181
#[error("Fetching claim queue failed {0:?}")]
8282
FetchClaimQueue(RuntimeApiError),
8383

84+
#[error("Fetching minimum backing votes failed {0:?}")]
85+
FetchMinimumBackingVotes(RuntimeApiError),
86+
87+
#[error("Fetching node features failed {0:?}")]
88+
FetchNodeFeatures(RuntimeApiError),
89+
8490
#[error("Attempted to share statement when not a validator or not assigned")]
8591
InvalidShare,
8692

polkadot/node/network/statement-distribution/src/v2/mod.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ use polkadot_node_subsystem::{
4343
overseer, ActivatedLeaf,
4444
};
4545
use polkadot_node_subsystem_util::{
46-
backing_implicit_view::View as ImplicitView,
47-
reputation::ReputationAggregator,
48-
runtime::{request_min_backing_votes, request_node_features, ClaimQueueSnapshot},
46+
backing_implicit_view::View as ImplicitView, reputation::ReputationAggregator,
47+
request_min_backing_votes, request_node_features, runtime::ClaimQueueSnapshot,
4948
};
5049
use polkadot_primitives::{
5150
node_features::FeatureIndex,
@@ -631,9 +630,17 @@ pub(crate) async fn handle_active_leaves_update<Context>(
631630
};
632631

633632
let minimum_backing_votes =
634-
request_min_backing_votes(new_relay_parent, session_index, ctx.sender()).await?;
633+
request_min_backing_votes(new_relay_parent, session_index, ctx.sender())
634+
.await
635+
.await
636+
.map_err(JfyiError::RuntimeApiUnavailable)?
637+
.map_err(JfyiError::FetchMinimumBackingVotes)?;
635638
let node_features =
636-
request_node_features(new_relay_parent, session_index, ctx.sender()).await?;
639+
request_node_features(new_relay_parent, session_index, ctx.sender())
640+
.await
641+
.await
642+
.map_err(JfyiError::RuntimeApiUnavailable)?
643+
.map_err(JfyiError::FetchNodeFeatures)?;
637644
let mut per_session_state = PerSessionState::new(
638645
session_info,
639646
&state.keystore,

polkadot/node/subsystem-util/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use polkadot_primitives::{
4848
ScrapedOnChainVotes,
4949
},
5050
AsyncBackingParams, AuthorityDiscoveryId, CandidateHash, CoreIndex, EncodeAs, ExecutorParams,
51-
GroupIndex, GroupRotationInfo, Hash, Id as ParaId, OccupiedCoreAssumption,
51+
GroupIndex, GroupRotationInfo, Hash, Id as ParaId, NodeFeatures, OccupiedCoreAssumption,
5252
PersistedValidationData, SessionIndex, SessionInfo, Signed, SigningContext, ValidationCode,
5353
ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
5454
};
@@ -314,6 +314,8 @@ specialize_requests! {
314314
fn request_claim_queue() -> BTreeMap<CoreIndex, VecDeque<ParaId>>; ClaimQueue;
315315
fn request_para_backing_state(para_id: ParaId) -> Option<BackingState>; ParaBackingState;
316316
fn request_backing_constraints(para_id: ParaId) -> Option<Constraints>; BackingConstraints;
317+
fn request_min_backing_votes(session_index: SessionIndex) -> u32; MinimumBackingVotes;
318+
fn request_node_features(session_index: SessionIndex) -> NodeFeatures; NodeFeatures;
317319

318320
}
319321

polkadot/node/subsystem-util/src/runtime/mod.rs

+5-35
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ use std::collections::{BTreeMap, VecDeque};
4444
use crate::{
4545
request_availability_cores, request_candidate_events, request_claim_queue,
4646
request_disabled_validators, request_from_runtime, request_key_ownership_proof,
47-
request_on_chain_votes, request_session_executor_params, request_session_index_for_child,
48-
request_session_info, request_submit_report_dispute_lost, request_unapplied_slashes,
49-
request_validation_code_by_hash, request_validator_groups,
47+
request_node_features, request_on_chain_votes, request_session_executor_params,
48+
request_session_index_for_child, request_session_info, request_submit_report_dispute_lost,
49+
request_unapplied_slashes, request_validation_code_by_hash, request_validator_groups,
5050
};
5151

5252
/// Errors that can happen on runtime fetches.
@@ -235,7 +235,8 @@ impl RuntimeInfo {
235235

236236
let validator_info = self.get_validator_info(&session_info)?;
237237

238-
let node_features = request_node_features(parent, session_index, sender).await?;
238+
let node_features =
239+
request_node_features(parent, session_index, sender).await.await??;
239240
let last_set_index = node_features.iter_ones().last().unwrap_or_default();
240241
if last_set_index >= FeatureIndex::FirstUnassigned as usize {
241242
gum::warn!(target: LOG_TARGET, "Runtime requires feature bit {} that node doesn't support, please upgrade node version", last_set_index);
@@ -466,37 +467,6 @@ where
466467
.await
467468
}
468469

469-
/// Request the min backing votes value.
470-
pub async fn request_min_backing_votes(
471-
parent: Hash,
472-
session_index: SessionIndex,
473-
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
474-
) -> Result<u32> {
475-
recv_runtime(
476-
request_from_runtime(parent, sender, |tx| {
477-
RuntimeApiRequest::MinimumBackingVotes(session_index, tx)
478-
})
479-
.await,
480-
)
481-
.await
482-
}
483-
484-
/// Request the node features enabled in the runtime.
485-
/// Pass in the session index for caching purposes, as it should only change on session boundaries.
486-
pub async fn request_node_features(
487-
parent: Hash,
488-
session_index: SessionIndex,
489-
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
490-
) -> Result<NodeFeatures> {
491-
recv_runtime(
492-
request_from_runtime(parent, sender, |tx| {
493-
RuntimeApiRequest::NodeFeatures(session_index, tx)
494-
})
495-
.await,
496-
)
497-
.await
498-
}
499-
500470
/// A snapshot of the runtime claim queue at an arbitrary relay chain block.
501471
#[derive(Default)]
502472
pub struct ClaimQueueSnapshot(pub BTreeMap<CoreIndex, VecDeque<ParaId>>);

0 commit comments

Comments
 (0)