// simple consensus from timestamps
// timestamps, u64, [0, u64_max]
// tolerance, u32, [0, u32_max]
// threshold, f64, [0.0, 1.0]
// 1. Remove a randomly selected timestamp from the array of timestamps, ts
// 2. Count the number of timestamps left in the array that are withn +/- tolerance (where tolerance may be zero)
// 3. compare the suporting number of times stamps divided by th enumber of remaining timestamps to the threshold. if >=, consensus for selected timestamp is true else false
fn ts_frequency(mut timestamps: Vec<u64>, tolerance: u32, threshold: f64, err_value: u64) -> Consensus {
timestamps.retain(|&ts| ts != err_value);
if timestamps.len() == 0 {
err_str: "Array must have at least one element".to_string(),
if timestamps.len() == 1 {
consensus_ts: timestamps[0],
if threshold < 0f64 || threshold > 1f64 {
err_str: "Threshold needs to be between [0.0,1.0]".to_string(),
let rnd_seed: u64 = timestamps.iter().sum();
let mut rng = WyRand::new_seed(rnd_seed);
let rnd_idx = rng.generate_range(0..timestamps.len());
let consensus_ts = timestamps.swap_remove(rnd_idx);
let mut support: u32 = 0;
for ts in timestamps.iter() {
if ts <= &(consensus_ts + tolerance as u64) && ts >= &(consensus_ts - tolerance as u64) {
let mut consensus = false;
if (support as f64 / timestamps.len() as f64) >= threshold {
n: timestamps.len() as u32,