feat: discv5 dns bootstrap

This commit is contained in:
Richard Ramos 2022-05-05 15:17:35 -04:00
parent 9845fea2a2
commit 79f3cf5b9f
3 changed files with 59 additions and 25 deletions

View File

@ -199,6 +199,22 @@ func Execute(options Options) {
nodeOpts = append(nodeOpts, node.WithRendezvous(pubsub.WithDiscoveryOpts(discovery.Limit(45), discovery.TTL(time.Duration(20)*time.Second))))
}
var discoveredNodes []dnsdisc.DiscoveredNode
if options.DNSDiscovery.Enable {
if options.DNSDiscovery.URL != "" {
utils.Logger().Info("attempting DNS discovery with ", zap.String("URL", options.DNSDiscovery.URL))
nodes, err := dnsdisc.RetrieveNodes(ctx, options.DNSDiscovery.URL, dnsdisc.WithNameserver(options.DNSDiscovery.Nameserver))
if err != nil {
utils.Logger().Warn("dns discovery error ", zap.Error(err))
} else {
utils.Logger().Info("found dns entries ", zap.Any("qty", len(nodes)))
discoveredNodes = nodes
}
} else {
utils.Logger().Fatal("DNS discovery URL is required")
}
}
if options.DiscV5.Enable {
var bootnodes []*enode.Node
for _, addr := range options.DiscV5.Nodes.Value() {
@ -208,6 +224,13 @@ func Execute(options Options) {
}
bootnodes = append(bootnodes, bootnode)
}
for _, n := range discoveredNodes {
if n.ENR != nil {
bootnodes = append(bootnodes, n.ENR)
}
}
nodeOpts = append(nodeOpts, node.WithDiscoveryV5(options.DiscV5.Port, bootnodes, options.DiscV5.AutoUpdate, pubsub.WithDiscoveryOpts(discovery.Limit(45), discovery.TTL(time.Duration(20)*time.Second))))
}
@ -252,27 +275,18 @@ func Execute(options Options) {
}(n)
}
if options.DNSDiscovery.Enable {
if options.DNSDiscovery.URL != "" {
utils.Logger().Info("attempting DNS discovery with ", zap.String("URL", options.DNSDiscovery.URL))
multiaddresses, err := dnsdisc.RetrieveNodes(ctx, options.DNSDiscovery.URL, dnsdisc.WithNameserver(options.DNSDiscovery.Nameserver))
if err != nil {
utils.Logger().Warn("dns discovery error ", zap.Error(err))
} else {
utils.Logger().Info("found dns entries ", zap.Any("multiaddresses", multiaddresses))
for _, m := range multiaddresses {
go func(ctx context.Context, m multiaddr.Multiaddr) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(3)*time.Second)
defer cancel()
err = wakuNode.DialPeerWithMultiAddress(ctx, m)
if err != nil {
utils.Logger().Error("error dialing peer ", zap.Error(err))
}
}(ctx, m)
}
if len(discoveredNodes) != 0 {
for _, n := range discoveredNodes {
for _, m := range n.Addresses {
go func(ctx context.Context, m multiaddr.Multiaddr) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(3)*time.Second)
defer cancel()
err = wakuNode.DialPeerWithMultiAddress(ctx, m)
if err != nil {
utils.Logger().Error("error dialing peer ", zap.Error(err))
}
}(ctx, m)
}
} else {
utils.Logger().Fatal("DNS discovery URL is required")
}
}

View File

@ -213,7 +213,7 @@ func (d *DiscoveryV5) listen() error {
d.listener = listener
d.log.Info(fmt.Sprintf("Started Discovery V5 at %s:%d, advertising IP: %s:%d", d.udpAddr.IP, d.udpAddr.Port, d.localnode.Node().IP(), d.localnode.Node().TCP()))
d.log.Info("Discv5: discoverable ENR", d.localnode.Node())
d.log.Info("Discv5: discoverable ENR ", d.localnode.Node())
return nil
}

View File

@ -4,6 +4,8 @@ import (
"context"
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/status-im/go-waku/waku/v2/utils"
ma "github.com/multiformats/go-multiaddr"
@ -22,9 +24,14 @@ func WithNameserver(nameserver string) DnsDiscoveryOption {
}
}
type DiscoveredNode struct {
Addresses []ma.Multiaddr
ENR *enode.Node
}
// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree
func RetrieveNodes(ctx context.Context, url string, opts ...DnsDiscoveryOption) ([]ma.Multiaddr, error) {
var multiAddrs []ma.Multiaddr
func RetrieveNodes(ctx context.Context, url string, opts ...DnsDiscoveryOption) ([]DiscoveredNode, error) {
var discoveredNodes []DiscoveredNode
params := new(dnsDiscoveryParameters)
for _, opt := range opts {
@ -46,8 +53,21 @@ func RetrieveNodes(ctx context.Context, url string, opts ...DnsDiscoveryOption)
return nil, err
}
multiAddrs = append(multiAddrs, m...)
d := DiscoveredNode{Addresses: m}
if hasUDP(node) {
d.ENR = node
}
discoveredNodes = append(discoveredNodes, d)
}
return multiAddrs, nil
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 {
return err == nil
}
return false
}