From 01e3f403ebe1f061180cf6c4364fabef502675e8 Mon Sep 17 00:00:00 2001 From: stringhandler Date: Mon, 23 Dec 2024 12:23:53 +0200 Subject: [PATCH] feat: try dial higher peers first --- src/server/p2p/network.rs | 16 ++++++++++------ src/server/p2p/peer_store.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/server/p2p/network.rs b/src/server/p2p/network.rs index 18300bb..ec3ff4d 100644 --- a/src/server/p2p/network.rs +++ b/src/server/p2p/network.rs @@ -2236,22 +2236,26 @@ where S: ShareChain warn!(target: LOG_TARGET, "Failed to dial seed peers: {e:?}"); }, } - continue; + // continue; } let mut num_dialed = 0; - let store_read_lock = self.network_peer_store.read().await; + let mut store_write_lock = self.network_peer_store.write().await; // Rather try and search good peers rather than randomly dialing // 1000 peers will take a long time to get through - for record in store_read_lock.whitelist_peers().values() { + for record in store_write_lock.best_peers_to_dial(100) { + debug!(target: LOG_TARGET, "Dialing peer: {:?} with height(rx/sha) {}/{}", record.peer_id, record.peer_info.current_random_x_height, record.peer_info.current_sha3x_height); + // dbg!(&record.peer_id); // Only dial seed peers if we have 0 connections if !self.swarm.is_connected(&record.peer_id) - && !store_read_lock.is_seed_peer(&record.peer_id) { - let _unused = self.swarm.dial(record.peer_id); + && !store_write_lock.is_seed_peer(&record.peer_id) { + store_write_lock.update_last_dial_attempt(&record.peer_id); + let dial_opts= DialOpts::peer_id(record.peer_id).condition(PeerCondition::Always).addresses(record.peer_info.public_addresses().clone()).extend_addresses_through_behaviour().build(); + let _unused = self.swarm.dial(dial_opts); num_dialed += 1; // We can only do 30 connections // after 30 it starts cancelling dials - if num_dialed > 30 { + if num_dialed > 5 { break; } } diff --git a/src/server/p2p/peer_store.rs b/src/server/p2p/peer_store.rs index aaae01a..e3d8467 100644 --- a/src/server/p2p/peer_store.rs +++ b/src/server/p2p/peer_store.rs @@ -30,6 +30,7 @@ pub(crate) struct PeerStoreRecord { pub last_grey_list_reason: Option, pub catch_up_attempts: u64, pub last_ping: Option, + pub last_dial_attempt: Option, } impl PeerStoreRecord { @@ -44,6 +45,7 @@ impl PeerStoreRecord { last_grey_list_reason: None, catch_up_attempts: 0, last_ping: None, + last_dial_attempt: None, } } @@ -223,6 +225,30 @@ impl PeerStore { peers.into_iter().cloned().collect() } + pub fn best_peers_to_dial(&self, count: usize) -> Vec { + let mut peers = self.whitelist_peers.values().collect::>(); + peers.retain(|peer| { + !peer.peer_info.public_addresses().is_empty() && + (peer.last_dial_attempt.is_none() || peer.last_dial_attempt.unwrap().elapsed().as_secs() > 60) + }); + peers.sort_by(|a, b| { + b.num_grey_listings + .cmp(&a.num_grey_listings) + .then(b.peer_info.current_random_x_pow.cmp(&a.peer_info.current_random_x_pow)) + .then(b.peer_info.current_sha3x_pow.cmp(&a.peer_info.current_sha3x_pow)) + }); + peers.truncate(count); + peers.into_iter().cloned().collect() + } + + pub fn update_last_dial_attempt(&mut self, peer_id: &PeerId) { + if let Some(entry) = self.whitelist_peers.get_mut(&peer_id.to_base58()) { + let mut new_record = entry.clone(); + new_record.last_dial_attempt = Some(Instant::now()); + *entry = new_record; + } + } + pub fn reset_last_sync_attempt(&mut self, peer_id: &PeerId) { if let Some(entry) = self.whitelist_peers.get_mut(&peer_id.to_base58()) { let mut new_record = entry.clone();