Adds peers and connected_peers to lighthouse http API (#1030)

This commit is contained in:
Age Manning
2020-04-21 23:27:49 +10:00
committed by GitHub
parent fa7147f7c5
commit 9e42a851e4
9 changed files with 249 additions and 18 deletions

View File

@@ -3,9 +3,10 @@
//! Currently using identify to fingerprint.
use libp2p::identify::IdentifyInfo;
use serde::Serialize;
#[derive(Debug)]
/// Various client and protocol information related to a node.
#[derive(Clone, Debug, Serialize)]
pub struct Client {
/// The client's name (Ex: lighthouse, prism, nimbus, etc)
pub kind: ClientKind,
@@ -19,7 +20,7 @@ pub struct Client {
pub agent_string: Option<String>,
}
#[derive(Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum ClientKind {
/// A lighthouse node (the best kind).
Lighthouse,
@@ -125,6 +126,11 @@ fn client_from_agent_version(agent_version: &str) -> (ClientKind, String, String
}
(kind, version, os_version)
}
Some("github.com") => {
let kind = ClientKind::Prysm;
let unknown = String::from("unknown");
(kind, unknown.clone(), unknown)
}
_ => {
let unknown = String::from("unknown");
(ClientKind::Unknown, unknown.clone(), unknown)

View File

@@ -2,12 +2,17 @@ use super::client::Client;
use super::peerdb::{Rep, DEFAULT_REPUTATION};
use crate::rpc::MetaData;
use crate::Multiaddr;
use serde::{
ser::{SerializeStructVariant, Serializer},
Serialize,
};
use std::time::Instant;
use types::{EthSpec, Slot, SubnetId};
use PeerConnectionStatus::*;
/// Information about a given connected peer.
#[derive(Debug)]
#[derive(Clone, Debug, Serialize)]
#[serde(bound = "T: EthSpec")]
pub struct PeerInfo<T: EthSpec> {
/// The connection status of the peer
_status: PeerStatus,
@@ -54,7 +59,7 @@ impl<T: EthSpec> PeerInfo<T> {
}
}
#[derive(Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum PeerStatus {
/// The peer is healthy
Healthy,
@@ -69,7 +74,7 @@ impl Default for PeerStatus {
}
/// Connection Status of the peer
#[derive(Debug, Clone)]
#[derive(Clone, Debug)]
pub enum PeerConnectionStatus {
Connected {
/// number of ingoing connections
@@ -86,12 +91,41 @@ pub enum PeerConnectionStatus {
since: Instant,
},
Unknown {
/// time since we know of this peer
/// time since we last saw this peer
since: Instant,
},
}
#[derive(Debug, Clone, PartialEq)]
/// Serialization for http requests.
impl Serialize for PeerConnectionStatus {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Connected { n_in, n_out } => {
let mut s = serializer.serialize_struct_variant("", 0, "Connected", 2)?;
s.serialize_field("in", n_in)?;
s.serialize_field("out", n_out)?;
s.end()
}
Disconnected { since } => {
let mut s = serializer.serialize_struct_variant("", 1, "Disconnected", 1)?;
s.serialize_field("since", &since.elapsed().as_secs())?;
s.end()
}
Banned { since } => {
let mut s = serializer.serialize_struct_variant("", 2, "Banned", 1)?;
s.serialize_field("since", &since.elapsed().as_secs())?;
s.end()
}
Unknown { since } => {
let mut s = serializer.serialize_struct_variant("", 3, "Unknown", 1)?;
s.serialize_field("since", &since.elapsed().as_secs())?;
s.end()
}
}
}
}
#[derive(Clone, Debug, Serialize)]
pub enum PeerSyncStatus {
/// At the current state as our node or ahead of us.
Synced {

View File

@@ -41,8 +41,13 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
.map_or(DEFAULT_REPUTATION, |info| info.reputation)
}
/// Returns an iterator over all peers in the db.
pub fn peers(&self) -> impl Iterator<Item = (&PeerId, &PeerInfo<TSpec>)> {
self.peers.iter()
}
/// Gives the ids of all known peers.
pub fn peers(&self) -> impl Iterator<Item = &PeerId> {
pub fn peer_ids(&self) -> impl Iterator<Item = &PeerId> {
self.peers.keys()
}
@@ -66,7 +71,14 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
}
/// Gives the ids of all known connected peers.
pub fn connected_peers(&self) -> impl Iterator<Item = &PeerId> {
pub fn connected_peers(&self) -> impl Iterator<Item = (&PeerId, &PeerInfo<TSpec>)> {
self.peers
.iter()
.filter(|(_, info)| info.connection_status.is_connected())
}
/// Gives the ids of all known connected peers.
pub fn connected_peer_ids(&self) -> impl Iterator<Item = &PeerId> {
self.peers
.iter()
.filter(|(_, info)| info.connection_status.is_connected())
@@ -373,7 +385,7 @@ mod tests {
}
assert_eq!(pdb.n_dc, 0);
for p in pdb.connected_peers().cloned().collect::<Vec<_>>() {
for p in pdb.connected_peer_ids().cloned().collect::<Vec<_>>() {
pdb.disconnect(&p);
}

View File

@@ -1,6 +1,7 @@
//! Available RPC methods types and ids.
use crate::types::EnrBitfield;
use serde::Serialize;
use ssz_derive::{Decode, Encode};
use types::{Epoch, EthSpec, Hash256, SignedBeaconBlock, Slot};
@@ -37,7 +38,8 @@ pub struct Ping {
}
/// The METADATA response structure.
#[derive(Encode, Decode, Clone, Debug, PartialEq)]
#[derive(Encode, Decode, Clone, Debug, PartialEq, Serialize)]
#[serde(bound = "T: EthSpec")]
pub struct MetaData<T: EthSpec> {
/// A sequential counter indicating when data gets modified.
pub seq_number: u64,

View File

@@ -80,7 +80,7 @@ impl<TSpec: EthSpec> NetworkGlobals<TSpec> {
/// Returns the number of libp2p connected peers.
pub fn connected_peers(&self) -> usize {
self.peers.read().connected_peers().count()
self.peers.read().connected_peer_ids().count()
}
/// Returns in the node is syncing.