Skip to content

Commit

Permalink
Apply most of clippy's feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed Jul 11, 2018
1 parent 550eab7 commit a04b839
Show file tree
Hide file tree
Showing 24 changed files with 133 additions and 146 deletions.
6 changes: 3 additions & 3 deletions benches/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn bench_banking_stage_multi_accounts(bencher: &mut Bencher) {
let bank = Arc::new(Bank::new(&mint));

let verified_setup: Vec<_> =
to_packets_chunked(&packet_recycler, setup_transactions.clone(), tx)
to_packets_chunked(&packet_recycler, &setup_transactions.clone(), tx)
.into_iter()
.map(|x| {
let len = (*x).read().unwrap().packets.len();
Expand All @@ -153,7 +153,7 @@ fn bench_banking_stage_multi_accounts(bencher: &mut Bencher) {

check_txs(verified_setup_len, &signal_receiver, num_src_accounts);

let verified: Vec<_> = to_packets_chunked(&packet_recycler, transactions.clone(), 192)
let verified: Vec<_> = to_packets_chunked(&packet_recycler, &transactions.clone(), 192)
.into_iter()
.map(|x| {
let len = (*x).read().unwrap().packets.len();
Expand Down Expand Up @@ -201,7 +201,7 @@ fn bench_banking_stage_single_from(bencher: &mut Bencher) {

bencher.iter(move || {
let bank = Arc::new(Bank::new(&mint));
let verified: Vec<_> = to_packets_chunked(&packet_recycler, transactions.clone(), tx)
let verified: Vec<_> = to_packets_chunked(&packet_recycler, &transactions.clone(), tx)
.into_iter()
.map(|x| {
let len = (*x).read().unwrap().packets.len();
Expand Down
8 changes: 2 additions & 6 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ impl Bank {

/// Commit funds to the `payment.to` party.
fn apply_payment(&self, payment: &Payment, balances: &mut HashMap<PublicKey, i64>) {
if balances.contains_key(&payment.to) {
*balances.get_mut(&payment.to).unwrap() += payment.tokens;
} else {
balances.insert(payment.to, payment.tokens);
}
*balances.entry(payment.to).or_insert(0) += payment.tokens;
}

/// Return the last entry ID registered.
Expand Down Expand Up @@ -511,7 +507,7 @@ impl Bank {
let bals = self.balances
.read()
.expect("'balances' read lock in get_balance");
bals.get(pubkey).map(|x| *x).unwrap_or(0)
bals.get(pubkey).cloned().unwrap_or(0)
}

pub fn transaction_count(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/client-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn generate_and_send_txs(
leader.contact_info.tpu
);
for tx in txs {
client.transfer_signed(tx.clone()).unwrap();
client.transfer_signed(tx).unwrap();
}
});
println!(
Expand Down
4 changes: 2 additions & 2 deletions src/choose_gossip_peer_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<'a> ChooseWeightedPeerStrategy<'a> {

// Return u32 b/c the weighted sampling API from rand::distributions
// only takes u32 for weights
if weighted_vote >= std::u32::MAX as f64 {
if weighted_vote >= f64::from(std::u32::MAX) {
return std::u32::MAX;
}

Expand All @@ -173,7 +173,7 @@ impl<'a> ChooseWeightedPeerStrategy<'a> {

impl<'a> ChooseGossipPeerStrategy for ChooseWeightedPeerStrategy<'a> {
fn choose_peer<'b>(&self, options: Vec<&'b NodeInfo>) -> Result<&'b NodeInfo> {
if options.len() < 1 {
if options.is_empty() {
Err(CrdtError::TooSmall)?;
}

Expand Down
77 changes: 38 additions & 39 deletions src/crdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn parse_port_or_addr(optstr: Option<String>) -> SocketAddr {
let daddr: SocketAddr = "0.0.0.0:8000".parse().expect("default socket address");
if let Some(addrstr) = optstr {
if let Ok(port) = addrstr.parse() {
let mut addr = daddr.clone();
let mut addr = daddr;
addr.set_port(port);
addr
} else if let Ok(addr) = addrstr.parse() {
Expand Down Expand Up @@ -173,12 +173,12 @@ impl NodeInfo {
make_debug_id(&self.id)
}
fn next_port(addr: &SocketAddr, nxt: u16) -> SocketAddr {
let mut nxt_addr = addr.clone();
let mut nxt_addr = *addr;
nxt_addr.set_port(addr.port() + nxt);
nxt_addr
}
pub fn new_leader_with_pubkey(pubkey: PublicKey, bind_addr: &SocketAddr) -> Self {
let transactions_addr = bind_addr.clone();
let transactions_addr = *bind_addr;
let gossip_addr = Self::next_port(&bind_addr, 1);
let replicate_addr = Self::next_port(&bind_addr, 2);
let requests_addr = Self::next_port(&bind_addr, 3);
Expand All @@ -201,9 +201,9 @@ impl NodeInfo {
NodeInfo::new(
PublicKey::default(),
gossip_addr,
daddr.clone(),
daddr.clone(),
daddr.clone(),
daddr,
daddr,
daddr,
daddr,
)
}
Expand Down Expand Up @@ -341,19 +341,19 @@ impl Crdt {
fn update_leader_liveness(&mut self) {
//TODO: (leaders should vote)
//until then we pet their liveness every time we see some votes from anyone
let ld = self.leader_data().map(|x| x.id.clone());
let ld = self.leader_data().map(|x| x.id);
trace!("leader_id {:?}", ld);
if let Some(leader_id) = ld {
self.update_liveness(leader_id);
}
}
pub fn insert_votes(&mut self, votes: Vec<(PublicKey, Vote, Hash)>) {
pub fn insert_votes(&mut self, votes: &[(PublicKey, Vote, Hash)]) {
static mut COUNTER_VOTE: Counter = create_counter!("crdt-vote-count", LOG_RATE);
inc_counter!(COUNTER_VOTE, votes.len());
if votes.len() > 0 {
if !votes.is_empty() {
info!("{:x}: INSERTING VOTES {}", self.debug_id(), votes.len());
}
for v in &votes {
for v in votes {
self.insert_vote(&v.0, &v.1, v.2);
}
}
Expand All @@ -371,7 +371,7 @@ impl Crdt {
);

self.update_index += 1;
let _ = self.table.insert(v.id.clone(), v.clone());
let _ = self.table.insert(v.id, v.clone());
let _ = self.local.insert(v.id, self.update_index);
static mut COUNTER_UPDATE: Counter = create_counter!("crdt-update-count", LOG_RATE);
inc_counter!(COUNTER_UPDATE, 1);
Expand Down Expand Up @@ -449,7 +449,7 @@ impl Crdt {
static mut COUNTER_PURGE: Counter = create_counter!("crdt-purge-count", LOG_RATE);
inc_counter!(COUNTER_PURGE, dead_ids.len());

for id in dead_ids.iter() {
for id in &dead_ids {
self.alive.remove(id);
self.table.remove(id);
self.remote.remove(id);
Expand All @@ -461,11 +461,7 @@ impl Crdt {
}
}

pub fn index_blobs(
me: &NodeInfo,
blobs: &Vec<SharedBlob>,
receive_index: &mut u64,
) -> Result<()> {
pub fn index_blobs(me: &NodeInfo, blobs: &[SharedBlob], receive_index: &mut u64) -> Result<()> {
// enumerate all the blobs, those are the indices
trace!("{:x}: INDEX_BLOBS {}", me.debug_id(), blobs.len());
for (i, b) in blobs.iter().enumerate() {
Expand Down Expand Up @@ -518,13 +514,13 @@ impl Crdt {
/// We need to avoid having obj locked while doing any io, such as the `send_to`
pub fn broadcast(
me: &NodeInfo,
broadcast_table: &Vec<NodeInfo>,
broadcast_table: &[NodeInfo],
window: &Window,
s: &UdpSocket,
transmit_index: &mut u64,
received_index: u64,
) -> Result<()> {
if broadcast_table.len() < 1 {
if broadcast_table.is_empty() {
warn!("{:x}:not enough peers in crdt table", me.debug_id());
Err(CrdtError::TooSmall)?;
}
Expand Down Expand Up @@ -676,7 +672,7 @@ impl Crdt {
Err(CrdtError::TooSmall)?;
}
let n = (Self::random() as usize) % valid.len();
let addr = valid[n].contact_info.ncp.clone();
let addr = valid[n].contact_info.ncp;
let req = Protocol::RequestWindowIndex(self.table[&self.me].clone(), ix);
let out = serialize(&req)?;
Ok((addr, out))
Expand Down Expand Up @@ -768,7 +764,7 @@ impl Crdt {
}
let mut sorted: Vec<(&PublicKey, usize)> = table.into_iter().collect();
let my_id = self.debug_id();
for x in sorted.iter() {
for x in &sorted {
trace!(
"{:x}: sorted leaders {:x} votes: {}",
my_id,
Expand All @@ -784,10 +780,8 @@ impl Crdt {
/// A t-shirt for the first person to actually use this bad behavior to attack the alpha testnet
fn update_leader(&mut self) {
if let Some(leader_id) = self.top_leader() {
if self.my_data().leader_id != leader_id {
if self.table.get(&leader_id).is_some() {
self.set_leader(leader_id);
}
if self.my_data().leader_id != leader_id && self.table.get(&leader_id).is_some() {
self.set_leader(leader_id);
}
}
}
Expand Down Expand Up @@ -822,7 +816,9 @@ impl Crdt {
continue;
}

let liveness_entry = self.external_liveness.entry(*pk).or_insert(HashMap::new());
let liveness_entry = self.external_liveness
.entry(*pk)
.or_insert_with(HashMap::new);
let peer_index = *liveness_entry.entry(from).or_insert(*external_remote_index);
if *external_remote_index > peer_index {
liveness_entry.insert(from, *external_remote_index);
Expand Down Expand Up @@ -854,7 +850,7 @@ impl Crdt {
obj.write().unwrap().purge(timestamp());
//TODO: possibly tune this parameter
//we saw a deadlock passing an obj.read().unwrap().timeout into sleep
let _ = obj.write().unwrap().update_leader();
obj.write().unwrap().update_leader();
let elapsed = timestamp() - start;
if GOSSIP_SLEEP_MILLIS > elapsed {
let time_left = GOSSIP_SLEEP_MILLIS - elapsed;
Expand Down Expand Up @@ -948,10 +944,7 @@ impl Crdt {
let me = obj.read().unwrap();
// 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 external_liveness = me.remote
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
let external_liveness = me.remote.iter().map(|(k, v)| (*k, *v)).collect();
drop(me);
trace!("get updates since response {} {}", v, data.len());
let len = data.len();
Expand Down Expand Up @@ -1091,6 +1084,12 @@ pub struct TestNode {
pub sockets: Sockets,
}

impl Default for TestNode {
fn default() -> Self {
Self::new()
}
}

impl TestNode {
pub fn new() -> Self {
let pubkey = KeyPair::new().pubkey();
Expand All @@ -1115,7 +1114,7 @@ impl TestNode {
repair.local_addr().unwrap(),
);
TestNode {
data: data,
data,
sockets: Sockets {
gossip,
gossip_send,
Expand All @@ -1130,19 +1129,19 @@ impl TestNode {
}
}
pub fn new_with_bind_addr(data: NodeInfo, bind_addr: SocketAddr) -> TestNode {
let mut local_gossip_addr = bind_addr.clone();
let mut local_gossip_addr = bind_addr;
local_gossip_addr.set_port(data.contact_info.ncp.port());

let mut local_replicate_addr = bind_addr.clone();
let mut local_replicate_addr = bind_addr;
local_replicate_addr.set_port(data.contact_info.tvu.port());

let mut local_requests_addr = bind_addr.clone();
let mut local_requests_addr = bind_addr;
local_requests_addr.set_port(data.contact_info.rpu.port());

let mut local_transactions_addr = bind_addr.clone();
let mut local_transactions_addr = bind_addr;
local_transactions_addr.set_port(data.contact_info.tpu.port());

let mut local_repair_addr = bind_addr.clone();
let mut local_repair_addr = bind_addr;
local_repair_addr.set_port(data.contact_info.tvu_window.port());

let transaction = UdpSocket::bind(local_transactions_addr).unwrap();
Expand All @@ -1160,7 +1159,7 @@ impl TestNode {
let broadcast = UdpSocket::bind("0.0.0.0:0").unwrap();
let retransmit = UdpSocket::bind("0.0.0.0:0").unwrap();
TestNode {
data: data,
data,
sockets: Sockets {
gossip,
gossip_send,
Expand Down Expand Up @@ -1300,7 +1299,7 @@ mod tests {
};
sleep(Duration::from_millis(100));
let votes = vec![(d.id.clone(), vote_new_version_old_addrs, Hash::default())];
crdt.insert_votes(votes);
crdt.insert_votes(&votes);
let updated = crdt.alive[&leader.id];
//should be accepted, since the update is for the same address field as the one we know
assert_eq!(crdt.table[&d.id].version, 1);
Expand Down
4 changes: 2 additions & 2 deletions src/drone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Drone {
airdrop_request_amount,
client_public_key,
} => {
request_amount = airdrop_request_amount.clone();
request_amount = airdrop_request_amount;
tx = Transaction::new(
&self.mint_keypair,
client_public_key,
Expand All @@ -136,7 +136,7 @@ impl Drone {
)
.to_owned(),
);
client.transfer_signed(tx)
client.transfer_signed(&tx)
} else {
Err(Error::new(ErrorKind::Other, "token limit reached"))
}
Expand Down
2 changes: 1 addition & 1 deletion src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn next_hash(start_hash: &Hash, num_hashes: u64, transactions: &[Transaction]) -

/// Creates the next Tick or Transaction Entry `num_hashes` after `start_hash`.
pub fn next_entry(start_hash: &Hash, num_hashes: u64, transactions: Vec<Transaction>) -> Entry {
assert!(num_hashes > 0 || transactions.len() == 0);
assert!(num_hashes > 0 || transactions.is_empty());
Entry {
num_hashes,
id: next_hash(start_hash, num_hashes, &transactions),
Expand Down
10 changes: 5 additions & 5 deletions src/fullnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl FullNode {
"starting... local gossip address: {} (advertising {})",
local_gossip_addr, node.data.contact_info.ncp
);
let requests_addr = node.data.contact_info.rpu.clone();
let requests_addr = node.data.contact_info.rpu;
let exit = Arc::new(AtomicBool::new(false));
if !leader {
let testnet_addr = network_entry_for_validator.expect("validator requires entry");
Expand All @@ -121,7 +121,7 @@ impl FullNode {
);
server
} else {
node.data.leader_id = node.data.id.clone();
node.data.leader_id = node.data.id;
let outfile_for_leader: Box<Write + Send> = match outfile_for_leader {
Some(OutFile::Path(file)) => Box::new(
OpenOptions::new()
Expand Down Expand Up @@ -218,11 +218,11 @@ impl FullNode {
let blob_recycler = BlobRecycler::default();
let crdt = Arc::new(RwLock::new(Crdt::new(node.data)));
let (tpu, blob_receiver) = Tpu::new(
bank.clone(),
crdt.clone(),
&bank.clone(),
&crdt.clone(),
tick_duration,
node.sockets.transaction,
blob_recycler.clone(),
&blob_recycler.clone(),
exit.clone(),
writer,
);
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ static INIT: Once = ONCE_INIT;
/// Setup function that is only run once, even if called multiple times.
pub fn setup() {
INIT.call_once(|| {
let _ = env_logger::init();
env_logger::init();
});
}
Loading

0 comments on commit a04b839

Please sign in to comment.