Change error handining in validator_shuffling

This commit is contained in:
Paul Hauner 2018-10-20 18:10:41 +11:00
parent a8d2b20570
commit 7508bfca6a
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 25 additions and 18 deletions

View File

@ -5,4 +5,7 @@ extern crate types;
mod active_validator_indices; mod active_validator_indices;
mod shuffle; mod shuffle;
pub use shuffle::shard_and_committees_for_cycle; pub use shuffle::{
shard_and_committees_for_cycle,
ValidatorAssignmentError,
};

View File

@ -1,7 +1,10 @@
use std::cmp::min; use std::cmp::min;
use honey_badger_split::SplitExt; use honey_badger_split::SplitExt;
use vec_shuffle::shuffle; use vec_shuffle::{
shuffle,
ShuffleErr,
};
use types::{ use types::{
ShardAndCommittee, ShardAndCommittee,
ValidatorRecord, ValidatorRecord,
@ -12,12 +15,12 @@ use super::active_validator_indices::active_validator_indices;
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>; type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum TransitionError { pub enum ValidatorAssignmentError {
InvalidInput(String), TooManyValidators,
TooFewShards,
} }
/// Delegates active validators into slots for a given cycle, given a random seed. /// Delegates active validators into slots for a given cycle, given a random seed.
/// 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.
@ -27,15 +30,11 @@ pub fn shard_and_committees_for_cycle(
validators: &[ValidatorRecord], validators: &[ValidatorRecord],
crosslinking_shard_start: u16, crosslinking_shard_start: u16,
config: &ChainConfig) config: &ChainConfig)
-> Result<DelegatedCycle, TransitionError> -> 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);
match shuffle(seed, validator_indices) { shuffle(seed, validator_indices)?
Ok(shuffled) => shuffled,
_ => return Err(TransitionError::InvalidInput(
String::from("Shuffle list length exceed.")))
}
}; };
let shard_indices: Vec<usize> = (0_usize..config.shard_count as usize).into_iter().collect(); let shard_indices: Vec<usize> = (0_usize..config.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;
@ -56,17 +55,14 @@ fn generate_cycle(
crosslinking_shard_start: usize, crosslinking_shard_start: usize,
cycle_length: usize, cycle_length: usize,
min_committee_size: usize) min_committee_size: usize)
-> Result<DelegatedCycle, TransitionError> -> 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 / cycle_length == 0 {
return Err(TransitionError::InvalidInput(String::from("Number of return Err(ValidatorAssignmentError::TooFewShards)
shards needs to be greater than
cycle length")));
} }
let (committees_per_slot, slots_per_committee) = { let (committees_per_slot, slots_per_committee) = {
@ -105,6 +101,14 @@ fn generate_cycle(
Ok(cycle) Ok(cycle)
} }
impl From<ShuffleErr> for ValidatorAssignmentError {
fn from(e: ShuffleErr) -> ValidatorAssignmentError {
match e {
ShuffleErr::ExceedsListLength => ValidatorAssignmentError::TooManyValidators,
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -115,7 +119,7 @@ mod tests {
crosslinking_shard_start: usize, crosslinking_shard_start: usize,
cycle_length: usize, cycle_length: usize,
min_committee_size: usize) min_committee_size: usize)
-> (Vec<usize>, Vec<usize>, Result<DelegatedCycle, TransitionError>) -> (Vec<usize>, Vec<usize>, Result<DelegatedCycle, ValidatorAssignmentError>)
{ {
let validator_indices: Vec<usize> = (0_usize..*validator_count).into_iter().collect(); let validator_indices: Vec<usize> = (0_usize..*validator_count).into_iter().collect();
let shard_indices: Vec<usize> = (0_usize..*shard_count).into_iter().collect(); let shard_indices: Vec<usize> = (0_usize..*shard_count).into_iter().collect();