Struct reth_discv4::Discv4

source ·
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

source

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()

source

pub fn noop() -> Self

Available on crate feature test-utils only.

Returns a new instance with the given channel directly

NOTE: this is only intended for test setups.

source

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();
source

pub const fn local_addr(&self) -> SocketAddr

Returns the address of the UDP socket.

source

pub fn node_record(&self) -> NodeRecord

Returns the NodeRecord of the local node.

This includes the currently tracked external IP address of the node.

source

pub fn external_ip(&self) -> IpAddr

Returns the currently tracked external IP of the node.

source

pub fn set_lookup_interval(&self, duration: Duration)

Sets the [Interval] used for periodically looking up targets over the network

source

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.

source

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.

source

pub async fn lookup_random(&self) -> Result<Vec<NodeRecord>, Discv4Error>

Performs a random lookup for node records.

source

pub fn send_lookup(&self, node_id: PeerId)

Sends a message to the service to lookup the closest nodes

source

pub fn send_lookup_self(&self)

Triggers a new self lookup without expecting a response

source

pub fn remove_peer(&self, node_id: PeerId)

Removes the peer from the table, if it exists.

source

pub fn add_node(&self, node_record: NodeRecord)

Adds the node to the table, if it is not already present.

source

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

source

pub fn ban_ip(&self, ip: IpAddr)

Adds the ip to the ban list.

This will prevent any future inclusion in the table

source

pub fn ban_node(&self, node_id: PeerId)

Adds the peer to the ban list.

This will prevent any future inclusion in the table

source

pub fn set_tcp_port(&self, port: u16)

Sets the tcp port

This will update our NodeRecord’s tcp port.

source

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

source

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.

source

pub async fn update_stream( &self, ) -> Result<ReceiverStream<DiscoveryUpdate>, Discv4Error>

Returns the receiver half of new listener channel that streams DiscoveryUpdates.

source

pub fn terminate(&self)

Terminates the spawned Discv4Service.

Trait Implementations§

source§

impl Clone for Discv4

source§

fn clone(&self) -> Discv4

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Discv4

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dst. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more

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