Skip to content

Commit

Permalink
rename NsPubsubTopic
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Aug 15, 2024
1 parent 2fd4eb6 commit 31200d6
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 104 deletions.
2 changes: 1 addition & 1 deletion tests/all_tests_waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Waku core test suite
import
./waku_core/test_namespaced_topics,
./waku_core/test_relay_shard,
./waku_core/test_time,
./waku_core/test_message_digest,
./waku_core/test_peers,
Expand Down
2 changes: 1 addition & 1 deletion tests/node/test_wakunode_sharding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ suite "Sharding":
let
contentTopic1 = "/toychat/2/huilong/proto"
pubsubTopic1 = "/waku/2/rs/0/58355"
pubsubTopic12 = NsPubsubTopic.parse(contentTopic1)
pubsubTopic12 = RelayShard.parse(contentTopic1)
# Automatically generated from the contentTopic above
contentTopic2 = "/0/toychat2/2/huilong/proto"
pubsubTopic2 = "/waku/2/rs/0/23286"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_waku_enr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ suite "Waku ENR - Relay static sharding":
clusterId: uint16 = 22
shardId: uint16 = 1

let topic = NsPubsubTopic.staticSharding(clusterId, shardId)
let shard = RelayShard.staticSharding(clusterId, shardId)

## When
let shardsTopics = RelayShards.init(clusterId, shardId).expect("Valid Shards")
Expand All @@ -292,14 +292,14 @@ suite "Waku ENR - Relay static sharding":

let topics = shardsTopics.topics.mapIt($it)
check:
topics == @[$topic]
topics == @[$shard]

check:
shardsTopics.contains(clusterId, shardId)
not shardsTopics.contains(clusterId, 33u16)
not shardsTopics.contains(20u16, 33u16)

shardsTopics.contains(topic)
shardsTopics.contains(shard)
shardsTopics.contains("/waku/2/rs/22/1")

test "new relay shards object with repeated but valid shard ids":
Expand Down
8 changes: 4 additions & 4 deletions tests/testlib/tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import std/[tables, sequtils, options]
import waku/waku_core/topics, ../testlib/wakucore

proc `==`*(
table: Table[pubsub_topic.NsPubsubTopic, seq[NsContentTopic]],
table: Table[pubsub_topic.RelayShard, seq[NsContentTopic]],
other: array[0 .. 0, (string, seq[string])],
): bool =
let otherTyped = other.map(
proc(item: (string, seq[string])): (NsPubsubTopic, seq[NsContentTopic]) =
proc(item: (string, seq[string])): (RelayShard, seq[NsContentTopic]) =
let
(pubsubTopic, contentTopics) = item
nsPubsubTopic = NsPubsubTopic.parse(pubsubTopic).value()
shard = RelayShard.parse(pubsubTopic).value()
nsContentTopics = contentTopics.map(
proc(contentTopic: string): NsContentTopic =
NsContentTopic.parse(contentTopic).value()
)
return (nsPubsubTopic, nsContentTopics)
return (shard, nsContentTopics)
)

