From fd01ffc2a1922a9979e09b868cfe8171666959f2 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 5 Oct 2018 14:51:16 +1000 Subject: [PATCH] Removes block_hash.rs and adds minor clippy fixes --- .../transition/src/delegation/block_hash.rs | 62 ------------------- beacon_chain/transition/src/delegation/mod.rs | 1 - .../transition/src/delegation/validator.rs | 62 +++++++++---------- 3 files changed, 29 insertions(+), 96 deletions(-) delete mode 100644 beacon_chain/transition/src/delegation/block_hash.rs diff --git a/beacon_chain/transition/src/delegation/block_hash.rs b/beacon_chain/transition/src/delegation/block_hash.rs deleted file mode 100644 index 3d0939d29..000000000 --- a/beacon_chain/transition/src/delegation/block_hash.rs +++ /dev/null @@ -1,62 +0,0 @@ -use super::utils::errors::ParameterError; -use super::utils::types::Hash256; - -/* - * Work-in-progress function: not ready for review. - */ - -pub fn get_block_hash( - active_state_recent_block_hashes: &[Hash256], - current_block_slot: u64, - slot: u64, - cycle_length: u64, // convert from standard u8 -) -> Result { - // active_state must have at 2*cycle_length hashes - assert_error!( - active_state_recent_block_hashes.len() as u64 == cycle_length * 2, - ParameterError::InvalidInput(String::from( - "active state has incorrect number of block hashes" - )) - ); - - let state_start_slot = (current_block_slot) - .checked_sub(cycle_length * 2) - .unwrap_or(0); - - assert_error!( - (state_start_slot <= slot) && (slot < current_block_slot), - ParameterError::InvalidInput(String::from("incorrect slot number")) - ); - - let index = 2 * cycle_length + slot - current_block_slot; // should always be positive - Ok(active_state_recent_block_hashes[index as usize]) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_get_block_hash() { - let block_slot: u64 = 10; - let slot: u64 = 3; - let cycle_length: u64 = 8; - - let mut block_hashes: Vec = Vec::new(); - for _i in 0..2 * cycle_length { - block_hashes.push(Hash256::random()); - } - - let result = get_block_hash( - &block_hashes, - block_slot, - slot, - cycle_length) - .unwrap(); - - assert_eq!( - result, - block_hashes[(2 * cycle_length + slot - block_slot) as usize] - ); - } -} diff --git a/beacon_chain/transition/src/delegation/mod.rs b/beacon_chain/transition/src/delegation/mod.rs index 2bc9bccd2..265b3e9b6 100644 --- a/beacon_chain/transition/src/delegation/mod.rs +++ b/beacon_chain/transition/src/delegation/mod.rs @@ -2,5 +2,4 @@ use super::types; use super::TransitionError; use super::shuffling::shuffle; -// mod block_hash; pub mod validator; diff --git a/beacon_chain/transition/src/delegation/validator.rs b/beacon_chain/transition/src/delegation/validator.rs index 45331c4c8..41c4c0e4d 100644 --- a/beacon_chain/transition/src/delegation/validator.rs +++ b/beacon_chain/transition/src/delegation/validator.rs @@ -3,12 +3,9 @@ use super::TransitionError; use super::shuffle; use std::cmp::min; -type DelegatedSlot = Vec; -type DelegatedCycle = Vec; +type DelegatedCycle = Vec>; -/* - * Iterator for the honey_badger_split function - */ +/// Iterator for the honey_badger_split function struct Split<'a, T: 'a> { n: usize, current_pos: usize, @@ -33,11 +30,10 @@ impl<'a,T> Iterator for Split<'a, T> { } } -/* - * splits a slice into chunks of size n. All postive n values are applicable, - * hence the honey_badger prefix. - * Returns an iterator over the original list. - */ + +/// splits a slice into chunks of size n. All postive n values are applicable, +/// hence the honey_badger prefix. +/// Returns an iterator over the original list. trait SplitExt { fn honey_badger_split(&self, n: usize) -> Split; } @@ -46,7 +42,7 @@ impl SplitExt for [T] { fn honey_badger_split(&self, n: usize) -> Split { Split { - n: n, + n, current_pos: 0, list: &self, list_length: self.len(), @@ -59,15 +55,15 @@ impl SplitExt for [T] { * dynasties are within the supplied `dynasty`. */ fn active_validator_indicies( - dynasty: &u64, - validators: &Vec) + dynasty: u64, + validators: &[ValidatorRecord]) -> Vec { validators.iter() .enumerate() .filter_map(|(i, validator)| { - if (validator.start_dynasty >= *dynasty) & - (validator.end_dynasty < *dynasty) + if (validator.start_dynasty >= dynasty) & + (validator.end_dynasty < dynasty) { Some(i) } else { @@ -85,9 +81,9 @@ fn active_validator_indicies( */ pub fn delegate_validators( seed: &[u8], - validators: &Vec, - dynasty: &u64, - crosslinking_shard_start: &u16, + validators: &[ValidatorRecord], + dynasty: u64, + crosslinking_shard_start: u16, config: &ChainConfig) -> Result { @@ -99,27 +95,27 @@ pub fn delegate_validators( String::from("Shuffle list length exceed."))) } }; - let shard_indices = (0_usize..config.shard_count as usize).into_iter().collect(); - let crosslinking_shard_start = *crosslinking_shard_start as usize; + let shard_indices: Vec = (0_usize..config.shard_count as usize).into_iter().collect(); + let crosslinking_shard_start = crosslinking_shard_start as usize; let cycle_length = config.cycle_length as usize; let min_committee_size = config.min_committee_size as usize; generate_cycle( &shuffled_validator_indices, &shard_indices, - &crosslinking_shard_start, - &cycle_length, - &min_committee_size) + crosslinking_shard_start, + cycle_length, + min_committee_size) } /* * Given the validator list, delegates the validators into slots and comittees for a given cycle. */ fn generate_cycle( - validator_indices: &Vec, - shard_indices: &Vec, - crosslinking_shard_start: &usize, - cycle_length: &usize, - min_committee_size: &usize) + validator_indices: &[usize], + shard_indices: &[usize], + crosslinking_shard_start: usize, + cycle_length: usize, + min_committee_size: usize) -> Result { @@ -144,21 +140,21 @@ fn generate_cycle( let committees_per_slot = 1; let mut slots_per_committee = 1; while (validator_count * slots_per_committee < cycle_length * min_committee_size) & - (slots_per_committee < *cycle_length) { - slots_per_committee = slots_per_committee * 2; + (slots_per_committee < cycle_length) { + slots_per_committee *= 2; } (committees_per_slot, slots_per_committee) } }; - let cycle = validator_indices.honey_badger_split(*cycle_length) + let cycle = validator_indices.honey_badger_split(cycle_length) .enumerate() .map(|(i, slot_indices)| { let shard_id_start = crosslinking_shard_start + i * committees_per_slot / slots_per_committee; - return slot_indices.honey_badger_split(committees_per_slot) + slot_indices.honey_badger_split(committees_per_slot) .enumerate() .map(|(j, shard_indices)| { - return ShardAndCommittee{ + ShardAndCommittee{ shard_id: ((shard_id_start + j) % shard_count) as u16, committee: shard_indices.to_vec(), }