feat: support admin_peers (#4435)

This commit is contained in:
Matthias Seitz
2023-08-31 13:36:50 -07:00
committed by GitHub
parent d8a7ee2eb4
commit a76da98316
11 changed files with 177 additions and 90 deletions

View File

@ -19,13 +19,14 @@
//!
//! - `serde` (default): Enable serde support
use async_trait::async_trait;
use reth_eth_wire::DisconnectReason;
use reth_eth_wire::{DisconnectReason, EthVersion, Status};
use reth_primitives::{NodeRecord, PeerId};
use reth_rpc_types::NetworkStatus;
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::Arc};
pub use error::NetworkError;
pub use reputation::{Reputation, ReputationChangeKind};
use reth_eth_wire::capability::Capabilities;
/// Network Error
pub mod error;
@ -81,6 +82,9 @@ pub trait Peers: PeersInfo {
/// Adds a peer to the known peer set, with the given kind.
fn add_peer_kind(&self, peer: PeerId, kind: PeerKind, addr: SocketAddr);
/// Returns the rpc [PeerInfo] for all connected peers.
async fn get_peers(&self) -> Result<Vec<PeerInfo>, NetworkError>;
/// Removes a peer from the peer set that corresponds to given kind.
fn remove_peer(&self, peer: PeerId, kind: PeerKind);
@ -106,3 +110,54 @@ pub enum PeerKind {
/// Trusted peer.
Trusted,
}
/// Info about an active peer session.
#[derive(Debug, Clone)]
pub struct PeerInfo {
/// Announced capabilities of the peer
pub capabilities: Arc<Capabilities>,
/// The identifier of the remote peer
pub remote_id: PeerId,
/// The client's name and version
pub client_version: Arc<String>,
/// The peer's address we're connected to
pub remote_addr: SocketAddr,
/// The local address of the connection
pub local_addr: Option<SocketAddr>,
/// The direction of the session
pub direction: Direction,
/// The negotiated eth version.
pub eth_version: EthVersion,
/// The Status message the peer sent for the `eth` handshake
pub status: Status,
}
/// The direction of the connection.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Direction {
/// Incoming connection.
Incoming,
/// Outgoing connection to a specific node.
Outgoing(PeerId),
}
impl Direction {
/// Returns `true` if this an incoming connection.
pub fn is_incoming(&self) -> bool {
matches!(self, Direction::Incoming)
}
/// Returns `true` if this an outgoing connection.
pub fn is_outgoing(&self) -> bool {
matches!(self, Direction::Outgoing(_))
}
}
impl std::fmt::Display for Direction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Direction::Incoming => write!(f, "incoming"),
Direction::Outgoing(_) => write!(f, "outgoing"),
}
}
}

View File

@ -4,7 +4,8 @@
//! generic over it.
use crate::{
NetworkError, NetworkInfo, PeerKind, Peers, PeersInfo, Reputation, ReputationChangeKind,
NetworkError, NetworkInfo, PeerInfo, PeerKind, Peers, PeersInfo, Reputation,
ReputationChangeKind,
};
use async_trait::async_trait;
use reth_discv4::DEFAULT_DISCOVERY_PORT;
@ -66,6 +67,10 @@ impl PeersInfo for NoopNetwork {
impl Peers for NoopNetwork {
fn add_peer_kind(&self, _peer: PeerId, _kind: PeerKind, _addr: SocketAddr) {}
async fn get_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
Ok(vec![])
}
fn remove_peer(&self, _peer: PeerId, _kind: PeerKind) {}
fn disconnect_peer(&self, _peer: PeerId) {}