table == otherTyped.toTable()
2 changes: 1 addition & 1 deletion tests/testlib/wakunode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ proc newTestWakuNode*(

let clusterId =
if pubsubTopics.len() > 0:
NsPubsubTopic.parse(pubsubTopics[0]).get().clusterId
RelayShard.parse(pubsubTopics[0]).get().clusterId
else:
1.uint16

Expand Down
2 changes: 1 addition & 1 deletion tests/waku_core/test_all.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import
./test_message_digest,
./test_namespaced_topics,
./test_relay_shard,
./test_peers,
./test_published_address,
./test_sharding,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ suite "Waku Message - Content topics namespacing":
suite "Waku Message - Pub-sub topics namespacing":
test "Stringify static sharding pub-sub topic":
## Given
var ns = NsPubsubTopic.staticSharding(clusterId = 0, shardId = 2)
var ns = RelayShard.staticSharding(clusterId = 0, shardId = 2)

## When
let topic = $ns
Expand All @@ -150,7 +150,7 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/waku-dev"

## When
let nsRes = NsPubsubTopic.parse(topic)
let nsRes = RelayShard.parse(topic)

## Then
check nsRes.isErr()
Expand All @@ -163,7 +163,7 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/rs/16/42"

## When
let nsRes = NsPubsubTopic.parse(topic)
let nsRes = RelayShard.parse(topic)

## Then
check nsRes.isOk()
Expand All @@ -178,7 +178,7 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/1/rs/16/42"

## When
let ns = NsPubsubTopic.parse(topic)
let ns = RelayShard.parse(topic)

## Then
check ns.isErr()
Expand All @@ -191,7 +191,7 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/rs//02"

## When
let ns = NsPubsubTopic.parse(topic)
let ns = RelayShard.parse(topic)

## Then
check ns.isErr()
Expand All @@ -205,7 +205,7 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/rs/xx/77"

## When
let ns = NsPubsubTopic.parse(topic)
let ns = RelayShard.parse(topic)

## Then
check ns.isErr()
Expand Down
16 changes: 8 additions & 8 deletions tests/waku_core/topics/test_pubsub_topic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import waku/waku_core/topics/pubsub_topic, ../../testlib/[wakucore]

suite "Static Sharding Functionality":
test "Shard Cluster Identification":
let topic = NsPubsubTopic.parseStaticSharding("/waku/2/rs/0/1").get()
let shard = RelayShard.parseStaticSharding("/waku/2/rs/0/1").get()
check:
topic.clusterId == 0
topic.shardId == 1
topic == NsPubsubTopic.staticSharding(0, 1)
shard.clusterId == 0
shard.shardId == 1
shard == RelayShard.staticSharding(0, 1)

test "Pubsub Topic Naming Compliance":
let topic = NsPubsubTopic.staticSharding(0, 1)
let shard = RelayShard.staticSharding(0, 1)
check:
topic.clusterId == 0
topic.shardId == 1
topic == "/waku/2/rs/0/1"
shard.clusterId == 0
shard.shardId == 1
shard == "/waku/2/rs/0/1"
78 changes: 34 additions & 44 deletions tests/waku_core/topics/test_sharding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,112 +41,102 @@ suite "Autosharding":

# When we generate a gen0 shard from them
let
nsPubsubTopic1 =
sharding.getGenZeroShard(nsContentTopic1, GenerationZeroShardsCount)
nsPubsubTopic2 =
sharding.getGenZeroShard(nsContentTopic2, GenerationZeroShardsCount)
nsPubsubTopic3 =
sharding.getGenZeroShard(nsContentTopic3, GenerationZeroShardsCount)
nsPubsubTopic4 =
sharding.getGenZeroShard(nsContentTopic4, GenerationZeroShardsCount)
nsPubsubTopic5 =
sharding.getGenZeroShard(nsContentTopic5, GenerationZeroShardsCount)
nsPubsubTopic6 =
sharding.getGenZeroShard(nsContentTopic6, GenerationZeroShardsCount)
nsPubsubTopic7 =
sharding.getGenZeroShard(nsContentTopic7, GenerationZeroShardsCount)
nsPubsubTopic8 =
sharding.getGenZeroShard(nsContentTopic8, GenerationZeroShardsCount)
nsPubsubTopic9 =
sharding.getGenZeroShard(nsContentTopic9, GenerationZeroShardsCount)
nsPubsubTopic10 =
sharding.getGenZeroShard(nsContentTopic10, GenerationZeroShardsCount)
shard1 = sharding.getGenZeroShard(nsContentTopic1, GenerationZeroShardsCount)
shard2 = sharding.getGenZeroShard(nsContentTopic2, GenerationZeroShardsCount)
shard3 = sharding.getGenZeroShard(nsContentTopic3, GenerationZeroShardsCount)
shard4 = sharding.getGenZeroShard(nsContentTopic4, GenerationZeroShardsCount)
shard5 = sharding.getGenZeroShard(nsContentTopic5, GenerationZeroShardsCount)
shard6 = sharding.getGenZeroShard(nsContentTopic6, GenerationZeroShardsCount)
shard7 = sharding.getGenZeroShard(nsContentTopic7, GenerationZeroShardsCount)
shard8 = sharding.getGenZeroShard(nsContentTopic8, GenerationZeroShardsCount)
shard9 = sharding.getGenZeroShard(nsContentTopic9, GenerationZeroShardsCount)
shard10 = sharding.getGenZeroShard(nsContentTopic10, GenerationZeroShardsCount)

# Then the generated shards are valid
check:
nsPubsubTopic1 == NsPubsubTopic.staticSharding(ClusterId, 3)
nsPubsubTopic2 == NsPubsubTopic.staticSharding(ClusterId, 3)
nsPubsubTopic3 == NsPubsubTopic.staticSharding(ClusterId, 6)
nsPubsubTopic4 == NsPubsubTopic.staticSharding(ClusterId, 6)
nsPubsubTopic5 == NsPubsubTopic.staticSharding(ClusterId, 3)
nsPubsubTopic6 == NsPubsubTopic.staticSharding(ClusterId, 3)
nsPubsubTopic7 == NsPubsubTopic.staticSharding(ClusterId, 3)
nsPubsubTopic8 == NsPubsubTopic.staticSharding(ClusterId, 3)
nsPubsubTopic9 == NsPubsubTopic.staticSharding(ClusterId, 7)
nsPubsubTopic10 == NsPubsubTopic.staticSharding(ClusterId, 3)
shard1 == RelayShard.staticSharding(ClusterId, 3)
shard2 == RelayShard.staticSharding(ClusterId, 3)
shard3 == RelayShard.staticSharding(ClusterId, 6)
shard4 == RelayShard.staticSharding(ClusterId, 6)
shard5 == RelayShard.staticSharding(ClusterId, 3)
shard6 == RelayShard.staticSharding(ClusterId, 3)
shard7 == RelayShard.staticSharding(ClusterId, 3)
shard8 == RelayShard.staticSharding(ClusterId, 3)
shard9 == RelayShard.staticSharding(ClusterId, 7)
shard10 == RelayShard.staticSharding(ClusterId, 3)

suite "getShard from NsContentTopic":
test "Generate Gen0 Shard with topic.generation==none":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)

