Change get_shuffling to borrow

This commit is contained in:
Paul Hauner 2018-07-11 14:16:09 +10:00
parent 5bcb62f6c4
commit 29cfc4dce9

View File

@ -12,14 +12,14 @@ fn get_shift_from_source(source: &[u8], offset: usize) -> u32 {
// Given entropy in the form of `seed`, return a shuffled list of validators // Given entropy in the form of `seed`, return a shuffled list of validators
// of size `validator_count` or `sample`. // of size `validator_count` or `sample`.
pub fn get_shuffling( pub fn get_shuffling(
seed: Sha256Digest, seed: &Sha256Digest,
validator_count: u32, validator_count: &u32,
sample: Option<u32>, sample: &Option<u32>,
config: Config) config: &Config)
-> Vec<u32> -> Vec<u32>
{ {
let max_validators = config.max_validators; let max_validators = config.max_validators;
assert!(validator_count <= max_validators); assert!(*validator_count <= max_validators);
// TODO: figure out why the Python implementation uses // TODO: figure out why the Python implementation uses
// this `rand_max` var. // this `rand_max` var.
@ -28,12 +28,12 @@ pub fn get_shuffling(
Some(x) => x, Some(x) => x,
None => validator_count None => validator_count
}; };
let mut output: Vec<u32> = (0..validator_range).collect(); let mut output: Vec<u32> = (0..*validator_range).collect();
let mut source = Blake2s::new(); let mut source = Blake2s::new();
source.input(&seed); source.input(&seed);
let mut v = 0; let mut v = 0;
while v < validator_range { while v < *validator_range {
let current_source = source.result(); let current_source = source.result();
let mut source_offset = 0; let mut source_offset = 0;
while source_offset < 30 { while source_offset < 30 {
@ -41,7 +41,7 @@ pub fn get_shuffling(
let shuffled_position = (m % (validator_count - v)) + v; let shuffled_position = (m % (validator_count - v)) + v;
output.swap(v as usize, shuffled_position as usize); output.swap(v as usize, shuffled_position as usize);
v += 1; v += 1;
if v >= validator_count { break; } if v >= *validator_count { break; }
source_offset += 3; source_offset += 3;
} }
// Re-hash the source // Re-hash the source
@ -83,10 +83,10 @@ mod tests {
#[test] #[test]
fn test_shuffling() { fn test_shuffling() {
let s = get_shuffling( let s = get_shuffling(
Sha256Digest::zero(), &Sha256Digest::zero(),
10, &10,
None, &None,
Config::standard()); &Config::standard());
assert_eq!(s, assert_eq!(s,
vec!(0, 9, 7, 6, 4, 1, 8, 5, 2, 3), vec!(0, 9, 7, 6, 4, 1, 8, 5, 2, 3),
"10 validator shuffle was not as expected"); "10 validator shuffle was not as expected");