Skip to content

Commit

Permalink
feat: peer store only retains the TCP protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jan 15, 2025
1 parent 9f48908 commit 24cf5f0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
6 changes: 0 additions & 6 deletions network/src/peer_store/addr_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! Address manager
#[cfg(target_family = "wasm")]
use crate::network::{find_type, TransportType};
use crate::peer_store::types::AddrInfo;
use p2p::{multiaddr::Multiaddr, utils::multiaddr_to_socketaddr};
use rand::Rng;
Expand Down Expand Up @@ -51,10 +49,6 @@ impl AddrManager {
let mut addr_infos = Vec::with_capacity(count);
let mut rng = rand::thread_rng();
let now_ms = ckb_systemtime::unix_time_as_millis();
#[cfg(target_family = "wasm")]
let filter = |peer_addr: &AddrInfo| {
filter(peer_addr) && matches!(find_type(&peer_addr.addr), TransportType::Ws)
};
for i in 0..self.random_ids.len() {
// reuse the for loop to shuffle random ids
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
Expand Down
10 changes: 0 additions & 10 deletions network/src/peer_store/peer_store_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ impl PeerStore {
if self.ban_list.is_addr_banned(&addr) {
return Ok(());
}
#[cfg(target_family = "wasm")]
if !matches!(find_type(&addr), TransportType::Ws) {
return Ok(());
}
self.check_purge()?;
let score = self.score_config.default_score;
self.addr_manager
Expand Down Expand Up @@ -180,12 +176,6 @@ impl PeerStore {
&& required_flags_filter(required_flags, Flags::from_bits_truncate(peer_addr.flags))
};

// Any protocol expect websocket
#[cfg(not(target_family = "wasm"))]
let filter = |peer_addr: &AddrInfo| {
filter(peer_addr) && !matches!(find_type(&peer_addr.addr), TransportType::Ws)
};

// get addrs that can attempt.
self.addr_manager.fetch_random(count, filter)
}
Expand Down
15 changes: 14 additions & 1 deletion network/src/peer_store/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,20 @@ impl AddrInfo {
/// Init
pub fn new(addr: Multiaddr, last_connected_at_ms: u64, score: Score, flags: u64) -> Self {
AddrInfo {
addr,
// 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(),
score,
last_connected_at_ms,
last_tried_at_ms: 0,
Expand Down
15 changes: 15 additions & 0 deletions network/src/tests/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,18 @@ fn test_addr_unique() {

assert_eq!(peer_store.addr_manager().addrs_iter().count(), 2);
}

#[test]
fn test_only_tcp_store() {
let mut peer_store = PeerStore::default();
let mut addr = random_addr();
addr.push(p2p::multiaddr::Protocol::Ws);
peer_store
.add_addr(addr.clone(), Flags::COMPATIBILITY)
.unwrap();
assert_eq!(peer_store.fetch_addrs_to_feeler(2).len(), 1);
assert_eq!(peer_store.fetch_addrs_to_feeler(1)[0].addr, {
addr.pop();
addr
});
}

0 comments on commit 24cf5f0

Please sign in to comment.