# When we get a shard from a topic without generation
let nsPubsubTopic = sharding.getShard(contentTopicShort)
let shard = sharding.getShard(contentTopicShort)

# Then the generated shard is valid
check:
nsPubsubTopic.value() == NsPubsubTopic.staticSharding(ClusterId, 3)
shard.value() == RelayShard.staticSharding(ClusterId, 3)

test "Generate Gen0 Shard with topic.generation==0":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)
# When we get a shard from a gen0 topic
let nsPubsubTopic = sharding.getShard(contentTopicFull)
let shard = sharding.getShard(contentTopicFull)

# Then the generated shard is valid
check:
nsPubsubTopic.value() == NsPubsubTopic.staticSharding(ClusterId, 3)
shard.value() == RelayShard.staticSharding(ClusterId, 3)

test "Generate Gen0 Shard with topic.generation==other":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)
# When we get a shard from ain invalid content topic
let nsPubsubTopic = sharding.getShard(contentTopicInvalid)
let shard = sharding.getShard(contentTopicInvalid)

# Then the generated shard is valid
check:
nsPubsubTopic.error() == "Generation > 0 are not supported yet"
shard.error() == "Generation > 0 are not supported yet"

suite "getShard from ContentTopic":
test "Generate Gen0 Shard with topic.generation==none":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)
# When we get a shard from it
let nsPubsubTopic = sharding.getShard(contentTopicShort)
let shard = sharding.getShard(contentTopicShort)

# Then the generated shard is valid
check:
nsPubsubTopic.value() == NsPubsubTopic.staticSharding(ClusterId, 3)
shard.value() == RelayShard.staticSharding(ClusterId, 3)

