pub struct Discv4 { /* private fields */ }
Expand description
The Discv4 frontend
This communicates with the Discv4Service
by sending commands over a channel.
See also Discv4::spawn
Implementations§
Source§impl Discv4
impl Discv4
Sourcepub async fn spawn(
local_address: SocketAddr,
local_enr: NodeRecord,
secret_key: SecretKey,
config: Discv4Config,
) -> Result<Self>
pub async fn spawn( local_address: SocketAddr, local_enr: NodeRecord, secret_key: SecretKey, config: Discv4Config, ) -> Result<Self>
Same as Self::bind
but also spawns the service onto a new task,
Discv4Service::spawn()
Sourcepub fn noop() -> Self
Available on crate feature test-utils
only.
pub fn noop() -> Self
test-utils
only.Returns a new instance with the given channel directly
NOTE: this is only intended for test setups.
Sourcepub async fn bind(
local_address: SocketAddr,
local_node_record: NodeRecord,
secret_key: SecretKey,
config: Discv4Config,
) -> Result<(Self, Discv4Service)>
pub async fn bind( local_address: SocketAddr, local_node_record: NodeRecord, secret_key: SecretKey, config: Discv4Config, ) -> Result<(Self, Discv4Service)>
Binds a new UdpSocket
and creates the service
use rand::thread_rng;
use reth_discv4::{Discv4, Discv4Config};
use reth_network_peers::{pk2id, NodeRecord, PeerId};
use secp256k1::SECP256K1;
use std::{net::SocketAddr, str::FromStr};
// generate a (random) keypair
let mut rng = thread_rng();
let (secret_key, pk) = SECP256K1.generate_keypair(&mut rng);
let id = pk2id(&pk);
let socket = SocketAddr::from_str("0.0.0.0:0").unwrap();
let local_enr =
NodeRecord { address: socket.ip(), tcp_port: socket.port(), udp_port: socket.port(), id };
let config = Discv4Config::default();
let (discv4, mut service) = Discv4::bind(socket, local_enr, secret_key, config).await.unwrap();
// get an update strea
let updates = service.update_stream();
let _handle = service.spawn();
// lookup the local node in the DHT
let _discovered = discv4.lookup_self().await.unwrap();
Sourcepub const fn local_addr(&self) -> SocketAddr
pub const fn local_addr(&self) -> SocketAddr
Returns the address of the UDP socket.
Sourcepub fn node_record(&self) -> NodeRecord
pub fn node_record(&self) -> NodeRecord
Returns the NodeRecord
of the local node.
This includes the currently tracked external IP address of the node.
Sourcepub fn external_ip(&self) -> IpAddr
pub fn external_ip(&self) -> IpAddr
Returns the currently tracked external IP of the node.
Sourcepub fn set_lookup_interval(&self, duration: Duration)
pub fn set_lookup_interval(&self, duration: Duration)
Sets the [Interval] used for periodically looking up targets over the network
Sourcepub async fn lookup_self(&self) -> Result<Vec<NodeRecord>, Discv4Error>
pub async fn lookup_self(&self) -> Result<Vec<NodeRecord>, Discv4Error>
Starts a FindNode
recursive lookup that locates the closest nodes to the given node id. See also: https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup
The lookup initiator starts by picking α closest nodes to the target it knows of. The
initiator then sends concurrent FindNode
packets to those nodes. α is a system-wide
concurrency parameter, such as 3. In the recursive step, the initiator resends FindNode
to
nodes it has learned about from previous queries. Of the k nodes the initiator has heard of
closest to the target, it picks α that it has not yet queried and resends FindNode
to
them. Nodes that fail to respond quickly are removed from consideration until and unless
they do respond.
Sourcepub async fn lookup(
&self,
node_id: PeerId,
) -> Result<Vec<NodeRecord>, Discv4Error>
pub async fn lookup( &self, node_id: PeerId, ) -> Result<Vec<NodeRecord>, Discv4Error>
Looks up the given node id.
Returning the closest nodes to the given node id.
Sourcepub async fn lookup_random(&self) -> Result<Vec<NodeRecord>, Discv4Error>
pub async fn lookup_random(&self) -> Result<Vec<NodeRecord>, Discv4Error>
Performs a random lookup for node records.
Sourcepub fn send_lookup(&self, node_id: PeerId)
pub fn send_lookup(&self, node_id: PeerId)
Sends a message to the service to lookup the closest nodes
Sourcepub fn send_lookup_self(&self)
pub fn send_lookup_self(&self)
Triggers a new self lookup without expecting a response
Sourcepub fn remove_peer(&self, node_id: PeerId)
pub fn remove_peer(&self, node_id: PeerId)
Removes the peer from the table, if it exists.
Sourcepub fn add_node(&self, node_record: NodeRecord)
pub fn add_node(&self, node_record: NodeRecord)
Adds the node to the table, if it is not already present.
Sourcepub fn ban(&self, node_id: PeerId, ip: IpAddr)
pub fn ban(&self, node_id: PeerId, ip: IpAddr)
Adds the peer and id to the ban list.
This will prevent any future inclusion in the table
Sourcepub fn ban_ip(&self, ip: IpAddr)
pub fn ban_ip(&self, ip: IpAddr)
Adds the ip to the ban list.
This will prevent any future inclusion in the table
Sourcepub fn ban_node(&self, node_id: PeerId)
pub fn ban_node(&self, node_id: PeerId)
Adds the peer to the ban list.
This will prevent any future inclusion in the table
Sourcepub fn set_tcp_port(&self, port: u16)
pub fn set_tcp_port(&self, port: u16)
Sets the tcp port
This will update our NodeRecord
’s tcp port.
Sourcepub fn set_eip868_rlp_pair(&self, key: Vec<u8>, rlp: Bytes)
pub fn set_eip868_rlp_pair(&self, key: Vec<u8>, rlp: Bytes)
Sets the pair in the EIP-868 [Enr
] of the node.
If the key already exists, this will update it.
CAUTION: The value must be rlp encoded
Sourcepub fn set_eip868_rlp(&self, key: Vec<u8>, value: impl Encodable)
pub fn set_eip868_rlp(&self, key: Vec<u8>, value: impl Encodable)
Sets the pair in the EIP-868 [Enr
] of the node.
If the key already exists, this will update it.
Sourcepub async fn update_stream(
&self,
) -> Result<ReceiverStream<DiscoveryUpdate>, Discv4Error>
pub async fn update_stream( &self, ) -> Result<ReceiverStream<DiscoveryUpdate>, Discv4Error>
Returns the receiver half of new listener channel that streams DiscoveryUpdate
s.
Sourcepub fn terminate(&self)
pub fn terminate(&self)
Terminates the spawned Discv4Service
.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Discv4
impl !RefUnwindSafe for Discv4
impl Send for Discv4
impl Sync for Discv4
impl Unpin for Discv4
impl !UnwindSafe for Discv4
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
impl<T> ErasedDestructor for Twhere
T: 'static,
impl<T> MaybeSendSync for T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 48 bytes