2023-08-03 16:21:15 +00:00
|
|
|
package peermanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2023-09-19 06:05:29 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/event"
|
2023-08-03 16:21:15 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
2023-08-15 01:27:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
2023-08-03 16:21:15 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-08-10 12:58:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2023-08-03 16:21:15 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2023-08-10 12:58:22 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/waku-org/go-waku/logging"
|
2023-08-03 16:21:15 +00:00
|
|
|
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
|
2023-09-14 15:00:06 +00:00
|
|
|
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
|
2023-09-14 08:36:08 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2023-08-10 12:58:22 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2023-08-03 16:21:15 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PeerManager applies various controls and manage connections towards peers.
|
|
|
|
type PeerManager struct {
|
2023-08-15 01:27:51 +00:00
|
|
|
peerConnector *PeerConnectionStrategy
|
|
|
|
maxRelayPeers int
|
2023-08-03 16:21:15 +00:00
|
|
|
logger *zap.Logger
|
2023-08-15 01:27:51 +00:00
|
|
|
InRelayPeersTarget int
|
|
|
|
OutRelayPeersTarget int
|
2023-08-03 16:21:15 +00:00
|
|
|
host host.Host
|
2023-08-30 11:57:22 +00:00
|
|
|
serviceSlots *ServiceSlots
|
2023-08-15 01:27:51 +00:00
|
|
|
ctx context.Context
|
2023-09-19 06:05:29 +00:00
|
|
|
sub event.Subscription
|
2023-08-03 16:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const peerConnectivityLoopSecs = 15
|
2023-08-30 11:57:22 +00:00
|
|
|
|
|
|
|
// 80% relay peers 20% service peers
|
|
|
|
func relayAndServicePeers(maxConnections int) (int, int) {
|
|
|
|
return maxConnections - maxConnections/5, maxConnections / 5
|
|
|
|
}
|
|
|
|
|
|
|
|
// 66% inRelayPeers 33% outRelayPeers
|
|
|
|
func inAndOutRelayPeers(relayPeers int) (int, int) {
|
|
|
|
outRelayPeers := relayPeers / 3
|
|
|
|
//
|
|
|
|
const minOutRelayConns = 10
|
|
|
|
if outRelayPeers < minOutRelayConns {
|
|
|
|
outRelayPeers = minOutRelayConns
|
|
|
|
}
|
|
|
|
return relayPeers - outRelayPeers, outRelayPeers
|
|
|
|
}
|
2023-08-03 16:21:15 +00:00
|
|
|
|
|
|
|
// NewPeerManager creates a new peerManager instance.
|
2023-08-15 01:27:51 +00:00
|
|
|
func NewPeerManager(maxConnections int, logger *zap.Logger) *PeerManager {
|
2023-08-03 16:21:15 +00:00
|
|
|
|
2023-08-30 11:57:22 +00:00
|
|
|
maxRelayPeers, _ := relayAndServicePeers(maxConnections)
|
|
|
|
inRelayPeersTarget, outRelayPeersTarget := inAndOutRelayPeers(maxRelayPeers)
|
2023-08-03 16:21:15 +00:00
|
|
|
|
|
|
|
pm := &PeerManager{
|
|
|
|
logger: logger.Named("peer-manager"),
|
2023-08-30 11:57:22 +00:00
|
|
|
maxRelayPeers: maxRelayPeers,
|
|
|
|
InRelayPeersTarget: inRelayPeersTarget,
|
|
|
|
OutRelayPeersTarget: outRelayPeersTarget,
|
|
|
|
serviceSlots: NewServiceSlot(),
|
2023-08-03 16:21:15 +00:00
|
|
|
}
|
2023-08-15 01:27:51 +00:00
|
|
|
logger.Info("PeerManager init values", zap.Int("maxConnections", maxConnections),
|
2023-08-30 11:57:22 +00:00
|
|
|
zap.Int("maxRelayPeers", maxRelayPeers),
|
|
|
|
zap.Int("outRelayPeersTarget", outRelayPeersTarget),
|
2023-08-15 01:27:51 +00:00
|
|
|
zap.Int("inRelayPeersTarget", pm.InRelayPeersTarget))
|
2023-08-03 16:21:15 +00:00
|
|
|
|
|
|
|
return pm
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:27:51 +00:00
|
|
|
// SetHost sets the host to be used in order to access the peerStore.
|
2023-08-03 16:21:15 +00:00
|
|
|
func (pm *PeerManager) SetHost(host host.Host) {
|
|
|
|
pm.host = host
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:27:51 +00:00
|
|
|
// SetPeerConnector sets the peer connector to be used for establishing relay connections.
|
|
|
|
func (pm *PeerManager) SetPeerConnector(pc *PeerConnectionStrategy) {
|
|
|
|
pm.peerConnector = pc
|
|
|
|
}
|
|
|
|
|
2023-09-19 06:05:29 +00:00
|
|
|
func (pm *PeerManager) SubscribeToRelayEvtBus(bus event.Bus) error {
|
|
|
|
var err error
|
|
|
|
pm.sub, err = bus.Subscribe(new(relay.EvtPeerTopic))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *PeerManager) peerEventLoop(ctx context.Context) {
|
|
|
|
defer pm.sub.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-pm.sub.Out():
|
|
|
|
peerEvt := e.(relay.EvtPeerTopic)
|
|
|
|
wps := pm.host.Peerstore().(*wps.WakuPeerstoreImpl)
|
|
|
|
peerID := peerEvt.PeerID
|
|
|
|
if peerEvt.State == relay.PEER_JOINED {
|
|
|
|
err := wps.AddPubSubTopic(peerID, peerEvt.Topic)
|
|
|
|
if err != nil {
|
|
|
|
pm.logger.Error("failed to add pubSubTopic for peer",
|
2023-09-20 06:54:16 +00:00
|
|
|
logging.HostID("peerID", peerID), zap.String("topic", peerEvt.Topic), zap.Error(err))
|
2023-09-19 06:05:29 +00:00
|
|
|
}
|
|
|
|
} else if peerEvt.State == relay.PEER_LEFT {
|
|
|
|
err := wps.RemovePubSubTopic(peerID, peerEvt.Topic)
|
|
|
|
if err != nil {
|
|
|
|
pm.logger.Error("failed to remove pubSubTopic for peer",
|
|
|
|
logging.HostID("peerID", peerID), zap.Error(err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pm.logger.Error("unknown peer event received", zap.Int("eventState", int(peerEvt.State)))
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 16:21:15 +00:00
|
|
|
// Start starts the processing to be done by peer manager.
|
|
|
|
func (pm *PeerManager) Start(ctx context.Context) {
|
2023-08-15 01:27:51 +00:00
|
|
|
pm.ctx = ctx
|
2023-09-19 06:05:29 +00:00
|
|
|
if pm.sub != nil {
|
|
|
|
go pm.peerEventLoop(ctx)
|
|
|
|
}
|
2023-08-03 16:21:15 +00:00
|
|
|
go pm.connectivityLoop(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a connectivity loop, which currently checks and prunes inbound connections.
|
|
|
|
func (pm *PeerManager) connectivityLoop(ctx context.Context) {
|
|
|
|
t := time.NewTicker(peerConnectivityLoopSecs * time.Second)
|
2023-08-15 01:27:51 +00:00
|
|
|
defer t.Stop()
|
2023-08-03 16:21:15 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
2023-08-15 01:27:51 +00:00
|
|
|
pm.connectToRelayPeers()
|
2023-08-03 16:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:27:51 +00:00
|
|
|
// GroupPeersByDirection returns all the connected peers in peer store grouped by Inbound or outBound direction
|
|
|
|
func (pm *PeerManager) GroupPeersByDirection() (inPeers peer.IDSlice, outPeers peer.IDSlice, err error) {
|
2023-08-17 12:56:20 +00:00
|
|
|
peers := pm.host.Network().Peers()
|
2023-08-15 01:27:51 +00:00
|
|
|
|
2023-08-17 12:56:20 +00:00
|
|
|
for _, p := range peers {
|
2023-08-15 01:27:51 +00:00
|
|
|
direction, err := pm.host.Peerstore().(wps.WakuPeerstore).Direction(p)
|
|
|
|
if err == nil {
|
|
|
|
if direction == network.DirInbound {
|
|
|
|
inPeers = append(inPeers, p)
|
|
|
|
} else if direction == network.DirOutbound {
|
|
|
|
outPeers = append(outPeers, p)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pm.logger.Error("Failed to retrieve peer direction",
|
|
|
|
logging.HostID("peerID", p), zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inPeers, outPeers, nil
|
|
|
|
}
|
2023-08-03 16:21:15 +00:00
|
|
|
|
2023-08-15 01:27:51 +00:00
|
|
|
func (pm *PeerManager) getRelayPeers() (inRelayPeers peer.IDSlice, outRelayPeers peer.IDSlice) {
|
2023-08-03 16:21:15 +00:00
|
|
|
//Group peers by their connected direction inbound or outbound.
|
2023-08-15 01:27:51 +00:00
|
|
|
inPeers, outPeers, err := pm.GroupPeersByDirection()
|
2023-08-03 16:21:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-28 08:35:59 +00:00
|
|
|
pm.logger.Debug("Number of peers connected", zap.Int("inPeers", inPeers.Len()),
|
2023-08-15 01:27:51 +00:00
|
|
|
zap.Int("outPeers", outPeers.Len()))
|
2023-08-03 16:21:15 +00:00
|
|
|
|
|
|
|
//Need to filter peers to check if they support relay
|
2023-08-17 12:56:20 +00:00
|
|
|
if inPeers.Len() != 0 {
|
2023-09-14 08:36:08 +00:00
|
|
|
inRelayPeers, _ = utils.FilterPeersByProto(pm.host, inPeers, relay.WakuRelayID_v200)
|
2023-08-17 12:56:20 +00:00
|
|
|
}
|
|
|
|
if outPeers.Len() != 0 {
|
2023-09-14 08:36:08 +00:00
|
|
|
outRelayPeers, _ = utils.FilterPeersByProto(pm.host, outPeers, relay.WakuRelayID_v200)
|
2023-08-17 12:56:20 +00:00
|
|
|
}
|
2023-08-15 01:27:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *PeerManager) connectToRelayPeers() {
|
|
|
|
|
|
|
|
//Check for out peer connections and connect to more peers.
|
|
|
|
inRelayPeers, outRelayPeers := pm.getRelayPeers()
|
2023-08-28 08:35:59 +00:00
|
|
|
pm.logger.Info("Number of Relay peers connected", zap.Int("inRelayPeers", inRelayPeers.Len()),
|
|
|
|
zap.Int("outRelayPeers", outRelayPeers.Len()))
|
2023-08-15 01:27:51 +00:00
|
|
|
if inRelayPeers.Len() > 0 &&
|
|
|
|
inRelayPeers.Len() > pm.InRelayPeersTarget {
|
2023-08-28 06:47:48 +00:00
|
|
|
pm.pruneInRelayConns(inRelayPeers)
|
2023-08-15 01:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if outRelayPeers.Len() > pm.OutRelayPeersTarget {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
totalRelayPeers := inRelayPeers.Len() + outRelayPeers.Len()
|
2023-09-07 09:31:31 +00:00
|
|
|
// Establish additional connections connected peers are lesser than target.
|
2023-08-15 01:27:51 +00:00
|
|
|
//What if the not connected peers in peerstore are not relay peers???
|
2023-09-07 09:31:31 +00:00
|
|
|
if totalRelayPeers < pm.maxRelayPeers {
|
2023-08-15 01:27:51 +00:00
|
|
|
//Find not connected peers.
|
|
|
|
notConnectedPeers := pm.getNotConnectedPers()
|
2023-09-07 09:31:31 +00:00
|
|
|
if notConnectedPeers.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-08-15 01:27:51 +00:00
|
|
|
//Connect to eligible peers.
|
|
|
|
numPeersToConnect := pm.maxRelayPeers - totalRelayPeers
|
|
|
|
|
|
|
|
if numPeersToConnect > notConnectedPeers.Len() {
|
2023-09-07 09:31:31 +00:00
|
|
|
numPeersToConnect = notConnectedPeers.Len()
|
2023-08-15 01:27:51 +00:00
|
|
|
}
|
|
|
|
pm.connectToPeers(notConnectedPeers[0:numPeersToConnect])
|
|
|
|
} //Else: Should we raise some sort of unhealthy event??
|
|
|
|
}
|
|
|
|
|
2023-09-19 00:39:39 +00:00
|
|
|
func addrInfoToPeerData(origin wps.Origin, peerID peer.ID, host host.Host) PeerData {
|
|
|
|
return PeerData{
|
|
|
|
Origin: origin,
|
|
|
|
AddrInfo: peer.AddrInfo{
|
|
|
|
ID: peerID,
|
|
|
|
Addrs: host.Peerstore().Addrs(peerID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 01:27:51 +00:00
|
|
|
func (pm *PeerManager) connectToPeers(peers peer.IDSlice) {
|
|
|
|
for _, peerID := range peers {
|
2023-09-19 00:39:39 +00:00
|
|
|
peerData := addrInfoToPeerData(wps.PeerManager, peerID, pm.host)
|
|
|
|
pm.peerConnector.PushToChan(peerData)
|
2023-08-15 01:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *PeerManager) getNotConnectedPers() (notConnectedPeers peer.IDSlice) {
|
|
|
|
for _, peerID := range pm.host.Peerstore().Peers() {
|
|
|
|
if pm.host.Network().Connectedness(peerID) != network.Connected {
|
|
|
|
notConnectedPeers = append(notConnectedPeers, peerID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-28 06:47:48 +00:00
|
|
|
func (pm *PeerManager) pruneInRelayConns(inRelayPeers peer.IDSlice) {
|
2023-08-15 01:27:51 +00:00
|
|
|
|
|
|
|
//Start disconnecting peers, based on what?
|
|
|
|
//For now, just disconnect most recently connected peers
|
|
|
|
//TODO: Need to have more intelligent way of doing this, maybe peer scores.
|
|
|
|
pm.logger.Info("Number of in peer connections exceed targer relay peers, hence pruning",
|
|
|
|
zap.Int("inRelayPeers", inRelayPeers.Len()), zap.Int("inRelayPeersTarget", pm.InRelayPeersTarget))
|
|
|
|
for pruningStartIndex := pm.InRelayPeersTarget; pruningStartIndex < inRelayPeers.Len(); pruningStartIndex++ {
|
|
|
|
p := inRelayPeers[pruningStartIndex]
|
|
|
|
err := pm.host.Network().ClosePeer(p)
|
|
|
|
if err != nil {
|
|
|
|
pm.logger.Warn("Failed to disconnect connection towards peer",
|
|
|
|
logging.HostID("peerID", p))
|
2023-08-03 16:21:15 +00:00
|
|
|
}
|
2023-08-15 01:27:51 +00:00
|
|
|
pm.logger.Debug("Successfully disconnected connection towards peer",
|
|
|
|
logging.HostID("peerID", p))
|
2023-08-03 16:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-10 12:58:22 +00:00
|
|
|
|
|
|
|
// AddDiscoveredPeer to add dynamically discovered peers.
|
|
|
|
// Note that these peers will not be set in service-slots.
|
|
|
|
// TODO: It maybe good to set in service-slots based on services supported in the ENR
|
|
|
|
func (pm *PeerManager) AddDiscoveredPeer(p PeerData) {
|
2023-09-14 15:00:06 +00:00
|
|
|
// Try to fetch shard info from ENR to arrive at pubSub topics.
|
|
|
|
if len(p.PubSubTopics) == 0 && p.ENR != nil {
|
|
|
|
shards, err := wenr.RelaySharding(p.ENR.Record())
|
|
|
|
if err != nil {
|
|
|
|
pm.logger.Error("Could not derive relayShards from ENR", zap.Error(err),
|
|
|
|
logging.HostID("peer", p.AddrInfo.ID), zap.String("enr", p.ENR.String()))
|
|
|
|
} else {
|
|
|
|
if shards != nil {
|
|
|
|
p.PubSubTopics = make([]string, 0)
|
|
|
|
topics := shards.Topics()
|
|
|
|
for _, topic := range topics {
|
|
|
|
topicStr := topic.String()
|
|
|
|
p.PubSubTopics = append(p.PubSubTopics, topicStr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pm.logger.Info("ENR doesn't have relay shards", logging.HostID("peer", p.AddrInfo.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 12:58:22 +00:00
|
|
|
|
2023-09-14 15:00:06 +00:00
|
|
|
_ = pm.addPeer(p.AddrInfo.ID, p.AddrInfo.Addrs, p.Origin, p.PubSubTopics)
|
2023-08-10 12:58:22 +00:00
|
|
|
|
|
|
|
if p.ENR != nil {
|
|
|
|
err := pm.host.Peerstore().(wps.WakuPeerstore).SetENR(p.AddrInfo.ID, p.ENR)
|
|
|
|
if err != nil {
|
2023-08-15 01:27:51 +00:00
|
|
|
pm.logger.Error("could not store enr", zap.Error(err),
|
|
|
|
logging.HostID("peer", p.AddrInfo.ID), zap.String("enr", p.ENR.String()))
|
2023-08-10 12:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-14 15:00:06 +00:00
|
|
|
|
2023-08-10 12:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addPeer adds peer to only the peerStore.
|
|
|
|
// It also sets additional metadata such as origin, ENR and supported protocols
|
2023-09-14 15:00:06 +00:00
|
|
|
func (pm *PeerManager) addPeer(ID peer.ID, addrs []ma.Multiaddr, origin wps.Origin, pubSubTopics []string, protocols ...protocol.ID) error {
|
2023-08-10 12:58:22 +00:00
|
|
|
pm.logger.Info("adding peer to peerstore", logging.HostID("peer", ID))
|
|
|
|
pm.host.Peerstore().AddAddrs(ID, addrs, peerstore.AddressTTL)
|
|
|
|
err := pm.host.Peerstore().(wps.WakuPeerstore).SetOrigin(ID, origin)
|
|
|
|
if err != nil {
|
|
|
|
pm.logger.Error("could not set origin", zap.Error(err), logging.HostID("peer", ID))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(protocols) > 0 {
|
|
|
|
err = pm.host.Peerstore().AddProtocols(ID, protocols...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 15:00:06 +00:00
|
|
|
if len(pubSubTopics) == 0 {
|
|
|
|
// Probably the peer is discovered via DNSDiscovery (for which we don't have pubSubTopic info)
|
|
|
|
//If pubSubTopic and enr is empty or no shard info in ENR,then set to defaultPubSubTopic
|
|
|
|
pubSubTopics = []string{relay.DefaultWakuTopic}
|
|
|
|
}
|
|
|
|
err = pm.host.Peerstore().(wps.WakuPeerstore).SetPubSubTopics(ID, pubSubTopics)
|
|
|
|
if err != nil {
|
|
|
|
pm.logger.Error("could not store pubSubTopic", zap.Error(err),
|
|
|
|
logging.HostID("peer", ID), zap.Strings("topics", pubSubTopics))
|
|
|
|
}
|
2023-08-10 12:58:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeer adds peer to the peerStore and also to service slots
|
2023-09-14 15:00:06 +00:00
|
|
|
func (pm *PeerManager) AddPeer(address ma.Multiaddr, origin wps.Origin, pubSubTopics []string, protocols ...protocol.ID) (peer.ID, error) {
|
2023-08-10 12:58:22 +00:00
|
|
|
//Assuming all addresses have peerId
|
|
|
|
info, err := peer.AddrInfoFromP2pAddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add Service peers to serviceSlots.
|
|
|
|
for _, proto := range protocols {
|
2023-08-30 14:33:57 +00:00
|
|
|
pm.addPeerToServiceSlot(proto, info.ID)
|
2023-08-10 12:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Add to the peer-store
|
2023-09-14 15:00:06 +00:00
|
|
|
err = pm.addPeer(info.ID, info.Addrs, origin, pubSubTopics, protocols...)
|
2023-08-10 12:58:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePeer deletes peer from the peerStore after disconnecting it.
|
|
|
|
// It also removes the peer from serviceSlot.
|
|
|
|
func (pm *PeerManager) RemovePeer(peerID peer.ID) {
|
|
|
|
pm.host.Peerstore().RemovePeer(peerID)
|
|
|
|
//Search if this peer is in serviceSlot and if so, remove it from there
|
|
|
|
// TODO:Add another peer which is statically configured to the serviceSlot.
|
2023-08-30 11:57:22 +00:00
|
|
|
pm.serviceSlots.removePeer(peerID)
|
2023-08-10 12:58:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:33:57 +00:00
|
|
|
// addPeerToServiceSlot adds a peerID to serviceSlot.
|
2023-08-10 12:58:22 +00:00
|
|
|
// Adding to peerStore is expected to be already done by caller.
|
|
|
|
// If relay proto is passed, it is not added to serviceSlot.
|
2023-08-30 14:33:57 +00:00
|
|
|
func (pm *PeerManager) addPeerToServiceSlot(proto protocol.ID, peerID peer.ID) {
|
2023-09-14 08:36:08 +00:00
|
|
|
if proto == relay.WakuRelayID_v200 {
|
2023-08-10 12:58:22 +00:00
|
|
|
pm.logger.Warn("Cannot add Relay peer to service peer slots")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//For now adding the peer to serviceSlot which means the latest added peer would be given priority.
|
|
|
|
//TODO: Ideally we should sort the peers per service and return best peer based on peer score or RTT etc.
|
2023-08-15 01:27:51 +00:00
|
|
|
pm.logger.Info("Adding peer to service slots", logging.HostID("peer", peerID),
|
|
|
|
zap.String("service", string(proto)))
|
2023-08-30 11:57:22 +00:00
|
|
|
// getPeers returns nil for WakuRelayIDv200 protocol, but we don't run this ServiceSlot code for WakuRelayIDv200 protocol
|
|
|
|
pm.serviceSlots.getPeers(proto).add(peerID)
|
2023-08-10 12:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectPeer is used to return a random peer that supports a given protocol.
|
|
|
|
// If a list of specific peers is passed, the peer will be chosen from that list assuming
|
|
|
|
// it supports the chosen protocol, otherwise it will chose a peer from the service slot.
|
|
|
|
// If a peer cannot be found in the service slot, a peer will be selected from node peerstore
|
2023-09-11 14:24:05 +00:00
|
|
|
func (pm *PeerManager) SelectPeer(proto protocol.ID, specificPeers []peer.ID) (peer.ID, error) {
|
2023-08-10 12:58:22 +00:00
|
|
|
// @TODO We need to be more strategic about which peers we dial. Right now we just set one on the service.
|
|
|
|
// Ideally depending on the query and our set of peers we take a subset of ideal peers.
|
|
|
|
// This will require us to check for various factors such as:
|
|
|
|
// - which topics they track
|
|
|
|
// - latency?
|
|
|
|
|
2023-08-30 11:57:22 +00:00
|
|
|
//Try to fetch from serviceSlot
|
|
|
|
if slot := pm.serviceSlots.getPeers(proto); slot != nil {
|
|
|
|
if peerID, err := slot.getRandom(); err == nil {
|
|
|
|
return peerID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not found in serviceSlots or proto == WakuRelayIDv200
|
2023-08-10 12:58:22 +00:00
|
|
|
filteredPeers, err := utils.FilterPeersByProto(pm.host, specificPeers, proto)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-11 14:24:05 +00:00
|
|
|
|
2023-08-10 12:58:22 +00:00
|
|
|
return utils.SelectRandomPeer(filteredPeers, pm.logger)
|
|
|
|
}
|