mirror of
https://github.com/status-im/go-waku.git
synced 2025-01-27 22:15:38 +00:00
feat: discv5 dns bootstrap
This commit is contained in:
parent
9845fea2a2
commit
79f3cf5b9f
54
waku/node.go
54
waku/node.go
@ -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))))
|
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 {
|
if options.DiscV5.Enable {
|
||||||
var bootnodes []*enode.Node
|
var bootnodes []*enode.Node
|
||||||
for _, addr := range options.DiscV5.Nodes.Value() {
|
for _, addr := range options.DiscV5.Nodes.Value() {
|
||||||
@ -208,6 +224,13 @@ func Execute(options Options) {
|
|||||||
}
|
}
|
||||||
bootnodes = append(bootnodes, bootnode)
|
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))))
|
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)
|
}(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.DNSDiscovery.Enable {
|
if len(discoveredNodes) != 0 {
|
||||||
if options.DNSDiscovery.URL != "" {
|
for _, n := range discoveredNodes {
|
||||||
utils.Logger().Info("attempting DNS discovery with ", zap.String("URL", options.DNSDiscovery.URL))
|
for _, m := range n.Addresses {
|
||||||
multiaddresses, err := dnsdisc.RetrieveNodes(ctx, options.DNSDiscovery.URL, dnsdisc.WithNameserver(options.DNSDiscovery.Nameserver))
|
go func(ctx context.Context, m multiaddr.Multiaddr) {
|
||||||
if err != nil {
|
ctx, cancel := context.WithTimeout(ctx, time.Duration(3)*time.Second)
|
||||||
utils.Logger().Warn("dns discovery error ", zap.Error(err))
|
defer cancel()
|
||||||
} else {
|
err = wakuNode.DialPeerWithMultiAddress(ctx, m)
|
||||||
utils.Logger().Info("found dns entries ", zap.Any("multiaddresses", multiaddresses))
|
if err != nil {
|
||||||
for _, m := range multiaddresses {
|
utils.Logger().Error("error dialing peer ", zap.Error(err))
|
||||||
go func(ctx context.Context, m multiaddr.Multiaddr) {
|
}
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Duration(3)*time.Second)
|
}(ctx, m)
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,7 +213,7 @@ func (d *DiscoveryV5) listen() error {
|
|||||||
d.listener = listener
|
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(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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
"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"
|
"github.com/status-im/go-waku/waku/v2/utils"
|
||||||
|
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
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
|
// 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) {
|
func RetrieveNodes(ctx context.Context, url string, opts ...DnsDiscoveryOption) ([]DiscoveredNode, error) {
|
||||||
var multiAddrs []ma.Multiaddr
|
var discoveredNodes []DiscoveredNode
|
||||||
|
|
||||||
params := new(dnsDiscoveryParameters)
|
params := new(dnsDiscoveryParameters)
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@ -46,8 +53,21 @@ func RetrieveNodes(ctx context.Context, url string, opts ...DnsDiscoveryOption)
|
|||||||
return nil, err
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user