Skip to content

Commit

Permalink
fix: --topic should be ignore when using --pubsub-topic or --content-…
Browse files Browse the repository at this point in the history
…topic (#1977)
  • Loading branch information
SionoiS authored Aug 31, 2023
1 parent 7d37f2e commit 037b166
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
39 changes: 19 additions & 20 deletions apps/wakunode2/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,19 @@ proc init*(T: type App, rng: ref HmacDrbgContext, conf: WakuNodeConf): T =

enrBuilder.withMultiaddrs(netConfig.enrMultiaddrs)

let contentTopicsRes = conf.contentTopics.mapIt(NsContentTopic.parse(it))
let topics =
if conf.pubsubTopics.len > 0 or conf.contentTopics.len > 0:
let shardsRes = conf.contentTopics.mapIt(getShard(it))
for res in shardsRes:
if res.isErr():
error "failed to shard content topic", error=res.error
quit(QuitFailure)

for res in contentTopicsRes:
if res.isErr():
error "failed to parse content topic", error=res.error
quit(QuitFailure)

let shardsRes = contentTopicsRes.mapIt(getShard(it.get()))
let shards = shardsRes.mapIt(it.get())

for res in shardsRes:
if res.isErr():
error "failed to shard content topic", error=res.error
quit(QuitFailure)

let shards = shardsRes.mapIt($it.get())

let topics = conf.topics & conf.pubsubTopics & shards
conf.pubsubTopics & shards
else:
conf.topics

let addShardedTopics = enrBuilder.withShardedTopics(topics)
if addShardedTopics.isErr():
Expand Down Expand Up @@ -360,12 +356,15 @@ proc setupProtocols(node: WakuNode,
peerExchangeHandler = some(handlePeerExchange)

if conf.relay:
# TODO autoshard content topics only once.
# Already checked for errors in app.init
let contentTopics = conf.contentTopics.mapIt(NsContentTopic.parse(it).expect("Parsing"))
let shards = contentTopics.mapIt($(getShard(it).expect("Sharding")))
let pubsubTopics =
if conf.pubsubTopics.len > 0 or conf.contentTopics.len > 0:
# TODO autoshard content topics only once.
# Already checked for errors in app.init
let shards = conf.contentTopics.mapIt(getShard(it).expect("Valid Shard"))
conf.pubsubTopics & shards
else:
conf.topics

let pubsubTopics = conf.topics & conf.pubsubTopics & shards
try:
await mountRelay(node, pubsubTopics, peerExchangeHandler = peerExchangeHandler)
except CatchableError:
Expand Down
8 changes: 8 additions & 0 deletions waku/waku_core/topics/sharding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ proc getShard*(topic: NsContentTopic): Result[NsPubsubTopic, string] =
of 0: return ok(getGenZeroShard(topic, GenerationZeroShardsCount))
else: return err("Generation > 0 are not supported yet")

proc getShard*(topic: ContentTopic): Result[PubsubTopic, string] =
let parsedTopic = NsContentTopic.parse(topic).valueOr:
return err($error)

let shard = ?getShard(parsedTopic)

ok($shard)

proc parseSharding*(pubsubTopic: Option[PubsubTopic], contentTopics: ContentTopic|seq[ContentTopic]): Result[Table[NsPubsubTopic, seq[NsContentTopic]], string] =
var topics: seq[ContentTopic]
when contentTopics is seq[ContentTopic]:
Expand Down

0 comments on commit 037b166

Please sign in to comment.