Documentation
¶
Overview ¶
Package discv4 implements the Ethereum Discovery v4 protocol.
Discovery v4 is a UDP-based protocol for finding peers in the Ethereum network. It uses a Kademlia-style DHT for distributed node discovery with a bond mechanism to prevent amplification attacks.
Key features:
- PING/PONG for liveness and endpoint verification
- FINDNODE/NEIGHBORS for peer discovery
- ENRREQUEST/ENRRESPONSE for ENR record exchange (EIP-868)
- Bond mechanism (PING/PONG required before FINDNODE)
- No encryption (packets are signed but not encrypted)
Index ¶
- func ParseNodeFromENR(record *enr.Record, addr *net.UDPAddr) (*node.Node, error)
- func ParseNodeFromEnode(enodeURL string) (*node.Node, error)
- type Config
- type ConfigBuilder
- func (b *ConfigBuilder) Build() (*Config, error)
- func (b *ConfigBuilder) MustBuild() *Config
- func (b *ConfigBuilder) WithBondExpiration(d time.Duration) *ConfigBuilder
- func (b *ConfigBuilder) WithExpirationWindow(d time.Duration) *ConfigBuilder
- func (b *ConfigBuilder) WithLocalENR(record *enr.Record) *ConfigBuilder
- func (b *ConfigBuilder) WithOnENRRequest(cb protocol.OnENRRequestCallback) *ConfigBuilder
- func (b *ConfigBuilder) WithOnFindnode(cb protocol.OnFindnodeCallback) *ConfigBuilder
- func (b *ConfigBuilder) WithOnNodeSeen(cb protocol.OnNodeSeenCallback) *ConfigBuilder
- func (b *ConfigBuilder) WithOnPing(cb protocol.OnPingCallback) *ConfigBuilder
- func (b *ConfigBuilder) WithOnPongReceived(cb protocol.OnPongReceivedCallback) *ConfigBuilder
- func (b *ConfigBuilder) WithPrivateKey(key *ecdsa.PrivateKey) *ConfigBuilder
- func (b *ConfigBuilder) WithRequestTimeout(d time.Duration) *ConfigBuilder
- type Service
- func (s *Service) AddBootstrapNode(enodeURL string) error
- func (s *Service) AddNode(n *node.Node)
- func (s *Service) AllNodes() []*node.Node
- func (s *Service) BondedNodes() []*node.Node
- func (s *Service) Findnode(n *node.Node, target []byte) ([]*node.Node, error)
- func (s *Service) GetNode(id node.ID) *node.Node
- func (s *Service) IsRunning() bool
- func (s *Service) LocalAddr() *net.UDPAddr
- func (s *Service) LocalENR() *enr.Record
- func (s *Service) LocalEnode() string
- func (s *Service) LocalNodeID() node.ID
- func (s *Service) LookupRandom() ([]*node.Node, error)
- func (s *Service) Ping(n *node.Node) (*protocol.Pong, error)
- func (s *Service) PingAddr(enodeURL string) (*protocol.Pong, error)
- func (s *Service) RequestENR(n *node.Node) (*enr.Record, error)
- func (s *Service) SetLocalENR(record *enr.Record)
- func (s *Service) Start() error
- func (s *Service) Stats() map[string]interface{}
- func (s *Service) Stop() error
- type Transport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseNodeFromENR ¶
ParseNodeFromENR creates a node from an ENR record.
Types ¶
type Config ¶
type Config struct {
// PrivateKey is the node's private key (required)
PrivateKey *ecdsa.PrivateKey
// LocalENR is the node's ENR record (optional)
// If provided, it will be shared via ENRRESPONSE
LocalENR *enr.Record
// BondExpiration is how long bonds last (default: 24 hours)
// Bonds must be refreshed via PING/PONG before expiration
BondExpiration time.Duration
// RequestTimeout is how long to wait for request responses (default: 500ms)
RequestTimeout time.Duration
// ExpirationWindow is the acceptable time range for packet expiration (default: 20s)
// Packets with expiration outside this window are rejected
ExpirationWindow time.Duration
// OnPing is called when a PING request is received
OnPing protocol.OnPingCallback
// OnFindnode is called when a FINDNODE request is received
// Should return the list of nodes to include in the NEIGHBORS response
OnFindnode protocol.OnFindnodeCallback
// OnENRRequest is called when an ENRREQUEST is received
OnENRRequest protocol.OnENRRequestCallback
// OnNodeSeen is called when we receive any valid packet from a node
// Useful for tracking last_seen timestamps in a database
OnNodeSeen protocol.OnNodeSeenCallback
// OnPongReceived is called when a PONG response is received
// Contains our external IP/port as seen by the remote peer
OnPongReceived protocol.OnPongReceivedCallback
}
Config contains configuration for the discv4 service.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a configuration with sensible defaults.
You must set PrivateKey before using.
func (*Config) ApplyDefaults ¶
func (c *Config) ApplyDefaults()
ApplyDefaults applies default values to unset configuration fields.
type ConfigBuilder ¶
type ConfigBuilder struct {
// contains filtered or unexported fields
}
ConfigBuilder provides a fluent interface for building configurations.
func NewConfigBuilder ¶
func NewConfigBuilder() *ConfigBuilder
NewConfigBuilder creates a new configuration builder.
func (*ConfigBuilder) Build ¶
func (b *ConfigBuilder) Build() (*Config, error)
Build returns the built configuration.
func (*ConfigBuilder) MustBuild ¶
func (b *ConfigBuilder) MustBuild() *Config
MustBuild returns the built configuration or panics on error.
func (*ConfigBuilder) WithBondExpiration ¶
func (b *ConfigBuilder) WithBondExpiration(d time.Duration) *ConfigBuilder
WithBondExpiration sets the bond expiration duration.
func (*ConfigBuilder) WithExpirationWindow ¶
func (b *ConfigBuilder) WithExpirationWindow(d time.Duration) *ConfigBuilder
WithExpirationWindow sets the expiration window.
func (*ConfigBuilder) WithLocalENR ¶
func (b *ConfigBuilder) WithLocalENR(record *enr.Record) *ConfigBuilder
WithLocalENR sets the local ENR record.
func (*ConfigBuilder) WithOnENRRequest ¶
func (b *ConfigBuilder) WithOnENRRequest(cb protocol.OnENRRequestCallback) *ConfigBuilder
WithOnENRRequest sets the ENRREQUEST callback.
func (*ConfigBuilder) WithOnFindnode ¶
func (b *ConfigBuilder) WithOnFindnode(cb protocol.OnFindnodeCallback) *ConfigBuilder
WithOnFindnode sets the FINDNODE callback.
func (*ConfigBuilder) WithOnNodeSeen ¶
func (b *ConfigBuilder) WithOnNodeSeen(cb protocol.OnNodeSeenCallback) *ConfigBuilder
WithOnNodeSeen sets the OnNodeSeen callback.
func (*ConfigBuilder) WithOnPing ¶
func (b *ConfigBuilder) WithOnPing(cb protocol.OnPingCallback) *ConfigBuilder
WithOnPing sets the PING callback.
func (*ConfigBuilder) WithOnPongReceived ¶
func (b *ConfigBuilder) WithOnPongReceived(cb protocol.OnPongReceivedCallback) *ConfigBuilder
WithOnPongReceived sets the OnPongReceived callback.
func (*ConfigBuilder) WithPrivateKey ¶
func (b *ConfigBuilder) WithPrivateKey(key *ecdsa.PrivateKey) *ConfigBuilder
WithPrivateKey sets the private key.
func (*ConfigBuilder) WithRequestTimeout ¶
func (b *ConfigBuilder) WithRequestTimeout(d time.Duration) *ConfigBuilder
WithRequestTimeout sets the request timeout.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service represents a discv4 service instance.
The service manages:
- Protocol handler
- Node database
- Request routing
func New ¶
New creates a new discv4 service.
The transport must be created first and passed to New(). The service will register its packet handler with the transport.
Example:
transport, _ := transport.NewUDPTransport(&transport.Config{
ListenAddr: "0.0.0.0:30303",
})
privKey, _ := crypto.GenerateKey()
config := DefaultConfig()
config.PrivateKey = privKey
service, err := New(config, transport)
if err != nil {
log.Fatal(err)
}
defer service.Stop()
func (*Service) AddBootstrapNode ¶
AddBootstrapNode adds a bootstrap node from an enode URL.
func (*Service) AddNode ¶
AddNode adds a node to the known nodes list.
This is useful for adding bootstrap nodes.
func (*Service) BondedNodes ¶
BondedNodes returns all bonded nodes.
func (*Service) Findnode ¶
Findnode sends a FINDNODE request to discover nodes near a target.
The node must have an active bond (will automatically PING if needed). Returns a list of discovered nodes.
func (*Service) LocalEnode ¶
LocalEnode returns the local enode:// URL.
func (*Service) LocalNodeID ¶
LocalNodeID returns the local node ID.
func (*Service) LookupRandom ¶
LookupRandom performs a random node lookup.
This is useful for populating the routing table with random nodes.
func (*Service) Ping ¶
Ping sends a PING request to a node.
This establishes a bond with the node and verifies liveness. Returns the PONG response or an error.
func (*Service) PingAddr ¶
PingAddr sends a PING to a node identified by address.
This is a convenience method that creates a temporary node from an enode URL.
func (*Service) RequestENR ¶
RequestENR requests a node's ENR record.
Returns the ENR record or an error.
func (*Service) SetLocalENR ¶
SetLocalENR updates the local ENR record.
This should be called when the ENR is updated (e.g., IP change).
func (*Service) Start ¶
Start starts the discv4 service.
Note: The transport is started separately and passed to New(). This method is kept for compatibility and lifecycle management.