Move shuffling mod into state/transition

This commit is contained in:
Paul Hauner 2018-08-24 16:01:24 +10:00
parent 1a00f5b429
commit 6f08700ea0
4 changed files with 12 additions and 11 deletions

View File

@ -8,13 +8,12 @@ extern crate futures;
pub mod db; pub mod db;
pub mod client; pub mod client;
pub mod shuffling;
pub mod state; pub mod state;
pub mod sync; pub mod sync;
pub mod utils; pub mod utils;
pub mod config; pub mod config;
use std::path::PathBuf; use std::path::PathBuf;
use slog::Drain; use slog::Drain;
use clap::{ Arg, App }; use clap::{ Arg, App };
@ -59,9 +58,9 @@ fn main() {
return; return;
} }
} }
// Log configuration // Log configuration
info!(log, ""; info!(log, "";
"data_dir" => &config.data_dir.to_str(), "data_dir" => &config.data_dir.to_str(),
"port" => &config.p2p_listen_port); "port" => &config.p2p_listen_port);

View File

@ -9,3 +9,5 @@ pub enum TransitionError {
InvalidInput(String), InvalidInput(String),
} }

View File

@ -9,18 +9,18 @@ pub enum ShuffleErr {
ExceedsListLength, ExceedsListLength,
} }
/// Performs a deterministic, in-place shuffle of a vector of bytes. /// Performs a deterministic, in-place shuffle of a vector of bytes.
/// The final order of the shuffle is determined by successive hashes /// The final order of the shuffle is determined by successive hashes
/// of the supplied `seed`. /// of the supplied `seed`.
pub fn shuffle( pub fn shuffle(
seed: &[u8], seed: &[u8],
mut list: Vec<usize>) mut list: Vec<usize>)
-> Result<Vec<usize>, ShuffleErr> -> Result<Vec<usize>, ShuffleErr>
{ {
let mut rng = ShuffleRng::new(seed); let mut rng = ShuffleRng::new(seed);
if list.len() > rng.rand_max as usize { if list.len() > rng.rand_max as usize {
return Err(ShuffleErr::ExceedsListLength); return Err(ShuffleErr::ExceedsListLength);
} }
for i in 0..(list.len() - 1) { for i in 0..(list.len() - 1) {
let n = list.len() - i; let n = list.len() - i;
let j = rng.rand_range(n as u32) as usize + i; let j = rng.rand_range(n as u32) as usize + i;

View File

@ -94,22 +94,22 @@ mod tests {
&[0, 1, 1], &[0, 1, 1],
0); 0);
assert_eq!(x, 257); assert_eq!(x, 257);
x = int_from_byte_slice( x = int_from_byte_slice(
&[1, 1, 1], &[1, 1, 1],
0); 0);
assert_eq!(x, 65793); assert_eq!(x, 65793);
x = int_from_byte_slice( x = int_from_byte_slice(
&[255, 1, 1], &[255, 1, 1],
0); 0);
assert_eq!(x, 16711937); assert_eq!(x, 16711937);
x = int_from_byte_slice( x = int_from_byte_slice(
&[255, 255, 255], &[255, 255, 255],
0); 0);
assert_eq!(x, 16777215); assert_eq!(x, 16777215);
x = int_from_byte_slice( x = int_from_byte_slice(
&[0x8f, 0xbb, 0xc7], &[0x8f, 0xbb, 0xc7],
0); 0);