Allow bootstrapper to scrape libp2p address

This commit is contained in:
Paul Hauner
2019-08-15 16:41:02 +10:00
parent 4678524659
commit ce37f95861
8 changed files with 107 additions and 33 deletions

View File

@@ -135,6 +135,9 @@ pub fn start_server<T: BeaconChainTypes + Clone + Send + Sync + 'static>(
(&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, "/node/network/listen_addresses") => {
network::get_listen_addresses::<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())),

View File

@@ -1,9 +1,28 @@
use crate::{success_response, ApiError, ApiResult, NetworkService};
use beacon_chain::BeaconChainTypes;
use eth2_libp2p::{Enr, PeerId};
use eth2_libp2p::{Enr, Multiaddr, PeerId};
use hyper::{Body, Request};
use std::sync::Arc;
/// HTTP handle to return the list of libp2p multiaddr the client is listening on.
///
/// Returns a list of `Multiaddr`, serialized according to their `serde` impl.
pub fn get_listen_addresses<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 multiaddresses: Vec<Multiaddr> = network.listen_multiaddrs();
Ok(success_response(Body::from(
serde_json::to_string(&multiaddresses)
.map_err(|e| ApiError::ServerError(format!("Unable to serialize Enr: {:?}", e)))?,
)))
}
/// HTTP handle to return the Discv5 ENR from the client's libp2p service.
///
/// ENR is encoded as base64 string.