chore: rename NsPubsubTopic (#2974)

This commit is contained in:
gabrielmer 2024-08-19 11:29:35 +02:00 committed by GitHub
parent b8550c5550
commit 67439057fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 110 additions and 120 deletions

View File

@ -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
@ -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
@ -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

View File

@ -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")
@ -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":

View File

@ -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()

View File

@ -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

View File

@ -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:
@ -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
@ -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
@ -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"
@ -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

View File

@ -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"

View File

@ -41,39 +41,29 @@ 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":
@ -81,72 +71,72 @@ suite "Autosharding":
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":

View File

@ -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
@ -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)
@ -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

View File

@ -16,7 +16,7 @@ type Sharding* = object
proc new*(T: type Sharding, clusterId: uint16, shardCount: uint32): T =
return Sharding(clusterId: clusterId, shardCountGenZero: shardCount)
proc getGenZeroShard*(s: Sharding, topic: NsContentTopic, count: int): NsPubsubTopic =
proc getGenZeroShard*(s: Sharding, topic: NsContentTopic, count: int): RelayShard =
let bytes = toBytes(topic.application) & toBytes(topic.version)
let hash = sha256.digest(bytes)
@ -27,9 +27,9 @@ proc getGenZeroShard*(s: Sharding, topic: NsContentTopic, count: int): NsPubsubT
# This is equilavent to modulo shard count but faster
let shard = hashValue and uint64((count - 1))
NsPubsubTopic.staticSharding(s.clusterId, uint16(shard))
RelayShard.staticSharding(s.clusterId, uint16(shard))
proc getShard*(s: Sharding, topic: NsContentTopic): Result[NsPubsubTopic, string] =
proc getShard*(s: Sharding, topic: NsContentTopic): Result[RelayShard, string] =
## Compute the (pubsub topic) shard to use for this content topic.
if topic.generation.isNone():
@ -54,14 +54,14 @@ proc parseSharding*(
s: Sharding,
pubsubTopic: Option[PubsubTopic],
contentTopics: ContentTopic | seq[ContentTopic],
): Result[Table[NsPubsubTopic, seq[NsContentTopic]], string] =
): Result[Table[RelayShard, seq[NsContentTopic]], string] =
var topics: seq[ContentTopic]
when contentTopics is seq[ContentTopic]:
topics = contentTopics
else:
topics = @[contentTopics]
var topicMap = initTable[NsPubsubTopic, seq[NsContentTopic]]()
var topicMap = initTable[RelayShard, seq[NsContentTopic]]()
for contentTopic in topics:
let parseRes = NsContentTopic.parse(contentTopic)
@ -73,7 +73,7 @@ proc parseSharding*(
let pubsub =
if pubsubTopic.isSome():
let parseRes = NsPubsubTopic.parse(pubsubTopic.get())
let parseRes = RelayShard.parse(pubsubTopic.get())
if parseRes.isErr():
return err("Cannot parse pubsub topic: " & $parseRes.error)
@ -97,7 +97,7 @@ proc parseSharding*(
ok(topicMap)
#type ShardsPriority = seq[tuple[topic: NsPubsubTopic, value: float64]]
#type ShardsPriority = seq[tuple[topic: RelayShard, value: float64]]
#[ proc shardCount*(topic: NsContentTopic): Result[int, string] =
## Returns the total shard count from the content topic.
@ -117,7 +117,7 @@ proc parseSharding*(
#[ proc applyWeight(hashValue: uint64, weight: float64): float64 =
(-weight) / math.ln(float64(hashValue) / float64(high(uint64))) ]#
#[ proc hashOrder*(x, y: (NsPubsubTopic, float64)): int =
#[ proc hashOrder*(x, y: (RelayShard, float64)): int =
cmp(x[1], y[1]) ]#
#[ proc weightedShardList*(topic: NsContentTopic, shardCount: int, weightList: seq[float64]): Result[ShardsPriority, string] =
@ -127,10 +127,10 @@ proc parseSharding*(
let shardsNWeights = zip(toSeq(0..shardCount), weightList)
var list = newSeq[(NsPubsubTopic, float64)](shardCount)
var list = newSeq[(RelayShard, float64)](shardCount)
for (shard, weight) in shardsNWeights:
let pubsub = NsPubsubTopic.staticSharding(ClusterId, uint16(shard))
let pubsub = RelayShard.staticSharding(ClusterId, uint16(shard))
let clusterBytes = toBytesBE(uint16(ClusterId))
let shardBytes = toBytesBE(uint16(shard))
@ -145,7 +145,7 @@ proc parseSharding*(
ok(list) ]#
#[ proc singleHighestWeigthShard*(topic: NsContentTopic): Result[NsPubsubTopic, string] =
#[ proc singleHighestWeigthShard*(topic: NsContentTopic): Result[RelayShard, string] =
let count = ? shardCount(topic)
let weights = repeat(1.0, count)

View File

@ -24,8 +24,8 @@ type RelayShards* = object
clusterId*: uint16
shardIds*: seq[uint16]
func topics*(rs: RelayShards): seq[NsPubsubTopic] =
rs.shardIds.mapIt(NsPubsubTopic.staticSharding(rs.clusterId, it))
func topics*(rs: RelayShards): seq[RelayShard] =
rs.shardIds.mapIt(RelayShard.staticSharding(rs.clusterId, it))
func init*(T: type RelayShards, clusterId, shardId: uint16): Result[T, string] =
if shardId > MaxShardIndex:
@ -61,7 +61,7 @@ func topicsToRelayShards*(topics: seq[string]): Result[Option[RelayShards], stri
if topics.len < 1:
return ok(none(RelayShards))
let parsedTopicsRes = topics.mapIt(NsPubsubTopic.parse(it))
let parsedTopicsRes = topics.mapIt(RelayShard.parse(it))
for res in parsedTopicsRes:
if res.isErr():
@ -80,11 +80,11 @@ func topicsToRelayShards*(topics: seq[string]): Result[Option[RelayShards], stri
func contains*(rs: RelayShards, clusterId, shardId: uint16): bool =
return rs.clusterId == clusterId and rs.shardIds.contains(shardId)
func contains*(rs: RelayShards, topic: NsPubsubTopic): bool =
return rs.contains(topic.clusterId, topic.shardId)
func contains*(rs: RelayShards, shard: RelayShard): bool =
return rs.contains(shard.clusterId, shard.shardId)
func contains*(rs: RelayShards, topic: PubsubTopic): bool =
let parseRes = NsPubsubTopic.parse(topic)
let parseRes = RelayShard.parse(topic)
if parseRes.isErr():
return false
@ -235,11 +235,11 @@ proc containsShard*(r: Record, clusterId, shardId: uint16): bool =
rs.contains(clusterId, shardId)
proc containsShard*(r: Record, topic: NsPubsubTopic): bool =
return containsShard(r, topic.clusterId, topic.shardId)
proc containsShard*(r: Record, shard: RelayShard): bool =
return containsShard(r, shard.clusterId, shard.shardId)
proc containsShard*(r: Record, topic: PubsubTopic): bool =
let parseRes = NsPubsubTopic.parse(topic)
let parseRes = RelayShard.parse(topic)
if parseRes.isErr():
debug "invalid static sharding topic", topic = topic, error = parseRes.error
return false

View File

@ -128,17 +128,17 @@ proc subscriptionsListener(wm: WakuMetadata) {.async.} =
let events = await wm.topicSubscriptionQueue.waitEvents(key)
for event in events:
let parsedTopic = NsPubsubTopic.parse(event.topic).valueOr:
let parsedShard = RelayShard.parse(event.topic).valueOr:
continue
if parsedTopic.clusterId != wm.clusterId:
if parsedShard.clusterId != wm.clusterId:
continue
case event.kind
of PubsubSub:
wm.shards.incl(parsedTopic.shardId)
wm.shards.incl(parsedShard.shardId)
of PubsubUnsub:
wm.shards.excl(parsedTopic.shardId)
wm.shards.excl(parsedShard.shardId)
else:
continue