Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename NsPubsubTopic #2974

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tests/node/test_wakunode_sharding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ suite "Sharding":
# Given a connected server and client subscribed to different content topics
let
contentTopic1 = "/toychat/2/huilong/proto"
pubsubTopic1 = "/waku/2/rs/0/58355"
pubsubTopic12 = NsPubsubTopic.parse(contentTopic1)
shard1 = "/waku/2/rs/0/58355"
shard12 = RelayShard.parse(contentTopic1)
# Automatically generated from the contentTopic above
contentTopic2 = "/0/toychat2/2/huilong/proto"
pubsubTopic2 = "/waku/2/rs/0/23286"
shard2 = "/waku/2/rs/0/23286"
# Automatically generated from the contentTopic above

let
Expand All @@ -201,7 +201,7 @@ suite "Sharding":

# When the server publishes a message in the server's subscribed topic
discard await server.publish(
some(pubsubTopic1),
some(shard1),
WakuMessage(payload: "message1".toBytes(), contentTopic: contentTopic1),
)
let
Expand All @@ -216,7 +216,7 @@ suite "Sharding":
serverHandler.reset()
clientHandler.reset()
discard await client.publish(
some(pubsubTopic2),
some(shard2),
WakuMessage(payload: "message2".toBytes(), contentTopic: contentTopic2),
)
let
Expand Down
8 changes: 4 additions & 4 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 @@ -290,16 +290,16 @@ suite "Waku ENR - Relay static sharding":
shardsTopics.clusterId == clusterId
shardsTopics.shardIds == @[1u16]

let topics = shardsTopics.topics.mapIt($it)
let shards = shardsTopics.topics.mapIt($it)
check:
topics == @[$topic]
shards == @[$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
38 changes: 19 additions & 19 deletions tests/waku_core/test_namespaced_topics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ 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 shard = RelayShard.staticSharding(clusterId = 0, shardId = 2)

## When
let topic = $ns
let topic = $shard

## Then
check:
Expand All @@ -150,11 +150,11 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/waku-dev"

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

## Then
check nsRes.isErr()
let err = nsRes.tryError()
check shardRes.isErr()
let err = shardRes.tryError()
check:
err.kind == ParsingErrorKind.InvalidFormat

Expand All @@ -163,26 +163,26 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/rs/16/42"

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

## Then
check nsRes.isOk()
check shardRes.isOk()

let ns = nsRes.get()
let shard = shardRes.get()
check:
ns.clusterId == 16
ns.shardId == 42
shard.clusterId == 16
shard.shardId == 42

test "Parse pub-sub topic string - Invalid string: invalid protocol version":
## Given
let topic = "/waku/1/rs/16/42"

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

## Then
check ns.isErr()
let err = ns.tryError()
check shard.isErr()
let err = shard.tryError()
check:
err.kind == ParsingErrorKind.InvalidFormat

Expand All @@ -191,11 +191,11 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/rs//02"

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

## Then
check ns.isErr()
let err = ns.tryError()
check shard.isErr()
let err = shard.tryError()
check:
err.kind == ParsingErrorKind.MissingPart
err.part == "cluster_id"
Expand All @@ -205,10 +205,10 @@ suite "Waku Message - Pub-sub topics namespacing":
let topic = "/waku/2/rs/xx/77"

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

## Then
check ns.isErr()
let err = ns.tryError()
check shard.isErr()
let err = shard.tryError()
check:
err.kind == ParsingErrorKind.InvalidFormat
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
Loading
Loading