mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 04:01:51 +00:00
Add network routes to API
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate network as client_network;
|
||||
|
||||
mod beacon;
|
||||
mod config;
|
||||
mod helpers;
|
||||
mod metrics;
|
||||
mod network;
|
||||
mod node;
|
||||
mod spec;
|
||||
mod url_query;
|
||||
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use client_network::Service as NetworkService;
|
||||
pub use config::Config as ApiConfig;
|
||||
use hyper::rt::Future;
|
||||
use hyper::service::service_fn_ok;
|
||||
@@ -68,10 +71,11 @@ impl From<state_processing::per_slot_processing::Error> for ApiError {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
|
||||
pub fn start_server<T: BeaconChainTypes + Clone + Send + Sync + 'static>(
|
||||
config: &ApiConfig,
|
||||
executor: &TaskExecutor,
|
||||
beacon_chain: Arc<BeaconChain<T>>,
|
||||
network_service: Arc<NetworkService<T>>,
|
||||
db_path: PathBuf,
|
||||
log: &slog::Logger,
|
||||
) -> Result<exit_future::Signal, hyper::Error> {
|
||||
@@ -99,6 +103,7 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
|
||||
let log = server_log.clone();
|
||||
let beacon_chain = server_bc.clone();
|
||||
let db_path = db_path.clone();
|
||||
let network_service = network_service.clone();
|
||||
|
||||
// Create a simple handler for the router, inject our stateful objects into the request.
|
||||
service_fn_ok(move |mut req| {
|
||||
@@ -109,6 +114,8 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
|
||||
req.extensions_mut()
|
||||
.insert::<Arc<BeaconChain<T>>>(beacon_chain.clone());
|
||||
req.extensions_mut().insert::<DBPath>(db_path.clone());
|
||||
req.extensions_mut()
|
||||
.insert::<Arc<NetworkService<T>>>(network_service.clone());
|
||||
|
||||
let path = req.uri().path().to_string();
|
||||
|
||||
@@ -124,6 +131,9 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
|
||||
(&Method::GET, "/metrics") => metrics::get_prometheus::<T>(req),
|
||||
(&Method::GET, "/node/version") => node::get_version(req),
|
||||
(&Method::GET, "/node/genesis_time") => node::get_genesis_time::<T>(req),
|
||||
(&Method::GET, "/node/network/enr") => network::get_enr::<T>(req),
|
||||
(&Method::GET, "/node/network/peer_count") => network::get_peer_count::<T>(req),
|
||||
(&Method::GET, "/node/network/peers") => network::get_peer_list::<T>(req),
|
||||
(&Method::GET, "/spec") => spec::get_spec::<T>(req),
|
||||
(&Method::GET, "/spec/slots_per_epoch") => spec::get_slots_per_epoch::<T>(req),
|
||||
_ => Err(ApiError::MethodNotAllowed(path.clone())),
|
||||
|
||||
61
beacon_node/rest_api/src/network.rs
Normal file
61
beacon_node/rest_api/src/network.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use crate::{success_response, ApiError, ApiResult, NetworkService};
|
||||
use beacon_chain::BeaconChainTypes;
|
||||
use eth2_libp2p::{Enr, PeerId};
|
||||
use hyper::{Body, Request};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// HTTP handle to return the Discv5 ENR from the client's libp2p service.
|
||||
///
|
||||
/// ENR is encoded as base64 string.
|
||||
pub fn get_enr<T: BeaconChainTypes + Send + Sync + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let network = req
|
||||
.extensions()
|
||||
.get::<Arc<NetworkService<T>>>()
|
||||
.ok_or_else(|| ApiError::ServerError("NetworkService extension missing".to_string()))?;
|
||||
|
||||
let enr: Enr = network.local_enr();
|
||||
|
||||
Ok(success_response(Body::from(
|
||||
serde_json::to_string(&enr.to_base64())
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to serialize Enr: {:?}", e)))?,
|
||||
)))
|
||||
}
|
||||
|
||||
/// HTTP handle to return the number of peers connected in the client's libp2p service.
|
||||
pub fn get_peer_count<T: BeaconChainTypes + Send + Sync + 'static>(
|
||||
req: Request<Body>,
|
||||
) -> ApiResult {
|
||||
let network = req
|
||||
.extensions()
|
||||
.get::<Arc<NetworkService<T>>>()
|
||||
.ok_or_else(|| ApiError::ServerError("NetworkService extension missing".to_string()))?;
|
||||
|
||||
let connected_peers: usize = network.connected_peers();
|
||||
|
||||
Ok(success_response(Body::from(
|
||||
serde_json::to_string(&connected_peers)
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to serialize Enr: {:?}", e)))?,
|
||||
)))
|
||||
}
|
||||
|
||||
/// HTTP handle to return the list of peers connected to the client's libp2p service.
|
||||
///
|
||||
/// Peers are presented as a list of `PeerId::to_string()`.
|
||||
pub fn get_peer_list<T: BeaconChainTypes + Send + Sync + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let network = req
|
||||
.extensions()
|
||||
.get::<Arc<NetworkService<T>>>()
|
||||
.ok_or_else(|| ApiError::ServerError("NetworkService extension missing".to_string()))?;
|
||||
|
||||
let connected_peers: Vec<String> = network
|
||||
.connected_peer_set()
|
||||
.iter()
|
||||
.map(PeerId::to_string)
|
||||
.collect();
|
||||
|
||||
Ok(success_response(Body::from(
|
||||
serde_json::to_string(&connected_peers).map_err(|e| {
|
||||
ApiError::ServerError(format!("Unable to serialize Vec<PeerId>: {:?}", e))
|
||||
})?,
|
||||
)))
|
||||
}
|
||||
Reference in New Issue
Block a user