diff --git a/waku/v2/node/wakunode2.go b/waku/v2/node/wakunode2.go index 5d68374b..7c0e70c8 100644 --- a/waku/v2/node/wakunode2.go +++ b/waku/v2/node/wakunode2.go @@ -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 } diff --git a/waku/v2/service/common_discovery_service.go b/waku/v2/service/common_discovery_service.go index 651c349b..c5c12b64 100644 --- a/waku/v2/service/common_discovery_service.go +++ b/waku/v2/service/common_discovery_service.go @@ -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