Skip to content

Commit 795322a

Browse files
fix: filter out peers to be considered for circuit-relay (#1130)
Co-authored-by: Richard Ramos <[email protected]>
1 parent a062083 commit 795322a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

waku/v2/node/wakunode2.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -893,13 +893,26 @@ func (w *WakuNode) findRelayNodes(ctx context.Context) {
893893
rand.Shuffle(len(peers), func(i, j int) { peers[i], peers[j] = peers[j], peers[i] })
894894

895895
for _, p := range peers {
896+
pENR, err := w.Host().Peerstore().(wps.WakuPeerstore).ENR(p.ID)
897+
if err != nil {
898+
w.log.Debug("could not get ENR for the peer, skipping for circuit-relay", zap.Stringer("peer", p.ID), zap.Error(err))
899+
continue
900+
}
901+
rs, err := enr.RelayShardList(pENR.Record())
902+
if err != nil || rs == nil {
903+
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))
904+
continue
905+
}
906+
if rs.ClusterID != w.ClusterID() {
907+
w.log.Debug("clusterID mismatch for the peer, skipping for circuit-relay", zap.Stringer("peer", p.ID), zap.Error(err))
908+
continue
909+
}
896910
info := w.Host().Peerstore().PeerInfo(p.ID)
897911
supportedProtocols, err := w.Host().Peerstore().SupportsProtocols(p.ID, proto.ProtoIDv2Hop)
898912
if err != nil {
899913
w.log.Error("could not check supported protocols", zap.Error(err))
900914
continue
901915
}
902-
903916
if len(supportedProtocols) == 0 {
904917
continue
905918
}

waku/v2/service/common_discovery_service.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type PeerData struct {
1818
}
1919

2020
type CommonDiscoveryService struct {
21-
commonService *CommonService
22-
channel chan PeerData
21+
commonService *CommonService
22+
channel chan PeerData
23+
canWriteToChannel sync.Mutex
2324
}
2425

2526
func NewCommonDiscoveryService() *CommonDiscoveryService {
@@ -42,7 +43,9 @@ func (sp *CommonDiscoveryService) Stop(stopFn func()) {
4243
stopFn()
4344
sp.WaitGroup().Wait() // waitgroup is waited here so that channel can be closed after all the go rountines have stopped in service.
4445
// there is a wait in the CommonService too
46+
sp.canWriteToChannel.Lock()
4547
close(sp.channel)
48+
sp.canWriteToChannel.Unlock()
4649
})
4750
}
4851
func (sp *CommonDiscoveryService) GetListeningChan() <-chan PeerData {
@@ -52,6 +55,10 @@ func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool {
5255
if err := sp.ErrOnNotRunning(); err != nil {
5356
return false
5457
}
58+
59+
sp.canWriteToChannel.Lock()
60+
defer sp.canWriteToChannel.Unlock()
61+
5562
select {
5663
case sp.channel <- data:
5764
return true

0 commit comments

Comments
 (0)