diff --git a/beacon_node/client/src/bootstrapper.rs b/beacon_node/client/src/bootstrapper.rs index 19f13e2da..eaaee4aa1 100644 --- a/beacon_node/client/src/bootstrapper.rs +++ b/beacon_node/client/src/bootstrapper.rs @@ -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 { - 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 { - 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 { match self.url.host()? { @@ -86,9 +75,8 @@ impl Bootstrapper { } /// Returns the servers listening libp2p addresses. - pub fn listen_addresses(&self) -> Result, String> { - get_listen_addresses(self.url.clone()) - .map_err(|e| format!("Unable to get listen addresses: {:?}", e)) + pub fn listen_port(&self) -> Result { + 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(mut url: Url, slot: Slot) -> Result, Err fn get_enr(mut url: Url) -> Result { 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 { .map_err(Into::into) } -fn get_listen_addresses(mut url: Url) -> Result, Error> { +fn get_listen_port(mut url: Url) -> Result { 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)?; diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index dc7e94140..152f4dc77 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -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 { libp2p_service: Arc>, + libp2p_port: u16, _libp2p_exit: oneshot::Sender<()>, _network_send: mpsc::UnboundedSender, _phantom: PhantomData, //message_handler: MessageHandler, @@ -56,6 +57,7 @@ impl Service { )?; 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 Service { .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() diff --git a/beacon_node/rest_api/src/lib.rs b/beacon_node/rest_api/src/lib.rs index 354b23403..a382c49e3 100644 --- a/beacon_node/rest_api/src/lib.rs +++ b/beacon_node/rest_api/src/lib.rs @@ -134,6 +134,7 @@ pub fn start_server( (&Method::GET, "/network/peer_count") => network::get_peer_count::(req), (&Method::GET, "/network/peer_id") => network::get_peer_id::(req), (&Method::GET, "/network/peers") => network::get_peer_list::(req), + (&Method::GET, "/network/listen_port") => network::get_listen_port::(req), (&Method::GET, "/network/listen_addresses") => { network::get_listen_addresses::(req) } diff --git a/beacon_node/rest_api/src/network.rs b/beacon_node/rest_api/src/network.rs index daded9d3d..a3e4c5ee7 100644 --- a/beacon_node/rest_api/src/network.rs +++ b/beacon_node/rest_api/src/network.rs @@ -21,6 +21,21 @@ pub fn get_listen_addresses(req: Request) -> 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(req: Request) -> ApiResult { + let network = req + .extensions() + .get::>>() + .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.