Move shuffling functions around

- Move `delegation` from the `transition` dir into its own dir:
`beacon_chain/validator_shuffling`
- Rename `beacon_chain/utils/shuffling` -> `vec_shuffle`
This commit is contained in:
Paul Hauner 2018-10-20 13:08:43 +11:00
parent b4566a776a
commit 606c32950d
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
11 changed files with 49 additions and 43 deletions

View File

@ -33,15 +33,15 @@ name = "lighthouse"
[workspace]
members = [
"beacon_chain/types",
"beacon_chain/transition",
"beacon_chain/utils/bls",
"beacon_chain/utils/boolean-bitfield",
"beacon_chain/utils/hashing",
"beacon_chain/utils/honey-badger-split",
"beacon_chain/utils/shuffling",
"beacon_chain/utils/ssz",
"beacon_chain/utils/ssz_helpers",
"beacon_chain/utils/vec_shuffle",
"beacon_chain/validation",
"beacon_chain/validator_induction",
"beacon_chain/validator_shuffling",
"lighthouse/db",
]

View File

@ -1,6 +0,0 @@
use super::honey_badger_split;
use super::types;
use super::TransitionError;
use super::shuffling::shuffle;
pub mod validator;

View File

@ -1,10 +0,0 @@
extern crate honey_badger_split;
extern crate types;
extern crate shuffling;
pub mod delegation;
#[derive(Debug)]
pub enum TransitionError {
InvalidInput(String),
}

View File

@ -1,5 +1,5 @@
[package]
name = "shuffling"
name = "vec_shuffle"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]

View File

@ -10,3 +10,7 @@ pub use inductor::{
ValidatorInductor,
ValidatorInductionError,
};
pub use registration::{
ValidatorRegistration,
};

View File

@ -1,9 +1,9 @@
[package]
name = "transition"
name = "validator_shuffling"
version = "0.1.0"
authors = ["Age Manning <Age@AgeManning.com>"]
authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies]
honey-badger-split = { path = "../utils/honey-badger-split" }
types = { path = "../types" }
shuffling = { path = "../utils/shuffling" }
vec_shuffle = { path = "../utils/vec_shuffle" }

View File

@ -0,0 +1,19 @@
use types::{
ValidatorRecord,
ValidatorStatus,
};
/// Returns the indicies of each active validator in a given vec of validators.
pub fn active_validator_indices(validators: &[ValidatorRecord])
-> Vec<usize>
{
validators.iter()
.enumerate()
.filter_map(|(i, validator)| {
match validator.status {
x if x == ValidatorStatus::Active as u8 => Some(i),
_ => None
}
})
.collect()
}

View File

@ -0,0 +1,8 @@
extern crate honey_badger_split;
extern crate vec_shuffle;
extern crate types;
mod active_validator_indices;
mod shuffle;
pub use shuffle::shard_and_committees_for_cycle;

View File

@ -1,29 +1,20 @@
use super::honey_badger_split::SplitExt;
use super::types::{
use std::cmp::min;
use honey_badger_split::SplitExt;
use vec_shuffle::shuffle;
use types::{
ShardAndCommittee,
ValidatorRecord,
ValidatorStatus,
ChainConfig,
};
use super::TransitionError;
use super::shuffle;
use std::cmp::min;
use super::active_validator_indices::active_validator_indices;
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
/// Returns the indicies of each active validator in a given vec of validators.
fn active_validator_indicies(validators: &[ValidatorRecord])
-> Vec<usize>
{
validators.iter()
.enumerate()
.filter_map(|(i, validator)| {
match validator.status {
x if x == ValidatorStatus::Active as u8 => Some(i),
_ => None
}
})
.collect()
#[derive(Debug)]
pub enum TransitionError {
InvalidInput(String),
}
@ -31,7 +22,7 @@ fn active_validator_indicies(validators: &[ValidatorRecord])
/// Returns a vector or ShardAndComitte vectors representing the shards and committiees for
/// each slot.
/// References get_new_shuffling (ethereum 2.1 specification)
pub fn delegate_validators(
pub fn shard_and_committees_for_cycle(
seed: &[u8],
validators: &[ValidatorRecord],
crosslinking_shard_start: u16,
@ -39,7 +30,7 @@ pub fn delegate_validators(
-> Result<DelegatedCycle, TransitionError>
{
let shuffled_validator_indices = {
let mut validator_indices = active_validator_indicies(validators);
let mut validator_indices = active_validator_indices(validators);
match shuffle(seed, validator_indices) {
Ok(shuffled) => shuffled,
_ => return Err(TransitionError::InvalidInput(