use polkadot_erasure_coding::systematic_recovery_threshold;
use polkadot_primitives::{node_features, ChunkIndex, CoreIndex, NodeFeatures, ValidatorIndex};
pub fn availability_chunk_index(
maybe_node_features: Option<&NodeFeatures>,
n_validators: usize,
core_index: CoreIndex,
validator_index: ValidatorIndex,
) -> Result<ChunkIndex, polkadot_erasure_coding::Error> {
if let Some(features) = maybe_node_features {
if let Some(&true) = features
.get(usize::from(node_features::FeatureIndex::AvailabilityChunkMapping as u8))
.as_deref()
{
let systematic_threshold = systematic_recovery_threshold(n_validators)? as u32;
let core_start_pos = core_index.0 * systematic_threshold;
return Ok(ChunkIndex((core_start_pos + validator_index.0) % n_validators as u32))
}
}
Ok(validator_index.into())
}
pub fn availability_chunk_indices(
maybe_node_features: Option<&NodeFeatures>,
n_validators: usize,
core_index: CoreIndex,
) -> Result<Vec<ChunkIndex>, polkadot_erasure_coding::Error> {
let identity = (0..n_validators).map(|index| ChunkIndex(index as u32));
if let Some(features) = maybe_node_features {
if let Some(&true) = features
.get(usize::from(node_features::FeatureIndex::AvailabilityChunkMapping as u8))
.as_deref()
{
let systematic_threshold = systematic_recovery_threshold(n_validators)? as u32;
let core_start_pos = core_index.0 * systematic_threshold;
return Ok(identity
.into_iter()
.cycle()
.skip(core_start_pos as usize)
.take(n_validators)
.collect())
}
}
Ok(identity.collect())
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
pub fn node_features_with_mapping_enabled() -> NodeFeatures {
let mut node_features = NodeFeatures::new();
node_features
.resize(node_features::FeatureIndex::AvailabilityChunkMapping as usize + 1, false);
node_features
.set(node_features::FeatureIndex::AvailabilityChunkMapping as u8 as usize, true);
node_features
}
pub fn node_features_with_other_bits_enabled() -> NodeFeatures {
let mut node_features = NodeFeatures::new();
node_features.resize(node_features::FeatureIndex::FirstUnassigned as usize + 1, true);
node_features
.set(node_features::FeatureIndex::AvailabilityChunkMapping as u8 as usize, false);
node_features
}
#[test]
fn test_availability_chunk_indices() {
let n_validators = 20u32;
let n_cores = 15u32;
{
for node_features in
[None, Some(NodeFeatures::EMPTY), Some(node_features_with_other_bits_enabled())]
{
for core_index in 0..n_cores {
let indices = availability_chunk_indices(
node_features.as_ref(),
n_validators as usize,
CoreIndex(core_index),
)
.unwrap();
for validator_index in 0..n_validators {
assert_eq!(
indices[validator_index as usize],
availability_chunk_index(
node_features.as_ref(),
n_validators as usize,
CoreIndex(core_index),
ValidatorIndex(validator_index)
)
.unwrap()
)
}
assert_eq!(
indices,
(0..n_validators).map(|i| ChunkIndex(i)).collect::<Vec<_>>()
);
}
}
}
{
let node_features = node_features_with_mapping_enabled();
let mut previous_indices = None;
for core_index in 0..n_cores {
let indices = availability_chunk_indices(
Some(&node_features),
n_validators as usize,
CoreIndex(core_index),
)
.unwrap();
for validator_index in 0..n_validators {
assert_eq!(
indices[validator_index as usize],
availability_chunk_index(
Some(&node_features),
n_validators as usize,
CoreIndex(core_index),
ValidatorIndex(validator_index)
)
.unwrap()
)
}
if let Some(previous_indices) = previous_indices {
assert_ne!(previous_indices, indices);
}
previous_indices = Some(indices.clone());
assert_eq!(
(0..n_validators).map(|i| ChunkIndex(i)).collect::<HashSet<_>>(),
indices.into_iter().collect::<HashSet<_>>()
);
}
}
}
#[test]
fn prevent_changes_to_mapping() {
let n_validators = 7;
let node_features = node_features_with_mapping_enabled();
assert_eq!(
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(0))
.unwrap()
.into_iter()
.map(|i| i.0)
.collect::<Vec<u32>>(),
vec![0, 1, 2, 3, 4, 5, 6]
);
assert_eq!(
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(1))
.unwrap()
.into_iter()
.map(|i| i.0)
.collect::<Vec<u32>>(),
vec![2, 3, 4, 5, 6, 0, 1]
);
assert_eq!(
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(2))
.unwrap()
.into_iter()
.map(|i| i.0)
.collect::<Vec<u32>>(),
vec![4, 5, 6, 0, 1, 2, 3]
);
assert_eq!(
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(3))
.unwrap()
.into_iter()
.map(|i| i.0)
.collect::<Vec<u32>>(),
vec![6, 0, 1, 2, 3, 4, 5]
);
assert_eq!(
availability_chunk_indices(Some(&node_features), n_validators, CoreIndex(4))
.unwrap()
.into_iter()
.map(|i| i.0)
.collect::<Vec<u32>>(),
vec![1, 2, 3, 4, 5, 6, 0]
);
}
}