Add /network/listen_port API endpoint

This commit is contained in:
Paul Hauner 2019-08-23 12:12:29 +10:00
parent 853344af8a
commit 94d987cb6a
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
4 changed files with 29 additions and 18 deletions

View File

@ -46,7 +46,7 @@ impl Bootstrapper {
/// `/ipv4/192.168.0.1/tcp/9000` if the server advertises a listening address of
/// `/ipv4/172.0.0.1/tcp/9000`.
pub fn best_effort_multiaddr(&self) -> Option<Multiaddr> {
let tcp_port = self.first_listening_tcp_port()?;
let tcp_port = self.listen_port().ok()?;
let mut multiaddr = Multiaddr::with_capacity(2);
@ -61,17 +61,6 @@ impl Bootstrapper {
Some(multiaddr)
}
/// Reads the server's listening libp2p addresses and returns the first TCP port protocol it
/// finds, if any.
fn first_listening_tcp_port(&self) -> Option<u16> {
self.listen_addresses().ok()?.iter().find_map(|multiaddr| {
multiaddr.iter().find_map(|protocol| match protocol {
Protocol::Tcp(port) => Some(port),
_ => None,
})
})
}
/// Returns the IPv4 address of the server URL, unless it contains a FQDN.
pub fn server_ipv4_addr(&self) -> Option<Ipv4Addr> {
match self.url.host()? {
@ -86,9 +75,8 @@ impl Bootstrapper {
}
/// Returns the servers listening libp2p addresses.
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>, String> {
get_listen_addresses(self.url.clone())
.map_err(|e| format!("Unable to get listen addresses: {:?}", e))
pub fn listen_port(&self) -> Result<u16, String> {
get_listen_port(self.url.clone()).map_err(|e| format!("Unable to get listen port: {:?}", e))
}
/// Returns the genesis block and state.
@ -179,7 +167,7 @@ fn get_block<T: EthSpec>(mut url: Url, slot: Slot) -> Result<BeaconBlock<T>, Err
fn get_enr(mut url: Url) -> Result<Enr, Error> {
url.path_segments_mut()
.map(|mut url| {
url.push("node").push("network").push("enr");
url.push("network").push("enr");
})
.map_err(|_| Error::InvalidUrl)?;
@ -189,10 +177,10 @@ fn get_enr(mut url: Url) -> Result<Enr, Error> {
.map_err(Into::into)
}
fn get_listen_addresses(mut url: Url) -> Result<Vec<Multiaddr>, Error> {
fn get_listen_port(mut url: Url) -> Result<u16, Error> {
url.path_segments_mut()
.map(|mut url| {
url.push("node").push("network").push("listen_addresses");
url.push("network").push("listen_port");
})
.map_err(|_| Error::InvalidUrl)?;

View File

@ -18,6 +18,7 @@ use tokio::sync::{mpsc, oneshot};
/// Service that handles communication between internal services and the eth2_libp2p network service.
pub struct Service<T: BeaconChainTypes> {
libp2p_service: Arc<Mutex<LibP2PService>>,
libp2p_port: u16,
_libp2p_exit: oneshot::Sender<()>,
_network_send: mpsc::UnboundedSender<NetworkMessage>,
_phantom: PhantomData<T>, //message_handler: MessageHandler,
@ -56,6 +57,7 @@ impl<T: BeaconChainTypes + 'static> Service<T> {
)?;
let network_service = Service {
libp2p_service,
libp2p_port: config.libp2p_port,
_libp2p_exit: libp2p_exit,
_network_send: network_send.clone(),
_phantom: PhantomData,
@ -87,6 +89,11 @@ impl<T: BeaconChainTypes + 'static> Service<T> {
.collect()
}
/// Returns the libp2p port that this node has been configured to listen using.
pub fn listen_port(&self) -> u16 {
self.libp2p_port
}
/// Returns the number of libp2p connected peers.
pub fn connected_peers(&self) -> usize {
self.libp2p_service.lock().swarm.connected_peers()

View File

@ -134,6 +134,7 @@ pub fn start_server<T: BeaconChainTypes>(
(&Method::GET, "/network/peer_count") => network::get_peer_count::<T>(req),
(&Method::GET, "/network/peer_id") => network::get_peer_id::<T>(req),
(&Method::GET, "/network/peers") => network::get_peer_list::<T>(req),
(&Method::GET, "/network/listen_port") => network::get_listen_port::<T>(req),
(&Method::GET, "/network/listen_addresses") => {
network::get_listen_addresses::<T>(req)
}

View File

@ -21,6 +21,21 @@ pub fn get_listen_addresses<T: BeaconChainTypes>(req: Request<Body>) -> ApiResul
)))
}
/// 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_port<T: BeaconChainTypes>(req: Request<Body>) -> ApiResult {
let network = req
.extensions()
.get::<Arc<NetworkService<T>>>()
.ok_or_else(|| ApiError::ServerError("NetworkService extension missing".to_string()))?;
Ok(success_response(Body::from(
serde_json::to_string(&network.listen_port())
.map_err(|e| ApiError::ServerError(format!("Unable to serialize port: {:?}", e)))?,
)))
}
/// HTTP handle to return the Discv5 ENR from the client's libp2p service.
///
/// ENR is encoded as base64 string.