Update (hacky) validator induction and shuffling.

This allows the `BeaconChain` struct to be updated but it doesn't bring
these functions in line with the spec.
This commit is contained in:
Paul Hauner 2018-12-12 18:32:19 +11:00
parent c944c435e2
commit 92786520e4
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
4 changed files with 24 additions and 21 deletions

View File

@ -19,7 +19,7 @@ pub enum ValidatorInductionError {
}
impl ValidatorInductor {
pub fn new(current_slot: u64, shard_count: u16, validators: Vec<ValidatorRecord>) -> Self {
pub fn new(current_slot: u64, shard_count: u64, validators: Vec<ValidatorRecord>) -> Self {
Self {
current_slot,
shard_count,

View File

@ -6,5 +6,6 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies]
active-validators = { path = "../utils/active-validators" }
honey-badger-split = { path = "../utils/honey-badger-split" }
spec = { path = "../spec" }
types = { path = "../types" }
vec_shuffle = { path = "../utils/vec_shuffle" }

View File

@ -1,5 +1,6 @@
extern crate active_validators;
extern crate honey_badger_split;
extern crate spec;
extern crate types;
extern crate vec_shuffle;

View File

@ -2,7 +2,8 @@ use std::cmp::min;
use active_validators::active_validator_indices;
use honey_badger_split::SplitExt;
use types::{ChainConfig, ShardAndCommittee, ValidatorRecord};
use spec::ChainSpec;
use types::{ShardAndCommittee, ValidatorRecord};
use vec_shuffle::{shuffle, ShuffleErr};
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
@ -21,21 +22,21 @@ pub fn shard_and_committees_for_cycle(
seed: &[u8],
validators: &[ValidatorRecord],
crosslinking_shard_start: u16,
config: &ChainConfig,
spec: &ChainSpec,
) -> Result<DelegatedCycle, ValidatorAssignmentError> {
let shuffled_validator_indices = {
let mut validator_indices = active_validator_indices(validators);
shuffle(seed, validator_indices)?
};
let shard_indices: Vec<usize> = (0_usize..config.shard_count as usize).into_iter().collect();
let shard_indices: Vec<usize> = (0_usize..spec.shard_count as usize).into_iter().collect();
let crosslinking_shard_start = crosslinking_shard_start as usize;
let cycle_length = config.cycle_length as usize;
let min_committee_size = config.min_committee_size as usize;
let epoch_length = spec.epoch_length as usize;
let min_committee_size = spec.target_committee_size as usize;
generate_cycle(
&shuffled_validator_indices,
&shard_indices,
crosslinking_shard_start,
cycle_length,
epoch_length,
min_committee_size,
)
}
@ -45,29 +46,29 @@ fn generate_cycle(
validator_indices: &[usize],
shard_indices: &[usize],
crosslinking_shard_start: usize,
cycle_length: usize,
epoch_length: usize,
min_committee_size: usize,
) -> Result<DelegatedCycle, ValidatorAssignmentError> {
let validator_count = validator_indices.len();
let shard_count = shard_indices.len();
if shard_count / cycle_length == 0 {
if shard_count / epoch_length == 0 {
return Err(ValidatorAssignmentError::TooFewShards);
}
let (committees_per_slot, slots_per_committee) = {
if validator_count >= cycle_length * min_committee_size {
if validator_count >= epoch_length * min_committee_size {
let committees_per_slot = min(
validator_count / cycle_length / (min_committee_size * 2) + 1,
shard_count / cycle_length,
validator_count / epoch_length / (min_committee_size * 2) + 1,
shard_count / epoch_length,
);
let slots_per_committee = 1;
(committees_per_slot, slots_per_committee)
} else {
let committees_per_slot = 1;
let mut slots_per_committee = 1;
while (validator_count * slots_per_committee < cycle_length * min_committee_size)
& (slots_per_committee < cycle_length)
while (validator_count * slots_per_committee < epoch_length * min_committee_size)
& (slots_per_committee < epoch_length)
{
slots_per_committee *= 2;
}
@ -76,7 +77,7 @@ fn generate_cycle(
};
let cycle = validator_indices
.honey_badger_split(cycle_length)
.honey_badger_split(epoch_length)
.enumerate()
.map(|(i, slot_indices)| {
let shard_start =
@ -108,7 +109,7 @@ mod tests {
validator_count: &usize,
shard_count: &usize,
crosslinking_shard_start: usize,
cycle_length: usize,
epoch_length: usize,
min_committee_size: usize,
) -> (
Vec<usize>,
@ -121,7 +122,7 @@ mod tests {
&validator_indices,
&shard_indices,
crosslinking_shard_start,
cycle_length,
epoch_length,
min_committee_size,
);
(validator_indices, shard_indices, result)
@ -183,13 +184,13 @@ mod tests {
let validator_count: usize = 100;
let shard_count: usize = 20;
let crosslinking_shard_start: usize = 0;
let cycle_length: usize = 20;
let epoch_length: usize = 20;
let min_committee_size: usize = 10;
let (validators, shards, result) = generate_cycle_helper(
&validator_count,
&shard_count,
crosslinking_shard_start,
cycle_length,
epoch_length,
min_committee_size,
);
let cycle = result.unwrap();
@ -242,13 +243,13 @@ mod tests {
let validator_count: usize = 523;
let shard_count: usize = 31;
let crosslinking_shard_start: usize = 0;
let cycle_length: usize = 11;
let epoch_length: usize = 11;
let min_committee_size: usize = 5;
let (validators, shards, result) = generate_cycle_helper(
&validator_count,
&shard_count,
crosslinking_shard_start,
cycle_length,
epoch_length,
min_committee_size,
);
let cycle = result.unwrap();