go-waku/library/discovery.go

120 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-08-10 13:30:38 +00:00
package library
import (
"context"
2023-06-09 12:43:41 +00:00
"encoding/json"
2022-12-19 23:21:14 +00:00
"errors"
"time"
2023-06-09 12:43:41 +00:00
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
)
2023-08-10 13:30:38 +00:00
type dnsDiscoveryItem struct {
PeerID string `json:"peerID"`
Addresses []string `json:"multiaddrs"`
ENR string `json:"enr,omitempty"`
}
2023-08-10 13:30:38 +00:00
// DNSDiscovery executes dns discovery on an url and returns a list of nodes
func DNSDiscovery(url string, nameserver string, ms int) (string, error) {
var ctx context.Context
var cancel context.CancelFunc
if ms > 0 {
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
defer cancel()
} else {
ctx = context.Background()
}
var dnsDiscOpt []dnsdisc.DNSDiscoveryOption
if nameserver != "" {
dnsDiscOpt = append(dnsDiscOpt, dnsdisc.WithNameserver(nameserver))
}
nodes, err := dnsdisc.RetrieveNodes(ctx, url, dnsDiscOpt...)
if err != nil {
2023-08-10 13:30:38 +00:00
return "", err
}
2023-08-10 13:30:38 +00:00
var response []dnsDiscoveryItem
for _, n := range nodes {
2023-08-10 13:30:38 +00:00
item := dnsDiscoveryItem{
PeerID: n.PeerID.String(),
}
for _, addr := range n.PeerInfo.Addrs {
item.Addresses = append(item.Addresses, addr.String())
}
2023-02-20 14:42:04 +00:00
if n.ENR != nil {
item.ENR = n.ENR.String()
}
response = append(response, item)
}
2023-08-10 13:30:38 +00:00
return marshalJSON(response)
}
2022-12-19 23:21:14 +00:00
2023-08-03 21:24:00 +00:00
// StartDiscoveryV5 starts discv5 discovery
func StartDiscoveryV5(instance *WakuInstance) error {
if err := validateInstance(instance, MustBeStarted); err != nil {
return err
2022-12-19 23:21:14 +00:00
}
if instance.node.DiscV5() == nil {
2023-08-10 13:30:38 +00:00
return errors.New("DiscV5 is not mounted")
2022-12-19 23:21:14 +00:00
}
return instance.node.DiscV5().Start(instance.ctx)
2022-12-19 23:21:14 +00:00
}
2023-08-10 13:30:38 +00:00
// StopDiscoveryV5 stops discv5 discovery
func StopDiscoveryV5(instance *WakuInstance) error {
if err := validateInstance(instance, MustBeStarted); err != nil {
return err
2022-12-19 23:21:14 +00:00
}
if instance.node.DiscV5() == nil {
2023-08-10 13:30:38 +00:00
return errors.New("DiscV5 is not mounted")
2022-12-19 23:21:14 +00:00
}
instance.node.DiscV5().Stop()
2023-08-10 13:30:38 +00:00
return nil
2022-12-19 23:21:14 +00:00
}
2023-06-09 12:43:41 +00:00
2023-08-10 13:30:38 +00:00
// SetBootnodes is used to update the bootnodes receiving a JSON array of ENRs
func SetBootnodes(instance *WakuInstance, bootnodes string) error {
if err := validateInstance(instance, MustBeStarted); err != nil {
return err
2023-06-09 12:43:41 +00:00
}
if instance.node.DiscV5() == nil {
2023-08-10 13:30:38 +00:00
return errors.New("DiscV5 is not mounted")
2023-06-09 12:43:41 +00:00
}
var tmp []json.RawMessage
if err := json.Unmarshal([]byte(bootnodes), &tmp); err != nil {
2023-08-10 13:30:38 +00:00
return err
2023-06-09 12:43:41 +00:00
}
var enrList []string
for _, el := range tmp {
var enr string
if err := json.Unmarshal(el, &enr); err != nil {
2023-08-10 13:30:38 +00:00
return err
2023-06-09 12:43:41 +00:00
}
enrList = append(enrList, enr)
}
var nodes []*enode.Node
for _, addr := range enrList {
node, err := enode.Parse(enode.ValidSchemes, addr)
if err != nil {
2023-08-10 13:30:38 +00:00
return err
2023-06-09 12:43:41 +00:00
}
nodes = append(nodes, node)
}
return instance.node.DiscV5().SetBootnodes(nodes)
2023-06-09 12:43:41 +00:00
}