mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
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:
@@ -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 {
|
||||
|
||||
@@ -9,7 +9,7 @@ use std::net::IpAddr;
|
||||
use std::time::Instant;
|
||||
use std::{cmp::Ordering, fmt::Display};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
collections::{hash_map::Entry, HashMap, HashSet},
|
||||
fmt::Formatter,
|
||||
};
|
||||
use sync_status::SyncStatus;
|
||||
@@ -79,6 +79,33 @@ impl<E: EthSpec> PeerDB<E> {
|
||||
self.peers.iter()
|
||||
}
|
||||
|
||||
pub fn set_trusted_peer(&mut self, enr: Enr) {
|
||||
match self.peers.entry(enr.peer_id()) {
|
||||
Entry::Occupied(mut info) => {
|
||||
let entry = info.get_mut();
|
||||
entry.score = Score::max_score();
|
||||
entry.is_trusted = true;
|
||||
}
|
||||
Entry::Vacant(entry) => {
|
||||
entry.insert(PeerInfo::trusted_peer_info());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unset_trusted_peer(&mut self, enr: Enr) {
|
||||
if let Some(info) = self.peers.get_mut(&enr.peer_id()) {
|
||||
info.is_trusted = false;
|
||||
info.score = Score::default();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trusted_peers(&self) -> Vec<PeerId> {
|
||||
self.peers
|
||||
.iter()
|
||||
.filter_map(|(id, info)| if info.is_trusted { Some(*id) } else { None })
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Gives the ids of all known peers.
|
||||
pub fn peer_ids(&self) -> impl Iterator<Item = &PeerId> {
|
||||
self.peers.keys()
|
||||
|
||||
@@ -21,7 +21,7 @@ use PeerConnectionStatus::*;
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct PeerInfo<E: EthSpec> {
|
||||
/// The peers reputation
|
||||
score: Score,
|
||||
pub(crate) score: Score,
|
||||
/// Client managing this peer
|
||||
client: Client,
|
||||
/// Connection status of this peer
|
||||
@@ -50,7 +50,7 @@ pub struct PeerInfo<E: EthSpec> {
|
||||
#[serde(skip)]
|
||||
min_ttl: Option<Instant>,
|
||||
/// Is the peer a trusted peer.
|
||||
is_trusted: bool,
|
||||
pub(crate) is_trusted: bool,
|
||||
/// Direction of the first connection of the last (or current) connected session with this peer.
|
||||
/// None if this peer was never connected.
|
||||
connection_direction: Option<ConnectionDirection>,
|
||||
|
||||
@@ -1236,6 +1236,21 @@ impl<E: EthSpec> Network<E> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds the given `enr` to the trusted peers mapping and tries to dial it
|
||||
/// every heartbeat to maintain the connection.
|
||||
pub fn dial_trusted_peer(&mut self, enr: Enr) {
|
||||
self.peer_manager_mut().add_trusted_peer(enr.clone());
|
||||
self.peer_manager_mut().dial_peer(enr);
|
||||
}
|
||||
|
||||
/// Remove the given peer from the trusted peers mapping if it exists and disconnect
|
||||
/// from it.
|
||||
pub fn remove_trusted_peer(&mut self, enr: Enr) {
|
||||
self.peer_manager_mut().remove_trusted_peer(enr.clone());
|
||||
self.peer_manager_mut()
|
||||
.disconnect_peer(enr.peer_id(), GoodbyeReason::TooManyPeers);
|
||||
}
|
||||
|
||||
/* Sub-behaviour event handling functions */
|
||||
|
||||
/// Handle a gossipsub event.
|
||||
|
||||
@@ -162,6 +162,18 @@ impl<E: EthSpec> NetworkGlobals<E> {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn add_trusted_peer(&self, enr: Enr) {
|
||||
self.peers.write().set_trusted_peer(enr);
|
||||
}
|
||||
|
||||
pub fn remove_trusted_peer(&self, enr: Enr) {
|
||||
self.peers.write().unset_trusted_peer(enr);
|
||||
}
|
||||
|
||||
pub fn trusted_peers(&self) -> Vec<PeerId> {
|
||||
self.peers.read().trusted_peers()
|
||||
}
|
||||
|
||||
/// Updates the syncing state of the node.
|
||||
///
|
||||
/// The old state is returned
|
||||
|
||||
Reference in New Issue
Block a user