Skip to content

Commit

Permalink
fix: resolved linter issues/failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Aug 28, 2024
1 parent 079b92d commit 53f9c5b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- main
- epic/*
push:
branches:
- main
Expand Down
47 changes: 23 additions & 24 deletions src/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::message::{
};
use crate::{Value, ValueSelector};
use rkyv::{AlignedVec, Deserialize, Infallible};
use seeded_random::{Random, Seed};
use std::cmp::{Ordering, PartialEq};
use std::collections::hash_map::DefaultHasher;
use std::collections::hash_map::Entry::Vacant;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use seeded_random::{Random, Seed};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::time::{self, Duration};

Expand Down Expand Up @@ -142,7 +142,7 @@ impl DefaultLeaderElector {
// Select the `k` suck that value 2^k >= `range` and 2^k is the smallest.
let mut k = 64;
while 1u64 << (k - 1) >= range {
k = k - 1;
k -= 1;
}

// The following algorithm selects a random u64 value using `ChaCha12Rng`
Expand All @@ -154,7 +154,7 @@ impl DefaultLeaderElector {
let rng = Random::from_seed(Seed::unsafe_new(seed));
loop {
let mut raw_res: u64 = rng.gen();
raw_res = (raw_res) >> (64 - k);
raw_res >>= 64 - k;

if raw_res < range {
return raw_res;
Expand Down Expand Up @@ -793,9 +793,10 @@ impl<V: Value, VS: ValueSelector<V>> Party<V, VS> {
mod tests {
use super::*;

use rand::Rng;
use seeded_random::{Random, Seed};
use std::collections::HashMap;
use std::thread;
use rand::Rng;

// Mock implementation of Value
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -893,11 +894,11 @@ mod tests {
party.ballot = 1;

// Must send this message from leader of the ballot.
let leader = party.elector.get_leader(&party).unwrap();
party.leader = 0; // this party's id

let msg = Message1aContent { ballot: 1 };
let routing = MessageRouting {
sender: leader,
sender: 0,
receivers: vec![2, 3],
is_broadcast: false,
msg_type: ProtocolMessage::Msg1a,
Expand Down Expand Up @@ -989,14 +990,14 @@ mod tests {
party.ballot = 1;

// Must send this message from leader of the ballot.
let leader = party.elector.get_leader(&party).unwrap();
party.leader = 0; // this party's id

let msg = Message2aContent {
ballot: 1,
value: bincode::serialize(&MockValue(42)).unwrap(),
};
let routing = MessageRouting {
sender: leader,
sender: 0,
receivers: vec![0],
is_broadcast: false,
msg_type: ProtocolMessage::Msg2a,
Expand Down Expand Up @@ -1272,32 +1273,32 @@ mod tests {
assert_eq!(event_receiver.recv().await.unwrap(), PartyEvent::Finalize);
}

use seeded_random::{Random, Seed};

fn debug_hash_to_range_new(seed: u64, range: u64) -> u64 {
assert!(range > 1);

let mut k = 64;
while 1u64 << (k - 1) >= range {
k = k - 1;
k -= 1;
}

let rng = Random::from_seed(Seed::unsafe_new(seed));

let mut iteration = 1u64;
loop {
let mut raw_res: u64 = rng.gen();
raw_res = (raw_res) >> (64 - k);
raw_res >>= 64 - k;

if raw_res < range {
return raw_res;
}

iteration = iteration + 1;
iteration += 1;
assert!(iteration <= 50)
}
}

#[test]
#[ignore] // Ignoring since it takes a while to run
fn test_hash_range_random() {
// test the uniform distribution

Expand All @@ -1306,27 +1307,25 @@ mod tests {

let mut cnt1: [i64; N] = [0; N];


for i in 0..M {
for _ in 0..M {
let mut rng = rand::thread_rng();
let seed: u64 = rng.gen();
let seed: u64 = rng.random();

let res1 = debug_hash_to_range_new(seed, N as u64);
assert!(res1 < N as u64);

cnt1[res1 as usize] += 1;
}


println!("1: {:?}", cnt1);

let mut avg1: i64 = 0;

for i in 0..N {
avg1 += (M / (N as i64) - cnt1[i]).abs();
for item in cnt1.iter().take(N) {
avg1 += (M / (N as i64) - item).abs();
}

avg1 = avg1 / (N as i64);
avg1 /= N as i64;

println!("Avg 1: {}", avg1);
}
Expand All @@ -1336,12 +1335,12 @@ mod tests {
let rng1 = Random::from_seed(Seed::unsafe_new(123456));
let rng2 = Random::from_seed(Seed::unsafe_new(123456));

println!("{}", rng1.gen::<u64>() as u64);
println!("{}", rng2.gen::<u64>() as u64);
println!("{}", rng1.gen::<u64>());
println!("{}", rng2.gen::<u64>());

thread::sleep(Duration::from_secs(2));

println!("{}", rng1.gen::<u64>() as u64);
println!("{}", rng2.gen::<u64>() as u64);
println!("{}", rng1.gen::<u64>());
println!("{}", rng2.gen::<u64>());
}
}

0 comments on commit 53f9c5b

Please sign in to comment.