Removes block_hash.rs and adds minor clippy fixes

This commit is contained in:
Age Manning 2018-10-05 14:51:16 +10:00
parent 1858b2d32b
commit fd01ffc2a1
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
3 changed files with 29 additions and 96 deletions

View File

@ -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<Hash256, ParameterError> {
// 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<Hash256> = 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]
);
}
}

View File

@ -2,5 +2,4 @@ use super::types;
use super::TransitionError; use super::TransitionError;
use super::shuffling::shuffle; use super::shuffling::shuffle;
// mod block_hash;
pub mod validator; pub mod validator;

View File

@ -3,12 +3,9 @@ use super::TransitionError;
use super::shuffle; use super::shuffle;
use std::cmp::min; use std::cmp::min;
type DelegatedSlot = Vec<ShardAndCommittee>; type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
type DelegatedCycle = Vec<DelegatedSlot>;
/* /// Iterator for the honey_badger_split function
* Iterator for the honey_badger_split function
*/
struct Split<'a, T: 'a> { struct Split<'a, T: 'a> {
n: usize, n: usize,
current_pos: 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, /// splits a slice into chunks of size n. All postive n values are applicable,
* hence the honey_badger prefix. /// hence the honey_badger prefix.
* Returns an iterator over the original list. /// Returns an iterator over the original list.
*/
trait SplitExt<T> { trait SplitExt<T> {
fn honey_badger_split(&self, n: usize) -> Split<T>; fn honey_badger_split(&self, n: usize) -> Split<T>;
} }
@ -46,7 +42,7 @@ impl<T> SplitExt<T> for [T] {
fn honey_badger_split(&self, n: usize) -> Split<T> { fn honey_badger_split(&self, n: usize) -> Split<T> {
Split { Split {
n: n, n,
current_pos: 0, current_pos: 0,
list: &self, list: &self,
list_length: self.len(), list_length: self.len(),
@ -59,15 +55,15 @@ impl<T> SplitExt<T> for [T] {
* dynasties are within the supplied `dynasty`. * dynasties are within the supplied `dynasty`.
*/ */
fn active_validator_indicies( fn active_validator_indicies(
dynasty: &u64, dynasty: u64,
validators: &Vec<ValidatorRecord>) validators: &[ValidatorRecord])
-> Vec<usize> -> Vec<usize>
{ {
validators.iter() validators.iter()
.enumerate() .enumerate()
.filter_map(|(i, validator)| { .filter_map(|(i, validator)| {
if (validator.start_dynasty >= *dynasty) & if (validator.start_dynasty >= dynasty) &
(validator.end_dynasty < *dynasty) (validator.end_dynasty < dynasty)
{ {
Some(i) Some(i)
} else { } else {
@ -85,9 +81,9 @@ fn active_validator_indicies(
*/ */
pub fn delegate_validators( pub fn delegate_validators(
seed: &[u8], seed: &[u8],
validators: &Vec<ValidatorRecord>, validators: &[ValidatorRecord],
dynasty: &u64, dynasty: u64,
crosslinking_shard_start: &u16, crosslinking_shard_start: u16,
config: &ChainConfig) config: &ChainConfig)
-> Result<DelegatedCycle, TransitionError> -> Result<DelegatedCycle, TransitionError>
{ {
@ -99,27 +95,27 @@ pub fn delegate_validators(
String::from("Shuffle list length exceed."))) String::from("Shuffle list length exceed.")))
} }
}; };
let shard_indices = (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;
let cycle_length = config.cycle_length as usize; let cycle_length = config.cycle_length as usize;
let min_committee_size = config.min_committee_size as usize; let min_committee_size = config.min_committee_size as usize;
generate_cycle( generate_cycle(
&shuffled_validator_indices, &shuffled_validator_indices,
&shard_indices, &shard_indices,
&crosslinking_shard_start, crosslinking_shard_start,
&cycle_length, cycle_length,
&min_committee_size) min_committee_size)
} }
/* /*
* Given the validator list, delegates the validators into slots and comittees for a given cycle. * Given the validator list, delegates the validators into slots and comittees for a given cycle.
*/ */
fn generate_cycle( fn generate_cycle(
validator_indices: &Vec<usize>, validator_indices: &[usize],
shard_indices: &Vec<usize>, shard_indices: &[usize],
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, TransitionError>
{ {
@ -144,21 +140,21 @@ fn generate_cycle(
let committees_per_slot = 1; let committees_per_slot = 1;
let mut slots_per_committee = 1; let mut slots_per_committee = 1;
while (validator_count * slots_per_committee < cycle_length * min_committee_size) & while (validator_count * slots_per_committee < cycle_length * min_committee_size) &
(slots_per_committee < *cycle_length) { (slots_per_committee < cycle_length) {
slots_per_committee = slots_per_committee * 2; slots_per_committee *= 2;
} }
(committees_per_slot, slots_per_committee) (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() .enumerate()
.map(|(i, slot_indices)| { .map(|(i, slot_indices)| {
let shard_id_start = crosslinking_shard_start + i * committees_per_slot / slots_per_committee; 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() .enumerate()
.map(|(j, shard_indices)| { .map(|(j, shard_indices)| {
return ShardAndCommittee{ ShardAndCommittee{
shard_id: ((shard_id_start + j) % shard_count) as u16, shard_id: ((shard_id_start + j) % shard_count) as u16,
committee: shard_indices.to_vec(), committee: shard_indices.to_vec(),
} }