Peer endpoint updates (#1893)

## Issue Addressed

N/A

## Proposed Changes

- rename `address` -> `last_seen_p2p_address`
- state and direction filters for `peers` endpoint
- metadata count addition to `peers` endpoint
- add `peer_count` endpoint


Co-authored-by: realbigsean <seananderson33@gmail.com>
This commit is contained in:
realbigsean
2020-11-13 02:02:41 +00:00
parent fcb4893f72
commit cb26c15eb6
4 changed files with 234 additions and 43 deletions

View File

@@ -722,7 +722,11 @@ impl BeaconNodeHttpClient {
}
/// `GET node/peers`
pub async fn get_node_peers(&self) -> Result<GenericResponse<Vec<PeerData>>, Error> {
pub async fn get_node_peers(
&self,
states: Option<&[PeerState]>,
directions: Option<&[PeerDirection]>,
) -> Result<PeersData, Error> {
let mut path = self.eth_path()?;
path.path_segments_mut()
@@ -730,6 +734,36 @@ impl BeaconNodeHttpClient {
.push("node")
.push("peers");
if let Some(states) = states {
let state_string = states
.iter()
.map(|i| i.to_string())
.collect::<Vec<_>>()
.join(",");
path.query_pairs_mut().append_pair("state", &state_string);
}
if let Some(directions) = directions {
let dir_string = directions
.iter()
.map(|i| i.to_string())
.collect::<Vec<_>>()
.join(",");
path.query_pairs_mut().append_pair("direction", &dir_string);
}
self.get(path).await
}
/// `GET node/peer_count`
pub async fn get_node_peer_count(&self) -> Result<GenericResponse<PeerCount>, Error> {
let mut path = self.eth_path()?;
path.path_segments_mut()
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
.push("node")
.push("peer_count");
self.get(path).await
}

View File

@@ -509,15 +509,32 @@ pub struct BeaconCommitteeSubscription {
pub is_aggregator: bool,
}
#[derive(Deserialize)]
pub struct PeersQuery {
pub state: Option<QueryVec<PeerState>>,
pub direction: Option<QueryVec<PeerDirection>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PeerData {
pub peer_id: String,
pub enr: Option<String>,
pub address: String,
pub last_seen_p2p_address: String,
pub state: PeerState,
pub direction: PeerDirection,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PeersData {
pub data: Vec<PeerData>,
pub meta: PeersMetaData,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PeersMetaData {
pub count: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PeerState {
@@ -554,6 +571,17 @@ impl FromStr for PeerState {
}
}
impl fmt::Display for PeerState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PeerState::Connected => write!(f, "connected"),
PeerState::Connecting => write!(f, "connecting"),
PeerState::Disconnected => write!(f, "disconnected"),
PeerState::Disconnecting => write!(f, "disconnecting"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PeerDirection {
@@ -582,6 +610,27 @@ impl FromStr for PeerDirection {
}
}
impl fmt::Display for PeerDirection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PeerDirection::Inbound => write!(f, "inbound"),
PeerDirection::Outbound => write!(f, "outbound"),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PeerCount {
#[serde(with = "serde_utils::quoted_u64")]
pub connected: u64,
#[serde(with = "serde_utils::quoted_u64")]
pub connecting: u64,
#[serde(with = "serde_utils::quoted_u64")]
pub disconnected: u64,
#[serde(with = "serde_utils::quoted_u64")]
pub disconnecting: u64,
}
#[cfg(test)]
mod tests {
use super::*;