2021-08-12 08:51:38 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[sequtils, tables],
|
|
|
|
stew/shims/net,
|
|
|
|
stew/[base32, results],
|
2023-03-06 16:18:41 +00:00
|
|
|
testutils/unittests,
|
2022-07-25 11:01:37 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2021-08-12 08:51:38 +00:00
|
|
|
libp2p/crypto/crypto,
|
|
|
|
eth/keys,
|
2024-01-15 14:54:02 +00:00
|
|
|
dnsdisc/builder
|
2022-07-25 11:01:37 +00:00
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/node/peer_manager,
|
|
|
|
waku/waku_node,
|
|
|
|
waku/discovery/waku_dnsdisc,
|
2023-02-13 10:43:49 +00:00
|
|
|
./testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore,
|
|
|
|
./testlib/wakunode
|
2021-08-12 08:51:38 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
suite "Waku DNS Discovery":
|
2021-08-12 08:51:38 +00:00
|
|
|
asyncTest "Waku DNS Discovery end-to-end":
|
|
|
|
## Tests integrated DNS discovery, from building
|
|
|
|
## the tree to connecting to discovered nodes
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
# Create nodes and ENR. These will be added to the discoverable list
|
|
|
|
let
|
2023-12-14 06:16:39 +00:00
|
|
|
bindIp = parseIpAddress("0.0.0.0")
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey1 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node1 = newTestWakuNode(nodeKey1, bindIp, Port(63500))
|
2021-08-12 08:51:38 +00:00
|
|
|
enr1 = node1.enr
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey2 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node2 = newTestWakuNode(nodeKey2, bindIp, Port(63502))
|
2021-08-12 08:51:38 +00:00
|
|
|
enr2 = node2.enr
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey3 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node3 = newTestWakuNode(nodeKey3, bindIp, Port(63503))
|
2021-08-12 08:51:38 +00:00
|
|
|
enr3 = node3.enr
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node1.mountRelay()
|
|
|
|
await node2.mountRelay()
|
|
|
|
await node3.mountRelay()
|
2021-08-12 08:51:38 +00:00
|
|
|
await allFutures([node1.start(), node2.start(), node3.start()])
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
# Build and sign tree
|
2024-03-15 23:08:47 +00:00
|
|
|
var tree = buildTree(
|
|
|
|
1, # Seq no
|
|
|
|
@[enr1, enr2, enr3], # ENR entries
|
|
|
|
@[],
|
|
|
|
)
|
|
|
|
.get() # No link entries
|
2021-08-12 08:51:38 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
let treeKeys = keys.KeyPair.random(rng[])
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
# Sign tree
|
|
|
|
check:
|
|
|
|
tree.signTree(treeKeys.seckey()).isOk()
|
|
|
|
|
|
|
|
# Create TXT records at domain
|
|
|
|
let
|
|
|
|
domain = "testnodes.aq"
|
|
|
|
zoneTxts = tree.buildTXT(domain).get()
|
|
|
|
username = Base32.encode(treeKeys.pubkey().toRawCompressed())
|
2024-03-15 23:08:47 +00:00
|
|
|
location = LinkPrefix & username & "@" & domain
|
|
|
|
# See EIP-1459: https://eips.ethereum.org/EIPS/eip-1459
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
# Create a resolver for the domain
|
|
|
|
|
|
|
|
proc resolver(domain: string): Future[string] {.async, gcsafe.} =
|
|
|
|
return zoneTxts[domain]
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
# Create Waku DNS discovery client on a new Waku v2 node using the resolver
|
|
|
|
|
|
|
|
let
|
2023-02-13 10:43:49 +00:00
|
|
|
nodeKey4 = generateSecp256k1Key()
|
2023-04-05 14:01:51 +00:00
|
|
|
node4 = newTestWakuNode(nodeKey4, bindIp, Port(63504))
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
await node4.mountRelay()
|
2021-08-12 08:51:38 +00:00
|
|
|
await node4.start()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-08-17 08:30:17 +00:00
|
|
|
var wakuDnsDisc = WakuDnsDiscovery.init(location, resolver).get()
|
2021-08-12 08:51:38 +00:00
|
|
|
|
2024-12-03 13:39:37 +00:00
|
|
|
let res = await wakuDnsDisc.findPeers()
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# We have discovered all three nodes
|
|
|
|
res.isOk()
|
|
|
|
res[].len == 3
|
2022-01-10 15:07:35 +00:00
|
|
|
res[].mapIt(it.peerId).contains(node1.switch.peerInfo.peerId)
|
|
|
|
res[].mapIt(it.peerId).contains(node2.switch.peerInfo.peerId)
|
|
|
|
res[].mapIt(it.peerId).contains(node3.switch.peerInfo.peerId)
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-08-12 08:51:38 +00:00
|
|
|
# Connect to discovered nodes
|
|
|
|
await node4.connectToNodes(res[])
|
|
|
|
|
|
|
|
check:
|
|
|
|
# We have successfully connected to all discovered nodes
|
2024-09-27 12:46:46 +00:00
|
|
|
node4.peerManager.wakuPeerStore.peers().anyIt(
|
2024-03-15 23:08:47 +00:00
|
|
|
it.peerId == node1.switch.peerInfo.peerId
|
|
|
|
)
|
2024-09-27 12:46:46 +00:00
|
|
|
node4.peerManager.wakuPeerStore.connectedness(node1.switch.peerInfo.peerId) ==
|
2024-03-15 23:08:47 +00:00
|
|
|
Connected
|
2024-09-27 12:46:46 +00:00
|
|
|
node4.peerManager.wakuPeerStore.peers().anyIt(
|
2024-03-15 23:08:47 +00:00
|
|
|
it.peerId == node2.switch.peerInfo.peerId
|
|
|
|
)
|
2024-09-27 12:46:46 +00:00
|
|
|
node4.peerManager.wakuPeerStore.connectedness(node2.switch.peerInfo.peerId) ==
|
2024-03-15 23:08:47 +00:00
|
|
|
Connected
|
2024-09-27 12:46:46 +00:00
|
|
|
node4.peerManager.wakuPeerStore.peers().anyIt(
|
2024-03-15 23:08:47 +00:00
|
|
|
it.peerId == node3.switch.peerInfo.peerId
|
|
|
|
)
|
2024-09-27 12:46:46 +00:00
|
|
|
node4.peerManager.wakuPeerStore.connectedness(node3.switch.peerInfo.peerId) ==
|
2024-03-15 23:08:47 +00:00
|
|
|
Connected
|
2021-08-12 08:51:38 +00:00
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop(), node3.stop(), node4.stop()])
|