Admin add/remove peer (#7198)

N/A


  Adds endpoints to add and remove trusted peers from the http api. The added peers are trusted peers so they won't be disconnected for bad scores. We try to maintain a connection to the peer in case they disconnect from us by trying to dial it every heartbeat.
This commit is contained in:
Pawan Dhananjay
2025-03-28 05:59:09 -07:00
committed by GitHub
parent a5ea05ce2a
commit 54aef2d043
10 changed files with 217 additions and 7 deletions

View File

@@ -114,6 +114,7 @@ pub struct PeerManager<E: EthSpec> {
metrics_enabled: bool,
/// Keeps track of whether the QUIC protocol is enabled or not.
quic_enabled: bool,
trusted_peers: HashSet<Enr>,
/// The logger associated with the `PeerManager`.
log: slog::Logger,
}
@@ -195,6 +196,7 @@ impl<E: EthSpec> PeerManager<E> {
discovery_enabled,
metrics_enabled,
quic_enabled,
trusted_peers: Default::default(),
log: log.clone(),
})
}
@@ -894,7 +896,7 @@ impl<E: EthSpec> PeerManager<E> {
}
// Gracefully disconnects a peer without banning them.
fn disconnect_peer(&mut self, peer_id: PeerId, reason: GoodbyeReason) {
pub fn disconnect_peer(&mut self, peer_id: PeerId, reason: GoodbyeReason) {
self.events
.push(PeerManagerEvent::DisconnectPeer(peer_id, reason));
self.network_globals
@@ -943,6 +945,13 @@ impl<E: EthSpec> PeerManager<E> {
}
}
fn maintain_trusted_peers(&mut self) {
let trusted_peers = self.trusted_peers.clone();
for trusted_peer in trusted_peers {
self.dial_peer(trusted_peer);
}
}
/// This function checks the status of our current peers and optionally requests a discovery
/// query if we need to find more peers to maintain the current number of peers
fn maintain_peer_count(&mut self, dialing_peers: usize) {
@@ -1234,6 +1243,7 @@ impl<E: EthSpec> PeerManager<E> {
fn heartbeat(&mut self) {
// Optionally run a discovery query if we need more peers.
self.maintain_peer_count(0);
self.maintain_trusted_peers();
// Cleans up the connection state of dialing peers.
// Libp2p dials peer-ids, but sometimes the response is from another peer-id or libp2p
@@ -1470,6 +1480,14 @@ impl<E: EthSpec> PeerManager<E> {
)
})
}
pub fn add_trusted_peer(&mut self, enr: Enr) {
self.trusted_peers.insert(enr);
}
pub fn remove_trusted_peer(&mut self, enr: Enr) {
self.trusted_peers.remove(&enr);
}
}
enum ConnectingType {