test "Generate Gen0 Shard with topic.generation==0":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)
# When we get a shard from it
let nsPubsubTopic = sharding.getShard(contentTopicFull)
let shard = sharding.getShard(contentTopicFull)

# Then the generated shard is valid
check:
nsPubsubTopic.value() == NsPubsubTopic.staticSharding(ClusterId, 3)
shard.value() == RelayShard.staticSharding(ClusterId, 3)

test "Generate Gen0 Shard with topic.generation==other":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)
# When we get a shard from it
let nsPubsubTopic = sharding.getShard(contentTopicInvalid)
let shard = sharding.getShard(contentTopicInvalid)

# Then the generated shard is valid
check:
nsPubsubTopic.error() == "Generation > 0 are not supported yet"
shard.error() == "Generation > 0 are not supported yet"

test "Generate Gen0 Shard invalid topic":
let sharding =
Sharding(clusterId: ClusterId, shardCountGenZero: GenerationZeroShardsCount)
# When we get a shard from it
let nsPubsubTopic = sharding.getShard("invalid")
let shard = sharding.getShard("invalid")

# Then the generated shard is valid
check:
nsPubsubTopic.error() == "invalid format: topic must start with slash"
shard.error() == "invalid format: topic must start with slash"

suite "parseSharding":
test "contentTopics is ContentTopic":
Expand Down
22 changes: 11 additions & 11 deletions waku/waku_core/topics/pubsub_topic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ const DefaultPubsubTopic* = PubsubTopic("/waku/2/rs/0/0")

## Namespaced pub-sub topic

type NsPubsubTopic* = object
type RelayShard* = object
clusterId*: uint16
shardId*: uint16

proc staticSharding*(T: type NsPubsubTopic, clusterId, shardId: uint16): T =
return NsPubsubTopic(clusterId: clusterId, shardId: shardId)
proc staticSharding*(T: type RelayShard, clusterId, shardId: uint16): T =
return RelayShard(clusterId: clusterId, shardId: shardId)

# Serialization

proc `$`*(topic: NsPubsubTopic): string =
proc `$`*(topic: RelayShard): string =
## Returns a string representation of a namespaced topic
## in the format `/waku/2/rs/<cluster-id>/<shard-id>
return "/waku/2/rs/" & $topic.clusterId & "/" & $topic.shardId
Expand All @@ -38,8 +38,8 @@ const
StaticShardingPubsubTopicPrefix = Waku2PubsubTopicPrefix & "/rs"

proc parseStaticSharding*(
T: type NsPubsubTopic, topic: PubsubTopic
): ParsingResult[NsPubsubTopic] =
T: type RelayShard, topic: PubsubTopic
): ParsingResult[RelayShard] =
if not topic.startsWith(StaticShardingPubsubTopicPrefix):
return err(
ParsingError.invalidFormat("must start with " & StaticShardingPubsubTopicPrefix)
Expand Down Expand Up @@ -67,19 +67,19 @@ proc parseStaticSharding*(
ParsingError.invalidFormat($err)
)

ok(NsPubsubTopic.staticSharding(clusterId, shardId))
ok(RelayShard.staticSharding(clusterId, shardId))

proc parse*(T: type NsPubsubTopic, topic: PubsubTopic): ParsingResult[NsPubsubTopic] =
proc parse*(T: type RelayShard, topic: PubsubTopic): ParsingResult[RelayShard] =
## Splits a namespaced topic string into its constituent parts.
## The topic string has to be in the format `/<application>/<version>/<topic-name>/<encoding>`
NsPubsubTopic.parseStaticSharding(topic)
RelayShard.parseStaticSharding(topic)

# Pubsub topic compatibility

converter toPubsubTopic*(topic: NsPubsubTopic): PubsubTopic =
converter toPubsubTopic*(topic: RelayShard): PubsubTopic =
$topic

proc `==`*[T: NsPubsubTopic](x, y: T): bool =
proc `==`*[T: RelayShard](x, y: T): bool =
if x.clusterId != y.clusterId:
return false

Expand Down
Loading

0 comments on commit 31200d6

Please sign in to comment.