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:
parent
b4566a776a
commit
606c32950d
@ -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",
|
||||||
]
|
]
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
use super::honey_badger_split;
|
|
||||||
use super::types;
|
|
||||||
use super::TransitionError;
|
|
||||||
use super::shuffling::shuffle;
|
|
||||||
|
|
||||||
pub mod validator;
|
|
@ -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),
|
|
||||||
}
|
|
@ -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>"]
|
||||||
|
|
@ -10,3 +10,7 @@ pub use inductor::{
|
|||||||
ValidatorInductor,
|
ValidatorInductor,
|
||||||
ValidatorInductionError,
|
ValidatorInductionError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub use registration::{
|
||||||
|
ValidatorRegistration,
|
||||||
|
};
|
||||||
|
@ -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" }
|
@ -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()
|
||||||
|
}
|
8
beacon_chain/validator_shuffling/src/lib.rs
Normal file
8
beacon_chain/validator_shuffling/src/lib.rs
Normal 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;
|
@ -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(
|
Loading…
Reference in New Issue
Block a user