diff --git a/blockchain/lib.rs b/blockchain/lib.rs index a52ce9d8dbcc9f622503b52dfd9b429065b7f973..7cbacc05b6a5e1fbaa6b2ab8838d8982df7c94de 100644 --- a/blockchain/lib.rs +++ b/blockchain/lib.rs @@ -16,7 +16,7 @@ //! Module managing the Duniter blockchain. #![cfg_attr(feature = "strict", deny(warnings))] -#![cfg_attr(feature = "cargo-clippy", allow(unused_collect))] +#![cfg_attr(feature = "cargo-clippy", allow(unused_collect, duration_subsec))] #![deny( missing_docs, missing_debug_implementations, missing_copy_implementations, trivial_casts, trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces, @@ -217,9 +217,9 @@ impl BlockchainModule { sync::sync_ts(profile, conf, db_ts_path, cautious, verif_inner_hash); } /// Request chunk from network (chunk = group of blocks) - fn request_chunk(&self, req_id: &ModuleReqId, from: u32) -> (ModuleReqId, NetworkRequest) { + fn request_chunk(&self, req_id: ModuleReqId, from: u32) -> (ModuleReqId, NetworkRequest) { let req = NetworkRequest::GetBlocks( - ModuleReqFullId(BlockchainModule::id(), *req_id), + ModuleReqFullId(BlockchainModule::id(), req_id), NodeFullId::default(), *CHUNK_SIZE, from, @@ -257,7 +257,7 @@ impl BlockchainModule { { req_id = ModuleReqId(req_id.0 + 1); } - let (req_id, req) = self.request_chunk(&req_id, from); + let (req_id, req) = self.request_chunk(req_id, from); requests_ids.insert(req_id, req); from += *CHUNK_SIZE; } @@ -568,7 +568,7 @@ impl BlockchainModule { req_id = ModuleReqId(req_id.0 + 1); } let from = consensus.id.0 - *CHUNK_SIZE - 1; - let (new_req_id, new_req) = self.request_chunk(&req_id, from); + let (new_req_id, new_req) = self.request_chunk(req_id, from); pending_network_requests.insert(new_req_id, new_req); } } diff --git a/blockchain/ts_parsers.rs b/blockchain/ts_parsers.rs index 2915f8def24c23e9da48f6097cb73707aea15e98..6fb27f4dffd8e59288a6807daec2d9df9b06776e 100644 --- a/blockchain/ts_parsers.rs +++ b/blockchain/ts_parsers.rs @@ -345,8 +345,8 @@ pub fn parse_transaction( TransactionOutput::parse_from_str( output .as_str() - .expect(&format!("Fail to parse output : {:?}", output)), - ).expect(&format!("Fail to parse output : {:?}", output)), + .unwrap_or_else(|| panic!("Fail to parse output : {:?}", output)), + ).unwrap_or_else(|_| panic!("Fail to parse output : {:?}", output)), ); } let signatures_array = source.get("signatures")?.as_array()?; diff --git a/conf/lib.rs b/conf/lib.rs index c7931d0f7c1023213bf3cf67e814c8ac889cfdf4..41a5c169134f6f30143f6174b2da63b4159bc7e8 100644 --- a/conf/lib.rs +++ b/conf/lib.rs @@ -249,10 +249,9 @@ pub fn get_profile_path(profile: &str) -> PathBuf { } profile_path.push(USER_DATAS_FOLDER); if !profile_path.as_path().exists() { - fs::create_dir(profile_path.as_path()).expect(&format!( - "Impossible to create ~/.config/{} dir !", - USER_DATAS_FOLDER - )); + fs::create_dir(profile_path.as_path()).unwrap_or_else(|_| { + panic!("Impossible to create ~/.config/{} dir !", USER_DATAS_FOLDER) + }); } profile_path.push(profile); if !profile_path.as_path().exists() { diff --git a/core/lib.rs b/core/lib.rs index 50cf7b326b3c8524dc915637e000b862ef2e6ed1..32777c664a29254d8a65d0c92cb1a8b1b9209874 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -326,10 +326,12 @@ impl DuniterCore<DuRsConf> { &module_conf, rooter_sender, sync_endpoint, - ).expect(&format!( - "Fatal error : fail to load {} Module !", - NM::id().to_string() - )); + ).unwrap_or_else(|_| { + panic!( + "Fatal error : fail to load {} Module !", + NM::id().to_string() + ) + }); }); self.modules_count += 1; info!("Success to load {} module.", NM::id().to_string()); @@ -370,10 +372,12 @@ impl DuniterCore<DuRsConf> { &module_conf, rooter_sender_clone, false, - ).expect(&format!( - "Fatal error : fail to load {} Module !", - M::id().to_string() - )); + ).unwrap_or_else(|_| { + panic!( + "Fatal error : fail to load {} Module !", + M::id().to_string() + ) + }); }); self.modules_count += 1; info!("Success to load {} module.", M::id().to_string()); diff --git a/dal/block.rs b/dal/block.rs index 3e8c9fff22c79a6c8388afb6cef7a637a94447bd..68a6db36a4acf7341bfe8c1e3e66c11949f348fb 100644 --- a/dal/block.rs +++ b/dal/block.rs @@ -222,10 +222,10 @@ impl DALBlock { } pub fn get_block_hash( db: &BinFileDB<LocalBlockchainV10Datas>, - block_number: &BlockId, + block_number: BlockId, ) -> Result<Option<BlockHash>, DALError> { Ok(db.read(|db| { - if let Some(dal_block) = db.get(block_number) { + if let Some(dal_block) = db.get(&block_number) { dal_block.block.hash } else { None @@ -336,7 +336,7 @@ impl DALBlock { for block_number in frame_begin..self.block.number.0 { let issuer = db .get(&BlockId(block_number)) - .expect(&format!("Fail to get block #{} !", block_number)) + .unwrap_or_else(|| panic!("Fail to get block #{} !", block_number)) .block .issuers()[0]; let issuer_count_blocks = diff --git a/dal/lib.rs b/dal/lib.rs index 29a15beeddff480ffd5ed361d210fd70b1db20d7..5887c633cf26d4837119c4e3c63180a4e56fe4ef 100644 --- a/dal/lib.rs +++ b/dal/lib.rs @@ -194,7 +194,7 @@ impl CurrencyV10DBs<FileBackend> { CurrencyV10DBs { du_db: open_db::<DUsV10Datas>(&db_path, "du.db").expect("Fail to open DUsV10DB"), tx_db: open_db::<TxV10Datas>(&db_path, "tx.db") - .expect(&format!("Fail to open TxV10DB : {:?} ", db_path.as_path())), + .unwrap_or_else(|_| panic!("Fail to open TxV10DB : {:?} ", db_path.as_path())), utxos_db: open_db::<UTXOsV10Datas>(&db_path, "sources.db") .expect("Fail to open UTXOsV10DB"), balances_db: open_db::<BalancesV10Datas>(&db_path, "balances.db") diff --git a/dal/tools.rs b/dal/tools.rs index 8943916f16b757f00aa4f5bb6ceb496675923a8d..8112525189ebc1c5fc53a77db3ad424c8a0d23dd 100644 --- a/dal/tools.rs +++ b/dal/tools.rs @@ -40,10 +40,9 @@ pub fn calculate_average_density<T: WebOfTrust>(wot: &T) -> usize { let enabled_members_count = enabled_members.len(); let mut count_actives_links: usize = 0; for member in &enabled_members { - count_actives_links += wot.issued_count(*member).expect(&format!( - "Fail to get issued_count of wot_id {}", - (*member).0 - )); + count_actives_links += wot + .issued_count(*member) + .unwrap_or_else(|| panic!("Fail to get issued_count of wot_id {}", (*member).0)); } ((count_actives_links as f32 / enabled_members_count as f32) * 1_000.0) as usize } diff --git a/dal/writers/dividend.rs b/dal/writers/dividend.rs index ec8e74e21d96cb8d3867987d2da61ab848aaa33f..cdeac04d8edc9eabf86f5dc71b2bd517a65c3808 100644 --- a/dal/writers/dividend.rs +++ b/dal/writers/dividend.rs @@ -26,7 +26,7 @@ pub fn create_du<B: Backend + Debug>( du_db: &BinDB<DUsV10Datas, B>, balances_db: &BinDB<BalancesV10Datas, B>, du_amount: &SourceAmount, - du_block_id: &BlockId, + du_block_id: BlockId, members: &[PubKey], revert: bool, ) -> Result<(), DALError> { @@ -35,9 +35,9 @@ pub fn create_du<B: Backend + Debug>( for pubkey in members { let mut pubkey_dus = db.get(&pubkey).cloned().unwrap_or_default(); if revert { - pubkey_dus.remove(du_block_id); + pubkey_dus.remove(&du_block_id); } else { - pubkey_dus.insert(*du_block_id); + pubkey_dus.insert(du_block_id); } db.insert(*pubkey, pubkey_dus); } diff --git a/dal/writers/requests.rs b/dal/writers/requests.rs index 865aaef94e1f3ef62e104bf2db9a01a94b8e7574..1f2428fdf1e02099c08888a984d68602255326c5 100644 --- a/dal/writers/requests.rs +++ b/dal/writers/requests.rs @@ -271,7 +271,7 @@ impl CurrencyDBsWriteQuery { &databases.du_db, &databases.balances_db, du_amount, - block_id, + *block_id, members, false, )?; @@ -281,7 +281,7 @@ impl CurrencyDBsWriteQuery { &databases.du_db, &databases.balances_db, du_amount, - block_id, + *block_id, members, true, )?; diff --git a/dal/writers/transaction.rs b/dal/writers/transaction.rs index d2021bb969978139c2219495a2e1bf5dabe561da..11382952b770b1385397783dd6e42aaccaf6402e 100644 --- a/dal/writers/transaction.rs +++ b/dal/writers/transaction.rs @@ -266,10 +266,12 @@ pub fn apply_and_write_tx<B: Backend + Debug>( for (source_index, source_amount) in &consumed_sources { if let SourceIndexV10::UTXO(utxo_index) = source_index { // Get utxo - let utxo = db.get(&utxo_index).expect(&format!( - "ApplyBLockError : unknow UTXO in inputs : {:?} !", - utxo_index - )); + let utxo = db.get(&utxo_index).unwrap_or_else(|| { + panic!( + "ApplyBLockError : unknow UTXO in inputs : {:?} !", + utxo_index + ) + }); // Get utxo conditions(=address) let conditions = &utxo.conditions.conditions; // Calculate new balances datas for "conditions" address @@ -466,7 +468,7 @@ mod tests { ¤cy_dbs.du_db, ¤cy_dbs.balances_db, &SourceAmount(TxAmount(1000), TxBase(0)), - &BlockId(1), + BlockId(1), &vec![tx_doc.issuers()[0], tortue_pubkey], false, ).expect("Fail to create first g1 DU !"); diff --git a/ws2p/datas.rs b/ws2p/datas.rs index 216b9f448b4a7797e56ca92ecddbac88c3dfc0fb..96aea97a0e128690ea7f4e4ebfc9ce6e7876d627 100644 --- a/ws2p/datas.rs +++ b/ws2p/datas.rs @@ -90,10 +90,14 @@ impl WS2PModuleDatas { pubkey, 0, 0, - ).expect(&format!( - "WS2PConf Error : fail to parse sync Endpoint = {:?}", - endpoint.as_str().expect("WS2P: Fail to get ep.as_str() !") - )), + ).unwrap_or_else(|| { + panic!( + "WS2PConf Error : fail to parse sync Endpoint = {:?}", + endpoint + .as_str() + .expect("WS2P: Fail to get ep.as_str() !") + ) + }), ); } } @@ -274,10 +278,9 @@ impl WS2PModuleDatas { .expect("Try to close an unexistant websocket !") .0 .close(ws::CloseCode::Normal); - self.websockets.remove(ws2p_full_id).expect(&format!( - "Fatal error : no websocket for {} !", - ws2p_full_id - ));; + self.websockets + .remove(ws2p_full_id) + .unwrap_or_else(|| panic!("Fatal error : no websocket for {} !", ws2p_full_id)); } pub fn ws2p_conn_message_pretreatment(&mut self, message: WS2PConnectionMessage) -> WS2PSignal { let ws2p_full_id = message.0; @@ -318,10 +321,7 @@ impl WS2PModuleDatas { debug!("Send: {:#?}", response); self.websockets .get_mut(&ws2p_full_id) - .expect(&format!( - "Fatal error : no websocket for {} !", - ws2p_full_id - )) + .unwrap_or_else(|| panic!("Fatal error : no websocket for {} !", ws2p_full_id)) .0 .send(Message::text(response)) .expect("WS2P: Fail to send OK Message !"); @@ -335,10 +335,9 @@ impl WS2PModuleDatas { trace!("DEBUG : Send: {:#?}", r); self.websockets .get_mut(&ws2p_full_id) - .expect(&format!( - "Fatal error : no websocket for {} !", - ws2p_full_id - )) + .unwrap_or_else(|| { + panic!("Fatal error : no websocket for {} !", ws2p_full_id) + }) .0 .send(Message::text(r)) .expect("WS2P: Fail to send Message in websocket !"); diff --git a/ws2p/parsers/blocks.rs b/ws2p/parsers/blocks.rs index 1d08cc31175f1920faba9f08492a6f8e99fb6d84..15545b8375fa59af08f166c72c6ce6bc250278be 100644 --- a/ws2p/parsers/blocks.rs +++ b/ws2p/parsers/blocks.rs @@ -13,7 +13,7 @@ use duniter_documents::{BlockHash, BlockId, Hash}; use duniter_network::{NetworkBlock, NetworkBlockV10}; use std::str::FromStr; -fn parse_previous_hash(block_number: &BlockId, source: &serde_json::Value) -> Option<Hash> { +fn parse_previous_hash(block_number: BlockId, source: &serde_json::Value) -> Option<Hash> { match source.get("previousHash")?.as_str() { Some(hash_str) => match Hash::from_hex(hash_str) { Ok(hash) => Some(hash), @@ -81,7 +81,7 @@ pub fn parse_json_block(source: &serde_json::Value) -> Option<NetworkBlock> { } else { None }; - let previous_hash = parse_previous_hash(&number, source)?; + let previous_hash = parse_previous_hash(number, source)?; let previous_issuer = parse_previous_issuer(source); let inner_hash = match Hash::from_hex(source.get("inner_hash")?.as_str()?) { Ok(hash) => Some(hash), diff --git a/ws2p/ws2p_connection.rs b/ws2p/ws2p_connection.rs index 5c1be57d031e136b4a053ee8f4479d134e3ead45..3a578a968252096887caac9e042ddbea6cea236e 100644 --- a/ws2p/ws2p_connection.rs +++ b/ws2p/ws2p_connection.rs @@ -246,8 +246,8 @@ impl WS2PConnectionState { } } } - pub fn to_u32(&self) -> u32 { - match *self { + pub fn to_u32(self) -> u32 { + match self { WS2PConnectionState::NeverTry => 0, _ => 1, }