Skip to content

Commit

Permalink
chore: upgrade multiaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Feb 19, 2025
1 parent 57defa5 commit fc52974
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 77 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ allow = [
"MPL-2.0",
"BSL-1.0",
"BSD-3-Clause",
"BSD-2-Clause",
"ISC",
"CC0-1.0",
"Unicode-DFS-2016",
Expand Down
49 changes: 5 additions & 44 deletions network/src/peer_store/addr_manager.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -95,19 +92,7 @@ impl AddrManager {

/// Remove an address by ip and port
pub fn remove(&mut 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.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
Expand All @@ -119,39 +104,15 @@ 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))
}

/// 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 {
Expand Down
22 changes: 21 additions & 1 deletion network/src/peer_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
}
16 changes: 2 additions & 14 deletions network/src/peer_store/peer_store_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ 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,
},
Flags, PeerId, SessionType,
};
use ipnetwork::IpNetwork;
use p2p::multiaddr::Protocol;
use rand::prelude::IteratorRandom;
use std::collections::{hash_map::Entry, HashMap};

Expand Down Expand Up @@ -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()
}
Expand Down
18 changes: 4 additions & 14 deletions network/src/peer_store/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fc52974

Please sign in to comment.