Skip to content

Commit

Permalink
Merge pull request #2771 from jacderida/chore-revert_relay_close_peers
Browse files Browse the repository at this point in the history
Revert Using Closer Peers for Relay Candidates
  • Loading branch information
jacderida authored Feb 24, 2025
2 parents 6638e91 + f7b6a98 commit e6e5a07
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions ant-networking/src/relay_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
driver::{BadNodes, NodeBehaviour},
NetworkAddress,
};
use crate::driver::{BadNodes, NodeBehaviour};
use itertools::Itertools;
use libp2p::swarm::ConnectionId;
use libp2p::{
core::transport::ListenerId, kad::KBucketDistance as Distance, multiaddr::Protocol, Multiaddr,
PeerId, StreamProtocol, Swarm,
core::transport::ListenerId, multiaddr::Protocol, Multiaddr, PeerId, StreamProtocol, Swarm,
};
#[cfg(feature = "open-metrics")]
use prometheus_client::metrics::gauge::Gauge;
#[cfg(feature = "open-metrics")]
use std::collections::VecDeque;
use std::collections::{BTreeMap, HashMap, HashSet};
use rand::Rng;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
#[cfg(feature = "open-metrics")]
use std::sync::atomic::AtomicU64;
use std::time::Instant;
Expand Down Expand Up @@ -56,7 +51,7 @@ pub(crate) fn is_a_relayed_peer<'a>(mut addrs: impl Iterator<Item = &'a Multiadd
pub(crate) struct RelayManager {
self_peer_id: PeerId,
/// The potential relay servers that we can connect to.
relay_server_candidates: BTreeMap<Distance, (PeerId, Multiaddr)>,
relay_server_candidates: VecDeque<(PeerId, Multiaddr)>,
/// The relay servers that we are waiting for a reservation from.
waiting_for_reservation: BTreeMap<PeerId, Multiaddr>,
/// The relay servers that we are connected to.
Expand Down Expand Up @@ -155,11 +150,8 @@ impl RelayManager {
// Hence here can add the addr directly.
if let Some(relay_addr) = Self::craft_relay_address(addr, Some(*peer_id)) {
debug!("Adding {peer_id:?} with {relay_addr:?} as a potential relay candidate");
let distance = NetworkAddress::from_peer(self.self_peer_id)
.distance(&NetworkAddress::from_peer(*peer_id));
let _ = self
.relay_server_candidates
.insert(distance, (*peer_id, relay_addr));
self.relay_server_candidates
.push_back((*peer_id, relay_addr));
}
}
} else {
Expand Down Expand Up @@ -189,10 +181,15 @@ impl RelayManager {
// todo: should we remove all our other `listen_addr`? And should we block from adding `add_external_address` if
// we're behind nat?

// Pick a closest candidate as a potential relay_server.
if let Some((_distance, (peer_id, relay_addr))) =
self.relay_server_candidates.pop_first()
{
// Pick a random candidate from the vector. Check if empty, or `gen_range` panics for empty range.
let index = if self.relay_server_candidates.is_empty() {
debug!("No more relay candidates.");
break;
} else {
rand::thread_rng().gen_range(0..self.relay_server_candidates.len())
};

if let Some((peer_id, relay_addr)) = self.relay_server_candidates.remove(index) {
// skip if detected as a bad node
if let Some((_, is_bad)) = bad_nodes.get(&peer_id) {
if *is_bad {
Expand Down

0 comments on commit e6e5a07

Please sign in to comment.