-
Notifications
You must be signed in to change notification settings - Fork 1
/
discovery.go
78 lines (63 loc) · 2.04 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package p2p
import (
"context"
"time"
host "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
ps "github.com/libp2p/go-libp2p-peerstore"
mdns "github.com/libp2p/go-libp2p/p2p/discovery"
pb "github.com/ovcharovvladimir/Prysm/proto/sharding/v1"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("prefix", "p2p")
// Discovery interval for multicast DNS querying.
var discoveryInterval = 1 * time.Minute
// mDNSTag is the name of the mDNS service.
var mDNSTag = mdns.ServiceTag
// startDiscovery protocols. Currently, this supports discovery via multicast
// DNS peer discovery.
//
// TODO: add other discovery protocols such as DHT, etc.
func startDiscovery(ctx context.Context, host host.Host, gsub topicPeerLister) error {
mdnsService, err := mdns.NewMdnsService(ctx, host, discoveryInterval, mDNSTag)
if err != nil {
return err
}
mdnsService.RegisterNotifee(&discovery{ctx, host, gsub})
return nil
}
// topicPeerLister has a method to return connected peers on a given topic.
// This is implemented by floodsub.PubSub.
type topicPeerLister interface {
ListPeers(string) []peer.ID
}
// Discovery implements mDNS notifee interface.
type discovery struct {
ctx context.Context
host host.Host
// Required for helper method.
gsub topicPeerLister
}
// HandlePeerFound registers the peer with the host.
func (d *discovery) HandlePeerFound(pi ps.PeerInfo) {
d.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, ps.PermanentAddrTTL)
if err := d.host.Connect(d.ctx, pi); err != nil {
log.Warnf("Failed to connect to peer: %v", err)
}
log.WithFields(logrus.Fields{
"peers": d.host.Peerstore().Peers(),
}).Debug("Peers are now")
log.WithFields(logrus.Fields{
"peerMap": d.topicPeerMap(),
}).Debug("Gsub has peers")
}
// topicPeerMap helper function for inspecting which peers are available for
// the p2p topics.
func (d *discovery) topicPeerMap() map[pb.Topic][]peer.ID {
m := make(map[pb.Topic][]peer.ID)
for topic := range topicTypeMapping {
peers := d.gsub.ListPeers(topic.String())
m[topic] = peers
}
return m
}