discover

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package discover implements node discovery operations for discv5.

The discover package provides:

  • Iterative FINDNODE lookups
  • PING/PONG operations
  • Random walks for table maintenance
  • ENR-based filtering during discovery

Index

Constants

View Source
const DefaultAlpha = 3

DefaultAlpha is the concurrency factor for lookups (Kademlia parameter).

View Source
const DefaultLookupTimeout = 30 * time.Second

DefaultLookupTimeout is the timeout for a complete lookup operation.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// LocalNode is our node information
	LocalNode *node.Node

	// Table is the routing table
	Table *table.FlatTable

	// Handler is the protocol handler
	Handler *protocol.Handler

	// Alpha is the concurrency factor (default 3)
	Alpha int

	// LookupTimeout is the timeout for lookup operations
	LookupTimeout time.Duration

	// OnNodeFound is called when a new node is discovered during lookup
	// The callback should handle admission checks and add the node if valid
	OnNodeFound func(*node.Node) bool

	// Logger for debug messages
	Logger logrus.FieldLogger
}

Config contains configuration for the lookup service.

type LookupService

type LookupService struct {
	// contains filtered or unexported fields
}

LookupService manages node discovery operations.

func NewLookupService

func NewLookupService(cfg Config) *LookupService

NewLookupService creates a new lookup service.

func (*LookupService) CleanupQueryHistory

func (ls *LookupService) CleanupQueryHistory(maxAge time.Duration) int

CleanupQueryHistory removes stale entries from the query history. This should be called periodically to prevent unbounded growth. Entries older than the given duration are removed.

func (*LookupService) GetStats

func (ls *LookupService) GetStats() LookupStats

GetStats returns discovery statistics.

func (*LookupService) Lookup

func (ls *LookupService) Lookup(ctx context.Context, target node.ID, k int) ([]*node.Node, error)

Lookup performs a node lookup by querying random nodes from the table.

Since we use a flat table without buckets, we simply:

  • Select 3 semi-random nodes (preferring alive ones)
  • Query them concurrently for nodes near the target
  • Add discovered nodes via callback

Parameters:

  • ctx: Context for cancellation and timeout
  • target: The target node ID to find
  • k: The number of closest nodes to return

Returns discovered nodes that were added to the table.

func (*LookupService) LookupWithFilter

func (ls *LookupService) LookupWithFilter(ctx context.Context, target node.ID, k int, filter enr.ENRFilter) ([]*node.Node, error)

LookupWithFilter performs a lookup with an ENR filter.

Only nodes that pass the filter are included in the results.

func (*LookupService) RandomWalk

func (ls *LookupService) RandomWalk(ctx context.Context) ([]*node.Node, error)

RandomWalk performs a random walk to discover new nodes.

This is used for routing table maintenance and exploring the network.

type LookupStats

type LookupStats struct {
	LookupsStarted   int
	LookupsCompleted int
	LookupsFailed    int
	NodesDiscovered  int
}

GetStats returns statistics about discovery operations.

type PingService

type PingService struct {
	// contains filtered or unexported fields
}

PingService handles PING/PONG operations for liveness checks.

func NewPingService

func NewPingService(handler *protocol.Handler, logger logrus.FieldLogger) *PingService

NewPingService creates a new ping service.

func (*PingService) GetStats

func (ps *PingService) GetStats() PingStats

GetStats returns PING statistics.

func (*PingService) Ping

func (ps *PingService) Ping(n *node.Node) (bool, time.Duration, error)

Ping sends a PING to a node and waits for PONG.

Returns true if the node responded, false on timeout. Also updates the node's RTT statistics.

func (*PingService) PingMultiple

func (ps *PingService) PingMultiple(nodes []*node.Node) map[node.ID]bool

PingMultiple sends PINGs to multiple nodes in parallel.

Returns a map of node ID to ping result (success/failure).

type PingStats

type PingStats struct {
	PingsSent     int
	PongsReceived int
	PingTimeouts  int
	AverageRTT    time.Duration
	SuccessRate   float64
}

PingStats returns statistics about PING operations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL