go-waku/waku/v2/dnsdisc/enr.go

134 lines
2.9 KiB
Go
Raw Normal View History

2021-11-16 14:22:01 +00:00
package dnsdisc
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
2022-05-05 19:17:35 +00:00
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/prometheus/client_golang/prometheus"
2023-04-20 18:51:13 +00:00
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
)
type dnsDiscoveryParameters struct {
nameserver string
resolver dnsdisc.Resolver
}
type DNSDiscoveryOption func(*dnsDiscoveryParameters) error
var ErrExclusiveOpts = errors.New("cannot set both nameserver and resolver")
2022-07-25 15:28:17 +00:00
// WithNameserver is a DnsDiscoveryOption that configures the nameserver to use
func WithNameserver(nameserver string) DNSDiscoveryOption {
return func(params *dnsDiscoveryParameters) error {
if params.resolver != nil {
return ErrExclusiveOpts
}
params.nameserver = nameserver
return nil
}
}
func WithResolver(resolver dnsdisc.Resolver) DNSDiscoveryOption {
return func(params *dnsDiscoveryParameters) error {
if params.nameserver != "" {
return ErrExclusiveOpts
}
params.resolver = resolver
return nil
}
}
2022-05-05 19:17:35 +00:00
type DiscoveredNode struct {
PeerID peer.ID
PeerInfo peer.AddrInfo
ENR *enode.Node
2022-05-05 19:17:35 +00:00
}
var metrics Metrics
// SetPrometheusRegisterer is used to setup a custom prometheus registerer for metrics
func SetPrometheusRegisterer(reg prometheus.Registerer, logger *zap.Logger) {
metrics = newMetrics(reg)
}
func init() {
SetPrometheusRegisterer(prometheus.DefaultRegisterer, utils.Logger())
}
// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree
func RetrieveNodes(ctx context.Context, url string, opts ...DNSDiscoveryOption) ([]DiscoveredNode, error) {
2022-05-05 19:17:35 +00:00
var discoveredNodes []DiscoveredNode
params := new(dnsDiscoveryParameters)
for _, opt := range opts {
err := opt(params)
if err != nil {
return nil, err
}
}
if params.resolver == nil {
params.resolver = GetResolver(ctx, params.nameserver)
}
client := dnsdisc.NewClient(dnsdisc.Config{
Resolver: params.resolver,
})
tree, err := client.SyncTree(url)
if err != nil {
metrics.RecordError(treeSyncFailure)
return nil, err
}
for _, node := range tree.Nodes() {
2023-04-20 18:51:13 +00:00
peerID, m, err := wenr.Multiaddress(node)
if err != nil {
metrics.RecordError(peerInfoFailure)
return nil, err
}
infoAddr, err := peer.AddrInfosFromP2pAddrs(m...)
if err != nil {
return nil, err
}
var info peer.AddrInfo
for _, i := range infoAddr {
if i.ID == peerID {
info = i
break
}
}
d := DiscoveredNode{
PeerID: peerID,
PeerInfo: info,
}
2022-05-05 19:17:35 +00:00
if hasUDP(node) {
d.ENR = node
}
discoveredNodes = append(discoveredNodes, d)
}
metrics.RecordDiscoveredNodes(len(discoveredNodes))
2022-05-05 19:17:35 +00:00
return discoveredNodes, nil
}
func hasUDP(node *enode.Node) bool {
enrUDP := new(enr.UDP)
if err := node.Record().Load(enr.WithEntry(enrUDP.ENRKey(), enrUDP)); err != nil {
2022-09-15 22:54:37 +00:00
return false
2022-05-05 19:17:35 +00:00
}
2022-09-15 22:54:37 +00:00
return true
}