Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions node/bft/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ impl<N: Network> Gateway<N> {
.collect();

if !handles.is_empty() {
info!("Reconnnecting to {} out of {} trusted validators", handles.len(), trusted_peers.len());
info!("Reconnecting to {} out of {} trusted validators", handles.len(), trusted_peers.len());
}
}

Expand Down Expand Up @@ -1492,7 +1492,24 @@ impl<N: Network> Handshake for Gateway<N> {
NodeType::Validator
};

if let Some(peer) = self.peer_pool.write().get_mut(&addr) {
let mut peer_pool = self.peer_pool.write();

// Validators may change their listening address, but not the Aleo address; traverse
// the peer pool, and retain previously connected (the prior Aleo address is known)
// candidate peers with the same Aleo address only if their listening address is the
// same; otherwise, it may be concluded that a known validator has changed their
// listening address, and thus the old entry should be removed as outdated.
Comment thread
ljedrz marked this conversation as resolved.
peer_pool.retain(|_, peer| {
if let Peer::Candidate(peer) = peer
&& let Some(old_aleo_addr) = peer.last_known_aleo_addr
{
old_aleo_addr != cr.address || peer.listener_addr == addr
} else {
true
}
});
Comment thread
ljedrz marked this conversation as resolved.

if let Some(peer) = peer_pool.get_mut(&addr) {
self.resolver.write().insert_peer(addr, peer_addr, Some(cr.address));
peer.upgrade_to_connected(
peer_addr,
Expand Down
16 changes: 13 additions & 3 deletions node/network/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{fmt, net::SocketAddr, time::Instant};
#[derive(Clone, Debug)]
pub enum Peer<N: Network> {
/// A candidate peer that's currently not connected to.
Candidate(CandidatePeer),
Candidate(CandidatePeer<N>),
/// A peer that's currently being connected to (the handshake is in progress).
Connecting(ConnectingPeer),
/// A fully connected (post-handshake) peer.
Expand All @@ -41,7 +41,7 @@ pub struct ConnectingPeer {

/// A candidate peer.
#[derive(Clone, Debug)]
pub struct CandidatePeer {
pub struct CandidatePeer<N: Network> {
/// The listening address of a candidate peer.
pub listener_addr: SocketAddr,
/// Indicates whether the peer is considered trusted.
Expand All @@ -53,6 +53,9 @@ pub struct CandidatePeer {
pub last_connection_attempt: Option<Instant>,
/// The total number of connection attempts, since the peer was last connected.
pub total_connection_attempts: u32,
/// The last known Aleo address of this peer, carried over from a prior connection.
/// Used to detect when a validator reconnects from a different IP address.
pub last_known_aleo_addr: Option<Address<N>>,
}

/// A fully connected peer.
Expand Down Expand Up @@ -100,13 +103,14 @@ impl fmt::Display for ConnectionMode {

impl<N: Network> Peer<N> {
/// Create a candidate peer.
pub const fn new_candidate(listener_addr: SocketAddr, trusted: bool) -> Self {
pub fn new_candidate(listener_addr: SocketAddr, trusted: bool) -> Self {
Self::Candidate(CandidatePeer {
listener_addr,
trusted,
last_height_seen: None,
last_connection_attempt: None,
total_connection_attempts: 0,
last_known_aleo_addr: None,
})
}

Expand Down Expand Up @@ -157,12 +161,18 @@ impl<N: Network> Peer<N> {

/// Demote a peer to candidate status, marking it as disconnected.
pub fn downgrade_to_candidate(&mut self, listener_addr: SocketAddr) {
let last_known_aleo_addr = match self {
Self::Connected(p) => Some(p.aleo_addr),
_ => None,
};

*self = Self::Candidate(CandidatePeer {
listener_addr,
trusted: self.is_trusted(),
last_height_seen: self.last_height_seen(),
last_connection_attempt: None,
total_connection_attempts: 0,
last_known_aleo_addr,
});
Comment thread
vicsn marked this conversation as resolved.
}

Expand Down
Loading