2018-10-20 02:08:43 +00:00
|
|
|
use std::cmp::min;
|
|
|
|
|
|
|
|
use honey_badger_split::SplitExt;
|
2018-12-13 03:22:44 +00:00
|
|
|
use types::{ChainConfig, ShardAndCommittee, ValidatorRecord, ValidatorStatus};
|
2018-11-04 14:35:00 +00:00
|
|
|
use vec_shuffle::{shuffle, ShuffleErr};
|
2018-10-03 05:26:41 +00:00
|
|
|
|
2018-10-05 04:51:16 +00:00
|
|
|
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
|
2018-10-03 05:26:41 +00:00
|
|
|
|
2018-10-20 07:10:41 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum ValidatorAssignmentError {
|
|
|
|
TooManyValidators,
|
|
|
|
TooFewShards,
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 03:04:42 +00:00
|
|
|
/// Delegates active validators into slots for a given cycle, given a random seed.
|
|
|
|
/// Returns a vector or ShardAndComitte vectors representing the shards and committiees for
|
|
|
|
/// each slot.
|
|
|
|
/// References get_new_shuffling (ethereum 2.1 specification)
|
2018-10-20 02:08:43 +00:00
|
|
|
pub fn shard_and_committees_for_cycle(
|
2018-10-03 05:26:41 +00:00
|
|
|
seed: &[u8],
|
2018-10-05 04:51:16 +00:00
|
|
|
validators: &[ValidatorRecord],
|
|
|
|
crosslinking_shard_start: u16,
|
2018-11-04 14:35:00 +00:00
|
|
|
config: &ChainConfig,
|
|
|
|
) -> Result<DelegatedCycle, ValidatorAssignmentError> {
|
2018-10-03 05:26:41 +00:00
|
|
|
let shuffled_validator_indices = {
|
2018-12-13 03:22:44 +00:00
|
|
|
let mut validator_indices = validators
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(i, validator)| {
|
|
|
|
if validator.status_is(ValidatorStatus::Active) {
|
|
|
|
Some(i)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2018-10-20 07:10:41 +00:00
|
|
|
shuffle(seed, validator_indices)?
|
2018-10-03 05:26:41 +00:00
|
|
|
};
|
2018-10-05 04:51:16 +00:00
|
|
|
let shard_indices: Vec<usize> = (0_usize..config.shard_count as usize).into_iter().collect();
|
|
|
|
let crosslinking_shard_start = crosslinking_shard_start as usize;
|
2018-10-03 05:26:41 +00:00
|
|
|
let cycle_length = config.cycle_length as usize;
|
|
|
|
let min_committee_size = config.min_committee_size as usize;
|
|
|
|
generate_cycle(
|
|
|
|
&shuffled_validator_indices,
|
|
|
|
&shard_indices,
|
2018-10-05 04:51:16 +00:00
|
|
|
crosslinking_shard_start,
|
|
|
|
cycle_length,
|
2018-11-04 14:35:00 +00:00
|
|
|
min_committee_size,
|
|
|
|
)
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 03:04:42 +00:00
|
|
|
/// Given the validator list, delegates the validators into slots and comittees for a given cycle.
|
2018-10-03 05:26:41 +00:00
|
|
|
fn generate_cycle(
|
2018-10-05 04:51:16 +00:00
|
|
|
validator_indices: &[usize],
|
|
|
|
shard_indices: &[usize],
|
|
|
|
crosslinking_shard_start: usize,
|
|
|
|
cycle_length: usize,
|
2018-11-04 14:35:00 +00:00
|
|
|
min_committee_size: usize,
|
|
|
|
) -> Result<DelegatedCycle, ValidatorAssignmentError> {
|
2018-10-03 05:26:41 +00:00
|
|
|
let validator_count = validator_indices.len();
|
|
|
|
let shard_count = shard_indices.len();
|
|
|
|
|
2018-10-03 08:47:18 +00:00
|
|
|
if shard_count / cycle_length == 0 {
|
2018-11-04 14:35:00 +00:00
|
|
|
return Err(ValidatorAssignmentError::TooFewShards);
|
2018-10-03 08:47:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:26:41 +00:00
|
|
|
let (committees_per_slot, slots_per_committee) = {
|
|
|
|
if validator_count >= cycle_length * min_committee_size {
|
2018-11-04 14:35:00 +00:00
|
|
|
let committees_per_slot = min(
|
|
|
|
validator_count / cycle_length / (min_committee_size * 2) + 1,
|
|
|
|
shard_count / cycle_length,
|
|
|
|
);
|
2018-10-03 05:26:41 +00:00
|
|
|
let slots_per_committee = 1;
|
|
|
|
(committees_per_slot, slots_per_committee)
|
|
|
|
} else {
|
|
|
|
let committees_per_slot = 1;
|
|
|
|
let mut slots_per_committee = 1;
|
2018-11-04 14:35:00 +00:00
|
|
|
while (validator_count * slots_per_committee < cycle_length * min_committee_size)
|
|
|
|
& (slots_per_committee < cycle_length)
|
|
|
|
{
|
2018-10-05 04:51:16 +00:00
|
|
|
slots_per_committee *= 2;
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
(committees_per_slot, slots_per_committee)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-04 14:35:00 +00:00
|
|
|
let cycle = validator_indices
|
|
|
|
.honey_badger_split(cycle_length)
|
2018-10-03 05:26:41 +00:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, slot_indices)| {
|
2018-11-04 14:35:00 +00:00
|
|
|
let shard_start =
|
|
|
|
crosslinking_shard_start + i * committees_per_slot / slots_per_committee;
|
|
|
|
slot_indices
|
|
|
|
.honey_badger_split(committees_per_slot)
|
2018-10-03 05:26:41 +00:00
|
|
|
.enumerate()
|
2018-11-04 14:35:00 +00:00
|
|
|
.map(|(j, shard_indices)| ShardAndCommittee {
|
|
|
|
shard: ((shard_start + j) % shard_count) as u16,
|
|
|
|
committee: shard_indices.to_vec(),
|
2018-12-13 03:22:52 +00:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect();
|
2018-10-03 05:26:41 +00:00
|
|
|
Ok(cycle)
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:10:41 +00:00
|
|
|
impl From<ShuffleErr> for ValidatorAssignmentError {
|
|
|
|
fn from(e: ShuffleErr) -> ValidatorAssignmentError {
|
|
|
|
match e {
|
|
|
|
ShuffleErr::ExceedsListLength => ValidatorAssignmentError::TooManyValidators,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 05:26:41 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn generate_cycle_helper(
|
|
|
|
validator_count: &usize,
|
|
|
|
shard_count: &usize,
|
2018-10-05 04:53:21 +00:00
|
|
|
crosslinking_shard_start: usize,
|
|
|
|
cycle_length: usize,
|
2018-11-04 14:35:00 +00:00
|
|
|
min_committee_size: usize,
|
|
|
|
) -> (
|
|
|
|
Vec<usize>,
|
|
|
|
Vec<usize>,
|
|
|
|
Result<DelegatedCycle, ValidatorAssignmentError>,
|
|
|
|
) {
|
2018-10-05 04:53:21 +00:00
|
|
|
let validator_indices: Vec<usize> = (0_usize..*validator_count).into_iter().collect();
|
|
|
|
let shard_indices: Vec<usize> = (0_usize..*shard_count).into_iter().collect();
|
2018-10-03 05:26:41 +00:00
|
|
|
let result = generate_cycle(
|
|
|
|
&validator_indices,
|
|
|
|
&shard_indices,
|
2018-10-05 04:53:21 +00:00
|
|
|
crosslinking_shard_start,
|
|
|
|
cycle_length,
|
2018-11-04 14:35:00 +00:00
|
|
|
min_committee_size,
|
|
|
|
);
|
2018-10-03 05:26:41 +00:00
|
|
|
(validator_indices, shard_indices, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn print_cycle(cycle: &DelegatedCycle) {
|
2018-11-04 14:35:00 +00:00
|
|
|
cycle.iter().enumerate().for_each(|(i, slot)| {
|
|
|
|
println!("slot {:?}", &i);
|
|
|
|
slot.iter().enumerate().for_each(|(i, sac)| {
|
|
|
|
println!(
|
|
|
|
"#{:?}\tshard={}\tcommittee.len()={}",
|
|
|
|
&i,
|
|
|
|
&sac.shard,
|
|
|
|
&sac.committee.len()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 14:35:00 +00:00
|
|
|
fn flatten_validators(cycle: &DelegatedCycle) -> Vec<usize> {
|
2018-10-03 05:26:41 +00:00
|
|
|
let mut flattened = vec![];
|
|
|
|
for slot in cycle.iter() {
|
|
|
|
for sac in slot.iter() {
|
|
|
|
for validator in sac.committee.iter() {
|
|
|
|
flattened.push(*validator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flattened
|
|
|
|
}
|
|
|
|
|
2018-11-04 14:35:00 +00:00
|
|
|
fn flatten_and_dedup_shards(cycle: &DelegatedCycle) -> Vec<usize> {
|
2018-10-03 05:26:41 +00:00
|
|
|
let mut flattened = vec![];
|
|
|
|
for slot in cycle.iter() {
|
|
|
|
for sac in slot.iter() {
|
2018-10-23 10:28:07 +00:00
|
|
|
flattened.push(sac.shard as usize);
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
flattened.dedup();
|
|
|
|
flattened
|
|
|
|
}
|
|
|
|
|
2018-11-04 14:35:00 +00:00
|
|
|
fn flatten_shards_in_slots(cycle: &DelegatedCycle) -> Vec<Vec<usize>> {
|
2018-10-03 05:26:41 +00:00
|
|
|
let mut shards_in_slots: Vec<Vec<usize>> = vec![];
|
|
|
|
for slot in cycle.iter() {
|
|
|
|
let mut shards: Vec<usize> = vec![];
|
|
|
|
for sac in slot.iter() {
|
2018-10-23 10:28:07 +00:00
|
|
|
shards.push(sac.shard as usize);
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
shards_in_slots.push(shards);
|
|
|
|
}
|
|
|
|
shards_in_slots
|
|
|
|
}
|
|
|
|
|
2018-10-04 03:21:16 +00:00
|
|
|
// TODO: Improve these tests to check committee lengths
|
2018-10-03 05:26:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_cycle() {
|
|
|
|
let validator_count: usize = 100;
|
2018-10-04 03:21:16 +00:00
|
|
|
let shard_count: usize = 20;
|
2018-10-03 05:26:41 +00:00
|
|
|
let crosslinking_shard_start: usize = 0;
|
|
|
|
let cycle_length: usize = 20;
|
|
|
|
let min_committee_size: usize = 10;
|
|
|
|
let (validators, shards, result) = generate_cycle_helper(
|
|
|
|
&validator_count,
|
|
|
|
&shard_count,
|
2018-10-05 04:53:21 +00:00
|
|
|
crosslinking_shard_start,
|
|
|
|
cycle_length,
|
2018-11-04 14:35:00 +00:00
|
|
|
min_committee_size,
|
|
|
|
);
|
2018-10-03 05:26:41 +00:00
|
|
|
let cycle = result.unwrap();
|
|
|
|
|
|
|
|
let assigned_validators = flatten_validators(&cycle);
|
|
|
|
let assigned_shards = flatten_and_dedup_shards(&cycle);
|
|
|
|
let shards_in_slots = flatten_shards_in_slots(&cycle);
|
2018-10-04 03:21:16 +00:00
|
|
|
let expected_shards = shards.get(0..10).unwrap();
|
2018-11-04 14:35:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
assigned_validators, validators,
|
|
|
|
"Validator assignment incorrect"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
assigned_shards, expected_shards,
|
|
|
|
"Shard assignment incorrect"
|
|
|
|
);
|
2018-10-03 05:26:41 +00:00
|
|
|
|
2018-10-03 08:47:18 +00:00
|
|
|
let expected_shards_in_slots: Vec<Vec<usize>> = vec![
|
2018-11-04 14:35:00 +00:00
|
|
|
vec![0],
|
|
|
|
vec![0], // Each line is 2 slots..
|
|
|
|
vec![1],
|
|
|
|
vec![1],
|
|
|
|
vec![2],
|
|
|
|
vec![2],
|
|
|
|
vec![3],
|
|
|
|
vec![3],
|
|
|
|
vec![4],
|
|
|
|
vec![4],
|
|
|
|
vec![5],
|
|
|
|
vec![5],
|
|
|
|
vec![6],
|
|
|
|
vec![6],
|
|
|
|
vec![7],
|
|
|
|
vec![7],
|
|
|
|
vec![8],
|
|
|
|
vec![8],
|
|
|
|
vec![9],
|
|
|
|
vec![9],
|
2018-10-03 08:47:18 +00:00
|
|
|
];
|
|
|
|
// assert!(compare_shards_in_slots(&cycle, &expected_shards_in_slots));
|
2018-11-04 14:35:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
expected_shards_in_slots, shards_in_slots,
|
|
|
|
"Shard assignment incorrect."
|
|
|
|
)
|
2018-10-03 08:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// Check that the committees per slot is upper bounded by shard count
|
|
|
|
fn test_generate_cycle_committees_bounded() {
|
2018-10-04 03:21:16 +00:00
|
|
|
let validator_count: usize = 523;
|
|
|
|
let shard_count: usize = 31;
|
2018-10-03 08:47:18 +00:00
|
|
|
let crosslinking_shard_start: usize = 0;
|
2018-10-04 03:21:16 +00:00
|
|
|
let cycle_length: usize = 11;
|
|
|
|
let min_committee_size: usize = 5;
|
2018-10-03 08:47:18 +00:00
|
|
|
let (validators, shards, result) = generate_cycle_helper(
|
|
|
|
&validator_count,
|
|
|
|
&shard_count,
|
2018-10-05 04:53:21 +00:00
|
|
|
crosslinking_shard_start,
|
|
|
|
cycle_length,
|
2018-11-04 14:35:00 +00:00
|
|
|
min_committee_size,
|
|
|
|
);
|
2018-10-03 08:47:18 +00:00
|
|
|
let cycle = result.unwrap();
|
|
|
|
let assigned_validators = flatten_validators(&cycle);
|
|
|
|
let assigned_shards = flatten_and_dedup_shards(&cycle);
|
|
|
|
let shards_in_slots = flatten_shards_in_slots(&cycle);
|
2018-10-04 03:21:16 +00:00
|
|
|
let expected_shards = shards.get(0..22).unwrap();
|
2018-11-04 14:35:00 +00:00
|
|
|
let expected_shards_in_slots: Vec<Vec<usize>> = (0_usize..11_usize)
|
|
|
|
.map(|x| vec![2 * x, 2 * x + 1])
|
|
|
|
.collect();
|
|
|
|
assert_eq!(
|
|
|
|
assigned_validators, validators,
|
|
|
|
"Validator assignment incorrect"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
assigned_shards, expected_shards,
|
|
|
|
"Shard assignment incorrect"
|
|
|
|
);
|
2018-10-03 05:26:41 +00:00
|
|
|
// assert!(compare_shards_in_slots(&cycle, &expected_shards_in_slots));
|
2018-11-04 14:35:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
expected_shards_in_slots, shards_in_slots,
|
|
|
|
"Shard assignment incorrect."
|
|
|
|
)
|
2018-10-03 05:26:41 +00:00
|
|
|
}
|
|
|
|
}
|