Documentation
¶
Overview ¶
Package discv5 implements the Ethereum Discovery v5 protocol.
This is a generic discv5 implementation providing:
- UDP transport for network communication
- Session management for encrypted communication
- Protocol handler for message processing
- ENR (Ethereum Node Record) support
Index ¶
- Variables
- type Config
- type Service
- func (s *Service) FindNode(n *node.Node, distances []uint) ([]*node.Node, error)
- func (s *Service) Handler() *protocol.Handler
- func (s *Service) LocalNode() *node.Node
- func (s *Service) Ping(n *node.Node) error
- func (s *Service) Sessions() *session.Cache
- func (s *Service) Start() error
- func (s *Service) Stop() error
- func (s *Service) TalkReq(n *node.Node, protocolName string, request []byte) ([]byte, error)
- type Transport
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingPrivateKey = fmt.Errorf("private key is required") ErrInvalidPort = fmt.Errorf("invalid port number") ErrAlreadyRunning = fmt.Errorf("service is already running") ErrNotRunning = fmt.Errorf("service is not running") )
Common errors
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Service context
Context context.Context
// LocalNode is the pre-created local node (optional, preferred)
// If provided, this takes precedence over LocalENR and ENR-related config
LocalNode *node.Node
// PrivateKey is the node's private key (required if LocalNode is nil)
PrivateKey *ecdsa.PrivateKey
// ENRIP is the IPv4 address to advertise in the ENR (optional)
ENRIP net.IP
// ENRIP6 is the IPv6 address to advertise in the ENR (optional)
ENRIP6 net.IP
// ENRPort is the UDP port to advertise in the ENR (optional)
// If not specified, the port will be obtained from the transport layer
ENRPort int
// ETH2Data is the eth2 field to include in the ENR (optional)
// This should be the 16-byte encoded eth2 field containing fork digest and next fork info
ETH2Data []byte
// LocalENR is an already-initialized ENR to use for this node (optional)
// If provided, this ENR will be used instead of creating a new one.
// The higher-level service is responsible for loading, creating, and persisting this ENR.
LocalENR *enr.Record
// OnHandshakeComplete is called when a handshake completes successfully
OnHandshakeComplete protocol.OnHandshakeCompleteCallback
// OnNodeUpdate is called when a node's ENR is updated
OnNodeUpdate protocol.OnNodeUpdateCallback
// OnNodeSeen is called when a node is seen (receives a message)
OnNodeSeen protocol.OnNodeSeenCallback
// OnFindNode is called when a FINDNODE request is received
OnFindNode protocol.OnFindNodeCallback
// OnTalkReq is called when a TALKREQ request is received
OnTalkReq protocol.OnTalkReqCallback
// OnPongReceived is called when a PONG response is received
OnPongReceived protocol.OnPongReceivedCallback
// SessionLifetime is how long sessions remain valid (default 30 minutes)
SessionLifetime time.Duration
// MaxSessions is the maximum number of cached sessions (default 1000)
MaxSessions int
// Logger for debug messages
Logger logrus.FieldLogger
}
Config contains configuration for the discv5 service.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the main discv5 service.
It provides a minimal, generic discv5 implementation that can be extended by higher-level services (e.g., beacon bootnode).
func New ¶
New creates a new discv5 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:9000",
})
privKey, _ := crypto.GenerateKey()
config := DefaultConfig()
config.PrivateKey = privKey
service, err := New(config, transport)
if err != nil {
log.Fatal(err)
}
defer service.Stop()
func (*Service) FindNode ¶
FindNode sends a FINDNODE request to a node and returns the discovered nodes.
The distances parameter specifies which distance buckets to query (0-255). Use distance 0 to request the node's own ENR.
Returns a slice of discovered nodes, or an error if the request fails.
Example:
// Find nodes at distance 256 (all nodes)
nodes, err := service.FindNode(targetNode, []uint{256})
if err != nil {
log.Printf("findnode failed: %v", err)
}
func (*Service) Handler ¶
Handler returns the protocol handler.
This allows higher-level services to access the handler for sending requests and managing the protocol.
func (*Service) Ping ¶
Ping sends a PING request to a node and waits for a PONG response.
Returns an error if the ping fails or times out.
Example:
if err := service.Ping(targetNode); err != nil {
log.Printf("ping failed: %v", err)
}
func (*Service) Start ¶
Start starts the discv5 service.
Note: The transport is started separately and passed to New(). This method is kept for compatibility and lifecycle management.
func (*Service) Stop ¶
Stop stops the discv5 service.
Note: This does NOT close the transport as it's managed externally. The caller is responsible for closing the transport.
func (*Service) TalkReq ¶
TalkReq sends a TALKREQ request to a node and returns the response.
The protocolName parameter identifies the application protocol. The request parameter contains the application-specific request data.
Returns the response data, or an error if the request fails.
Example:
resp, err := service.TalkReq(targetNode, "my-protocol", []byte("my-request"))
if err != nil {
log.Printf("talkreq failed: %v", err)
}
type Transport ¶ added in v0.0.2
type Transport interface {
protocol.Transport
LocalAddr() *net.UDPAddr
AddHandler(handler func(data []byte, from *net.UDPAddr, localAddr *net.UDPAddr) bool)
AddHandlerFor(protocol string, handler func(data []byte, from *net.UDPAddr, localAddr *net.UDPAddr) bool)
}
Transport is the interface for sending packets. It must have a LocalAddr() method to get the bind address.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package node provides core types for representing network nodes.
|
Package node provides core types for representing network nodes. |
|
Package protocol implements the discv5 wire protocol message types and codec.
|
Package protocol implements the discv5 wire protocol message types and codec. |
|
Package session implements session management and encryption for discv5.
|
Package session implements session management and encryption for discv5. |