feat(net): introduce PeersInfo trait (#860)

This commit is contained in:
Matthias Seitz
2023-01-13 12:04:39 +01:00
committed by GitHub
parent 513df631e3
commit 3f9e7a080a
6 changed files with 20 additions and 8 deletions

1
Cargo.lock generated
View File

@ -4123,6 +4123,7 @@ dependencies = [
"reth-consensus",
"reth-interfaces",
"reth-network",
"reth-network-api",
"reth-primitives",
"reth-provider",
"reth-rpc-api",

View File

@ -11,8 +11,16 @@
use std::net::SocketAddr;
/// Provides general purpose information about the network
/// Provides general purpose information about the network.
pub trait NetworkInfo: Send + Sync {
/// Returns the [`SocketAddr`] that listens for incoming connections.
fn local_addr(&self) -> SocketAddr;
}
/// Provides general purpose information about Peers in the network.
pub trait PeersInfo: Send + Sync {
/// Returns how many peers the network is currently connected to.
///
/// Note: this should only include established connections and _not_ ongoing attempts.
fn num_connected_peers(&self) -> usize;
}

View File

@ -13,7 +13,7 @@ use reth_interfaces::{
sync::{SyncState, SyncStateProvider, SyncStateUpdater},
};
use reth_net_common::bandwidth_meter::BandwidthMeter;
use reth_network_api::NetworkInfo;
use reth_network_api::{NetworkInfo, PeersInfo};
use reth_primitives::{PeerId, TransactionSigned, TxHash, H256, U256};
use std::{
net::SocketAddr,
@ -60,11 +60,6 @@ impl NetworkHandle {
Self { inner: Arc::new(inner) }
}
/// How many peers we're currently connected to.
pub fn num_connected_peers(&self) -> usize {
self.inner.num_active_peers.load(Ordering::Relaxed)
}
/// Returns the [`PeerId`] used in the network.
pub fn peer_id(&self) -> &PeerId {
&self.inner.local_peer_id
@ -208,6 +203,12 @@ impl NetworkHandle {
// === API Implementations ===
impl PeersInfo for NetworkHandle {
fn num_connected_peers(&self) -> usize {
self.inner.num_active_peers.load(Ordering::Relaxed)
}
}
impl NetworkInfo for NetworkHandle {
fn local_addr(&self) -> SocketAddr {
*self.inner.listener_address.lock()

View File

@ -14,7 +14,7 @@ use reth_interfaces::{
};
use reth_net_common::ban_list::BanList;
use reth_network::{NetworkConfig, NetworkEvent, NetworkManager, PeersConfig};
use reth_network_api::NetworkInfo;
use reth_network_api::{NetworkInfo, PeersInfo};
use reth_primitives::{HeadersDirection, NodeRecord, PeerId};
use reth_provider::test_utils::NoopProvider;
use reth_transaction_pool::test_utils::testing_pool;

View File

@ -17,6 +17,7 @@ reth-rpc-types = { path = "../rpc-types" }
reth-provider = { path = "../../storage/provider" }
reth-transaction-pool = { path = "../../transaction-pool" }
reth-network = { path = "../network" }
reth-network-api = { path = "../network-api" }
reth-consensus = { path = "../../consensus", features = ["serde"] }
# rpc

View File

@ -1,6 +1,7 @@
use crate::eth::EthApiSpec;
use jsonrpsee::core::RpcResult as Result;
use reth_network::NetworkHandle;
use reth_network_api::PeersInfo;
use reth_rpc_api::NetApiServer;
use reth_rpc_types::PeerCount;