fix: use simple protocol and pubsubTopic based selection for peerExchange (#1295)

This commit is contained in:
Prem Chaitanya Prathi 2025-10-15 06:37:06 +05:30 committed by GitHub
parent 06c9af60f3
commit 84a4b1be7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 1 deletions

View File

@ -96,6 +96,7 @@ type PeerSelection int
const (
Automatic PeerSelection = iota
LowestRTT
ProtoPubSubTopicOnly //This is added to address an issue with peerExchange where on-demand discovery cannot be used.
)
const maxFailedAttempts = 5

View File

@ -204,6 +204,12 @@ func (pm *PeerManager) SelectPeers(criteria PeerSelectionCriteria) (peer.IDSlice
}
//TODO: Update this once peer Ping cache PR is merged into this code.
return []peer.ID{peerID}, nil
case ProtoPubSubTopicOnly:
peers, err := pm.SelectPeersByProto(criteria.Proto, criteria.SpecificPeers, criteria.PubsubTopics)
if err != nil {
return nil, err
}
return peers, nil
default:
return nil, errors.New("unknown peer selection type specified")
}
@ -257,3 +263,16 @@ func (pm *PeerManager) FilterPeersByProto(specificPeers peer.IDSlice, excludePee
}
return peers, nil
}
func (pm *PeerManager) SelectPeersByProto(protocol protocol.ID, specificPeers peer.IDSlice, pubsubTopics []string) (peer.IDSlice, error) {
var selectedPeers peer.IDSlice
selectedPeers, err := pm.FilterPeersByProto(specificPeers, nil, protocol)
if err != nil {
return nil, err
}
selectedPeers = pm.host.Peerstore().(wps.WakuPeerstore).PeersByPubSubTopics(pubsubTopics, selectedPeers...)
if len(selectedPeers) == 0 {
return nil, utils.ErrNoPeersAvailable
}
return selectedPeers, nil
}

View File

@ -53,7 +53,7 @@ func (wakuPX *WakuPeerExchange) Request(ctx context.Context, numPeers int, opts
}
selectedPeers, err := wakuPX.pm.SelectPeers(
peermanager.PeerSelectionCriteria{
SelectionType: params.peerSelectionType,
SelectionType: peermanager.ProtoPubSubTopicOnly, //Overriding selection type, this is hacky but to avoid refactor
Proto: PeerExchangeID_v20alpha1,
PubsubTopics: pubsubTopics,
SpecificPeers: params.preferredPeers,