Skip to content

Commit 291cc3d

Browse files
committed
refactor: remove Option from node_features
1 parent 5d8dcd7 commit 291cc3d

File tree

16 files changed

+61
-85
lines changed

16 files changed

+61
-85
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ use polkadot_primitives::{
5656
ClaimQueueOffset, CommittedCandidateReceiptV2, TransposedClaimQueue,
5757
},
5858
CandidateCommitments, CandidateDescriptor, CollatorPair, CoreIndex, Hash, Id as ParaId,
59-
NodeFeatures, OccupiedCoreAssumption, PersistedValidationData, SessionIndex,
60-
ValidationCodeHash,
59+
OccupiedCoreAssumption, PersistedValidationData, SessionIndex, ValidationCodeHash,
6160
};
6261
use schnellru::{ByLength, LruMap};
6362
use sp_core::crypto::Pair;
@@ -510,9 +509,7 @@ impl SessionInfoCache {
510509
let n_validators =
511510
request_validators(relay_parent, &mut sender.clone()).await.await??.len();
512511

513-
let node_features = request_node_features(relay_parent, session_index, sender)
514-
.await?
515-
.unwrap_or(NodeFeatures::EMPTY);
512+
let node_features = request_node_features(relay_parent, session_index, sender).await?;
516513

517514
let info = PerSessionInfo {
518515
v2_receipts: node_features

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use polkadot_node_subsystem_util::TimeoutExt;
2929
use polkadot_primitives::{
3030
node_features,
3131
vstaging::{CandidateDescriptorVersion, CoreSelector, UMPSignal, UMP_SEPARATOR},
32-
CollatorPair, PersistedValidationData,
32+
CollatorPair, NodeFeatures, PersistedValidationData,
3333
};
3434
use polkadot_primitives_test_helpers::dummy_head_data;
3535
use rstest::rstest;

polkadot/node/core/av-store/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ fn store_available_data(
13291329
})
13301330
.collect();
13311331

1332-
let chunk_indices = availability_chunk_indices(Some(&node_features), n_validators, core_index)?;
1332+
let chunk_indices = availability_chunk_indices(&node_features, n_validators, core_index)?;
13331333
for (validator_index, chunk_index) in chunk_indices.into_iter().enumerate() {
13341334
write_chunk(
13351335
&mut tx,

polkadot/node/core/av-store/src/tests.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,8 @@ fn store_pov_and_queries_work() {
510510

511511
let query_all_chunks_res = query_all_chunks(
512512
&mut virtual_overseer,
513-
availability_chunk_indices(
514-
Some(&node_features),
515-
n_validators as usize,
516-
core_index,
517-
)
518-
.unwrap(),
513+
availability_chunk_indices(&node_features, n_validators as usize, core_index)
514+
.unwrap(),
519515
candidate_hash,
520516
)
521517
.await;
@@ -596,12 +592,8 @@ fn store_pov_and_queries_work() {
596592

597593
let query_all_chunks_res = query_all_chunks(
598594
&mut virtual_overseer,
599-
availability_chunk_indices(
600-
Some(&node_features),
601-
n_validators as usize,
602-
core_index,
603-
)
604-
.unwrap(),
595+
availability_chunk_indices(&node_features, n_validators as usize, core_index)
596+
.unwrap(),
605597
candidate_hash,
606598
)
607599
.await;
@@ -618,7 +610,7 @@ fn store_pov_and_queries_work() {
618610
.await
619611
.unwrap();
620612
let expected_chunk_index = availability_chunk_index(
621-
Some(&node_features),
613+
&node_features,
622614
n_validators as usize,
623615
core_index,
624616
ValidatorIndex(validator_index),
@@ -723,7 +715,8 @@ fn query_all_chunks_works() {
723715
}
724716

725717
let chunk_indices =
726-
availability_chunk_indices(None, n_validators as usize, CoreIndex(0)).unwrap();
718+
availability_chunk_indices(&NodeFeatures::EMPTY, n_validators as usize, CoreIndex(0))
719+
.unwrap();
727720

728721
assert_eq!(
729722
query_all_chunks(&mut virtual_overseer, chunk_indices.clone(), candidate_hash_1)

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ struct PerSessionCache {
260260
/// Cache for storing validators list, retrieved from the runtime.
261261
validators_cache: LruMap<SessionIndex, Arc<Vec<ValidatorId>>>,
262262
/// Cache for storing node features, retrieved from the runtime.
263-
node_features_cache: LruMap<SessionIndex, Option<NodeFeatures>>,
263+
node_features_cache: LruMap<SessionIndex, NodeFeatures>,
264264
/// Cache for storing executor parameters, retrieved from the runtime.
265265
executor_params_cache: LruMap<SessionIndex, Arc<ExecutorParams>>,
266266
/// Cache for storing the minimum backing votes threshold, retrieved from the runtime.
@@ -322,15 +322,14 @@ impl PerSessionCache {
322322
session_index: SessionIndex,
323323
parent: Hash,
324324
sender: &mut impl overseer::SubsystemSender<RuntimeApiMessage>,
325-
) -> Result<Option<NodeFeatures>, Error> {
325+
) -> Result<NodeFeatures, Error> {
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: Option<NodeFeatures> =
333-
request_node_features(parent, session_index, sender).await?;
332+
let node_features = request_node_features(parent, session_index, sender).await?;
334333

335334
// Cache the fetched node features for future use.
336335
self.node_features_cache.insert(session_index, node_features.clone());
@@ -1152,10 +1151,8 @@ async fn construct_per_relay_parent_state<Context>(
11521151
let validators = per_session_cache.validators(session_index, parent, ctx.sender()).await;
11531152
let validators = try_runtime_api!(validators);
11541153

1155-
let node_features = per_session_cache
1156-
.node_features(session_index, parent, ctx.sender())
1157-
.await?
1158-
.unwrap_or(NodeFeatures::EMPTY);
1154+
let node_features =
1155+
per_session_cache.node_features(session_index, parent, ctx.sender()).await?;
11591156

11601157
let inject_core_index = node_features
11611158
.get(FeatureIndex::ElasticScalingMVP as usize)

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use polkadot_node_subsystem_util::{
4141
use polkadot_primitives::{
4242
node_features::FeatureIndex,
4343
vstaging::{BackedCandidate, CoreState},
44-
CandidateHash, CoreIndex, Hash, Id as ParaId, NodeFeatures, SessionIndex,
45-
SignedAvailabilityBitfield, ValidatorIndex,
44+
CandidateHash, CoreIndex, Hash, Id as ParaId, SessionIndex, SignedAvailabilityBitfield,
45+
ValidatorIndex,
4646
};
4747
use std::collections::{BTreeMap, HashMap};
4848

@@ -204,7 +204,6 @@ async fn handle_active_leaves_update(
204204
if per_session.get(&session_index).is_none() {
205205
let elastic_scaling_mvp = request_node_features(leaf.hash, session_index, sender)
206206
.await?
207-
.unwrap_or(NodeFeatures::EMPTY)
208207
.get(FeatureIndex::ElasticScalingMVP as usize)
209208
.map(|b| *b)
210209
.unwrap_or(false);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct SessionInfo {
6666
pub our_group: Option<GroupIndex>,
6767

6868
/// Node features.
69-
pub node_features: Option<NodeFeatures>,
69+
pub node_features: NodeFeatures,
7070
}
7171

7272
/// Report of bad validators.

polkadot/node/network/availability-distribution/src/tests/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl TestState {
109109
let session_info = make_session_info();
110110

111111
let our_chunk_index = availability_chunk_index(
112-
Some(&node_features),
112+
&node_features,
113113
session_info.validators.len(),
114114
CoreIndex(1),
115115
ValidatorIndex(0),

polkadot/node/network/availability-recovery/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ async fn handle_recover<Context>(
485485
) && chunk_mapping_enabled
486486
{
487487
let chunk_indices =
488-
availability_chunk_indices(Some(node_features), n_validators, core_index)?;
488+
availability_chunk_indices(node_features, n_validators, core_index)?;
489489

490490
let chunk_indices: VecDeque<_> = chunk_indices
491491
.iter()

polkadot/node/network/availability-recovery/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ fn map_chunks(
715715
core_index: CoreIndex,
716716
) -> IndexedVec<ValidatorIndex, ErasureChunk> {
717717
let chunk_indices =
718-
availability_chunk_indices(Some(node_features), n_validators, core_index).unwrap();
718+
availability_chunk_indices(node_features, n_validators, core_index).unwrap();
719719

720720
(0..n_validators)
721721
.map(|val_idx| chunks[chunk_indices[val_idx].0 as usize].clone())

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

-1
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,6 @@ where
12841284

12851285
let v2_receipts = request_node_features(*leaf, session_index, sender)
12861286
.await?
1287-
.unwrap_or_default()
12881287
.get(node_features::FeatureIndex::CandidateReceiptV2 as usize)
12891288
.map(|b| *b)
12901289
.unwrap_or(false);

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ use polkadot_primitives::{
5151
node_features::FeatureIndex,
5252
vstaging::{transpose_claim_queue, CandidateDescriptorVersion, TransposedClaimQueue},
5353
AuthorityDiscoveryId, CandidateHash, CompactStatement, CoreIndex, GroupIndex,
54-
GroupRotationInfo, Hash, Id as ParaId, IndexedVec, NodeFeatures, SessionIndex, SessionInfo,
55-
SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorId, ValidatorIndex,
54+
GroupRotationInfo, Hash, Id as ParaId, IndexedVec, SessionIndex, SessionInfo, SignedStatement,
55+
SigningContext, UncheckedSignedStatement, ValidatorId, ValidatorIndex,
5656
};
5757

5858
use sp_keystore::KeystorePtr;
@@ -639,7 +639,6 @@ pub(crate) async fn handle_active_leaves_update<Context>(
639639
&state.keystore,
640640
minimum_backing_votes,
641641
node_features
642-
.unwrap_or(NodeFeatures::EMPTY)
643642
.get(FeatureIndex::CandidateReceiptV2 as usize)
644643
.map(|b| *b)
645644
.unwrap_or(false),

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ use polkadot_node_subsystem_test_helpers as test_helpers;
3434
use polkadot_node_subsystem_util::TimeoutExt;
3535
use polkadot_primitives::{
3636
vstaging::CommittedCandidateReceiptV2 as CommittedCandidateReceipt, AssignmentPair, Block,
37-
BlockNumber, GroupRotationInfo, HeadData, Header, IndexedVec, PersistedValidationData,
38-
SessionIndex, SessionInfo, ValidatorPair, DEFAULT_SCHEDULING_LOOKAHEAD,
37+
BlockNumber, GroupRotationInfo, HeadData, Header, IndexedVec, NodeFeatures,
38+
PersistedValidationData, SessionIndex, SessionInfo, ValidatorPair,
39+
DEFAULT_SCHEDULING_LOOKAHEAD,
3940
};
4041
use sc_keystore::LocalKeystore;
4142
use sc_network::ProtocolName;

polkadot/node/subsystem-bench/src/lib/availability/test_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl TestState {
118118
test_state.chunk_indices = (0..config.n_cores)
119119
.map(|core_index| {
120120
availability_chunk_indices(
121-
Some(&default_node_features()),
121+
&default_node_features(),
122122
config.n_validators,
123123
CoreIndex(core_index as u32),
124124
)

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

+31-37
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@ use polkadot_primitives::{node_features, ChunkIndex, CoreIndex, NodeFeatures, Va
2222
/// Any modification to the output of the function needs to be coordinated via the runtime.
2323
/// It's best to use minimal/no external dependencies.
2424
pub fn availability_chunk_index(
25-
maybe_node_features: Option<&NodeFeatures>,
25+
node_features: &NodeFeatures,
2626
n_validators: usize,
2727
core_index: CoreIndex,
2828
validator_index: ValidatorIndex,
2929
) -> Result<ChunkIndex, polkadot_erasure_coding::Error> {
30-
if let Some(features) = maybe_node_features {
31-
if let Some(&true) = features
32-
.get(usize::from(node_features::FeatureIndex::AvailabilityChunkMapping as u8))
33-
.as_deref()
34-
{
35-
let systematic_threshold = systematic_recovery_threshold(n_validators)? as u32;
36-
let core_start_pos = core_index.0 * systematic_threshold;
37-
38-
return Ok(ChunkIndex((core_start_pos + validator_index.0) % n_validators as u32))
39-
}
30+
if let Some(&true) = node_features
31+
.get(usize::from(node_features::FeatureIndex::AvailabilityChunkMapping as u8))
32+
.as_deref()
33+
{
34+
let systematic_threshold = systematic_recovery_threshold(n_validators)? as u32;
35+
let core_start_pos = core_index.0 * systematic_threshold;
36+
37+
return Ok(ChunkIndex((core_start_pos + validator_index.0) % n_validators as u32))
4038
}
4139

4240
Ok(validator_index.into())
@@ -48,26 +46,24 @@ pub fn availability_chunk_index(
4846
/// Any modification to the output of the function needs to be coordinated via the
4947
/// runtime. It's best to use minimal/no external dependencies.
5048
pub fn availability_chunk_indices(
51-
maybe_node_features: Option<&NodeFeatures>,
49+
node_features: &NodeFeatures,
5250
n_validators: usize,
5351
core_index: CoreIndex,
5452
) -> Result<Vec<ChunkIndex>, polkadot_erasure_coding::Error> {
5553
let identity = (0..n_validators).map(|index| ChunkIndex(index as u32));
56-
if let Some(features) = maybe_node_features {
57-
if let Some(&true) = features
58-
.get(usize::from(node_features::FeatureIndex::AvailabilityChunkMapping as u8))
59-
.as_deref()
60-
{
61-
let systematic_threshold = systematic_recovery_threshold(n_validators)? as u32;
62-
let core_start_pos = core_index.0 * systematic_threshold;
63-
64-
return Ok(identity
65-
.into_iter()
66-
.cycle()
67-
.skip(core_start_pos as usize)
68-
.take(n_validators)
69-
.collect())
70-
}
54+
if let Some(&true) = node_features
55+
.get(usize::from(node_features::FeatureIndex::AvailabilityChunkMapping as u8))
56+
.as_deref()
57+
{
58+
let systematic_threshold = systematic_recovery_threshold(n_validators)? as u32;
59+
let core_start_pos = core_index.0 * systematic_threshold;
60+
61+
return Ok(identity
62+
.into_iter()
63+
.cycle()
64+
.skip(core_start_pos as usize)
65+
.take(n_validators)
66+
.collect())
7167
}
7268

7369
Ok(identity.collect())
@@ -102,9 +98,7 @@ mod tests {
10298

10399
// If the mapping feature is not enabled, it should always be the identity vector.
104100
{
105-
for node_features in
106-
[None, Some(NodeFeatures::EMPTY), Some(node_features_with_other_bits_enabled())]
107-
{
101+
for node_features in [NodeFeatures::EMPTY, node_features_with_other_bits_enabled()] {
108102
for core_index in 0..n_cores {
109103
let indices = availability_chunk_indices(
110104
node_features.as_ref(),
@@ -141,7 +135,7 @@ mod tests {
141135

142136
for core_index in 0..n_cores {
143137
let indices = availability_chunk_indices(
144-
Some(&node_features),
138+
&node_features,
145139
n_validators as usize,
146140
CoreIndex(core_index),
147141
)
@@ -151,7 +145,7 @@ mod tests {
151145
assert_eq!(
152146
indices[validator_index as usize],
153147
availability_chunk_index(
154-
Some(&node_features),
148+
&node_features,
155149
n_validators as usize,
156150
CoreIndex(core_index),
157151
ValidatorIndex(validator_index)
@@ -184,39 +178,39 @@ mod tests {
184178
let node_features = node_features_with_mapping_enabled();
185179

186180
assert_eq!(
187-
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(0))
181+
availability_chunk_indices(&node_features, n_validators, CoreIndex(0))
188182
.unwrap()
189183
.into_iter()
190184
.map(|i| i.0)
191185
.collect::<Vec<u32>>(),
192186
vec![0, 1, 2, 3, 4, 5, 6]
193187
);
194188
assert_eq!(
195-
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(1))
189+
availability_chunk_indices(&node_features, n_validators, CoreIndex(1))
196190
.unwrap()
197191
.into_iter()
198192
.map(|i| i.0)
199193
.collect::<Vec<u32>>(),
200194
vec![2, 3, 4, 5, 6, 0, 1]
201195
);
202196
assert_eq!(
203-
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(2))
197+
availability_chunk_indices(&node_features, n_validators, CoreIndex(2))
204198
.unwrap()
205199
.into_iter()
206200
.map(|i| i.0)
207201
.collect::<Vec<u32>>(),
208202
vec![4, 5, 6, 0, 1, 2, 3]
209203
);
210204
assert_eq!(
211-
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(3))
205+
availability_chunk_indices(&node_features, n_validators, CoreIndex(3))
212206
.unwrap()
213207
.into_iter()
214208
.map(|i| i.0)
215209
.collect::<Vec<u32>>(),
216210
vec![6, 0, 1, 2, 3, 4, 5]
217211
);
218212
assert_eq!(
219-
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(4))
213+
availability_chunk_indices(&node_features, n_validators, CoreIndex(4))
220214
.unwrap()
221215
.into_iter()
222216
.map(|i| i.0)

0 commit comments

Comments
 (0)