Skip to content

Commit a47dc9e

Browse files
authored
fix: cluster id & sharding terminology (#2104)
1 parent 7b5c36b commit a47dc9e

8 files changed

+172
-192
lines changed

tests/test_waku_discv5.nim

+15-15
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ procSuite "Waku Discovery v5":
276276
# Cleanup
277277
await allFutures(node1.stop(), node2.stop(), node3.stop(), node4.stop())
278278

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

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

289289
## When
290290

@@ -298,7 +298,7 @@ procSuite "Waku Discovery v5":
298298
assert mixedRes.isErr(), $mixedRes.value
299299
assert shardedRes.isOk(), shardedRes.error
300300
assert shardedRes.value.isSome()
301-
assert shardedRes.value.get() == relayShards, $shardedRes.value.get()
301+
assert shardedRes.value.get() == shardsTopics, $shardedRes.value.get()
302302
assert namedRes.isOk(), namedRes.error
303303
assert namedRes.value.isNone(), $namedRes.value
304304
assert gibberishRes.isErr(), $gibberishRes.value
@@ -313,13 +313,13 @@ procSuite "Waku Discovery v5":
313313
enrPrivKey = generatesecp256k1key()
314314

315315
let
316-
shardCluster: uint16 = 21
317-
shardIndices: seq[uint16] = @[1u16, 2u16, 5u16, 7u16, 9u16, 11u16]
316+
clusterId: uint16 = 21
317+
shardIds: seq[uint16] = @[1u16, 2u16, 5u16, 7u16, 9u16, 11u16]
318318

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

321321
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
322-
require builder.withWakuRelaySharding(shards).isOk()
322+
require builder.withWakuRelaySharding(shardsTopics).isOk()
323323

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

333333
let
334-
shardCluster: uint16 = 22
335-
shardIndices: seq[uint16] = @[2u16, 4u16, 5u16, 8u16, 10u16, 12u16]
334+
clusterId: uint16 = 22
335+
shardIds: seq[uint16] = @[2u16, 4u16, 5u16, 8u16, 10u16, 12u16]
336336

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

339339
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
340-
require builder.withWakuRelaySharding(shards).isOk()
340+
require builder.withWakuRelaySharding(shardsTopics).isOk()
341341

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

351351
let
352-
shardCluster: uint16 = 22
353-
shardIndices: seq[uint16] = @[1u16, 3u16, 6u16, 7u16, 9u16, 11u16]
352+
clusterId: uint16 = 22
353+
shardIds: seq[uint16] = @[1u16, 3u16, 6u16, 7u16, 9u16, 11u16]
354354

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

357357
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
358-
require builder.withWakuRelaySharding(shards).isOk()
358+
require builder.withWakuRelaySharding(shardsTopics).isOk()
359359

360360
let recordRes = builder.build()
361361
require recordRes.isOk()

tests/test_waku_enr.nim

+44-44
Original file line numberDiff line numberDiff line change
@@ -255,71 +255,71 @@ suite "Waku ENR - Multiaddresses":
255255

256256
suite "Waku ENR - Relay static sharding":
257257

258-
test "new relay shards field with single invalid index":
258+
test "new relay shards object with single invalid shard id":
259259
## Given
260260
let
261-
shardCluster: uint16 = 22
262-
shardIndex: uint16 = 1024
261+
clusterId: uint16 = 22
262+
shard: uint16 = 1024
263263

264264
## When
265-
let res = RelayShards.init(shardCluster, shardIndex)
265+
let shardsTopics = RelayShards.init(clusterId, shard)
266266

267267
## Then
268-
assert res.isErr(), $res.get()
268+
assert shardsTopics.isErr(), $shardsTopics.get()
269269

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

276276
## When
277-
let res = RelayShards.init(shardCluster, shardIndices)
277+
let shardsTopics = RelayShards.init(clusterId, shardIds)
278278

279279
## Then
280-
assert res.isErr(), $res.get()
280+
assert shardsTopics.isErr(), $shardsTopics.get()
281281

282-
test "new relay shards field with single valid index":
282+
test "new relay shards object with single valid shard id":
283283
## Given
284284
let
285-
shardCluster: uint16 = 22
286-
shardIndex: uint16 = 1
285+
clusterId: uint16 = 22
286+
shardId: uint16 = 1
287287

288-
let topic = NsPubsubTopic.staticSharding(shardCluster, shardIndex)
288+
let topic = NsPubsubTopic.staticSharding(clusterId, shardId)
289289

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

293293
## Then
294294
check:
295-
shards.cluster == shardCluster
296-
shards.indices == @[1u16]
295+
shardsTopics.clusterId == clusterId
296+
shardsTopics.shardIds == @[1u16]
297297

298-
let topics = shards.topics.mapIt($it)
298+
let topics = shardsTopics.topics.mapIt($it)
299299
check:
300300
topics == @[$topic]
301301

302302
check:
303-
shards.contains(shardCluster, shardIndex)
304-
not shards.contains(shardCluster, 33u16)
305-
not shards.contains(20u16, 33u16)
303+
shardsTopics.contains(clusterId, shardId)
304+
not shardsTopics.contains(clusterId, 33u16)
305+
not shardsTopics.contains(20u16, 33u16)
306306

307-
shards.contains(topic)
308-
shards.contains("/waku/2/rs/22/1")
307+
shardsTopics.contains(topic)
308+
shardsTopics.contains("/waku/2/rs/22/1")
309309

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

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

319319
## Then
320320
check:
321-
shards.cluster == shardCluster
322-
shards.indices == @[1u16, 2u16, 3u16]
321+
shardsTopics.clusterId == clusterId
322+
shardsTopics.shardIds == @[1u16, 2u16, 3u16]
323323

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

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

347347
let
348-
shardCluster: uint16 = 22
349-
shardIndices: seq[uint16] = @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16]
348+
clusterId: uint16 = 22
349+
shardIds: seq[uint16] = @[1u16, 1u16, 2u16, 3u16, 5u16, 8u16]
350350

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

