Skip to content

Commit

Permalink
fix: cluster id & sharding terminology (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
SionoiS committed Oct 5, 2023
1 parent 7b5c36b commit a47dc9e
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 192 deletions.
30 changes: 15 additions & 15 deletions tests/test_waku_discv5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,15 @@ procSuite "Waku Discovery v5":
# Cleanup
await allFutures(node1.stop(), node2.stop(), node3.stop(), node4.stop())

asyncTest "get relayShards from topics":
asyncTest "get shards from topics":
## Given
let mixedTopics = @["/waku/2/thisisatest", "/waku/2/rs/0/2", "/waku/2/rs/0/8"]
let shardedTopics = @["/waku/2/rs/0/2", "/waku/2/rs/0/4", "/waku/2/rs/0/8"]
let namedTopics = @["/waku/2/thisisatest", "/waku/2/atestthisis", "/waku/2/isthisatest"]
let gibberish = @["aedyttydcb/uioasduyio", "jhdfsjhlsdfjhk/sadjhk", "khfsd/hjfdsgjh/dfs"]
let empty: seq[string] = @[]

let relayShards = RelayShards.init(0, @[uint16(2), uint16(4), uint16(8)]).expect("Valid Shards")
let shardsTopics = RelayShards.init(0, @[uint16(2), uint16(4), uint16(8)]).expect("Valid shardIds")

## When

Expand All @@ -298,7 +298,7 @@ procSuite "Waku Discovery v5":
assert mixedRes.isErr(), $mixedRes.value
assert shardedRes.isOk(), shardedRes.error
assert shardedRes.value.isSome()
assert shardedRes.value.get() == relayShards, $shardedRes.value.get()
assert shardedRes.value.get() == shardsTopics, $shardedRes.value.get()
assert namedRes.isOk(), namedRes.error
assert namedRes.value.isNone(), $namedRes.value
assert gibberishRes.isErr(), $gibberishRes.value
Expand All @@ -313,13 +313,13 @@ procSuite "Waku Discovery v5":
enrPrivKey = generatesecp256k1key()

let
shardCluster: uint16 = 21
shardIndices: seq[uint16] = @[1u16, 2u16, 5u16, 7u16, 9u16, 11u16]
clusterId: uint16 = 21
shardIds: seq[uint16] = @[1u16, 2u16, 5u16, 7u16, 9u16, 11u16]

let shards = RelayShards.init(shardCluster, shardIndices).expect("Valid Shards")
let shardsTopics = RelayShards.init(clusterId, shardIds).expect("Valid shardIds")

var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shards).isOk()
require builder.withWakuRelaySharding(shardsTopics).isOk()

let recordRes = builder.build()
require recordRes.isOk()
Expand All @@ -331,13 +331,13 @@ procSuite "Waku Discovery v5":
enrPrivKey = generatesecp256k1key()

let
shardCluster: uint16 = 22
shardIndices: seq[uint16] = @[2u16, 4u16, 5u16, 8u16, 10u16, 12u16]
clusterId: uint16 = 22
shardIds: seq[uint16] = @[2u16, 4u16, 5u16, 8u16, 10u16, 12u16]

let shards = RelayShards.init(shardCluster, shardIndices).expect("Valid Shards")
let shardsTopics = RelayShards.init(clusterId, shardIds).expect("Valid shardIds")

var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shards).isOk()
require builder.withWakuRelaySharding(shardsTopics).isOk()

let recordRes = builder.build()
require recordRes.isOk()
Expand All @@ -349,13 +349,13 @@ procSuite "Waku Discovery v5":
enrPrivKey = generatesecp256k1key()

let
shardCluster: uint16 = 22
shardIndices: seq[uint16] = @[1u16, 3u16, 6u16, 7u16, 9u16, 11u16]
clusterId: uint16 = 22
shardIds: seq[uint16] = @[1u16, 3u16, 6u16, 7u16, 9u16, 11u16]

let shards = RelayShards.init(shardCluster, shardIndices).expect("Valid Shards")
let shardsTopics = RelayShards.init(clusterId, shardIds).expect("Valid shardIds")

var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shards).isOk()
require builder.withWakuRelaySharding(shardsTopics).isOk()

let recordRes = builder.build()
require recordRes.isOk()
Expand Down
88 changes: 44 additions & 44 deletions tests/test_waku_enr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -255,71 +255,71 @@ suite "Waku ENR - Multiaddresses":

suite "Waku ENR - Relay static sharding":

test "new relay shards field with single invalid index":
test "new relay shards object with single invalid shard id":
## Given
let
shardCluster: uint16 = 22
shardIndex: uint16 = 1024
clusterId: uint16 = 22
shard: uint16 = 1024

## When
let res = RelayShards.init(shardCluster, shardIndex)
let shardsTopics = RelayShards.init(clusterId, shard)

## Then
assert res.isErr(), $res.get()
assert shardsTopics.isErr(), $shardsTopics.get()

test "new relay shards field with single invalid index in list":
test "new relay shards object with single invalid shard id in list":
## Given
let
shardCluster: uint16 = 22
shardIndices: seq[uint16] = @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16, 1024u16]
clusterId: uint16 = 22
shardIds: seq[uint16] = @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16, 1024u16]

