From eefd36e1f4a6e0008cc4a44de2907ac65e096194 Mon Sep 17 00:00:00 2001 From: tommytrg Date: Wed, 18 Oct 2023 14:08:53 +0200 Subject: [PATCH 1/3] chore(clippy): use `or_default` instead of `or_insert_with` Fix clippy warning for rust 1.73. Replace `or_insert_with` with `or_default` --- data_structures/src/chain/mod.rs | 4 ++-- data_structures/src/data_request.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/src/chain/mod.rs b/data_structures/src/chain/mod.rs index 34910b8a5..a6a1de429 100644 --- a/data_structures/src/chain/mod.rs +++ b/data_structures/src/chain/mod.rs @@ -2681,7 +2681,7 @@ impl TransactionsPool { for input in &vt_tx.body.inputs { self.output_pointer_map .entry(input.output_pointer) - .or_insert_with(Vec::new) + .or_default() .push(vt_tx.hash()); } @@ -2706,7 +2706,7 @@ impl TransactionsPool { for input in &dr_tx.body.inputs { self.output_pointer_map .entry(input.output_pointer) - .or_insert_with(Vec::new) + .or_default() .push(dr_tx.hash()); } diff --git a/data_structures/src/data_request.rs b/data_structures/src/data_request.rs index 6dd3493a1..fc3823037 100644 --- a/data_structures/src/data_request.rs +++ b/data_structures/src/data_request.rs @@ -143,7 +143,7 @@ impl DataRequestPool { self.data_requests_by_epoch .entry(epoch) - .or_insert_with(HashSet::new) + .or_default() .insert(dr_hash); self.data_request_pool.insert(dr_hash, dr_state); From 5a7c8b7d2f3afd57a68baf69c8f2062e5a9618d8 Mon Sep 17 00:00:00 2001 From: tommytrg Date: Wed, 18 Oct 2023 16:38:12 +0200 Subject: [PATCH 2/3] chore(clippy): use `.fold` instead of `format!` Fix clippy warnings to rust 1.73. Refactor code to use a `.fold` to return a `Vec` instead of use `format!` in a `.map`. --- data_structures/src/superblock.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/data_structures/src/superblock.rs b/data_structures/src/superblock.rs index 8bbf2c0aa..e87eef56c 100644 --- a/data_structures/src/superblock.rs +++ b/data_structures/src/superblock.rs @@ -158,19 +158,25 @@ impl SuperBlockVotesMempool { } fn get_valid_votes_pretty(&self) -> String { - let mut s: String = self - .votes_on_each_superblock - .iter() - .map(|(superblock_hash, votes)| { + let mut s: String = self.votes_on_each_superblock.iter().fold( + String::new(), + |mut acc, (superblock_hash, votes)| { let pkhs: Vec = votes .iter() .map(|vote| vote.secp256k1_signature.public_key.pkh()) .map(|pkh| pkh.to_string()) .collect(); - format!(" {}: {} votes: {:?}\n", superblock_hash, pkhs.len(), pkhs) - }) - .collect(); + acc.push_str(&format!( + " {}: {} votes: {:?}\n", + superblock_hash, + pkhs.len(), + pkhs + )); + + acc + }, + ); // Remove trailing "\n" if `s` is not empty s.pop(); From 7ab51a90a1af67c9f88eebff1e9b8f19569393ea Mon Sep 17 00:00:00 2001 From: tommytrg Date: Wed, 18 Oct 2023 16:38:59 +0200 Subject: [PATCH 3/3] refactor(data_structures): avoid iterating twice over the same vector Get the pkh and call `to_string` in the same `.map` instead of doing it in two different iterations. --- data_structures/src/superblock.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/src/superblock.rs b/data_structures/src/superblock.rs index e87eef56c..7e3d30ad3 100644 --- a/data_structures/src/superblock.rs +++ b/data_structures/src/superblock.rs @@ -163,8 +163,7 @@ impl SuperBlockVotesMempool { |mut acc, (superblock_hash, votes)| { let pkhs: Vec = votes .iter() - .map(|vote| vote.secp256k1_signature.public_key.pkh()) - .map(|pkh| pkh.to_string()) + .map(|vote| vote.secp256k1_signature.public_key.pkh().to_string()) .collect(); acc.push_str(&format!(