2024-03-14 18:01:13 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
stew/results,
|
|
|
|
stew/shims/net,
|
|
|
|
chronos,
|
|
|
|
testutils/unittests,
|
|
|
|
libp2p/crypto/crypto as libp2p_keys,
|
|
|
|
eth/keys as eth_keys
|
|
|
|
|
|
|
|
import
|
2024-04-17 19:48:20 +00:00
|
|
|
../../../waku/[waku_enr, discovery/waku_discv5, waku_core],
|
2024-03-14 18:01:13 +00:00
|
|
|
../testlib/wakucore,
|
|
|
|
../waku_discv5/utils,
|
|
|
|
./utils
|
|
|
|
|
|
|
|
suite "Sharding":
|
|
|
|
suite "topicsToRelayShards":
|
|
|
|
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"]
|
2024-03-15 23:08:47 +00:00
|
|
|
let namedTopics =
|
|
|
|
@["/waku/2/thisisatest", "/waku/2/atestthisis", "/waku/2/isthisatest"]
|
|
|
|
let gibberish =
|
|
|
|
@["aedyttydcb/uioasduyio", "jhdfsjhlsdfjhk/sadjhk", "khfsd/hjfdsgjh/dfs"]
|
2024-03-14 18:01:13 +00:00
|
|
|
let empty: seq[string] = @[]
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let shardsTopics =
|
|
|
|
RelayShards.init(0, @[uint16(2), uint16(4), uint16(8)]).expect("Valid shardIds")
|
2024-03-14 18:01:13 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
|
|
|
|
let mixedRes = topicsToRelayShards(mixedTopics)
|
|
|
|
let shardedRes = topicsToRelayShards(shardedTopics)
|
|
|
|
let namedRes = topicsToRelayShards(namedTopics)
|
|
|
|
let gibberishRes = topicsToRelayShards(gibberish)
|
|
|
|
let emptyRes = topicsToRelayShards(empty)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
assert mixedRes.isErr(), $mixedRes.value
|
|
|
|
assert shardedRes.isOk(), shardedRes.error
|
|
|
|
assert shardedRes.value.isSome()
|
|
|
|
assert shardedRes.value.get() == shardsTopics, $shardedRes.value.get()
|
|
|
|
assert namedRes.isOk(), namedRes.error
|
|
|
|
assert namedRes.value.isNone(), $namedRes.value
|
|
|
|
assert gibberishRes.isErr(), $gibberishRes.value
|
|
|
|
assert emptyRes.isOk(), emptyRes.error
|
|
|
|
assert emptyRes.value.isNone(), $emptyRes.value
|
|
|
|
|
|
|
|
suite "containsShard":
|
|
|
|
asyncTest "update ENR from subscriptions":
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
shard1 = "/waku/2/rs/0/1"
|
|
|
|
shard2 = "/waku/2/rs/0/2"
|
|
|
|
shard3 = "/waku/2/rs/0/3"
|
|
|
|
privKey = generateSecp256k1Key()
|
|
|
|
bindIp = "0.0.0.0"
|
|
|
|
extIp = "127.0.0.1"
|
|
|
|
tcpPort = 61500u16
|
|
|
|
udpPort = 9000u16
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let record = newTestEnrRecord(
|
|
|
|
privKey = privKey, extIp = extIp, tcpPort = tcpPort, udpPort = udpPort
|
|
|
|
)
|
2024-03-14 18:01:13 +00:00
|
|
|
|
|
|
|
let queue = newAsyncEventQueue[SubscriptionEvent](30)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let node = newTestDiscv5(
|
|
|
|
privKey = privKey,
|
|
|
|
bindIp = bindIp,
|
|
|
|
tcpPort = tcpPort,
|
|
|
|
udpPort = udpPort,
|
|
|
|
record = record,
|
|
|
|
queue = queue,
|
|
|
|
)
|
2024-03-14 18:01:13 +00:00
|
|
|
|
|
|
|
let res = await node.start()
|
|
|
|
assert res.isOk(), res.error
|
|
|
|
|
|
|
|
## Then
|
|
|
|
queue.emit((kind: PubsubSub, topic: shard1))
|
|
|
|
queue.emit((kind: PubsubSub, topic: shard2))
|
|
|
|
queue.emit((kind: PubsubSub, topic: shard3))
|
|
|
|
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
|
|
|
check:
|
|
|
|
node.protocol.localNode.record.containsShard(shard1) == true
|
|
|
|
node.protocol.localNode.record.containsShard(shard2) == true
|
|
|
|
node.protocol.localNode.record.containsShard(shard3) == true
|
|
|
|
|
|
|
|
queue.emit((kind: PubsubSub, topic: shard1))
|
|
|
|
queue.emit((kind: PubsubSub, topic: shard2))
|
|
|
|
queue.emit((kind: PubsubSub, topic: shard3))
|
|
|
|
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
|
|
|
check:
|
|
|
|
node.protocol.localNode.record.containsShard(shard1) == true
|
|
|
|
node.protocol.localNode.record.containsShard(shard2) == true
|
|
|
|
node.protocol.localNode.record.containsShard(shard3) == true
|
|
|
|
|
|
|
|
queue.emit((kind: PubsubUnsub, topic: shard1))
|
|
|
|
queue.emit((kind: PubsubUnsub, topic: shard2))
|
|
|
|
|
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
|
|
|
check:
|
|
|
|
node.protocol.localNode.record.containsShard(shard1) == false
|
|
|
|
node.protocol.localNode.record.containsShard(shard2) == false
|
|
|
|
node.protocol.localNode.record.containsShard(shard3) == true
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await node.stop()
|