fix: filter out peers to be considered for circuit-relay (#1130)

Co-authored-by: Richard Ramos <info@richardramos.me>
This commit is contained in:
Prem Chaitanya Prathi 2024-06-18 21:19:23 +05:30 committed by GitHub
parent a06208321e
commit 795322a196
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View File

@ -893,13 +893,26 @@ func (w *WakuNode) findRelayNodes(ctx context.Context) {
rand.Shuffle(len(peers), func(i, j int) { peers[i], peers[j] = peers[j], peers[i] })
for _, p := range peers {
pENR, err := w.Host().Peerstore().(wps.WakuPeerstore).ENR(p.ID)
if err != nil {
w.log.Debug("could not get ENR for the peer, skipping for circuit-relay", zap.Stringer("peer", p.ID), zap.Error(err))
continue
}
rs, err := enr.RelayShardList(pENR.Record())
if err != nil || rs == nil {
w.log.Debug("could not get shard info for the peer from ENR, skipping for circuit-relay", zap.Stringer("peer", p.ID), zap.Error(err))
continue
}
if rs.ClusterID != w.ClusterID() {
w.log.Debug("clusterID mismatch for the peer, skipping for circuit-relay", zap.Stringer("peer", p.ID), zap.Error(err))
continue
}
info := w.Host().Peerstore().PeerInfo(p.ID)
supportedProtocols, err := w.Host().Peerstore().SupportsProtocols(p.ID, proto.ProtoIDv2Hop)
if err != nil {
w.log.Error("could not check supported protocols", zap.Error(err))
continue
}
if len(supportedProtocols) == 0 {
continue
}

View File

@ -18,8 +18,9 @@ type PeerData struct {
}
type CommonDiscoveryService struct {
commonService *CommonService
channel chan PeerData
commonService *CommonService
channel chan PeerData
canWriteToChannel sync.Mutex
}
func NewCommonDiscoveryService() *CommonDiscoveryService {
@ -42,7 +43,9 @@ func (sp *CommonDiscoveryService) Stop(stopFn func()) {
stopFn()
sp.WaitGroup().Wait() // waitgroup is waited here so that channel can be closed after all the go rountines have stopped in service.
// there is a wait in the CommonService too
sp.canWriteToChannel.Lock()
close(sp.channel)
sp.canWriteToChannel.Unlock()
})
}
func (sp *CommonDiscoveryService) GetListeningChan() <-chan PeerData {
@ -52,6 +55,10 @@ func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool {
if err := sp.ErrOnNotRunning(); err != nil {
return false
}
sp.canWriteToChannel.Lock()
defer sp.canWriteToChannel.Unlock()
select {
case sp.channel <- data:
return true