From fc52974184b16e5677492eb443cea35948eddbc6 Mon Sep 17 00:00:00 2001 From: driftluo Date: Wed, 19 Feb 2025 14:55:47 +0800 Subject: [PATCH] chore: upgrade multiaddr --- Cargo.lock | 17 ++++++-- deny.toml | 1 + network/src/peer_store/addr_manager.rs | 49 +++-------------------- network/src/peer_store/mod.rs | 22 +++++++++- network/src/peer_store/peer_store_impl.rs | 16 +------- network/src/peer_store/types.rs | 18 ++------- 6 files changed, 46 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93a64d322a..b952941a92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,6 +155,12 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + [[package]] name = "arrayvec" version = "0.7.6" @@ -6126,12 +6132,15 @@ dependencies = [ [[package]] name = "tentacle-multiaddr" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9e71b28bf0bbf274b92f47cb2c5b42755d84a11e2246cf7bcb7b65c89483b9" +checksum = "35d1c1fa03d7fbdde1314f89e740cf5dbb5484ccc2b13fff9b82d4ddd0d51cef" dependencies = [ + "arrayref", "bs58", + "byteorder", "bytes", + "data-encoding", "serde", "sha2", "unsigned-varint", @@ -6139,9 +6148,9 @@ dependencies = [ [[package]] name = "tentacle-secio" -version = "0.6.4" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d53673d63399e1d557c2e884b045d242811079d3b2f17a569310e5c3f1db33" +checksum = "01d1c8be16ed68e4d69b60e445d453c392d07e613fe9fab0688d46910f6dd013" dependencies = [ "bs58", "bytes", diff --git a/deny.toml b/deny.toml index c03480c563..61f781ab1e 100644 --- a/deny.toml +++ b/deny.toml @@ -99,6 +99,7 @@ allow = [ "MPL-2.0", "BSL-1.0", "BSD-3-Clause", + "BSD-2-Clause", "ISC", "CC0-1.0", "Unicode-DFS-2016", diff --git a/network/src/peer_store/addr_manager.rs b/network/src/peer_store/addr_manager.rs index 62e96ee169..38b4389000 100644 --- a/network/src/peer_store/addr_manager.rs +++ b/network/src/peer_store/addr_manager.rs @@ -1,9 +1,6 @@ //! Address manager -use crate::peer_store::types::AddrInfo; -use p2p::{ - multiaddr::{Multiaddr, Protocol}, - utils::multiaddr_to_socketaddr, -}; +use crate::peer_store::{base_addr, types::AddrInfo}; +use p2p::{multiaddr::Multiaddr, utils::multiaddr_to_socketaddr}; use rand::Rng; use std::collections::{HashMap, HashSet}; @@ -95,19 +92,7 @@ impl AddrManager { /// Remove an address by ip and port pub fn remove(&mut self, addr: &Multiaddr) -> Option { - let base_addr = addr - .iter() - .filter_map(|p| { - if matches!( - p, - Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_) - ) { - None - } else { - Some(p) - } - }) - .collect(); + let base_addr = base_addr(addr); self.addr_to_id.remove(&base_addr).and_then(|id| { let random_id_pos = self.id_to_info.get(&id).expect("exists").random_id_pos; // swap with last index, then remove the last index @@ -119,19 +104,7 @@ impl AddrManager { /// Get an address information by ip and port pub fn get(&self, addr: &Multiaddr) -> Option<&AddrInfo> { - let base_addr = addr - .iter() - .filter_map(|p| { - if matches!( - p, - Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_) - ) { - None - } else { - Some(p) - } - }) - .collect(); + let base_addr = base_addr(addr); self.addr_to_id .get(&base_addr) .and_then(|id| self.id_to_info.get(id)) @@ -139,19 +112,7 @@ impl AddrManager { /// Get a mutable address information by ip and port pub fn get_mut(&mut self, addr: &Multiaddr) -> Option<&mut AddrInfo> { - let base_addr = addr - .iter() - .filter_map(|p| { - if matches!( - p, - Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_) - ) { - None - } else { - Some(p) - } - }) - .collect(); + let base_addr = base_addr(addr); if let Some(id) = self.addr_to_id.get(&base_addr) { self.id_to_info.get_mut(id) } else { diff --git a/network/src/peer_store/mod.rs b/network/src/peer_store/mod.rs index ab71862676..0fade21e7d 100644 --- a/network/src/peer_store/mod.rs +++ b/network/src/peer_store/mod.rs @@ -17,7 +17,7 @@ pub mod types; pub(crate) use crate::Behaviour; pub use crate::SessionType; -use p2p::multiaddr::Multiaddr; +use p2p::multiaddr::{Multiaddr, Protocol}; pub(crate) use peer_store_impl::required_flags_filter; pub use peer_store_impl::PeerStore; @@ -86,3 +86,23 @@ impl ReportResult { self == ReportResult::Ok } } + +// Remove unnecessary protocols +pub(crate) fn base_addr(addr: &Multiaddr) -> Multiaddr { + addr.iter() + .filter_map(|p| { + if matches!( + p, + Protocol::Ws + | Protocol::Wss + | Protocol::Memory(_) + | Protocol::Tls(_) + | Protocol::Onion3(_) + ) { + None + } else { + Some(p) + } + }) + .collect() +} diff --git a/network/src/peer_store/peer_store_impl.rs b/network/src/peer_store/peer_store_impl.rs index 3fd4a594a6..d239511479 100644 --- a/network/src/peer_store/peer_store_impl.rs +++ b/network/src/peer_store/peer_store_impl.rs @@ -5,6 +5,7 @@ use crate::{ peer_store::{ addr_manager::AddrManager, ban_list::BanList, + base_addr, types::{ip_to_network, AddrInfo, BannedAddr, PeerInfo}, Behaviour, Multiaddr, PeerScoreConfig, ReportResult, Status, ADDR_COUNT_LIMIT, ADDR_TIMEOUT_MS, ADDR_TRY_TIMEOUT_MS, DIAL_INTERVAL, @@ -12,7 +13,6 @@ use crate::{ Flags, PeerId, SessionType, }; use ipnetwork::IpNetwork; -use p2p::multiaddr::Protocol; use rand::prelude::IteratorRandom; use std::collections::{hash_map::Entry, HashMap}; @@ -111,19 +111,7 @@ impl PeerStore { if self.ban_list.is_addr_banned(&addr) { return; } - let base_addr = addr - .iter() - .filter_map(|p| { - if matches!( - p, - Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_) - ) { - None - } else { - Some(p) - } - }) - .collect(); + let base_addr = base_addr(&addr); if let Some(info) = self.addr_manager.get_mut(&base_addr) { info.last_connected_at_ms = ckb_systemtime::unix_time_as_millis() } diff --git a/network/src/peer_store/types.rs b/network/src/peer_store/types.rs index 2db34c306b..1f73de7efd 100644 --- a/network/src/peer_store/types.rs +++ b/network/src/peer_store/types.rs @@ -1,6 +1,8 @@ //! Type used on peer store use crate::{ - peer_store::{Score, SessionType, ADDR_MAX_FAILURES, ADDR_MAX_RETRIES, ADDR_TIMEOUT_MS}, + peer_store::{ + base_addr, Score, SessionType, ADDR_MAX_FAILURES, ADDR_MAX_RETRIES, ADDR_TIMEOUT_MS, + }, Flags, }; use ipnetwork::IpNetwork; @@ -63,19 +65,7 @@ impl AddrInfo { pub fn new(addr: Multiaddr, last_connected_at_ms: u64, score: Score, flags: u64) -> Self { AddrInfo { // only store tcp protocol - addr: addr - .iter() - .filter_map(|p| { - if matches!( - p, - Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_) - ) { - None - } else { - Some(p) - } - }) - .collect(), + addr: base_addr(&addr), score, last_connected_at_ms, last_tried_at_ms: 0,