## When
let res = RelayShards.init(shardCluster, shardIndices)
let shardsTopics = RelayShards.init(clusterId, shardIds)

## Then
assert res.isErr(), $res.get()
assert shardsTopics.isErr(), $shardsTopics.get()

test "new relay shards field with single valid index":
test "new relay shards object with single valid shard id":
## Given
let
shardCluster: uint16 = 22
shardIndex: uint16 = 1
clusterId: uint16 = 22
shardId: uint16 = 1

let topic = NsPubsubTopic.staticSharding(shardCluster, shardIndex)
let topic = NsPubsubTopic.staticSharding(clusterId, shardId)

## When
let shards = RelayShards.init(shardCluster, shardIndex).expect("Valid Shards")
let shardsTopics = RelayShards.init(clusterId, shardId).expect("Valid Shards")

## Then
check:
shards.cluster == shardCluster
shards.indices == @[1u16]
shardsTopics.clusterId == clusterId
shardsTopics.shardIds == @[1u16]

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

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

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

test "new relay shards field with repeated but valid indices":
test "new relay shards object with repeated but valid shard ids":
## Given
let
shardCluster: uint16 = 22
shardIndices: seq[uint16] = @[1u16, 2u16, 2u16, 3u16, 3u16, 3u16]
clusterId: uint16 = 22
shardIds: seq[uint16] = @[1u16, 2u16, 2u16, 3u16, 3u16, 3u16]

## When
let shards = RelayShards.init(shardCluster, shardIndices).expect("Valid Shards")
let shardsTopics = RelayShards.init(clusterId, shardIds).expect("Valid Shards")

## Then
check:
shards.cluster == shardCluster
shards.indices == @[1u16, 2u16, 3u16]
shardsTopics.clusterId == clusterId
shardsTopics.shardIds == @[1u16, 2u16, 3u16]

test "cannot decode relay shards from record if not present":
## Given
Expand All @@ -338,21 +338,21 @@ suite "Waku ENR - Relay static sharding":
## Then
check fieldOpt.isNone()

test "encode and decode record with relay shards field (EnrBuilder ext - indices list)":
test "encode and decode record with relay shards field (EnrBuilder ext - shardIds list)":
## Given
let
enrSeqNum = 1u64
enrPrivKey = generatesecp256k1key()

let
shardCluster: uint16 = 22
shardIndices: seq[uint16] = @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16]
clusterId: uint16 = 22
shardIds: seq[uint16] = @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16]

let shards = RelayShards.init(shardCluster, shardIndices).expect("Valid Shards")
let shardsTopics = RelayShards.init(clusterId, shardIds).expect("Valid Shards")

## When
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shards).isOk()
require builder.withWakuRelaySharding(shardsTopics).isOk()

let recordRes = builder.build()

Expand All @@ -366,18 +366,18 @@ suite "Waku ENR - Relay static sharding":
let shardsOpt = typedRecord.value.relaySharding
check:
shardsOpt.isSome()
shardsOpt.get() == shards
shardsOpt.get() == shardsTopics

test "encode and decode record with relay shards field (EnrBuilder ext - bit vector)":
## Given
let
enrSeqNum = 1u64
enrPrivKey = generatesecp256k1key()

let shards = RelayShards.init(33, toSeq(0u16 ..< 64u16)).expect("Valid Shards")
let shardsTopics = RelayShards.init(33, toSeq(0u16 ..< 64u16)).expect("Valid Shards")

var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelaySharding(shards).isOk()
require builder.withWakuRelaySharding(shardsTopics).isOk()

let recordRes = builder.build()
require recordRes.isOk()
Expand All @@ -393,22 +393,22 @@ suite "Waku ENR - Relay static sharding":
## Then
check:
shardsOpt.isSome()
shardsOpt.get() == shards
shardsOpt.get() == shardsTopics

test "decode record with relay shards indices list and bit vector fields":
test "decode record with relay shards shard list and bit vector fields":
## Given
let
enrSeqNum = 1u64
enrPrivKey = generatesecp256k1key()

let
shardsIndicesList = RelayShards.init(22, @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16]).expect("Valid Shards")
shardsBitVector = RelayShards.init(33, @[13u16, 24u16, 37u16, 61u16, 98u16, 159u16]).expect("Valid Shards")
relayShardsIndicesList = RelayShards.init(22, @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16]).expect("Valid Shards")
relayShardsBitVector = RelayShards.init(33, @[13u16, 24u16, 37u16, 61u16, 98u16, 159u16]).expect("Valid Shards")


var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
require builder.withWakuRelayShardingIndicesList(shardsIndicesList).isOk()
require builder.withWakuRelayShardingBitVector(shardsBitVector).isOk()
require builder.withWakuRelayShardingIndicesList(relayShardsIndicesList).isOk()
require builder.withWakuRelayShardingBitVector(relayShardsBitVector).isOk()

