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

@@ -53,7 +53,7 @@ use eth2::types::{
use eth2::{CONSENSUS_VERSION_HEADER, CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER};
use health_metrics::observe::Observe;
use lighthouse_network::rpc::methods::MetaData;
use lighthouse_network::{types::SyncState, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
use lighthouse_network::{types::SyncState, Enr, EnrExt, NetworkGlobals, PeerId, PubsubMessage};
use lighthouse_version::version_with_platform;
use logging::SSELoggingComponents;
use network::{NetworkMessage, NetworkSenders, ValidatorSubscriptionMessage};
@@ -72,6 +72,7 @@ use std::future::Future;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;
use sysinfo::{System, SystemExt};
use system_health::{observe_nat, observe_system_health_bn};
@@ -3676,7 +3677,7 @@ pub fn serve<T: BeaconChainTypes>(
.and(task_spawner_filter.clone())
.and(chain_filter.clone())
.and(warp_utils::json::json())
.and(network_tx_filter)
.and(network_tx_filter.clone())
.and(log_filter.clone())
.then(
|not_synced_filter: Result<(), Rejection>,
@@ -4122,6 +4123,77 @@ pub fn serve<T: BeaconChainTypes>(
},
);
// POST lighthouse/add_peer
let post_lighthouse_add_peer = warp::path("lighthouse")
.and(warp::path("add_peer"))
.and(warp::path::end())
.and(warp_utils::json::json())
.and(task_spawner_filter.clone())
.and(network_globals.clone())
.and(network_tx_filter.clone())
.and(log_filter.clone())
.then(
|request_data: api_types::AdminPeer,
task_spawner: TaskSpawner<T::EthSpec>,
network_globals: Arc<NetworkGlobals<T::EthSpec>>,
network_tx: UnboundedSender<NetworkMessage<T::EthSpec>>,
log: Logger| {
task_spawner.blocking_json_task(Priority::P0, move || {
let enr = Enr::from_str(&request_data.enr).map_err(|e| {
warp_utils::reject::custom_bad_request(format!("invalid enr error {}", e))
})?;
info!(
log,
"Adding trusted peer";
"peer_id" => %enr.peer_id(),
"multiaddr" => ?enr.multiaddr()
);
network_globals.add_trusted_peer(enr.clone());
publish_network_message(&network_tx, NetworkMessage::ConnectTrustedPeer(enr))?;
Ok(())
})
},
);
// POST lighthouse/remove_peer
let post_lighthouse_remove_peer = warp::path("lighthouse")
.and(warp::path("remove_peer"))
.and(warp::path::end())
.and(warp_utils::json::json())
.and(task_spawner_filter.clone())
.and(network_globals.clone())
.and(network_tx_filter.clone())
.and(log_filter.clone())
.then(
|request_data: api_types::AdminPeer,
task_spawner: TaskSpawner<T::EthSpec>,
network_globals: Arc<NetworkGlobals<T::EthSpec>>,
network_tx: UnboundedSender<NetworkMessage<T::EthSpec>>,
log: Logger| {
task_spawner.blocking_json_task(Priority::P0, move || {
let enr = Enr::from_str(&request_data.enr).map_err(|e| {
warp_utils::reject::custom_bad_request(format!("invalid enr error {}", e))
})?;
info!(
log,
"Removing trusted peer";
"peer_id" => %enr.peer_id(),
"multiaddr" => ?enr.multiaddr()
);
network_globals.remove_trusted_peer(enr.clone());
publish_network_message(
&network_tx,
NetworkMessage::DisconnectTrustedPeer(enr),
)?;
Ok(())
})
},
);
// POST lighthouse/liveness
let post_lighthouse_liveness = warp::path("lighthouse")
.and(warp::path("liveness"))
@@ -4896,6 +4968,8 @@ pub fn serve<T: BeaconChainTypes>(
.uor(post_lighthouse_ui_validator_info)
.uor(post_lighthouse_finalize)
.uor(post_lighthouse_compaction)
.uor(post_lighthouse_add_peer)
.uor(post_lighthouse_remove_peer)
.recover(warp_utils::reject::handle_rejection),
),
)

View File

@@ -5768,6 +5768,27 @@ impl ApiTester {
self
}
pub async fn test_post_lighthouse_add_remove_peer(self) -> Self {
let trusted_peers = self.ctx.network_globals.as_ref().unwrap().trusted_peers();
// Check that there aren't any trusted peers on startup
assert!(trusted_peers.is_empty());
let enr = AdminPeer {enr: "enr:-QESuEDpVVjo8dmDuneRhLnXdIGY3e9NQiaG4sJR3GS-VMQCQDsmBYoQhJRaPeZzPlTsZj2F8v-iV4lKJEYIRIyztqexHodhdHRuZXRziAwAAAAAAAAAhmNsaWVudNiKTGlnaHRob3VzZYw3LjAuMC1iZXRhLjSEZXRoMpDS8Zl_YAAJEAAIAAAAAAAAgmlkgnY0gmlwhIe11XmDaXA2kCoBBPkAOitZAAAAAAAAAAKEcXVpY4IjKYVxdWljNoIjg4lzZWNwMjU2azGhA43ihEr9BUVVnIHIfFqBR3Izs4YRHHPsTqIbUgEb3Hc8iHN5bmNuZXRzD4N0Y3CCIyiEdGNwNoIjgoN1ZHCCIyiEdWRwNoIjgg".to_string()};
self.client
.post_lighthouse_add_peer(enr.clone())
.await
.unwrap();
let trusted_peers = self.ctx.network_globals.as_ref().unwrap().trusted_peers();
// Should have 1 trusted peer
assert_eq!(trusted_peers.len(), 1);
self.client.post_lighthouse_remove_peer(enr).await.unwrap();
let trusted_peers = self.ctx.network_globals.as_ref().unwrap().trusted_peers();
// Should be empty after removing
assert!(trusted_peers.is_empty());
self
}
pub async fn test_post_lighthouse_liveness(self) -> Self {
let epoch = self.chain.epoch().unwrap();
let head_state = self.chain.head_beacon_state_cloned();
@@ -7334,6 +7355,8 @@ async fn lighthouse_endpoints() {
.test_post_lighthouse_database_reconstruct()
.await
.test_post_lighthouse_liveness()
.await
.test_post_lighthouse_add_remove_peer()
.await;
}