ran linter

This commit is contained in:
OEM Configuration (temporary user) 2018-06-25 03:59:15 -07:00 committed by Greg Fitzgerald
parent 08bcb62016
commit da3bb6fb93
3 changed files with 94 additions and 112 deletions

View File

@ -1,6 +1,6 @@
use crdt::ReplicatedData; use crdt::ReplicatedData;
use rand::thread_rng;
use rand::distributions::{IndependentSample, Weighted, WeightedChoice}; use rand::distributions::{IndependentSample, Weighted, WeightedChoice};
use rand::thread_rng;
use result::{Error, Result}; use result::{Error, Result};
use signature::PublicKey; use signature::PublicKey;
use std; use std;
@ -9,8 +9,7 @@ use std::collections::HashMap;
pub const DEFAULT_WEIGHT: u32 = 1; pub const DEFAULT_WEIGHT: u32 = 1;
pub trait ChooseGossipPeerStrategy { pub trait ChooseGossipPeerStrategy {
fn choose_peer(&self, options: Vec<&ReplicatedData>) -> fn choose_peer(&self, options: Vec<&ReplicatedData>) -> Result<ReplicatedData>;
Result<ReplicatedData>;
} }
pub struct ChooseRandomPeerStrategy<'a> { pub struct ChooseRandomPeerStrategy<'a> {
@ -18,7 +17,7 @@ pub struct ChooseRandomPeerStrategy<'a> {
} }
impl<'a> ChooseRandomPeerStrategy<'a> { impl<'a> ChooseRandomPeerStrategy<'a> {
pub fn new(random: &'a Fn() -> u64,) -> Self { pub fn new(random: &'a Fn() -> u64) -> Self {
ChooseRandomPeerStrategy { random } ChooseRandomPeerStrategy { random }
} }
} }
@ -45,9 +44,12 @@ impl<'a> ChooseWeightedPeerStrategy<'a> {
remote: &'a HashMap<PublicKey, u64>, remote: &'a HashMap<PublicKey, u64>,
external_liveness: &'a HashMap<PublicKey, HashMap<PublicKey, u64>>, external_liveness: &'a HashMap<PublicKey, HashMap<PublicKey, u64>>,
get_stake: &'a Fn(PublicKey) -> f64, get_stake: &'a Fn(PublicKey) -> f64,
) -> Self ) -> Self {
{ ChooseWeightedPeerStrategy {
ChooseWeightedPeerStrategy { remote, external_liveness, get_stake } remote,
external_liveness,
get_stake,
}
} }
fn calculate_weighted_remote_index(&self, peer_id: PublicKey) -> u32 { fn calculate_weighted_remote_index(&self, peer_id: PublicKey) -> u32 {
@ -73,9 +75,7 @@ impl<'a> ChooseWeightedPeerStrategy<'a> {
// Calculate the weighted average of the rumors // Calculate the weighted average of the rumors
let mut relevant_votes = vec![]; let mut relevant_votes = vec![];
let total_stake = votes.iter().fold( let total_stake = votes.iter().fold(0.0, |total_stake, (&id, &vote)| {
0.0,
|total_stake, (&id, &vote)| {
let stake = (self.get_stake)(id); let stake = (self.get_stake)(id);
// If the total stake is going to overflow u64, pick // If the total stake is going to overflow u64, pick
// the larger of either the current total_stake, or the // the larger of either the current total_stake, or the
@ -92,12 +92,9 @@ impl<'a> ChooseWeightedPeerStrategy<'a> {
relevant_votes.push((stake, vote)); relevant_votes.push((stake, vote));
total_stake + stake total_stake + stake
} }
} });
);
let weighted_vote = relevant_votes.iter().fold( let weighted_vote = relevant_votes.iter().fold(0.0, |sum, &(stake, vote)| {
0.0,
|sum, &(stake, vote)| {
if vote < last_seen_index { if vote < last_seen_index {
// This should never happen b/c we maintain the invariant that the indexes // This should never happen b/c we maintain the invariant that the indexes
// in the external_liveness table are always greater than the corresponding // in the external_liveness table are always greater than the corresponding
@ -126,8 +123,7 @@ impl<'a> ChooseWeightedPeerStrategy<'a> {
} }
sum + new_weight sum + new_weight
}, });
);
// Return u32 b/c the weighted sampling API from rand::distributions // Return u32 b/c the weighted sampling API from rand::distributions
// only takes u32 for weights // only takes u32 for weights
@ -159,17 +155,19 @@ impl<'a> ChooseGossipPeerStrategy for ChooseWeightedPeerStrategy<'a> {
} }
let mut rng = thread_rng(); let mut rng = thread_rng();
Ok(WeightedChoice::new(&mut weighted_peers).ind_sample(&mut rng).clone()) Ok(WeightedChoice::new(&mut weighted_peers)
.ind_sample(&mut rng)
.clone())
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use choose_gossip_peer_strategy::{ChooseWeightedPeerStrategy, DEFAULT_WEIGHT};
use logger; use logger;
use signature::{KeyPair, KeyPairUtil, PublicKey}; use signature::{KeyPair, KeyPairUtil, PublicKey};
use std; use std;
use std::collections::HashMap; use std::collections::HashMap;
use choose_gossip_peer_strategy::{ChooseWeightedPeerStrategy, DEFAULT_WEIGHT};
fn get_stake(id: PublicKey) -> f64 { fn get_stake(id: PublicKey) -> f64 {
return 1.0; return 1.0;
@ -185,11 +183,8 @@ mod tests {
let remote: HashMap<PublicKey, u64> = HashMap::new(); let remote: HashMap<PublicKey, u64> = HashMap::new();
let external_liveness: HashMap<PublicKey, HashMap<PublicKey, u64>> = HashMap::new(); let external_liveness: HashMap<PublicKey, HashMap<PublicKey, u64>> = HashMap::new();
let weighted_strategy = ChooseWeightedPeerStrategy::new( let weighted_strategy =
&remote, ChooseWeightedPeerStrategy::new(&remote, &external_liveness, &get_stake);
&external_liveness,
&get_stake,
);
// If external_liveness table doesn't contain this entry, // If external_liveness table doesn't contain this entry,
// return the default weight // return the default weight
@ -215,11 +210,8 @@ mod tests {
rumors.insert(key2, test_value as u64); rumors.insert(key2, test_value as u64);
external_liveness.insert(key1, rumors); external_liveness.insert(key1, rumors);
let weighted_strategy = ChooseWeightedPeerStrategy::new( let weighted_strategy =
&remote, ChooseWeightedPeerStrategy::new(&remote, &external_liveness, &get_stake);
&external_liveness,
&get_stake,
);
let result = weighted_strategy.calculate_weighted_remote_index(key1); let result = weighted_strategy.calculate_weighted_remote_index(key1);
assert_eq!(result, test_value + DEFAULT_WEIGHT); assert_eq!(result, test_value + DEFAULT_WEIGHT);
@ -242,11 +234,8 @@ mod tests {
rumors.insert(key2, test_value); rumors.insert(key2, test_value);
external_liveness.insert(key1, rumors); external_liveness.insert(key1, rumors);
let weighted_strategy = ChooseWeightedPeerStrategy::new( let weighted_strategy =
&remote, ChooseWeightedPeerStrategy::new(&remote, &external_liveness, &get_stake);
&external_liveness,
&get_stake,
);
let result = weighted_strategy.calculate_weighted_remote_index(key1); let result = weighted_strategy.calculate_weighted_remote_index(key1);
assert_eq!(result, std::u32::MAX); assert_eq!(result, std::u32::MAX);
@ -275,11 +264,8 @@ mod tests {
external_liveness.insert(key1, rumors); external_liveness.insert(key1, rumors);
let weighted_strategy = ChooseWeightedPeerStrategy::new( let weighted_strategy =
&remote, ChooseWeightedPeerStrategy::new(&remote, &external_liveness, &get_stake);
&external_liveness,
&get_stake,
);
let result = weighted_strategy.calculate_weighted_remote_index(key1); let result = weighted_strategy.calculate_weighted_remote_index(key1);
assert_eq!(result, (num_peers / 2) as u32); assert_eq!(result, (num_peers / 2) as u32);
@ -309,11 +295,8 @@ mod tests {
external_liveness.insert(key1, rumors); external_liveness.insert(key1, rumors);
let weighted_strategy = ChooseWeightedPeerStrategy::new( let weighted_strategy =
&remote, ChooseWeightedPeerStrategy::new(&remote, &external_liveness, &get_stake);
&external_liveness,
&get_stake,
);
let result = weighted_strategy.calculate_weighted_remote_index(key1); let result = weighted_strategy.calculate_weighted_remote_index(key1);

View File

@ -15,11 +15,8 @@
use bincode::{deserialize, serialize}; use bincode::{deserialize, serialize};
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use choose_gossip_peer_strategy::{ use choose_gossip_peer_strategy::{ChooseGossipPeerStrategy, ChooseRandomPeerStrategy,
ChooseGossipPeerStrategy, ChooseWeightedPeerStrategy};
ChooseRandomPeerStrategy,
ChooseWeightedPeerStrategy,
};
use hash::Hash; use hash::Hash;
use packet::{to_blob, Blob, BlobRecycler, SharedBlob, BLOB_SIZE}; use packet::{to_blob, Blob, BlobRecycler, SharedBlob, BLOB_SIZE};
use pnet_datalink as datalink; use pnet_datalink as datalink;
@ -241,11 +238,7 @@ impl Crdt {
self.insert(&me); self.insert(&me);
} }
pub fn get_external_liveness_entry( pub fn get_external_liveness_entry(&self, key: &PublicKey) -> Option<&HashMap<PublicKey, u64>> {
&self,
key: &PublicKey,
) -> Option<&HashMap<PublicKey, u64>>
{
self.external_liveness.get(key) self.external_liveness.get(key)
} }
@ -309,6 +302,9 @@ impl Crdt {
self.remote.remove(id); self.remote.remove(id);
self.local.remove(id); self.local.remove(id);
self.external_liveness.remove(id); self.external_liveness.remove(id);
for map in self.external_liveness.values_mut() {
map.remove(id);
}
} }
} }
@ -555,7 +551,7 @@ impl Crdt {
self.table.len() self.table.len()
); );
return Err(Error::CrdtTooSmall); return Err(Error::CrdtTooSmall);
}, }
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@ -627,7 +623,8 @@ impl Crdt {
/// * `update_index` - the number of updates that `from` has completed and this set of `data` represents /// * `update_index` - the number of updates that `from` has completed and this set of `data` represents
/// * `data` - the update data /// * `data` - the update data
fn apply_updates( fn apply_updates(
&mut self, from: PublicKey, &mut self,
from: PublicKey,
update_index: u64, update_index: u64,
data: &[ReplicatedData], data: &[ReplicatedData],
external_liveness: &[(PublicKey, u64)], external_liveness: &[(PublicKey, u64)],
@ -640,8 +637,7 @@ impl Crdt {
} }
for (pk, external_remote_index) in external_liveness.iter() { for (pk, external_remote_index) in external_liveness.iter() {
let remote_entry = let remote_entry = if let Some(v) = self.remote.get(pk) {
if let Some(v) = self.remote.get(pk) {
*v *v
} else { } else {
0 0
@ -757,7 +753,10 @@ impl Crdt {
let me = obj.read().unwrap(); let me = obj.read().unwrap();
// only lock for these two calls, dont lock during IO `sock.send_to` or `sock.recv_from` // only lock for these two calls, dont lock during IO `sock.send_to` or `sock.recv_from`
let (from, ups, data) = me.get_updates_since(v); let (from, ups, data) = me.get_updates_since(v);
let external_liveness = me.remote.iter().map(|(k, v)| (k.clone(), v.clone())).collect(); let external_liveness = me.remote
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
drop(me); drop(me);
trace!("get updates since response {} {}", v, data.len()); trace!("get updates since response {} {}", v, data.len());
let len = data.len(); let len = data.len();

View File

@ -13,6 +13,7 @@ pub mod bank;
pub mod banking_stage; pub mod banking_stage;
pub mod blob_fetch_stage; pub mod blob_fetch_stage;
pub mod budget; pub mod budget;
mod choose_gossip_peer_strategy;
pub mod crdt; pub mod crdt;
pub mod drone; pub mod drone;
pub mod entry; pub mod entry;
@ -47,7 +48,6 @@ pub mod transaction;
pub mod tvu; pub mod tvu;
pub mod window_stage; pub mod window_stage;
pub mod write_stage; pub mod write_stage;
mod choose_gossip_peer_strategy;
extern crate bincode; extern crate bincode;
extern crate byteorder; extern crate byteorder;
extern crate chrono; extern crate chrono;