353353
## When
354354
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
355-
require builder.withWakuRelaySharding(shards).isOk()
355+
require builder.withWakuRelaySharding(shardsTopics).isOk()
356356

357357
let recordRes = builder.build()
358358

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

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

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

379379
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
380-
require builder.withWakuRelaySharding(shards).isOk()
380+
require builder.withWakuRelaySharding(shardsTopics).isOk()
381381

382382
let recordRes = builder.build()
383383
require recordRes.isOk()
@@ -393,22 +393,22 @@ suite "Waku ENR - Relay static sharding":
393393
## Then
394394
check:
395395
shardsOpt.isSome()
396-
shardsOpt.get() == shards
396+
shardsOpt.get() == shardsTopics
397397

398-
test "decode record with relay shards indices list and bit vector fields":
398+
test "decode record with relay shards shard list and bit vector fields":
399399
## Given
400400
let
401401
enrSeqNum = 1u64
402402
enrPrivKey = generatesecp256k1key()
403403

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

408408

409409
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
410-
require builder.withWakuRelayShardingIndicesList(shardsIndicesList).isOk()
411-
require builder.withWakuRelayShardingBitVector(shardsBitVector).isOk()
410+
require builder.withWakuRelayShardingIndicesList(relayShardsIndicesList).isOk()
411+
require builder.withWakuRelayShardingBitVector(relayShardsBitVector).isOk()
412412

413413
let recordRes = builder.build()
414414
require recordRes.isOk()
@@ -424,4 +424,4 @@ suite "Waku ENR - Relay static sharding":
424424
## Then
425425
check:
426426
shardsOpt.isSome()
427-
shardsOpt.get() == shardsIndicesList
427+
shardsOpt.get() == relayShardsIndicesList

tests/waku_core/test_namespaced_topics.nim

+6-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ suite "Waku Message - Pub-sub topics namespacing":
151151

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

156156
## When
157157
let topic = $ns
@@ -186,8 +186,8 @@ suite "Waku Message - Pub-sub topics namespacing":
186186

187187
let ns = nsRes.get()
188188
check:
189-
ns.cluster == 16
190-
ns.shard == 42
189+
ns.clusterId == 16
190+
ns.shardId == 42
191191

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

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

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

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

tests/waku_core/test_sharding.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ suite "Waku Sharding":
109109

110110
## Then
111111
check:
112-
pubsub == NsPubsubTopic.staticSharding(ClusterIndex, 3)
112+
pubsub == NsPubsubTopic.staticSharding(ClusterId, 3)
113113

114114
test "Shard Choice Simulation":
115115
## Given

waku/waku_core/topics/pubsub_topic.nim

+12-12
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ type
3535
NsPubsubTopic* = object
3636
case kind*: NsPubsubTopicKind
3737
of NsPubsubTopicKind.StaticSharding:
38-
cluster*: uint16
39-
shard*: uint16
38+
clusterId*: uint16
39+
shardId*: uint16
4040
of NsPubsubTopicKind.NamedSharding:
4141
name*: string
4242

43-
proc staticSharding*(T: type NsPubsubTopic, cluster, shard: uint16): T =
43+
proc staticSharding*(T: type NsPubsubTopic, clusterId, shardId: uint16): T =
4444
NsPubsubTopic(
4545
kind: NsPubsubTopicKind.StaticSharding,
46-
cluster: cluster,
47-
shard: shard
46+
clusterId: clusterId,
47+
shardId: shardId
4848
)
4949

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

6868

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

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

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

94-
ok(NsPubsubTopic.staticSharding(cluster, shard))
94+
ok(NsPubsubTopic.staticSharding(clusterId, shardId))
9595

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

126-
if x.cluster != y.cluster:
126+
if x.clusterId != y.clusterId:
127127
return false
128128

129-
if x.shard != y.shard:
129+
if x.shardId != y.shardId:
130130
return false
131131
of NsPubsubTopicKind.NamedSharding:
132132
if x.kind != NsPubsubTopicKind.NamedSharding:

0 commit comments

Comments
 (0)