Skip to main content

Purpose and Scope

This page explains the peer discovery mechanisms used in the P2P networking layer of StableNet.
Peer discovery is the process by which a node identifies other nodes in the network and obtains candidates for connection, which are then passed to the P2P server’s connection management logic.
This document covers the Discovery v4 and Discovery v5 protocols, Ethereum Node Records (ENR), bootstrap nodes, and DNS-based discovery.
For connection management after discovery, see P2P Server and Peer Management.
For data exchange and synchronization with discovered peers, see Chain Synchronization.

Discovery Architecture Overview

StableNet does not rely on a single method, but instead uses multiple discovery mechanisms in parallel to build a pool of peer candidates. Discovery mechanisms:
  • Discovery V4: Basic peer discovery using a Kademlia-based DHT
  • Discovery V5: An extended protocol with topic advertisements and enhanced cryptography
  • DNS Discovery: Tree-based discovery using DNS TXT records
  • Bootstrap Nodes: Static seed nodes for initial connectivity
  • Protocol-specific sources: Custom iterators provided by specific protocols
Discovery mixer: enode.FairMix fairly mixes node candidates collected from multiple discovery sources.
It iterates over each source in a round-robin manner to prevent any single source from monopolizing the candidate pool, and applies per-source timeouts.

Discovery V4 Protocol

Discovery V4 is a UDP-based Kademlia DHT protocol that maintains a routing table using an XOR distance metric.
Nodes explore the network by issuing iterative queries to discover arbitrary peers.

V4 Protocol Operations

OperationPacket TypePurpose
PING0x01Check node liveness and exchange endpoints
PONG0x02Respond to PING and confirm endpoint
FINDNODE0x03Request nodes close to a target ID
NEIGHBORS0x04Return up to 16 node records
ENRREQUEST0x05Request the latest ENR (EIP-868)
ENRRESPONSE0x06Respond with an ENR

V4 Configuration and Setup

The Discovery V4 service is configured via p2p.Config and started during the server initialization phase in setupDiscovery(). Initialization process:
  1. Create a UDP socket
  2. Build a discover.Config including the private key, network restrictions, and bootstrap nodes
  3. Call discover.ListenV4() to start the V4 service
  4. Register the ntab.RandomNodes() iterator with the discovery mixer

Discovery V5 Protocol

Discovery V5 is an extension of V4 that provides topic-based discovery and stronger cryptography.
It supports both random node discovery and searching for peers interested in specific topics.

V5 Enhancements

  • Topic advertisements: Nodes can advertise and search for interest-based topics
  • Session keys: Reduced handshake cost for repeated communication
  • ENR updates: More efficient ENR refresh mechanisms
  • Improved NAT handling: Better external endpoint estimation
  • Packet encryption: Authenticated encryption applied to all packets

V5 Configuration

Discovery V5 uses a separate list of bootstrap nodes and can be enabled alongside V4. Shared UDP socket: When V4 and V5 are both enabled, they share the same UDP port.
Packets not handled by V4 are forwarded to V5, preventing conflicts.

Ethereum Node Records (ENR)

ENR is a signed data structure that contains a node’s identity and connectivity information.
Both Discovery V4 and V5 use ENRs to exchange node metadata.

ENR Structure

Core fields:
  • Node ID: Identifier derived from the public key
  • Sequence Number: Incremented on each ENR update
  • Signature: Verifies record ownership
  • Key-Value Pairs: Extensible set of attributes
Common key-value entries:
KeyTypeDescription
idstringIdentity scheme (usually v4)
secp256k1bytesCompressed public key
ipbytesIPv4 address
ip6bytesIPv6 address
tcpuint16RLPx TCP port
udpuint16UDP discovery port
eth[chainID, forkHash, forkNext]Chain information
enode.LocalNode manages the local node’s ENR and automatically updates it when network conditions change.

Bootstrap Nodes

Bootstrap nodes are seed nodes used to initiate peer discovery when joining the network.
They provide the first entry point when a local node has no knowledge of other peers.

Bootstrap Configuration

  • Static specification via configuration files
  • Specification via command-line flags
    • --bootnodes for V4 bootstrap nodes
    • --bootnodesv5 for V5 bootstrap nodes
Requirements:
  • Publicly reachable endpoints
  • High availability
  • Typically operated by the network maintainers

DNS Discovery

DNS Discovery distributes ENR information encoded in DNS TXT records.
This allows bootstrap lists to be updated dynamically without changing node software.

DNS Discovery Tree Structure

DNS-based discovery is organized as a Merkle tree.
Record TypeDescription
RootTree entry point and signature information
BranchLinks to child nodes
ENRActual node records
LinkReferences to external trees
The DNS client traverses the tree, extracts ENR records, and passes them to the discovery mixer.

Integration with the P2P Server

All discovery mechanisms are initialized in setupDiscovery() and integrated via enode.FairMix.
Discovered node candidates are forwarded to the dial scheduler, which attempts actual connections.

Fair Mixing Algorithm

FairMix follows these rules:
  1. Iterate over active sources in round-robin order
  2. Apply a maximum wait time per source
  3. Skip unresponsive sources and move to the next
  4. Periodically retry failed sources

Discovery Metrics

The discovery subsystem maintains the following state:
  • Discovery V4 routing table
  • Discovery V5 service state
  • Discovery mixer state
  • Local ENR
  • Persistent node database
Discovery results are passed to the dial scheduler, which limits connection attempts and tracks failures to prevent retry storms.

Summary

StableNet’s peer discovery system is designed as a multi-layered architecture resilient to network changes.
  1. Discovery V4 provides stable, DHT-based random discovery
  2. Discovery V5 adds topic-based search and enhanced security
  3. ENR enables extensible, signed node information exchange
  4. Bootstrap Nodes provide initial network entry points
  5. DNS Discovery enables dynamic and flexible bootstrapping
All of these mechanisms are combined through FairMix to continuously supply a balanced set of peer candidates to the dial scheduler.