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 { 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 { Self {
current_slot, current_slot,
shard_count, shard_count,

View File

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

View File

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

View File

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