discovery

package
v0.5.0-rc.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: GPL-3.0 Imports: 37 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// DHTDiscoveryProtocol is the protocol prefix used for local discovery (routingTbl), in addition to mDNS
	DHTDiscoveryProtocol = "/ssv/discovery/0.0.1"
)
View Source
const (
	// LocalDiscoveryServiceTag is used in our mDNS advertisements to discover other peers
	LocalDiscoveryServiceTag = "ssv.discovery"
)

Variables

This section is empty.

Functions

func NewKadDHT

func NewKadDHT(ctx context.Context, host host.Host, mode dht.ModeOpt) (routing.Routing, discovery.Discovery, error)

NewKadDHT creates a new kademlia DHT and a corresponding discovery service NOTE: that the caller must bootstrap the routing.Routing instance

func ParseENR

func ParseENR(schemes enr.SchemeMap, tcpRequired bool, enrs ...string) ([]*enode.Node, error)

ParseENR takes a list of ENR strings and returns the corresponding enode.Node objects. it also accepts custom schemes, defaults to enode.ValidSchemes (v4)

func PeerID

func PeerID(node *enode.Node) (peer.ID, error)

PeerID returns the peer id of the node

func ToMultiAddr

func ToMultiAddr(node *enode.Node) (ma.Multiaddr, error)

ToMultiAddr returns the node's multiaddr.

func ToPeer

func ToPeer(node *enode.Node) (*peer.AddrInfo, error)

ToPeer creates peer info from the given node

Types

type Bootnode

type Bootnode struct {
	ENR string // Ethereum Node Records https://eips.ethereum.org/EIPS/eip-778
	// contains filtered or unexported fields
}

Bootnode represents a bootnode used for tests

func NewBootnode

func NewBootnode(pctx context.Context, logger *zap.Logger, opts *BootnodeOptions) (*Bootnode, error)

NewBootnode creates a new bootnode

func (*Bootnode) Close

func (b *Bootnode) Close() error

Close implements io.Closer

type BootnodeOptions

type BootnodeOptions struct {
	PrivateKey string `yaml:"PrivateKey" env:"BOOTNODE_NETWORK_KEY" env-description:"Bootnode private key (default will generate new)"`
	ExternalIP string `yaml:"ExternalIP" env:"BOOTNODE_EXTERNAL_IP" env-description:"Override boot node's IP' "`
	Port       int    `yaml:"Port" env:"BOOTNODE_PORT" env-description:"Override boot node's port' "`
}

BootnodeOptions contains options to create the node

type CheckPeerLimit

type CheckPeerLimit func() bool

CheckPeerLimit enables listener to check peers limit

type DiscV5Options

type DiscV5Options struct {
	// StoragePath is the path used to store the DB (DHT)
	// if an empty path was given, the DB will be created in memory
	StoragePath string
	// IP of the node
	IP string
	// BindIP is the IP to bind to the UDP listener
	BindIP string
	// Port is the UDP port used by discv5
	Port int
	// TCPPort is the TCP port exposed in the ENR
	TCPPort int
	// NetworkKey is the private key used to create the peer.ID if the node
	NetworkKey *ecdsa.PrivateKey
	// Bootnodes is a list of bootstrapper nodes
	Bootnodes []string
	// Subnets is a bool slice represents all the subnets the node is intreseted in
	Subnets []byte
	// OperatorID is the operator id (optional)
	OperatorID string
	// EnableLogging when true enables logs to be emitted
	EnableLogging bool
}

DiscV5Options for creating a new discv5 listener

func DefaultOptions

func DefaultOptions(privateKey *ecdsa.PrivateKey) DiscV5Options

DefaultOptions returns the default options

func (*DiscV5Options) DiscV5Cfg

func (opts *DiscV5Options) DiscV5Cfg(logger *zap.Logger) (*discover.Config, error)

DiscV5Cfg creates discv5 config from the options

func (*DiscV5Options) IPs

func (opts *DiscV5Options) IPs() (net.IP, net.IP, string)

IPs returns the external ip and bind ip

func (*DiscV5Options) Validate

func (opts *DiscV5Options) Validate() error

Validate validates the options

