Skip to content

Commit

Permalink
feat: try dial higher peers first
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Dec 23, 2024
1 parent 275093e commit 01e3f40
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/server/p2p/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) struct PeerStoreRecord {
pub last_grey_list_reason: Option<String>,
pub catch_up_attempts: u64,
pub last_ping: Option<EpochTime>,
pub last_dial_attempt: Option<Instant>,
}

impl PeerStoreRecord {
Expand All @@ -44,6 +45,7 @@ impl PeerStoreRecord {
last_grey_list_reason: None,
catch_up_attempts: 0,
last_ping: None,
last_dial_attempt: None,
}
}

Expand Down Expand Up @@ -223,6 +225,30 @@ impl PeerStore {
peers.into_iter().cloned().collect()
}

pub fn best_peers_to_dial(&self, count: usize) -> Vec<PeerStoreRecord> {
let mut peers = self.whitelist_peers.values().collect::<Vec<_>>();
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();
Expand Down

0 comments on commit 01e3f40

Please sign in to comment.