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
- type Config
- type LookupService
- func (ls *LookupService) CleanupQueryHistory(maxAge time.Duration) int
- func (ls *LookupService) GetStats() LookupStats
- func (ls *LookupService) Lookup(ctx context.Context, target node.ID, k int) ([]*node.Node, error)
- func (ls *LookupService) LookupWithFilter(ctx context.Context, target node.ID, k int, filter enr.ENRFilter) ([]*node.Node, error)
- func (ls *LookupService) RandomWalk(ctx context.Context) ([]*node.Node, error)
- type LookupStats
- type PingService
- type PingStats
Constants ¶
const DefaultAlpha = 3
DefaultAlpha is the concurrency factor for lookups (Kademlia parameter).
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 ¶
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 ¶
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 ¶
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 ¶
PingMultiple sends PINGs to multiple nodes in parallel.
Returns a map of node ID to ping result (success/failure).