2021-11-01 18:02:39 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2023-06-19 22:16:25 +00:00
|
|
|
std/[sequtils],
|
2023-06-06 14:36:20 +00:00
|
|
|
stew/results,
|
2023-04-05 07:46:13 +00:00
|
|
|
stew/shims/net,
|
2021-11-01 18:02:39 +00:00
|
|
|
chronos,
|
|
|
|
testutils/unittests,
|
2023-06-06 14:36:20 +00:00
|
|
|
libp2p/crypto/crypto as libp2p_keys,
|
|
|
|
eth/keys as eth_keys
|
2022-11-04 09:52:08 +00:00
|
|
|
import
|
2023-06-06 14:36:20 +00:00
|
|
|
../../waku/v2/waku_enr,
|
2023-04-18 13:22:10 +00:00
|
|
|
../../waku/v2/waku_discv5,
|
2023-02-13 10:43:49 +00:00
|
|
|
./testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore,
|
|
|
|
./testlib/wakunode
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
|
|
|
|
proc newTestEnrRecord(privKey: libp2p_keys.PrivateKey,
|
|
|
|
extIp: string, tcpPort: uint16, udpPort: uint16,
|
|
|
|
flags = none(CapabilitiesBitfield)): waku_enr.Record =
|
|
|
|
var builder = EnrBuilder.init(privKey)
|
|
|
|
builder.withIpAddressAndPorts(
|
|
|
|
ipAddr = some(ValidIpAddress.init(extIp)),
|
|
|
|
tcpPort = some(Port(tcpPort)),
|
|
|
|
udpPort = some(Port(udpPort)),
|
|
|
|
)
|
|
|
|
|
|
|
|
if flags.isSome():
|
|
|
|
builder.withWakuCapabilities(flags.get())
|
|
|
|
|
|
|
|
builder.build().tryGet()
|
|
|
|
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
proc newTestDiscv5(privKey: libp2p_keys.PrivateKey,
|
2023-06-06 14:36:20 +00:00
|
|
|
bindIp: string, tcpPort: uint16, udpPort: uint16,
|
|
|
|
record: waku_enr.Record,
|
2023-06-27 13:50:11 +00:00
|
|
|
bootstrapRecords = newSeq[waku_enr.Record]()): WakuDiscoveryV5 =
|
2023-06-06 14:36:20 +00:00
|
|
|
let config = WakuDiscoveryV5Config(
|
|
|
|
privateKey: eth_keys.PrivateKey(privKey.skkey),
|
|
|
|
address: ValidIpAddress.init(bindIp),
|
|
|
|
port: Port(udpPort),
|
|
|
|
bootstrapRecords: bootstrapRecords,
|
|
|
|
)
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
let discv5 = WakuDiscoveryV5.new(rng(), config, some(record))
|
2023-06-06 14:36:20 +00:00
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
return discv5
|
2023-06-06 14:36:20 +00:00
|
|
|
|
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
procSuite "Waku Discovery v5":
|
2023-06-06 14:36:20 +00:00
|
|
|
asyncTest "find random peers":
|
|
|
|
## Given
|
|
|
|
# Node 1
|
2021-11-01 18:02:39 +00:00
|
|
|
let
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey1 = generateSecp256k1Key()
|
|
|
|
bindIp1 = "0.0.0.0"
|
|
|
|
extIp1 = "127.0.0.1"
|
|
|
|
tcpPort1 = 61500u16
|
|
|
|
udpPort1 = 9000u16
|
|
|
|
|
|
|
|
let record1 = newTestEnrRecord(
|
|
|
|
privKey = privKey1,
|
|
|
|
extIp = extIp1,
|
|
|
|
tcpPort = tcpPort1,
|
|
|
|
udpPort = udpPort1,
|
|
|
|
)
|
2023-06-27 13:50:11 +00:00
|
|
|
let node1 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey1,
|
|
|
|
bindIp = bindIp1,
|
|
|
|
tcpPort = tcpPort1,
|
|
|
|
udpPort = udpPort1,
|
|
|
|
record = record1
|
|
|
|
)
|
|
|
|
|
|
|
|
# Node 2
|
|
|
|
let
|
|
|
|
privKey2 = generateSecp256k1Key()
|
|
|
|
bindIp2 = "0.0.0.0"
|
|
|
|
extIp2 = "127.0.0.1"
|
|
|
|
tcpPort2 = 61502u16
|
|
|
|
udpPort2 = 9002u16
|
|
|
|
|
|
|
|
let record2 = newTestEnrRecord(
|
|
|
|
privKey = privKey2,
|
|
|
|
extIp = extIp2,
|
|
|
|
tcpPort = tcpPort2,
|
|
|
|
udpPort = udpPort2,
|
|
|
|
)
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
let node2 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey2,
|
|
|
|
bindIp = bindIp2,
|
|
|
|
tcpPort = tcpPort2,
|
|
|
|
udpPort = udpPort2,
|
|
|
|
record = record2,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Node 3
|
|
|
|
let
|
|
|
|
privKey3 = generateSecp256k1Key()
|
|
|
|
bindIp3 = "0.0.0.0"
|
|
|
|
extIp3 = "127.0.0.1"
|
|
|
|
tcpPort3 = 61504u16
|
|
|
|
udpPort3 = 9004u16
|
|
|
|
|
|
|
|
let record3 = newTestEnrRecord(
|
|
|
|
privKey = privKey3,
|
|
|
|
extIp = extIp3,
|
|
|
|
tcpPort = tcpPort3,
|
|
|
|
udpPort = udpPort3,
|
|
|
|
)
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
let node3 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey3,
|
|
|
|
bindIp = bindIp3,
|
|
|
|
tcpPort = tcpPort3,
|
|
|
|
udpPort = udpPort3,
|
|
|
|
record = record3,
|
|
|
|
bootstrapRecords = @[record1, record2]
|
|
|
|
)
|
|
|
|
|
2023-06-27 19:16:59 +00:00
|
|
|
let res1 = node1.start()
|
|
|
|
assert res1.isOk(), res1.error
|
|
|
|
|
|
|
|
let res2 = node2.start()
|
|
|
|
assert res2.isOk(), res2.error
|
|
|
|
|
|
|
|
let res3 = node3.start()
|
|
|
|
assert res3.isOk(), res3.error
|
2023-06-06 14:36:20 +00:00
|
|
|
|
|
|
|
## When
|
2023-06-27 13:50:11 +00:00
|
|
|
let res = await node3.findRandomPeers()
|
2023-06-06 14:36:20 +00:00
|
|
|
|
|
|
|
## Then
|
2021-11-01 18:02:39 +00:00
|
|
|
check:
|
2023-06-06 14:36:20 +00:00
|
|
|
res.len >= 1
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(node1.stop(), node2.stop(), node3.stop())
|
|
|
|
|
|
|
|
asyncTest "find random peers with predicate":
|
|
|
|
## Setup
|
|
|
|
# Records
|
|
|
|
let
|
|
|
|
privKey1 = generateSecp256k1Key()
|
|
|
|
bindIp1 = "0.0.0.0"
|
|
|
|
extIp1 = "127.0.0.1"
|
|
|
|
tcpPort1 = 61500u16
|
|
|
|
udpPort1 = 9000u16
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
let record1 = newTestEnrRecord(
|
|
|
|
privKey = privKey1,
|
|
|
|
extIp = extIp1,
|
|
|
|
tcpPort = tcpPort1,
|
|
|
|
udpPort = udpPort1,
|
|
|
|
flags = some(CapabilitiesBitfield.init(Capabilities.Relay))
|
|
|
|
)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
let
|
|
|
|
privKey2 = generateSecp256k1Key()
|
|
|
|
bindIp2 = "0.0.0.0"
|
|
|
|
extIp2 = "127.0.0.1"
|
|
|
|
tcpPort2 = 61502u16
|
|
|
|
udpPort2 = 9002u16
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
let record2 = newTestEnrRecord(
|
|
|
|
privKey = privKey2,
|
|
|
|
extIp = extIp2,
|
|
|
|
tcpPort = tcpPort2,
|
|
|
|
udpPort = udpPort2,
|
|
|
|
flags = some(CapabilitiesBitfield.init(Capabilities.Relay, Capabilities.Store))
|
|
|
|
)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
let
|
|
|
|
privKey3 = generateSecp256k1Key()
|
|
|
|
bindIp3 = "0.0.0.0"
|
|
|
|
extIp3 = "127.0.0.1"
|
|
|
|
tcpPort3 = 61504u16
|
|
|
|
udpPort3 = 9004u16
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
let record3 = newTestEnrRecord(
|
|
|
|
privKey = privKey3,
|
|
|
|
extIp = extIp3,
|
|
|
|
tcpPort = tcpPort3,
|
|
|
|
udpPort = udpPort3,
|
|
|
|
flags = some(CapabilitiesBitfield.init(Capabilities.Relay, Capabilities.Filter))
|
|
|
|
)
|
2023-02-07 13:06:50 +00:00
|
|
|
|
|
|
|
let
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey4 = generateSecp256k1Key()
|
|
|
|
bindIp4 = "0.0.0.0"
|
|
|
|
extIp4 = "127.0.0.1"
|
|
|
|
tcpPort4 = 61506u16
|
|
|
|
udpPort4 = 9006u16
|
|
|
|
|
|
|
|
let record4 = newTestEnrRecord(
|
|
|
|
privKey = privKey4,
|
|
|
|
extIp = extIp4,
|
|
|
|
tcpPort = tcpPort4,
|
|
|
|
udpPort = udpPort4,
|
|
|
|
flags = some(CapabilitiesBitfield.init(Capabilities.Relay, Capabilities.Store))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Nodes
|
2023-06-27 13:50:11 +00:00
|
|
|
let node1 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey1,
|
|
|
|
bindIp = bindIp1,
|
|
|
|
tcpPort = tcpPort1,
|
|
|
|
udpPort = udpPort1,
|
|
|
|
record = record1,
|
|
|
|
bootstrapRecords = @[record2]
|
|
|
|
)
|
2023-06-27 13:50:11 +00:00
|
|
|
let node2 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey2,
|
|
|
|
bindIp = bindIp2,
|
|
|
|
tcpPort = tcpPort2,
|
|
|
|
udpPort = udpPort2,
|
|
|
|
record = record2,
|
|
|
|
bootstrapRecords = @[record3, record4]
|
|
|
|
)
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
let node3 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey3,
|
|
|
|
bindIp = bindIp3,
|
|
|
|
tcpPort = tcpPort3,
|
|
|
|
udpPort = udpPort3,
|
|
|
|
record = record3
|
|
|
|
)
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
let node4 = newTestDiscv5(
|
2023-06-06 14:36:20 +00:00
|
|
|
privKey = privKey4,
|
|
|
|
bindIp = bindIp4,
|
|
|
|
tcpPort = tcpPort4,
|
|
|
|
udpPort = udpPort4,
|
|
|
|
record = record4
|
|
|
|
)
|
|
|
|
|
|
|
|
# Start nodes' discoveryV5 protocols
|
2023-06-27 19:16:59 +00:00
|
|
|
let res1 = node1.start()
|
|
|
|
assert res1.isOk(), res1.error
|
|
|
|
|
|
|
|
let res2 = node2.start()
|
|
|
|
assert res2.isOk(), res2.error
|
|
|
|
|
|
|
|
let res3 = node3.start()
|
|
|
|
assert res3.isOk(), res3.error
|
|
|
|
|
|
|
|
let res4 = node4.start()
|
|
|
|
assert res4.isOk(), res4.error
|
2023-06-06 14:36:20 +00:00
|
|
|
|
|
|
|
## Given
|
2023-06-20 16:08:10 +00:00
|
|
|
let recordPredicate: WakuDiscv5Predicate = proc(record: waku_enr.Record): bool =
|
2023-06-06 14:36:20 +00:00
|
|
|
let typedRecord = record.toTyped()
|
|
|
|
if typedRecord.isErr():
|
|
|
|
return false
|
|
|
|
|
|
|
|
let capabilities = typedRecord.value.waku2
|
|
|
|
if capabilities.isNone():
|
|
|
|
return false
|
|
|
|
|
|
|
|
return capabilities.get().supportsCapability(Capabilities.Store)
|
|
|
|
|
|
|
|
|
|
|
|
## When
|
2023-06-27 13:50:11 +00:00
|
|
|
let peers = await node1.findRandomPeers(some(recordPredicate))
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
## Then
|
2023-02-07 13:06:50 +00:00
|
|
|
check:
|
2023-06-06 14:36:20 +00:00
|
|
|
peers.len >= 1
|
|
|
|
peers.allIt(it.supportsCapability(Capabilities.Store))
|
2023-02-07 13:06:50 +00:00
|
|
|
|
2023-06-06 14:36:20 +00:00
|
|
|
# Cleanup
|
|
|
|
await allFutures(node1.stop(), node2.stop(), node3.stop(), node4.stop())
|
2023-06-19 22:16:25 +00:00
|
|
|
|
|
|
|
asyncTest "get relayShards 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)])
|
|
|
|
|
|
|
|
## 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() == relayShards, $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
|
|
|
|
|
2023-06-20 16:08:10 +00:00
|
|
|
asyncTest "filter peer per static shard":
|
|
|
|
## Given
|
|
|
|
let recordCluster21 = block:
|
|
|
|
let
|
|
|
|
enrSeqNum = 1u64
|
|
|
|
enrPrivKey = generatesecp256k1key()
|
|
|
|
|
|
|
|
let
|
|
|
|
shardCluster: uint16 = 21
|
|
|
|
shardIndices: seq[uint16] = @[1u16, 2u16, 5u16, 7u16, 9u16, 11u16]
|
|
|
|
|
|
|
|
let shards = RelayShards.init(shardCluster, shardIndices)
|
|
|
|
|
|
|
|
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
|
|
|
|
require builder.withWakuRelaySharding(shards).isOk()
|
|
|
|
|
|
|
|
let recordRes = builder.build()
|
|
|
|
require recordRes.isOk()
|
|
|
|
recordRes.tryGet()
|
|
|
|
|
|
|
|
let recordCluster22Indices1 = block:
|
|
|
|
let
|
|
|
|
enrSeqNum = 1u64
|
|
|
|
enrPrivKey = generatesecp256k1key()
|
|
|
|
|
|
|
|
let
|
|
|
|
shardCluster: uint16 = 22
|
|
|
|
shardIndices: seq[uint16] = @[2u16, 4u16, 5u16, 8u16, 10u16, 12u16]
|
|
|
|
|
|
|
|
let shards = RelayShards.init(shardCluster, shardIndices)
|
|
|
|
|
|
|
|
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
|
|
|
|
require builder.withWakuRelaySharding(shards).isOk()
|
|
|
|
|
|
|
|
let recordRes = builder.build()
|
|
|
|
require recordRes.isOk()
|
|
|
|
recordRes.tryGet()
|
|
|
|
|
|
|
|
let recordCluster22Indices2 = block:
|
|
|
|
let
|
|
|
|
enrSeqNum = 1u64
|
|
|
|
enrPrivKey = generatesecp256k1key()
|
|
|
|
|
|
|
|
let
|
|
|
|
shardCluster: uint16 = 22
|
|
|
|
shardIndices: seq[uint16] = @[1u16, 3u16, 6u16, 7u16, 9u16, 11u16]
|
|
|
|
|
|
|
|
let shards = RelayShards.init(shardCluster, shardIndices)
|
|
|
|
|
|
|
|
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
|
|
|
|
require builder.withWakuRelaySharding(shards).isOk()
|
|
|
|
|
|
|
|
let recordRes = builder.build()
|
|
|
|
require recordRes.isOk()
|
|
|
|
recordRes.tryGet()
|
|
|
|
|
|
|
|
## When
|
|
|
|
let predicateCluster21Op = shardingPredicate(recordCluster21)
|
|
|
|
require predicateCluster21Op.isSome()
|
|
|
|
let predicateCluster21 = predicateCluster21Op.get()
|
|
|
|
|
|
|
|
let predicateCluster22Op = shardingPredicate(recordCluster22Indices1)
|
|
|
|
require predicateCluster22Op.isSome()
|
|
|
|
let predicateCluster22 = predicateCluster22Op.get()
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
predicateCluster21(recordCluster21) == true
|
|
|
|
predicateCluster21(recordCluster22Indices1) == false
|
|
|
|
predicateCluster21(recordCluster22Indices2) == false
|
|
|
|
predicateCluster22(recordCluster21) == false
|
|
|
|
predicateCluster22(recordCluster22Indices1) == true
|
|
|
|
predicateCluster22(recordCluster22Indices2) == false
|
|
|
|
|
|
|
|
|