2023-11-07 17:13:19 +00:00
|
|
|
package peermanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-22 06:15:53 +00:00
|
|
|
"encoding/json"
|
2023-11-07 17:13:19 +00:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
|
|
|
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
|
|
|
|
waku_proto "github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"go.uber.org/zap"
|
2024-01-26 08:45:15 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-11-07 17:13:19 +00:00
|
|
|
)
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
type PeerSet map[peer.ID]struct{}
|
|
|
|
|
|
|
|
func PeerInSet(peers PeerSet, peer peer.ID) bool {
|
|
|
|
if len(peers) > 0 {
|
|
|
|
if _, ok := peers[peer]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2024-01-26 08:45:15 +00:00
|
|
|
|
2023-11-07 17:13:19 +00:00
|
|
|
// SelectPeerByContentTopic is used to return a random peer that supports a given protocol for given contentTopic.
|
|
|
|
// If a list of specific peers is passed, the peer will be chosen from that list assuming
|
|
|
|
// it supports the chosen protocol and contentTopic, 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-11-13 22:52:46 +00:00
|
|
|
func (pm *PeerManager) SelectPeerByContentTopics(proto protocol.ID, contentTopics []string, specificPeers ...peer.ID) (peer.ID, error) {
|
|
|
|
pubsubTopics := []string{}
|
|
|
|
for _, cTopic := range contentTopics {
|
|
|
|
pubsubTopic, err := waku_proto.GetPubSubTopicFromContentTopic(cTopic)
|
|
|
|
if err != nil {
|
2023-11-15 14:39:09 +00:00
|
|
|
pm.logger.Debug("selectPeer: failed to get contentTopic from pubsubTopic", zap.String("contentTopic", cTopic))
|
2023-11-13 22:52:46 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pubsubTopics = append(pubsubTopics, pubsubTopic)
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
2024-01-26 08:45:15 +00:00
|
|
|
peers, err := pm.SelectPeers(PeerSelectionCriteria{PubsubTopics: pubsubTopics, Proto: proto, SpecificPeers: specificPeers})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return peers[0], nil
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectRandomPeer 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
|
|
|
|
// if pubSubTopic is specified, peer is selected from list that support the pubSubTopic
|
2024-01-26 08:45:15 +00:00
|
|
|
func (pm *PeerManager) SelectRandom(criteria PeerSelectionCriteria) (peer.IDSlice, error) {
|
2023-11-07 17:13:19 +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?
|
2024-01-26 08:45:15 +00:00
|
|
|
peerIDs, err := pm.selectServicePeer(criteria)
|
|
|
|
if err == nil && len(peerIDs) == criteria.MaxPeers {
|
|
|
|
return maps.Keys(peerIDs), nil
|
2023-11-07 17:13:19 +00:00
|
|
|
} else if !errors.Is(err, ErrNoPeersAvailable) {
|
|
|
|
pm.logger.Debug("could not retrieve random peer from slot", zap.String("protocol", string(criteria.Proto)),
|
2023-11-13 22:52:46 +00:00
|
|
|
zap.Strings("pubsubTopics", criteria.PubsubTopics), zap.Error(err))
|
2024-01-26 08:45:15 +00:00
|
|
|
return nil, err
|
|
|
|
} else if len(peerIDs) == 0 {
|
2024-05-22 06:15:53 +00:00
|
|
|
peerIDs = make(PeerSet)
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
// if not found in serviceSlots or proto == WakuRelayIDv200
|
2024-05-22 06:15:53 +00:00
|
|
|
filteredPeers, err := pm.FilterPeersByProto(criteria.SpecificPeers, criteria.ExcludePeers, criteria.Proto)
|
2023-11-07 17:13:19 +00:00
|
|
|
if err != nil {
|
2024-01-26 08:45:15 +00:00
|
|
|
return nil, err
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
2023-11-13 22:52:46 +00:00
|
|
|
if len(criteria.PubsubTopics) > 0 {
|
|
|
|
filteredPeers = pm.host.Peerstore().(wps.WakuPeerstore).PeersByPubSubTopics(criteria.PubsubTopics, filteredPeers...)
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
2024-05-22 06:15:53 +00:00
|
|
|
//Not passing excludePeers as filterPeers are already considering excluded ones.
|
|
|
|
randomPeers, err := selectRandomPeers(filteredPeers, nil, criteria.MaxPeers-len(peerIDs))
|
2024-01-26 08:45:15 +00:00
|
|
|
if err != nil && len(peerIDs) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for tmpPeer := range randomPeers {
|
|
|
|
peerIDs[tmpPeer] = struct{}{}
|
|
|
|
}
|
|
|
|
return maps.Keys(peerIDs), nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
func getRandom(filter PeerSet, count int, excludePeers PeerSet) (PeerSet, error) {
|
2024-01-26 08:45:15 +00:00
|
|
|
i := 0
|
2024-05-22 06:15:53 +00:00
|
|
|
selectedPeers := make(PeerSet)
|
2024-01-26 08:45:15 +00:00
|
|
|
for pID := range filter {
|
2024-05-22 06:15:53 +00:00
|
|
|
if PeerInSet(excludePeers, pID) {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-26 08:45:15 +00:00
|
|
|
//Map's iterator in golang works using randomness and hence not random function is being used.
|
|
|
|
selectedPeers[pID] = struct{}{}
|
|
|
|
i++
|
|
|
|
if i == count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(selectedPeers) == 0 {
|
|
|
|
return nil, ErrNoPeersAvailable
|
|
|
|
}
|
|
|
|
return selectedPeers, nil
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
// selects count random peers from list of peers
|
2024-05-22 06:15:53 +00:00
|
|
|
func selectRandomPeers(peers peer.IDSlice, excludePeers PeerSet, count int) (PeerSet, error) {
|
|
|
|
filteredPeerMap := PeerSliceToMap(peers)
|
|
|
|
return getRandom(filteredPeerMap, count, excludePeers)
|
2024-01-26 08:45:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
func PeerSliceToMap(peers peer.IDSlice) PeerSet {
|
|
|
|
peerSet := make(PeerSet, peers.Len())
|
2024-01-26 08:45:15 +00:00
|
|
|
for _, peer := range peers {
|
|
|
|
peerSet[peer] = struct{}{}
|
|
|
|
}
|
|
|
|
return peerSet
|
|
|
|
}
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
func (pm *PeerManager) selectServicePeer(criteria PeerSelectionCriteria) (PeerSet, error) {
|
|
|
|
peers := make(PeerSet)
|
2023-11-07 17:13:19 +00:00
|
|
|
var err error
|
|
|
|
for retryCnt := 0; retryCnt < 1; retryCnt++ {
|
|
|
|
//Try to fetch from serviceSlot
|
2024-01-26 08:45:15 +00:00
|
|
|
if slot := pm.serviceSlots.getPeers(criteria.Proto); slot != nil {
|
|
|
|
if len(criteria.PubsubTopics) == 0 || (len(criteria.PubsubTopics) == 1 && criteria.PubsubTopics[0] == "") {
|
2024-05-22 06:15:53 +00:00
|
|
|
return slot.getRandom(criteria.MaxPeers, criteria.ExcludePeers)
|
2023-11-07 17:13:19 +00:00
|
|
|
} else { //PubsubTopic based selection
|
|
|
|
keys := make([]peer.ID, 0, len(slot.m))
|
|
|
|
for i := range slot.m {
|
2024-05-22 06:15:53 +00:00
|
|
|
if PeerInSet(criteria.ExcludePeers, i) {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-07 17:13:19 +00:00
|
|
|
keys = append(keys, i)
|
|
|
|
}
|
2024-01-26 08:45:15 +00:00
|
|
|
selectedPeers := pm.host.Peerstore().(wps.WakuPeerstore).PeersByPubSubTopics(criteria.PubsubTopics, keys...)
|
2024-05-22 06:15:53 +00:00
|
|
|
tmpPeers, err := selectRandomPeers(selectedPeers, criteria.ExcludePeers, criteria.MaxPeers)
|
2024-01-26 08:45:15 +00:00
|
|
|
for tmpPeer := range tmpPeers {
|
|
|
|
peers[tmpPeer] = struct{}{}
|
|
|
|
}
|
|
|
|
if err == nil && len(peers) == criteria.MaxPeers {
|
|
|
|
return peers, nil
|
2023-11-07 17:13:19 +00:00
|
|
|
} else {
|
2024-01-26 08:45:15 +00:00
|
|
|
pm.logger.Debug("discovering peers by pubsubTopic", zap.Strings("pubsubTopics", criteria.PubsubTopics))
|
2023-11-07 17:13:19 +00:00
|
|
|
//Trigger on-demand discovery for this topic and connect to peer immediately.
|
|
|
|
//For now discover atleast 1 peer for the criteria
|
2024-01-26 08:45:15 +00:00
|
|
|
pm.discoverPeersByPubsubTopics(criteria.PubsubTopics, criteria.Proto, criteria.Ctx, 1)
|
2023-11-07 17:13:19 +00:00
|
|
|
//Try to fetch peers again.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-26 08:45:15 +00:00
|
|
|
if len(peers) == 0 {
|
2023-11-07 17:13:19 +00:00
|
|
|
pm.logger.Debug("could not retrieve random peer from slot", zap.Error(err))
|
|
|
|
}
|
2024-01-26 08:45:15 +00:00
|
|
|
return peers, ErrNoPeersAvailable
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PeerSelectionCriteria is the selection Criteria that is used by PeerManager to select peers.
|
|
|
|
type PeerSelectionCriteria struct {
|
2024-05-22 06:15:53 +00:00
|
|
|
SelectionType PeerSelection `json:"selectionType"`
|
|
|
|
Proto protocol.ID `json:"protocolId"`
|
|
|
|
PubsubTopics []string `json:"pubsubTopics"`
|
|
|
|
SpecificPeers peer.IDSlice `json:"specificPeers"`
|
|
|
|
MaxPeers int `json:"maxPeerCount"`
|
|
|
|
Ctx context.Context `json:"-"`
|
|
|
|
ExcludePeers PeerSet `json:"excludePeers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (psc PeerSelectionCriteria) String() string {
|
|
|
|
pscJson, err := json.Marshal(psc)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(pscJson)
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
// SelectPeers selects a peer based on selectionType specified.
|
2023-11-07 17:13:19 +00:00
|
|
|
// Context is required only in case of selectionType set to LowestRTT
|
2024-01-26 08:45:15 +00:00
|
|
|
func (pm *PeerManager) SelectPeers(criteria PeerSelectionCriteria) (peer.IDSlice, error) {
|
|
|
|
if criteria.MaxPeers == 0 {
|
|
|
|
criteria.MaxPeers = 1
|
|
|
|
}
|
2024-05-22 06:15:53 +00:00
|
|
|
excPeers := maps.Keys(criteria.ExcludePeers)
|
|
|
|
var excPeer peer.ID
|
|
|
|
if len(excPeers) > 0 {
|
|
|
|
excPeer = excPeers[0]
|
|
|
|
}
|
|
|
|
pm.logger.Debug("Select Peers", zap.Stringer("selectionCriteria", criteria), zap.Stringer("excludedPeers", excPeer))
|
2023-11-07 17:13:19 +00:00
|
|
|
switch criteria.SelectionType {
|
|
|
|
case Automatic:
|
2024-01-26 08:45:15 +00:00
|
|
|
return pm.SelectRandom(criteria)
|
2023-11-07 17:13:19 +00:00
|
|
|
case LowestRTT:
|
2024-01-26 08:45:15 +00:00
|
|
|
peerID, err := pm.SelectPeerWithLowestRTT(criteria)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//TODO: Update this once peer Ping cache PR is merged into this code.
|
|
|
|
return []peer.ID{peerID}, nil
|
2023-11-07 17:13:19 +00:00
|
|
|
default:
|
2024-01-26 08:45:15 +00:00
|
|
|
return nil, errors.New("unknown peer selection type specified")
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectPeerWithLowestRTT will select a peer that supports a specific protocol with the lowest reply time
|
|
|
|
// 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 node peerstore
|
|
|
|
// TO OPTIMIZE: As of now the peer with lowest RTT is identified when select is called, this should be optimized
|
|
|
|
// to maintain the RTT as part of peer-scoring and just select based on that.
|
|
|
|
func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) (peer.ID, error) {
|
|
|
|
var peers peer.IDSlice
|
|
|
|
var err error
|
|
|
|
if criteria.Ctx == nil {
|
|
|
|
pm.logger.Warn("context is not passed for peerSelectionwithRTT, using background context")
|
|
|
|
criteria.Ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-11-13 22:52:46 +00:00
|
|
|
if len(criteria.PubsubTopics) == 0 || (len(criteria.PubsubTopics) == 1 && criteria.PubsubTopics[0] == "") {
|
|
|
|
peers = pm.host.Peerstore().(wps.WakuPeerstore).PeersByPubSubTopics(criteria.PubsubTopics, criteria.SpecificPeers...)
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
peers, err = pm.FilterPeersByProto(peers, criteria.ExcludePeers, criteria.Proto)
|
2023-11-07 17:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-05-13 18:56:34 +00:00
|
|
|
return pm.rttCache.FastestPeer(criteria.Ctx, peers)
|
2023-11-07 17:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FilterPeersByProto filters list of peers that support specified protocols.
|
|
|
|
// If specificPeers is nil, all peers in the host's peerStore are considered for filtering.
|
2024-05-22 06:15:53 +00:00
|
|
|
func (pm *PeerManager) FilterPeersByProto(specificPeers peer.IDSlice, excludePeers PeerSet, proto ...protocol.ID) (peer.IDSlice, error) {
|
2023-11-07 17:13:19 +00:00
|
|
|
peerSet := specificPeers
|
|
|
|
if len(peerSet) == 0 {
|
|
|
|
peerSet = pm.host.Peerstore().Peers()
|
|
|
|
}
|
|
|
|
var peers peer.IDSlice
|
|
|
|
for _, peer := range peerSet {
|
|
|
|
protocols, err := pm.host.Peerstore().SupportsProtocols(peer, proto...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(protocols) > 0 {
|
2024-05-22 06:15:53 +00:00
|
|
|
//Maybe we can optimize below set of statements a better way??
|
|
|
|
if PeerInSet(excludePeers, peer) {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-07 17:13:19 +00:00
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return peers, nil
|
|
|
|
}
|