let recordRes = builder.build()
require recordRes.isOk()
Expand All @@ -424,4 +424,4 @@ suite "Waku ENR - Relay static sharding":
## Then
check:
shardsOpt.isSome()
shardsOpt.get() == shardsIndicesList
shardsOpt.get() == relayShardsIndicesList
12 changes: 6 additions & 6 deletions tests/waku_core/test_namespaced_topics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ suite "Waku Message - Pub-sub topics namespacing":

test "Stringify static sharding pub-sub topic":
## Given
var ns = NsPubsubTopic.staticSharding(cluster=0, shard=2)
var ns = NsPubsubTopic.staticSharding(clusterId=0, shardId=2)

## When
let topic = $ns
Expand Down Expand Up @@ -186,8 +186,8 @@ suite "Waku Message - Pub-sub topics namespacing":

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

test "Parse pub-sub topic string - Invalid string: invalid protocol version":
## Given
Expand All @@ -202,7 +202,7 @@ suite "Waku Message - Pub-sub topics namespacing":
check:
err.kind == ParsingErrorKind.InvalidFormat

test "Parse static sharding pub-sub topic string - Invalid string: empty shard value":
test "Parse static sharding pub-sub topic string - Invalid string: empty cluster id value":
## Given
let topic = "/waku/2/rs//02"

Expand All @@ -214,9 +214,9 @@ suite "Waku Message - Pub-sub topics namespacing":
let err = ns.tryError()
check:
err.kind == ParsingErrorKind.MissingPart
err.part == "shard_cluster_index"
err.part == "cluster_id"

test "Parse static sharding pub-sub topic string - Invalid string: cluster value":
test "Parse static sharding pub-sub topic string - Invalid string: cluster id value":
## Given
let topic = "/waku/2/rs/xx/77"

Expand Down
2 changes: 1 addition & 1 deletion tests/waku_core/test_sharding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ suite "Waku Sharding":

## Then
check:
pubsub == NsPubsubTopic.staticSharding(ClusterIndex, 3)
pubsub == NsPubsubTopic.staticSharding(ClusterId, 3)

test "Shard Choice Simulation":
## Given
Expand Down
24 changes: 12 additions & 12 deletions waku/waku_core/topics/pubsub_topic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ type
NsPubsubTopic* = object
case kind*: NsPubsubTopicKind
of NsPubsubTopicKind.StaticSharding:
cluster*: uint16
shard*: uint16
clusterId*: uint16
shardId*: uint16
of NsPubsubTopicKind.NamedSharding:
name*: string

proc staticSharding*(T: type NsPubsubTopic, cluster, shard: uint16): T =
proc staticSharding*(T: type NsPubsubTopic, clusterId, shardId: uint16): T =
NsPubsubTopic(
kind: NsPubsubTopicKind.StaticSharding,
cluster: cluster,
shard: shard
clusterId: clusterId,
shardId: shardId
)

proc named*(T: type NsPubsubTopic, name: string): T =
Expand All @@ -63,7 +63,7 @@ proc `$`*(topic: NsPubsubTopic): string =
of NsPubsubTopicKind.NamedSharding:
"/waku/2/" & topic.name
of NsPubsubTopicKind.StaticSharding:
"/waku/2/rs/" & $topic.cluster & "/" & $topic.shard
"/waku/2/rs/" & $topic.clusterId & "/" & $topic.shardId


# Deserialization
Expand All @@ -83,15 +83,15 @@ proc parseStaticSharding*(T: type NsPubsubTopic, topic: PubsubTopic|string): Par

let clusterPart = parts[0]
if clusterPart.len == 0:
return err(ParsingError.missingPart("shard_cluster_index"))
let cluster = ?Base10.decode(uint16, clusterPart).mapErr(proc(err: auto): auto = ParsingError.invalidFormat($err))
return err(ParsingError.missingPart("cluster_id"))
let clusterId = ?Base10.decode(uint16, clusterPart).mapErr(proc(err: auto): auto = ParsingError.invalidFormat($err))

let shardPart = parts[1]
if shardPart.len == 0:
return err(ParsingError.missingPart("shard_number"))
let shard = ?Base10.decode(uint16, shardPart).mapErr(proc(err: auto): auto = ParsingError.invalidFormat($err))
let shardId = ?Base10.decode(uint16, shardPart).mapErr(proc(err: auto): auto = ParsingError.invalidFormat($err))

ok(NsPubsubTopic.staticSharding(cluster, shard))
ok(NsPubsubTopic.staticSharding(clusterId, shardId))

proc parseNamedSharding*(T: type NsPubsubTopic, topic: PubsubTopic|string): ParsingResult[NsPubsubTopic] =
if not topic.startsWith(Waku2PubsubTopicPrefix):
Expand Down Expand Up @@ -123,10 +123,10 @@ proc `==`*[T: NsPubsubTopic](x, y: T): bool =
if x.kind != NsPubsubTopicKind.StaticSharding:
return false

if x.cluster != y.cluster:
if x.clusterId != y.clusterId:
return false

if x.shard != y.shard:
if x.shardId != y.shardId:
return false
of NsPubsubTopicKind.NamedSharding:
if x.kind != NsPubsubTopicKind.NamedSharding:
Expand Down

0 comments on commit a47dc9e

Please sign in to comment.