feat(net): add way to get the NodeRecord of the local node (#875)

Co-authored-by: Enrique Ortiz <evalir@users.noreply.github.com>
This commit is contained in:
Enrique Ortiz
2023-01-13 10:57:29 -04:00
committed by GitHub
parent 18dbcd4e6a
commit 0603f07c6c
4 changed files with 16 additions and 2 deletions

3
Cargo.lock generated
View File

@ -4016,6 +4016,9 @@ dependencies = [
[[package]]
name = "reth-network-api"
version = "0.1.0"
dependencies = [
"reth-primitives",
]
[[package]]
name = "reth-primitives"

View File

@ -8,3 +8,4 @@ readme = "README.md"
description = "Network interfaces"
[dependencies]
reth-primitives = { path = "../../primitives" }

View File

@ -9,6 +9,7 @@
//!
//! Provides abstractions for the reth-network crate.
use reth_primitives::NodeRecord;
use std::net::SocketAddr;
/// Provides general purpose information about the network.
@ -23,4 +24,7 @@ pub trait PeersInfo: Send + Sync {
///
/// Note: this should only include established connections and _not_ ongoing attempts.
fn num_connected_peers(&self) -> usize;
/// Returns the Ethereum Node Record of the node.
fn local_node_record(&self) -> NodeRecord;
}

View File

@ -14,7 +14,7 @@ use reth_interfaces::{
};
use reth_net_common::bandwidth_meter::BandwidthMeter;
use reth_network_api::{NetworkInfo, PeersInfo};
use reth_primitives::{PeerId, TransactionSigned, TxHash, H256, U256};
use reth_primitives::{NodeRecord, PeerId, TransactionSigned, TxHash, H256, U256};
use std::{
net::SocketAddr,
sync::{
@ -207,6 +207,12 @@ impl PeersInfo for NetworkHandle {
fn num_connected_peers(&self) -> usize {
self.inner.num_active_peers.load(Ordering::Relaxed)
}
fn local_node_record(&self) -> NodeRecord {
let id = *self.peer_id();
let socket_addr = *self.inner.listener_address.lock();
NodeRecord::new(socket_addr, id)
}
}
impl NetworkInfo for NetworkHandle {