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] [workspace]
members = [ members = [
"beacon_chain/types", "beacon_chain/types",
"beacon_chain/transition",
"beacon_chain/utils/bls", "beacon_chain/utils/bls",
"beacon_chain/utils/boolean-bitfield", "beacon_chain/utils/boolean-bitfield",
"beacon_chain/utils/hashing", "beacon_chain/utils/hashing",
"beacon_chain/utils/honey-badger-split", "beacon_chain/utils/honey-badger-split",
"beacon_chain/utils/shuffling",
"beacon_chain/utils/ssz", "beacon_chain/utils/ssz",
"beacon_chain/utils/ssz_helpers", "beacon_chain/utils/ssz_helpers",
"beacon_chain/utils/vec_shuffle",
"beacon_chain/validation", "beacon_chain/validation",
"beacon_chain/validator_induction", "beacon_chain/validator_induction",
"beacon_chain/validator_shuffling",
"lighthouse/db", "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] [package]
name = "shuffling" name = "vec_shuffle"
version = "0.1.0" version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"] authors = ["Paul Hauner <paul@paulhauner.com>"]

View File

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

View File

@ -1,9 +1,9 @@
[package] [package]
name = "transition" name = "validator_shuffling"
version = "0.1.0" version = "0.1.0"
authors = ["Age Manning <Age@AgeManning.com>"] authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies] [dependencies]
honey-badger-split = { path = "../utils/honey-badger-split" } honey-badger-split = { path = "../utils/honey-badger-split" }
types = { path = "../types" } 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 std::cmp::min;
use super::types::{
use honey_badger_split::SplitExt;
use vec_shuffle::shuffle;
use types::{
ShardAndCommittee, ShardAndCommittee,
ValidatorRecord, ValidatorRecord,
ValidatorStatus,
ChainConfig, ChainConfig,
}; };
use super::TransitionError;
use super::shuffle; use super::active_validator_indices::active_validator_indices;
use std::cmp::min;
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>; type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
/// Returns the indicies of each active validator in a given vec of validators. #[derive(Debug)]
fn active_validator_indicies(validators: &[ValidatorRecord]) pub enum TransitionError {
-> Vec<usize> InvalidInput(String),
{
validators.iter()
.enumerate()
.filter_map(|(i, validator)| {
match validator.status {
x if x == ValidatorStatus::Active as u8 => Some(i),
_ => None
}
})
.collect()
} }
@ -31,7 +22,7 @@ fn active_validator_indicies(validators: &[ValidatorRecord])
/// Returns a vector or ShardAndComitte vectors representing the shards and committiees for /// Returns a vector or ShardAndComitte vectors representing the shards and committiees for
/// each slot. /// each slot.
/// References get_new_shuffling (ethereum 2.1 specification) /// References get_new_shuffling (ethereum 2.1 specification)
pub fn delegate_validators( pub fn shard_and_committees_for_cycle(
seed: &[u8], seed: &[u8],
validators: &[ValidatorRecord], validators: &[ValidatorRecord],
crosslinking_shard_start: u16, crosslinking_shard_start: u16,
@ -39,7 +30,7 @@ pub fn delegate_validators(
-> Result<DelegatedCycle, TransitionError> -> Result<DelegatedCycle, TransitionError>
{ {
let shuffled_validator_indices = { 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) { match shuffle(seed, validator_indices) {
Ok(shuffled) => shuffled, Ok(shuffled) => shuffled,
_ => return Err(TransitionError::InvalidInput( _ => return Err(TransitionError::InvalidInput(