diff --git a/Cargo.lock b/Cargo.lock index 3a435a39..80e70d75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,7 +504,6 @@ dependencies = [ "cuprate-p2p-core", "cuprate-pruning", "cuprate-test-utils", - "cuprate-wire", "futures", "indexmap", "rand", diff --git a/p2p/address-book/Cargo.toml b/p2p/address-book/Cargo.toml index 9cff78a8..08711639 100644 --- a/p2p/address-book/Cargo.toml +++ b/p2p/address-book/Cargo.toml @@ -8,7 +8,6 @@ authors = ["Boog900"] [dependencies] cuprate-pruning = { path = "../../pruning" } -cuprate-wire = { path= "../../net/wire" } cuprate-p2p-core = { path = "../p2p-core" } tower = { workspace = true, features = ["util"] } @@ -29,3 +28,6 @@ borsh = { workspace = true, features = ["derive", "std"]} cuprate-test-utils = {path = "../../test-utils"} tokio = { workspace = true, features = ["rt-multi-thread", "macros"]} + +[lints] +workspace = true \ No newline at end of file diff --git a/p2p/address-book/src/book.rs b/p2p/address-book/src/book.rs index 2f0ce6db..9c229816 100644 --- a/p2p/address-book/src/book.rs +++ b/p2p/address-book/src/book.rs @@ -36,7 +36,7 @@ use crate::{ mod tests; /// An entry in the connected list. -pub struct ConnectionPeerEntry { +pub(crate) struct ConnectionPeerEntry { addr: Option, id: u64, handle: ConnectionHandle, @@ -109,14 +109,14 @@ impl AddressBook { match handle.poll_unpin(cx) { Poll::Pending => return, Poll::Ready(Ok(Err(e))) => { - tracing::error!("Could not save peer list to disk, got error: {}", e) + tracing::error!("Could not save peer list to disk, got error: {e}"); } Poll::Ready(Err(e)) => { if e.is_panic() { panic::resume_unwind(e.into_panic()) } } - _ => (), + Poll::Ready(_) => (), } } // the task is finished. @@ -144,6 +144,7 @@ impl AddressBook { let mut internal_addr_disconnected = Vec::new(); let mut addrs_to_ban = Vec::new(); + #[expect(clippy::iter_over_hash_type, reason = "ordering doesn't matter here")] for (internal_addr, peer) in &mut self.connected_peers { if let Some(time) = peer.handle.check_should_ban() { match internal_addr { @@ -158,7 +159,7 @@ impl AddressBook { } } - for (addr, time) in addrs_to_ban.into_iter() { + for (addr, time) in addrs_to_ban { self.ban_peer(addr, time); } @@ -172,12 +173,7 @@ impl AddressBook { .remove(&addr); // If the amount of peers with this ban id is 0 remove the whole set. - if self - .connected_peers_ban_id - .get(&addr.ban_id()) - .unwrap() - .is_empty() - { + if self.connected_peers_ban_id[&addr.ban_id()].is_empty() { self.connected_peers_ban_id.remove(&addr.ban_id()); } // remove the peer from the anchor list. @@ -188,7 +184,7 @@ impl AddressBook { fn ban_peer(&mut self, addr: Z::Addr, time: Duration) { if self.banned_peers.contains_key(&addr.ban_id()) { - tracing::error!("Tried to ban peer twice, this shouldn't happen.") + tracing::error!("Tried to ban peer twice, this shouldn't happen."); } if let Some(connected_peers_with_ban_id) = self.connected_peers_ban_id.get(&addr.ban_id()) { @@ -242,10 +238,10 @@ impl AddressBook { peer_list.retain_mut(|peer| { peer.adr.make_canonical(); - if !peer.adr.should_add_to_peer_list() { - false - } else { + if peer.adr.should_add_to_peer_list() { !self.is_peer_banned(&peer.adr) + } else { + false } // TODO: check rpc/ p2p ports not the same }); @@ -391,7 +387,7 @@ impl Service> for AddressBook { rpc_credits_per_hash, }, ) - .map(|_| AddressBookResponse::Ok), + .map(|()| AddressBookResponse::Ok), AddressBookRequest::IncomingPeerList(peer_list) => { self.handle_incoming_peer_list(peer_list); Ok(AddressBookResponse::Ok) diff --git a/p2p/address-book/src/book/tests.rs b/p2p/address-book/src/book/tests.rs index 1abea043..aefbd842 100644 --- a/p2p/address-book/src/book/tests.rs +++ b/p2p/address-book/src/book/tests.rs @@ -109,7 +109,7 @@ async fn add_new_peer_already_connected() { }, ), Err(AddressBookError::PeerAlreadyConnected) - ) + ); } #[tokio::test] @@ -143,5 +143,5 @@ async fn banned_peer_removed_from_peer_lists() { .unwrap() .into_inner(), TestNetZoneAddr(1) - ) + ); } diff --git a/p2p/address-book/src/peer_list.rs b/p2p/address-book/src/peer_list.rs index f0a905ae..9b98a8aa 100644 --- a/p2p/address-book/src/peer_list.rs +++ b/p2p/address-book/src/peer_list.rs @@ -7,31 +7,31 @@ use cuprate_p2p_core::{services::ZoneSpecificPeerListEntryBase, NetZoneAddress, use cuprate_pruning::{PruningSeed, CRYPTONOTE_MAX_BLOCK_HEIGHT}; #[cfg(test)] -pub mod tests; +pub(crate) mod tests; /// A Peer list in the address book. /// /// This could either be the white list or gray list. #[derive(Debug)] -pub struct PeerList { +pub(crate) struct PeerList { /// The peers with their peer data. pub peers: IndexMap>, /// An index of Pruning seed to address, so can quickly grab peers with the blocks /// we want. /// - /// Pruning seeds are sorted by first their log_stripes and then their stripe. + /// Pruning seeds are sorted by first their `log_stripes` and then their stripe. /// This means the first peers in this list will store more blocks than peers /// later on. So when we need a peer with a certain block we look at the peers /// storing more blocks first then work our way to the peers storing less. /// pruning_seeds: BTreeMap>, - /// A hashmap linking ban_ids to addresses. + /// A hashmap linking `ban_ids` to addresses. ban_ids: HashMap<::BanID, Vec>, } impl PeerList { /// Creates a new peer list. - pub fn new(list: Vec>) -> PeerList { + pub(crate) fn new(list: Vec>) -> Self { let mut peers = IndexMap::with_capacity(list.len()); let mut pruning_seeds = BTreeMap::new(); let mut ban_ids = HashMap::with_capacity(list.len()); @@ -49,7 +49,7 @@ impl PeerList { peers.insert(peer.adr, peer); } - PeerList { + Self { peers, pruning_seeds, ban_ids, @@ -57,21 +57,20 @@ impl PeerList { } /// Gets the length of the peer list - pub fn len(&self) -> usize { + pub(crate) fn len(&self) -> usize { self.peers.len() } /// Adds a new peer to the peer list - pub fn add_new_peer(&mut self, peer: ZoneSpecificPeerListEntryBase) { + pub(crate) fn add_new_peer(&mut self, peer: ZoneSpecificPeerListEntryBase) { if self.peers.insert(peer.adr, peer).is_none() { - // It's more clear with this - #[allow(clippy::unwrap_or_default)] + #[expect(clippy::unwrap_or_default, reason = "It's more clear with this")] self.pruning_seeds .entry(peer.pruning_seed) .or_insert_with(Vec::new) .push(peer.adr); - #[allow(clippy::unwrap_or_default)] + #[expect(clippy::unwrap_or_default)] self.ban_ids .entry(peer.adr.ban_id()) .or_insert_with(Vec::new) @@ -85,7 +84,7 @@ impl PeerList { /// list. /// /// The given peer will be removed from the peer list. - pub fn take_random_peer( + pub(crate) fn take_random_peer( &mut self, r: &mut R, block_needed: Option, @@ -127,7 +126,7 @@ impl PeerList { None } - pub fn get_random_peers( + pub(crate) fn get_random_peers( &self, r: &mut R, len: usize, @@ -142,7 +141,7 @@ impl PeerList { } /// Returns a mutable reference to a peer. - pub fn get_peer_mut( + pub(crate) fn get_peer_mut( &mut self, peer: &Z::Addr, ) -> Option<&mut ZoneSpecificPeerListEntryBase> { @@ -150,7 +149,7 @@ impl PeerList { } /// Returns true if the list contains this peer. - pub fn contains_peer(&self, peer: &Z::Addr) -> bool { + pub(crate) fn contains_peer(&self, peer: &Z::Addr) -> bool { self.peers.contains_key(peer) } @@ -189,11 +188,11 @@ impl PeerList { /// MUST NOT BE USED ALONE fn remove_peer_from_all_idxs(&mut self, peer: &ZoneSpecificPeerListEntryBase) { self.remove_peer_pruning_idx(peer); - self.remove_peer_ban_idx(peer) + self.remove_peer_ban_idx(peer); } /// Removes a peer from the peer list - pub fn remove_peer( + pub(crate) fn remove_peer( &mut self, peer: &Z::Addr, ) -> Option> { @@ -203,7 +202,7 @@ impl PeerList { } /// Removes all peers with a specific ban id. - pub fn remove_peers_with_ban_id(&mut self, ban_id: &::BanID) { + pub(crate) fn remove_peers_with_ban_id(&mut self, ban_id: &::BanID) { let Some(addresses) = self.ban_ids.get(ban_id) else { // No peers to ban return; @@ -217,8 +216,8 @@ impl PeerList { /// Tries to reduce the peer list to `new_len`. /// /// This function could keep the list bigger than `new_len` if `must_keep_peers`s length - /// is larger than new_len, in that case we will remove as much as we can. - pub fn reduce_list(&mut self, must_keep_peers: &HashSet, new_len: usize) { + /// is larger than `new_len`, in that case we will remove as much as we can. + pub(crate) fn reduce_list(&mut self, must_keep_peers: &HashSet, new_len: usize) { if new_len >= self.len() { return; } diff --git a/p2p/address-book/src/peer_list/tests.rs b/p2p/address-book/src/peer_list/tests.rs index 8d2d2200..4b13ae7f 100644 --- a/p2p/address-book/src/peer_list/tests.rs +++ b/p2p/address-book/src/peer_list/tests.rs @@ -14,7 +14,7 @@ fn make_fake_peer( ) -> ZoneSpecificPeerListEntryBase { ZoneSpecificPeerListEntryBase { adr: TestNetZoneAddr(id), - id: id as u64, + id: u64::from(id), last_seen: 0, pruning_seed: PruningSeed::decompress(pruning_seed.unwrap_or(0)).unwrap(), rpc_port: 0, @@ -22,14 +22,14 @@ fn make_fake_peer( } } -pub fn make_fake_peer_list( +pub(crate) fn make_fake_peer_list( start_idx: u32, numb_o_peers: u32, ) -> PeerList> { let mut peer_list = Vec::with_capacity(numb_o_peers as usize); for idx in start_idx..(start_idx + numb_o_peers) { - peer_list.push(make_fake_peer(idx, None)) + peer_list.push(make_fake_peer(idx, None)); } PeerList::new(peer_list) @@ -50,7 +50,7 @@ fn make_fake_peer_list_with_random_pruning_seeds( } else { r.gen_range(384..=391) }), - )) + )); } PeerList::new(peer_list) } @@ -70,7 +70,7 @@ fn peer_list_reduce_length() { #[test] fn peer_list_reduce_length_with_peers_we_need() { let mut peer_list = make_fake_peer_list(0, 500); - let must_keep_peers = HashSet::from_iter(peer_list.peers.keys().copied()); + let must_keep_peers = peer_list.peers.keys().copied().collect::>(); let target_len = 49; @@ -92,7 +92,7 @@ fn peer_list_remove_specific_peer() { let peers = peer_list.peers; for (_, addrs) in pruning_idxs { - addrs.iter().for_each(|adr| assert_ne!(adr, &peer.adr)) + addrs.iter().for_each(|adr| assert_ne!(adr, &peer.adr)); } assert!(!peers.contains_key(&peer.adr)); @@ -104,13 +104,13 @@ fn peer_list_pruning_idxs_are_correct() { let mut total_len = 0; for (seed, list) in peer_list.pruning_seeds { - for peer in list.iter() { + for peer in &list { assert_eq!(peer_list.peers.get(peer).unwrap().pruning_seed, seed); total_len += 1; } } - assert_eq!(total_len, peer_list.peers.len()) + assert_eq!(total_len, peer_list.peers.len()); } #[test] @@ -122,11 +122,7 @@ fn peer_list_add_new_peer() { assert_eq!(peer_list.len(), 11); assert_eq!(peer_list.peers.get(&new_peer.adr), Some(&new_peer)); - assert!(peer_list - .pruning_seeds - .get(&new_peer.pruning_seed) - .unwrap() - .contains(&new_peer.adr)); + assert!(peer_list.pruning_seeds[&new_peer.pruning_seed].contains(&new_peer.adr)); } #[test] @@ -164,7 +160,7 @@ fn peer_list_get_peer_with_block() { assert!(peer .pruning_seed .get_next_unpruned_block(1, 1_000_000) - .is_ok()) + .is_ok()); } #[test] diff --git a/p2p/address-book/src/store.rs b/p2p/address-book/src/store.rs index abc42d69..07c117e1 100644 --- a/p2p/address-book/src/store.rs +++ b/p2p/address-book/src/store.rs @@ -1,3 +1,8 @@ +#![expect( + single_use_lifetimes, + reason = "false positive on generated derive code on `SerPeerDataV1`" +)] + use std::fs; use borsh::{from_slice, to_vec, BorshDeserialize, BorshSerialize}; @@ -21,7 +26,7 @@ struct DeserPeerDataV1 { gray_list: Vec>, } -pub fn save_peers_to_disk( +pub(crate) fn save_peers_to_disk( cfg: &AddressBookConfig, white_list: &PeerList, gray_list: &PeerList, @@ -38,7 +43,7 @@ pub fn save_peers_to_disk( spawn_blocking(move || fs::write(&file, &data)) } -pub async fn read_peers_from_disk( +pub(crate) async fn read_peers_from_disk( cfg: &AddressBookConfig, ) -> Result< (