mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add PeersInfo::local_enr (#7539)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -6631,6 +6631,7 @@ dependencies = [
|
||||
name = "reth-network-api"
|
||||
version = "0.2.0-beta.5"
|
||||
dependencies = [
|
||||
"enr",
|
||||
"reth-discv4",
|
||||
"reth-eth-wire",
|
||||
"reth-primitives",
|
||||
|
||||
@ -17,11 +17,13 @@ reth-primitives.workspace = true
|
||||
reth-eth-wire.workspace = true
|
||||
reth-rpc-types.workspace = true
|
||||
reth-discv4.workspace = true
|
||||
# io
|
||||
serde = { workspace = true, features = ["derive"], optional = true }
|
||||
|
||||
# eth
|
||||
enr = { workspace = true, default-features = false, features = ["rust-secp256k1"] }
|
||||
|
||||
# misc
|
||||
thiserror.workspace = true
|
||||
serde = { workspace = true, features = ["derive"], optional = true }
|
||||
tokio = { workspace = true, features = ["sync"] }
|
||||
|
||||
[features]
|
||||
|
||||
@ -57,6 +57,9 @@ pub trait PeersInfo: Send + Sync {
|
||||
|
||||
/// Returns the Ethereum Node Record of the node.
|
||||
fn local_node_record(&self) -> NodeRecord;
|
||||
|
||||
/// Returns the local ENR of the node.
|
||||
fn local_enr(&self) -> enr::Enr<enr::secp256k1::SecretKey>;
|
||||
}
|
||||
|
||||
/// Provides an API for managing the peers of the network.
|
||||
|
||||
@ -7,6 +7,7 @@ use crate::{
|
||||
NetworkError, NetworkInfo, PeerInfo, PeerKind, Peers, PeersInfo, Reputation,
|
||||
ReputationChangeKind,
|
||||
};
|
||||
use enr::{secp256k1::SecretKey, Enr};
|
||||
use reth_discv4::DEFAULT_DISCOVERY_PORT;
|
||||
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
|
||||
use reth_primitives::{Chain, NodeRecord, PeerId};
|
||||
@ -60,6 +61,11 @@ impl PeersInfo for NoopNetwork {
|
||||
fn local_node_record(&self) -> NodeRecord {
|
||||
NodeRecord::new(self.local_addr(), PeerId::random())
|
||||
}
|
||||
|
||||
fn local_enr(&self) -> Enr<SecretKey> {
|
||||
let sk = SecretKey::from_slice(&[0xcd; 32]).unwrap();
|
||||
Enr::builder().build(&sk).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Peers for NoopNetwork {
|
||||
|
||||
@ -3,6 +3,7 @@ use crate::{
|
||||
peers::PeersHandle, protocol::RlpxSubProtocol, swarm::NetworkConnectionState,
|
||||
transactions::TransactionsHandle, FetchClient,
|
||||
};
|
||||
use enr::Enr;
|
||||
use parking_lot::Mutex;
|
||||
use reth_discv4::Discv4;
|
||||
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
|
||||
@ -237,6 +238,20 @@ impl PeersInfo for NetworkHandle {
|
||||
NodeRecord::new(socket_addr, id)
|
||||
}
|
||||
}
|
||||
|
||||
fn local_enr(&self) -> Enr<SecretKey> {
|
||||
let local_node_record = self.local_node_record();
|
||||
let mut builder = Enr::builder();
|
||||
builder.ip(local_node_record.address);
|
||||
if local_node_record.address.is_ipv4() {
|
||||
builder.udp4(local_node_record.udp_port);
|
||||
builder.tcp4(local_node_record.tcp_port);
|
||||
} else {
|
||||
builder.udp6(local_node_record.udp_port);
|
||||
builder.tcp6(local_node_record.tcp_port);
|
||||
}
|
||||
builder.build(&self.inner.secret_key).expect("valid enr")
|
||||
}
|
||||
}
|
||||
|
||||
impl Peers for NetworkHandle {
|
||||
|
||||
Reference in New Issue
Block a user