From 18f65802c3ce3e3657a07ac25f06d3372d4ef96a Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 31 Aug 2023 08:51:17 -0400 Subject: [PATCH] fix: allow mixing named and static shards --- waku/v2/discv5/discover.go | 9 ++++++++- waku/v2/node/localnode.go | 15 +++++---------- waku/v2/protocol/shard.go | 4 ++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/waku/v2/discv5/discover.go b/waku/v2/discv5/discover.go index 0dee3dee8..8482c1448 100644 --- a/waku/v2/discv5/discover.go +++ b/waku/v2/discv5/discover.go @@ -453,10 +453,17 @@ func (d *DiscoveryV5) peerLoop(ctx context.Context) error { } nodeRS, err := wenr.RelaySharding(n.Record()) - if err != nil || nodeRS == nil { + if err != nil { return false } + if nodeRS == nil { + // TODO: Node has no shard registered. + // Since for now, status-go uses both mixed static and named shards, we assume the node is valid + // Once status-go uses only static shards, we can't return true anymore. + return true + } + if nodeRS.Cluster != localRS.Cluster { return false } diff --git a/waku/v2/node/localnode.go b/waku/v2/node/localnode.go index 329f3feb5..34d39daa7 100644 --- a/waku/v2/node/localnode.go +++ b/waku/v2/node/localnode.go @@ -315,22 +315,17 @@ func (w *WakuNode) watchTopicShards(ctx context.Context) error { if len(rs) > 0 { if len(rs) > 1 { - w.log.Warn("could not set ENR shard info", zap.String("error", "use sharded topics within the same cluster")) - continue - } - - tcount := 0 - for _, r := range rs { - tcount += len(r.Indices) - } - if tcount != len(topics) { - w.log.Warn("could not set ENR shard info", zap.String("error", "can't use a mix of static shards and named shards")) + w.log.Warn("could not set ENR shard info", zap.String("error", "multiple clusters found, use sharded topics within the same cluster")) continue } } if len(rs) == 1 { w.log.Info("updating advertised relay shards in ENR") + if len(rs[0].Indices) != len(topics) { + w.log.Warn("A mix of named and static shards found. ENR shard will contain only the following shards", zap.Any("shards", rs[0])) + } + err = wenr.Update(w.localNode, wenr.WithWakuRelaySharding(rs[0])) if err != nil { w.log.Warn("could not set ENR shard info", zap.Error(err)) diff --git a/waku/v2/protocol/shard.go b/waku/v2/protocol/shard.go index e98496acd..1bad75463 100644 --- a/waku/v2/protocol/shard.go +++ b/waku/v2/protocol/shard.go @@ -20,8 +20,8 @@ const ClusterIndex = 1 const GenerationZeroShardsCount = 8 type RelayShards struct { - Cluster uint16 - Indices []uint16 + Cluster uint16 `json:"cluster"` + Indices []uint16 `json:"indices"` } func NewRelayShards(cluster uint16, indices ...uint16) (RelayShards, error) {