mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(net): introduce PeersInfo trait (#860)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -4123,6 +4123,7 @@ dependencies = [
|
||||
"reth-consensus",
|
||||
"reth-interfaces",
|
||||
"reth-network",
|
||||
"reth-network-api",
|
||||
"reth-primitives",
|
||||
"reth-provider",
|
||||
"reth-rpc-api",
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user