Skip to content

discv4::discv4_client

Discovery v4 protocol client. More...

#include <discv4_client.hpp>

Public Functions

Name
discv4_client(asio::io_context & io_context, const discv4Config & config)
~discv4_client()
rlpx::VoidResult start()
void stop()
discv4::Result< discv4_pong > ping(const std::string & ip, uint16_t port, const NodeId & node_id, boost::asio::yield_context yield)
Send PING to a specific node.
rlpx::VoidResult find_node(const std::string & ip, uint16_t port, const NodeId & target_id, boost::asio::yield_context yield)
Send FIND_NODE to discover peers near a target.
discv4::Result< discv4_enr_response > request_enr(const std::string & ip, uint16_t port, boost::asio::yield_context yield)
Send ENRRequest to a bonded peer and return the raw ENR record bytes.
std::vector< DiscoveredPeer > get_peers() const
void set_peer_discovered_callback(PeerDiscoveredCallback callback)
void set_error_callback(ErrorCallback callback)
const NodeId & local_node_id() const
uint16_t bound_port() const
Return the local UDP port the socket is bound to. Useful in tests where bind_port=0 (OS-assigned ephemeral port).

Detailed Description

class discv4::discv4_client;

Discovery v4 protocol client.

Implements the Ethereum Discovery v4 protocol for peer discovery. Uses UDP for communication with bootstrap nodes and discovered peers.

Protocol flow:

  1. Send PING to bootstrap nodes
  2. Receive PONG responses
  3. Send FIND_NODE to discover more peers
  4. Receive NEIGHBOURS responses with peer lists
  5. Maintain K-bucket routing table

Public Functions Documentation

function discv4_client

explicit discv4_client(
    asio::io_context & io_context,
    const discv4Config & config
)

function ~discv4_client

~discv4_client()

function start

rlpx::VoidResult start()

function stop

void stop()

function ping

discv4::Result< discv4_pong > ping(
    const std::string & ip,
    uint16_t port,
    const NodeId & node_id,
    boost::asio::yield_context yield
)

Send PING to a specific node.

Parameters:

  • ip Target node IP address.
  • port Target node UDP port.
  • node_id Target node identifier.
  • yield Boost.Asio stackful coroutine context.

function find_node

rlpx::VoidResult find_node(
    const std::string & ip,
    uint16_t port,
    const NodeId & target_id,
    boost::asio::yield_context yield
)

Send FIND_NODE to discover peers near a target.

Parameters:

  • ip Target node IP address.
  • port Target node UDP port.
  • target_id Target node identifier to search near.
  • yield Boost.Asio stackful coroutine context.

function request_enr

discv4::Result< discv4_enr_response > request_enr(
    const std::string & ip,
    uint16_t port,
    boost::asio::yield_context yield
)

Send ENRRequest to a bonded peer and return the raw ENR record bytes.

Parameters:

  • ip Target node IP address.
  • port Target node UDP port.
  • yield Boost.Asio stackful coroutine context.

Return: Parsed ENRResponse on success, error on timeout or parse failure.

The peer must already be bonded (ping/pong complete) before calling this. Mirrors go-ethereum UDPv4::RequestENR().

function get_peers

std::vector< DiscoveredPeer > get_peers() const

function set_peer_discovered_callback

void set_peer_discovered_callback(
    PeerDiscoveredCallback callback
)

function set_error_callback

void set_error_callback(
    ErrorCallback callback
)

function local_node_id

inline const NodeId & local_node_id() const

function bound_port

inline uint16_t bound_port() const

Return the local UDP port the socket is bound to. Useful in tests where bind_port=0 (OS-assigned ephemeral port).


Updated on 2026-04-13 at 23:22:46 -0700