From 616cc616db1509519908e2687ad9eeed6b555cb6 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 22 Sep 2018 08:17:31 +1000 Subject: [PATCH] Fix some clippy lints --- lighthouse/client/mod.rs | 10 ++-- lighthouse/config/mod.rs | 2 +- lighthouse/db/disk_db.rs | 2 +- lighthouse/db/memory_db.rs | 24 ++++----- lighthouse/main.rs | 2 +- .../transition/attestation_parent_hashes.rs | 50 +++++++++---------- lighthouse/state/transition/shuffling/rng.rs | 27 +++++----- lighthouse/sync/block.rs | 24 --------- lighthouse/sync/mod.rs | 1 - lighthouse/sync/network.rs | 37 ++++++++------ lighthouse/sync/sync_future.rs | 10 ++-- lighthouse/sync/wire_protocol.rs | 2 +- lighthouse/utils/logging.rs | 5 +- 13 files changed, 85 insertions(+), 111 deletions(-) delete mode 100644 lighthouse/sync/block.rs diff --git a/lighthouse/client/mod.rs b/lighthouse/client/mod.rs index ee73c2d29..8c65da1a5 100644 --- a/lighthouse/client/mod.rs +++ b/lighthouse/client/mod.rs @@ -25,8 +25,8 @@ impl Client { /// /// Presently, this means starting network and sync threads /// and plumbing them together. - pub fn new(config: LighthouseConfig, - log: Logger) + pub fn new(config: &LighthouseConfig, + log: &Logger) -> Self { // Open the local db @@ -65,8 +65,8 @@ impl Client { sync_db, network_tx.clone(), network_rx, - sync_out_sender, - sync_in_receiver, + &sync_out_sender, + &sync_in_receiver, sync_log, ); }); @@ -75,7 +75,7 @@ impl Client { // Return the client struct Self { - db: db, + db, network_thread, sync_thread, } diff --git a/lighthouse/config/mod.rs b/lighthouse/config/mod.rs index 42dd919e6..725cb455e 100644 --- a/lighthouse/config/mod.rs +++ b/lighthouse/config/mod.rs @@ -23,7 +23,7 @@ impl LighthouseConfig { home.join(DEFAULT_LIGHTHOUSE_DIR) }; fs::create_dir_all(&data_dir) - .expect(&format!("Unable to create {:?}", &data_dir)); + .unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir)); let p2p_listen_port = 0; Self { data_dir, diff --git a/lighthouse/db/disk_db.rs b/lighthouse/db/disk_db.rs index f8f8a7a4c..86face602 100644 --- a/lighthouse/db/disk_db.rs +++ b/lighthouse/db/disk_db.rs @@ -40,7 +40,7 @@ impl DiskDB { * Initialise the path */ fs::create_dir_all(&path) - .expect(&format!("Unable to create {:?}", &path)); + .unwrap_or_else(|_| panic!("Unable to create {:?}", &path)); let db_path = path.join("database"); /* diff --git a/lighthouse/db/memory_db.rs b/lighthouse/db/memory_db.rs index c875b5554..29c2091de 100644 --- a/lighthouse/db/memory_db.rs +++ b/lighthouse/db/memory_db.rs @@ -53,12 +53,11 @@ impl ClientDB for MemoryDB { let db = self.db.read().unwrap(); let known_columns = self.known_columns.read().unwrap(); - match known_columns.contains(&col.to_string()) { - false => Err(DBError{ message: "Unknown column".to_string() }), - true => { - let column_key = MemoryDB::get_key_for_col(col, key); - Ok(db.get(&column_key).and_then(|val| Some(val.clone()))) - } + if known_columns.contains(&col.to_string()) { + let column_key = MemoryDB::get_key_for_col(col, key); + Ok(db.get(&column_key).and_then(|val| Some(val.clone()))) + } else { + Err(DBError{ message: "Unknown column".to_string() }) } } @@ -70,13 +69,12 @@ impl ClientDB for MemoryDB { let mut db = self.db.write().unwrap(); let known_columns = self.known_columns.read().unwrap(); - match known_columns.contains(&col.to_string()) { - false => Err(DBError{ message: "Unknown column".to_string() }), - true => { - let column_key = MemoryDB::get_key_for_col(col, key); - db.insert(column_key, val.to_vec()); - Ok(()) - } + if known_columns.contains(&col.to_string()) { + let column_key = MemoryDB::get_key_for_col(col, key); + db.insert(column_key, val.to_vec()); + Ok(()) + } else { + Err(DBError{ message: "Unknown column".to_string() }) } } } diff --git a/lighthouse/main.rs b/lighthouse/main.rs index 0a9f35288..367b71920 100644 --- a/lighthouse/main.rs +++ b/lighthouse/main.rs @@ -64,7 +64,7 @@ fn main() { "data_dir" => &config.data_dir.to_str(), "port" => &config.p2p_listen_port); - let client = Client::new(config, log.new(o!())); + let client = Client::new(&config, &log); client.sync_thread.join().unwrap(); info!(log, "Exiting."); diff --git a/lighthouse/state/transition/attestation_parent_hashes.rs b/lighthouse/state/transition/attestation_parent_hashes.rs index 624d8c443..35866159f 100644 --- a/lighthouse/state/transition/attestation_parent_hashes.rs +++ b/lighthouse/state/transition/attestation_parent_hashes.rs @@ -13,16 +13,16 @@ use super::TransitionError; /// See this slide for more information: /// https://tinyurl.com/ybzn2spw pub fn attestation_parent_hashes( - cycle_length: &u8, - block_slot: &u64, - attestation_slot: &u64, - current_hashes: &Vec, - oblique_hashes: &Vec) + cycle_length: u8, + block_slot: u64, + attestation_slot: u64, + current_hashes: &[Hash256], + oblique_hashes: &[Hash256]) -> Result, TransitionError> { // This cast places a limit on cycle_length. If you change it, check math // for overflow. - let cycle_length: u64 = *cycle_length as u64; + let cycle_length: u64 = u64::from(cycle_length); if current_hashes.len() as u64 != (cycle_length * 2) { return Err(TransitionError::InvalidInput(String::from( @@ -69,7 +69,7 @@ pub fn attestation_parent_hashes( let mut hashes = Vec::new(); hashes.extend_from_slice( ¤t_hashes[(start as usize)..(end as usize)]); - hashes.append(&mut oblique_hashes.clone()); + hashes.extend_from_slice(oblique_hashes); Ok(hashes) } @@ -98,9 +98,9 @@ mod tests { let current_hashes = get_range_of_hashes(3, 19); let oblique_hashes = get_range_of_hashes(100, 102); let result = attestation_parent_hashes( - &cycle_length, - &block_slot, - &attestation_slot, + cycle_length, + block_slot, + attestation_slot, ¤t_hashes, &oblique_hashes); assert!(result.is_ok()); @@ -123,9 +123,9 @@ mod tests { let current_hashes = get_range_of_hashes(3, 19); let oblique_hashes = get_range_of_hashes(100, 108); let result = attestation_parent_hashes( - &cycle_length, - &block_slot, - &attestation_slot, + cycle_length, + block_slot, + attestation_slot, ¤t_hashes, &oblique_hashes); assert!(result.is_ok()); @@ -148,9 +148,9 @@ mod tests { let current_hashes = get_range_of_hashes(3, 19); let oblique_hashes = vec![]; let result = attestation_parent_hashes( - &cycle_length, - &block_slot, - &attestation_slot, + cycle_length, + block_slot, + attestation_slot, ¤t_hashes, &oblique_hashes); assert!(result.is_ok()); @@ -171,9 +171,9 @@ mod tests { let current_hashes = get_range_of_hashes(0, 16); let oblique_hashes = vec![]; let result = attestation_parent_hashes( - &cycle_length, - &block_slot, - &attestation_slot, + cycle_length, + block_slot, + attestation_slot, ¤t_hashes, &oblique_hashes); assert!(result.is_ok()); @@ -194,9 +194,9 @@ mod tests { let current_hashes = get_range_of_hashes(0, 16); let oblique_hashes = vec![]; let result = attestation_parent_hashes( - &cycle_length, - &block_slot, - &attestation_slot, + cycle_length, + block_slot, + attestation_slot, ¤t_hashes, &oblique_hashes); assert!(result.is_err()); @@ -213,9 +213,9 @@ mod tests { let current_hashes = get_range_of_hashes(0, 15); let oblique_hashes = vec![]; let result = attestation_parent_hashes( - &cycle_length, - &block_slot, - &attestation_slot, + cycle_length, + block_slot, + attestation_slot, ¤t_hashes, &oblique_hashes); assert!(result.is_err()); diff --git a/lighthouse/state/transition/shuffling/rng.rs b/lighthouse/state/transition/shuffling/rng.rs index 224e9130f..e43c582ff 100644 --- a/lighthouse/state/transition/shuffling/rng.rs +++ b/lighthouse/state/transition/shuffling/rng.rs @@ -2,7 +2,7 @@ use super::blake2_rfc::blake2s::{ Blake2s, Blake2sResult }; const SEED_SIZE_BYTES: usize = 32; const RAND_BYTES: usize = 3; // 24 / 8 -const RAND_MAX: u32 = 16777216; // 2**24 +const RAND_MAX: u32 = 16_777_216; // 2**24 /// A pseudo-random number generator which given a seed /// uses successive blake2s hashing to generate "entropy". @@ -31,17 +31,14 @@ impl ShuffleRng { /// Extracts 3 bytes from the `seed`. Rehashes seed if required. fn rand(&mut self) -> u32 { self.idx += RAND_BYTES; - match self.idx >= SEED_SIZE_BYTES { - true => { - self.rehash_seed(); - self.rand() - } - false => { - int_from_byte_slice( - self.seed.as_bytes(), - self.idx - RAND_BYTES, - ) - } + if self.idx >= SEED_SIZE_BYTES { + self.rehash_seed(); + self.rand() + } else { + int_from_byte_slice( + self.seed.as_bytes(), + self.idx - RAND_BYTES, + ) } } @@ -65,9 +62,9 @@ impl ShuffleRng { /// Returns that integer. fn int_from_byte_slice(source: &[u8], offset: usize) -> u32 { ( - source[offset + 2] as u32) | - ((source[offset + 1] as u32) << 8) | - ((source[offset ] as u32) << 16 + u32::from(source[offset + 2])) | + (u32::from(source[offset + 1]) << 8) | + (u32::from(source[offset ]) << 16 ) } diff --git a/lighthouse/sync/block.rs b/lighthouse/sync/block.rs deleted file mode 100644 index 72d827285..000000000 --- a/lighthouse/sync/block.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::sync::Arc; -use super::db::ClientDB; -use slog::Logger; - -pub enum BlockStatus { - Valid, - AlreadyKnown, - TooOld, - TimeInvalid, - UnknownPoWHash, - NoAttestations, - InvalidAttestation, - NotProposerSigned, -} - -pub fn process_unverified_blocks( - _serialized_block: &[u8], - _db: Arc, - _log: Logger) -{ - // -} - - diff --git a/lighthouse/sync/mod.rs b/lighthouse/sync/mod.rs index f56260e4f..6e1f0be11 100644 --- a/lighthouse/sync/mod.rs +++ b/lighthouse/sync/mod.rs @@ -3,7 +3,6 @@ extern crate slog; extern crate tokio; extern crate network_libp2p; -pub mod block; pub mod network; pub mod sync_future; pub mod wire_protocol; diff --git a/lighthouse/sync/network.rs b/lighthouse/sync/network.rs index 1383b5040..1204a2093 100644 --- a/lighthouse/sync/network.rs +++ b/lighthouse/sync/network.rs @@ -8,8 +8,6 @@ use super::network_libp2p::message::{ NetworkEventType, }; -use super::block::process_unverified_blocks; - use super::wire_protocol::{ WireMessage, WireMessageHeader, @@ -25,9 +23,9 @@ use super::futures::sync::mpsc::{ /// (e.g., libp2p) has an event to push up to the sync process. pub fn handle_network_event( event: NetworkEvent, - db: Arc, - network_tx: UnboundedSender, - log: Logger) + db: &Arc, + network_tx: &UnboundedSender, + log: &Logger) -> Result<(), ()> { debug!(&log, ""; @@ -38,10 +36,10 @@ pub fn handle_network_event( NetworkEventType::Message => { if let Some(data) = event.data { handle_network_message( - data, - db, - network_tx, - log) + &data, + &db, + &network_tx, + &log) } else { Ok(()) } @@ -55,10 +53,10 @@ pub fn handle_network_event( /// This function should be called whenever a peer from a network /// (e.g., libp2p) has sent a message to us. fn handle_network_message( - message: Vec, - db: Arc, - _network_tx: UnboundedSender, - log: Logger) + message: &[u8], + db: &Arc, + _network_tx: &UnboundedSender, + log: &Logger) -> Result<(), ()> { match WireMessage::decode(&message) { @@ -67,8 +65,8 @@ fn handle_network_message( WireMessageHeader::Blocks => { process_unverified_blocks( msg.body, - db, - log + &db, + &log ); Ok(()) } @@ -76,7 +74,14 @@ fn handle_network_message( } } Err(_) => { - return Ok(()) // No need to pass the error back + Ok(()) // No need to pass the error back } } } + +fn process_unverified_blocks(_message: &[u8], + _db: &Arc, + _log: &Logger) +{ + // +} diff --git a/lighthouse/sync/sync_future.rs b/lighthouse/sync/sync_future.rs index 0e46e9e50..31cc933ca 100644 --- a/lighthouse/sync/sync_future.rs +++ b/lighthouse/sync/sync_future.rs @@ -28,8 +28,8 @@ pub fn run_sync_future( db: Arc, network_tx: NetworkSender, network_rx: NetworkReceiver, - _sync_tx: SyncSender, - _sync_rx: SyncReceiver, + _sync_tx: &SyncSender, + _sync_rx: &SyncReceiver, log: Logger) { let network_future = { @@ -37,9 +37,9 @@ pub fn run_sync_future( .for_each(move |event| { handle_network_event( event, - db.clone(), - network_tx.clone(), - log.clone()) + &db.clone(), + &network_tx.clone(), + &log.clone()) }) .map_err(|_| panic!("rx failed")) }; diff --git a/lighthouse/sync/wire_protocol.rs b/lighthouse/sync/wire_protocol.rs index 999d14a73..8f9c8bd57 100644 --- a/lighthouse/sync/wire_protocol.rs +++ b/lighthouse/sync/wire_protocol.rs @@ -19,7 +19,7 @@ pub struct WireMessage<'a> { } impl<'a> WireMessage<'a> { - pub fn decode(bytes: &'a Vec) + pub fn decode(bytes: &'a [u8]) -> Result { if let Some((header_byte, body)) = bytes.split_first() { diff --git a/lighthouse/utils/logging.rs b/lighthouse/utils/logging.rs index 67b27bf2c..7dc186d30 100644 --- a/lighthouse/utils/logging.rs +++ b/lighthouse/utils/logging.rs @@ -7,11 +7,10 @@ pub use slog::Logger; pub fn test_logger() -> slog::Logger { let plain = slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter); - let logger = Logger::root( + Logger::root( slog_term::FullFormat::new(plain) .build().fuse(), o!() - ); - logger + ) } pub fn get_logger() -> slog::Logger {