type DiscV5Service

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

DiscV5Service wraps discover.UDPv5 with additional functionality it implements go-libp2p/core/discovery.Discovery currently using ENR entry (subnets) to facilitate subnets discovery TODO: should be changed once discv5 supports topics (v5.2)

func (*DiscV5Service) Advertise

func (dvs *DiscV5Service) Advertise(ctx context.Context, ns string, opt ...discovery.Option) (time.Duration, error)

Advertise advertises a service implementation of discovery.Advertiser

func (*DiscV5Service) Bootstrap

func (dvs *DiscV5Service) Bootstrap(logger *zap.Logger, handler HandleNewPeer) error

Bootstrap start looking for new nodes, note that this function blocks. if we reached peers limit, make sure to accept peers with more than 1 shared subnet, which lets other components to determine whether we'll want to connect to this node or not.

func (*DiscV5Service) Close

func (dvs *DiscV5Service) Close() error

Close implements io.Closer

func (*DiscV5Service) DeregisterSubnets

func (dvs *DiscV5Service) DeregisterSubnets(logger *zap.Logger, subnets ...int) error

DeregisterSubnets removes the given subnets and publish the updated node record

func (*DiscV5Service) FindPeers

func (dvs *DiscV5Service) FindPeers(ctx context.Context, ns string, opt ...discovery.Option) (<-chan peer.AddrInfo, error)

FindPeers discovers peers providing a service implementation of discovery.Discoverer

func (*DiscV5Service) Node

func (dvs *DiscV5Service) Node(logger *zap.Logger, info peer.AddrInfo) (*enode.Node, error)

Node tries to find the enode.Node of the given peer

func (*DiscV5Service) RegisterSubnets

func (dvs *DiscV5Service) RegisterSubnets(logger *zap.Logger, subnets ...int) error

RegisterSubnets adds the given subnets and publish the updated node record

func (*DiscV5Service) Self

func (dvs *DiscV5Service) Self() *enode.LocalNode

Self returns self node

func (*DiscV5Service) UpdateForkVersion added in v0.3.0

func (dvs *DiscV5Service) UpdateForkVersion(logger *zap.Logger, forkv forksprotocol.ForkVersion) error

UpdateForkVersion updates the fork version used to filter nodes, and also the entry in ENR

type HandleNewPeer

type HandleNewPeer func(e PeerEvent)

HandleNewPeer is the function interface for handling new peer

type NodeFilter

type NodeFilter func(*enode.Node) bool

NodeFilter can be used for nodes filtering during discovery

type NodeProvider

type NodeProvider interface {
	Self() *enode.LocalNode
	Node(logger *zap.Logger, info peer.AddrInfo) (*enode.Node, error)
}

NodeProvider is an interface for managing ENRs

type Options

type Options struct {
	Host        host.Host
	DiscV5Opts  *DiscV5Options
	ConnIndex   peers.ConnectionIndex
	SubnetsIdx  peers.SubnetsIndex
	HostAddress string
	HostDNS     string

	ForkVersion forksprotocol.ForkVersion
}

Options represents the options passed to create a service

type PeerEvent

type PeerEvent struct {
	AddrInfo peer.AddrInfo
	Node     *enode.Node
}

PeerEvent is the data passed to peer handler

type Service

type Service interface {
	discovery.Discovery
	io.Closer
	RegisterSubnets(logger *zap.Logger, subnets ...int) error
	DeregisterSubnets(logger *zap.Logger, subnets ...int) error
	Bootstrap(logger *zap.Logger, handler HandleNewPeer) error
	UpdateForkVersion(logger *zap.Logger, forkv forksprotocol.ForkVersion) error
}

Service is the interface for discovery

func NewLocalDiscovery

func NewLocalDiscovery(ctx context.Context, logger *zap.Logger, host host.Host) (Service, error)

NewLocalDiscovery creates an mDNS discovery service and attaches it to the libp2p Host. This lets us automatically discover peers on the same LAN and connect to them.

func NewService

func NewService(ctx context.Context, logger *zap.Logger, opts Options) (Service, error)

NewService creates new discovery.Service

Jump to

Keyboard shortcuts

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