use crate::error::ApiResult; use crate::response_builder::ResponseBuilder; use crate::NetworkGlobals; use beacon_chain::BeaconChainTypes; use eth2_libp2p::{Multiaddr, PeerId}; use hyper::{Body, Request}; use std::sync::Arc; /// HTTP handler 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( req: Request, network: Arc>, ) -> ApiResult { let multiaddresses: Vec = network.listen_multiaddrs(); ResponseBuilder::new(&req)?.body_no_ssz(&multiaddresses) } /// HTTP handler to return the network port the client is listening on. /// /// Returns the TCP port number in its plain form (which is also valid JSON serialization) pub fn get_listen_port( req: Request, network: Arc>, ) -> ApiResult { ResponseBuilder::new(&req)?.body(&network.listen_port_tcp()) } /// HTTP handler to return the Discv5 ENR from the client's libp2p service. /// /// ENR is encoded as base64 string. pub fn get_enr( req: Request, network: Arc>, ) -> ApiResult { ResponseBuilder::new(&req)?.body_no_ssz(&network.local_enr().to_base64()) } /// HTTP handler to return the `PeerId` from the client's libp2p service. /// /// PeerId is encoded as base58 string. pub fn get_peer_id( req: Request, network: Arc>, ) -> ApiResult { ResponseBuilder::new(&req)?.body_no_ssz(&network.local_peer_id().to_base58()) } /// HTTP handler to return the number of peers connected in the client's libp2p service. pub fn get_peer_count( req: Request, network: Arc>, ) -> ApiResult { ResponseBuilder::new(&req)?.body(&network.connected_peers()) } /// HTTP handler 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( req: Request, network: Arc>, ) -> ApiResult { let connected_peers: Vec = network .peers .read() .connected_peer_ids() .map(PeerId::to_string) .collect(); ResponseBuilder::new(&req)?.body_no_ssz(&connected_peers) }