2024-01-24 08:09:11 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2021-03-29 13:26:34 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-02-17 15:36:04 +00:00
|
|
|
import
|
2024-01-24 08:09:11 +00:00
|
|
|
std/[tables, sequtils, net],
|
|
|
|
chronos, chronicles, stint, testutils/unittests,
|
2022-06-17 20:45:37 +00:00
|
|
|
stew/byteutils,
|
2024-09-29 08:52:19 +00:00
|
|
|
../../eth/common/keys,
|
2022-05-02 14:49:19 +00:00
|
|
|
../../eth/p2p/discoveryv5/[enr, node, routing_table, encoding, sessions,
|
|
|
|
messages, nodes_verification],
|
2021-04-06 11:33:24 +00:00
|
|
|
../../eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2022-11-10 08:01:58 +00:00
|
|
|
../stubloglevel,
|
2020-06-17 11:51:30 +00:00
|
|
|
./discv5_test_helper
|
2020-02-17 15:36:04 +00:00
|
|
|
|
2021-12-11 18:12:55 +00:00
|
|
|
suite "Discovery v5 Tests":
|
|
|
|
setup:
|
2023-10-29 04:06:48 +00:00
|
|
|
let rng {.used.} = newRng()
|
2020-07-07 08:56:26 +00:00
|
|
|
|
2020-03-20 15:38:46 +00:00
|
|
|
asyncTest "GetNode":
|
|
|
|
# TODO: This could be tested in just a routing table only context
|
|
|
|
let
|
2020-07-07 08:56:26 +00:00
|
|
|
node = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
targetNode = generateNode(PrivateKey.random(rng[]))
|
2020-03-20 15:38:46 +00:00
|
|
|
|
2020-05-28 08:19:36 +00:00
|
|
|
check node.addNode(targetNode)
|
2020-03-20 15:38:46 +00:00
|
|
|
|
|
|
|
for i in 0..<1000:
|
2020-07-07 08:56:26 +00:00
|
|
|
discard node.addNode(generateNode(PrivateKey.random(rng[])))
|
2020-03-20 15:38:46 +00:00
|
|
|
|
2020-04-20 11:50:22 +00:00
|
|
|
let n = node.getNode(targetNode.id)
|
2020-05-28 08:19:36 +00:00
|
|
|
check n.isSome()
|
2020-04-20 11:50:22 +00:00
|
|
|
check n.get() == targetNode
|
2020-03-20 15:38:46 +00:00
|
|
|
|
|
|
|
await node.closeWait()
|
|
|
|
|
2020-03-24 09:51:34 +00:00
|
|
|
asyncTest "Node deletion":
|
|
|
|
let
|
2020-07-07 08:56:26 +00:00
|
|
|
bootnode = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20301))
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302),
|
2020-03-24 09:51:34 +00:00
|
|
|
@[bootnode.localNode.record])
|
2020-07-07 08:56:26 +00:00
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303),
|
2020-03-24 09:51:34 +00:00
|
|
|
@[bootnode.localNode.record])
|
|
|
|
pong1 = await discv5_protocol.ping(node1, bootnode.localNode)
|
|
|
|
pong2 = await discv5_protocol.ping(node1, node2.localNode)
|
|
|
|
|
2020-05-28 08:19:36 +00:00
|
|
|
check pong1.isOk() and pong2.isOk()
|
2020-03-24 09:51:34 +00:00
|
|
|
|
|
|
|
await bootnode.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
|
|
|
await node1.revalidateNode(bootnode.localNode)
|
|
|
|
await node1.revalidateNode(node2.localNode)
|
|
|
|
|
2020-04-20 11:50:22 +00:00
|
|
|
let n = node1.getNode(bootnode.localNode.id)
|
2020-05-28 08:19:36 +00:00
|
|
|
check:
|
|
|
|
n.isSome()
|
|
|
|
n.get() == bootnode.localNode
|
|
|
|
node1.getNode(node2.localNode.id).isNone()
|
2020-03-24 09:51:34 +00:00
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
|
2020-03-16 22:56:00 +00:00
|
|
|
test "Distance check":
|
|
|
|
const
|
|
|
|
targetId = "0x0000"
|
|
|
|
testValues = [
|
2021-10-07 14:03:12 +00:00
|
|
|
("0x0000", 0'u16),
|
2021-07-13 08:05:46 +00:00
|
|
|
("0x0001", 1'u16),
|
|
|
|
("0x0002", 2'u16),
|
|
|
|
("0x0003", 2'u16),
|
|
|
|
("0x0004", 3'u16),
|
|
|
|
("0x0007", 3'u16),
|
|
|
|
("0x0008", 4'u16),
|
|
|
|
("0x000f", 4'u16),
|
|
|
|
("0x0080", 8'u16),
|
|
|
|
("0x00ff", 8'u16),
|
|
|
|
("0x0100", 9'u16),
|
|
|
|
("0x01ff", 9'u16),
|
|
|
|
("0x8000", 16'u16),
|
|
|
|
("0xffff", 16'u16)
|
2020-03-16 22:56:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for (id, d) in testValues:
|
2021-10-07 14:03:12 +00:00
|
|
|
check logDistance(parse(targetId, UInt256, 16), parse(id, UInt256, 16)) == d
|
2020-03-16 22:56:00 +00:00
|
|
|
|
|
|
|
test "Distance check with keys":
|
2021-11-17 21:55:19 +00:00
|
|
|
# Values for this test are taken from
|
|
|
|
# https://github.com/ethereum/go-ethereum/blob/d8ff53dfb8a516f47db37dbc7fd7ad18a1e8a125/p2p/discover/v4_lookup_test.go#L176
|
2020-03-16 22:56:00 +00:00
|
|
|
const
|
|
|
|
targetKey = "5d485bdcbe9bc89314a10ae9231e429d33853e3a8fa2af39f5f827370a2e4185e344ace5d16237491dad41f278f1d3785210d29ace76cd627b9147ee340b1125"
|
|
|
|
testValues = [
|
2021-07-13 08:05:46 +00:00
|
|
|
("29738ba0c1a4397d6a65f292eee07f02df8e58d41594ba2be3cf84ce0fc58169", 251'u16),
|
2021-11-17 21:55:19 +00:00
|
|
|
("511b1686e4e58a917f7f848e9bf5539d206a68f5ad6b54b552c2399fe7d174ae", 251'u16),
|
|
|
|
("d09e5eaeec0fd596236faed210e55ef45112409a5aa7f3276d26646080dcfaeb", 251'u16),
|
|
|
|
("c1e20dbbf0d530e50573bd0a260b32ec15eb9190032b4633d44834afc8afe578", 251'u16),
|
|
|
|
("ed5f38f5702d92d306143e5d9154fb21819777da39af325ea359f453d179e80b", 251'u16),
|
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
("1c9b1cafbec00848d2c174b858219914b42a7d5c9359b1ca03fd650e8239ae94", 252'u16),
|
2021-11-17 21:55:19 +00:00
|
|
|
("e0e1e8db4a6f13c1ffdd3e96b72fa7012293ced187c9dcdcb9ba2af37a46fa10", 252'u16),
|
|
|
|
("3d53823e0a0295cb09f3e11d16c1b44d07dd37cec6f739b8df3a590189fe9fb9", 252'u16),
|
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
("2d0511ae9bf590166597eeab86b6f27b1ab761761eaea8965487b162f8703847", 253'u16),
|
2021-11-17 21:55:19 +00:00
|
|
|
("6cfbd7b8503073fc3dbdb746a7c672571648d3bd15197ccf7f7fef3d904f53a2", 253'u16),
|
|
|
|
("a30599b12827b69120633f15b98a7f6bc9fc2e9a0fd6ae2ebb767c0e64d743ab", 253'u16),
|
|
|
|
("14a98db9b46a831d67eff29f3b85b1b485bb12ae9796aea98d91be3dc78d8a91", 253'u16),
|
|
|
|
("2369ff1fc1ff8ca7d20b17e2673adc3365c3674377f21c5d9dafaff21fe12e24", 253'u16),
|
|
|
|
("9ae91101d6b5048607f41ec0f690ef5d09507928aded2410aabd9237aa2727d7", 253'u16),
|
|
|
|
("05e3c59090a3fd1ae697c09c574a36fcf9bedd0afa8fe3946f21117319ca4973", 253'u16),
|
|
|
|
("06f31c5ea632658f718a91a1b1b9ae4b7549d7b3bc61cbc2be5f4a439039f3ad", 253'u16),
|
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
("dec742079ec00ff4ec1284d7905bc3de2366f67a0769431fd16f80fd68c58a7c", 254'u16),
|
2021-11-17 21:55:19 +00:00
|
|
|
("ff02c8861fa12fbd129d2a95ea663492ef9c1e51de19dcfbbfe1c59894a28d2b", 254'u16),
|
|
|
|
("4dded9e4eefcbce4262be4fd9e8a773670ab0b5f448f286ec97dfc8cf681444a", 254'u16),
|
|
|
|
("750d931e2a8baa2c9268cb46b7cd851f4198018bed22f4dceb09dd334a2395f6", 254'u16),
|
|
|
|
("ce1435a956a98ffec484cd11489c4f165cf1606819ab6b521cee440f0c677e9e", 254'u16),
|
|
|
|
("996e7f8d1638be92d7328b4770f47e5420fc4bafecb4324fd33b1f5d9f403a75", 254'u16),
|
|
|
|
("46bd1eddcf6431bea66fc19ebc45df191c1c7d6ed552dcdc7392885009c322f0", 254'u16),
|
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
("da8645f90826e57228d9ea72aff84500060ad111a5d62e4af831ed8e4b5acfb8", 255'u16),
|
2021-11-17 21:55:19 +00:00
|
|
|
("3c944c5d9af51d4c1d43f5d0f3a1a7ef65d5e82744d669b58b5fed242941a566", 255'u16),
|
|
|
|
("5ebcde76f1d579eebf6e43b0ffe9157e65ffaa391175d5b9aa988f47df3e33da", 255'u16),
|
|
|
|
("97f78253a7d1d796e4eaabce721febcc4550dd68fb11cc818378ba807a2cb7de", 255'u16),
|
|
|
|
("a38cd7dc9b4079d1c0406afd0fdb1165c285f2c44f946eca96fc67772c988c7d", 255'u16),
|
|
|
|
("d64cbb3ffdf712c372b7a22a176308ef8f91861398d5dbaf326fd89c6eaeef1c", 255'u16),
|
|
|
|
("d269609743ef29d6446e3355ec647e38d919c82a4eb5837e442efd7f4218944f", 255'u16),
|
|
|
|
("d8f7bcc4a530efde1d143717007179e0d9ace405ddaaf151c4d863753b7fd64c", 255'u16),
|
|
|
|
|
|
|
|
("8c5b422155d33ea8e9d46f71d1ad3e7b24cb40051413ffa1a81cff613d243ba9", 256'u16),
|
|
|
|
("937b1af801def4e8f5a3a8bd225a8bcff1db764e41d3e177f2e9376e8dd87233", 256'u16),
|
|
|
|
("120260dce739b6f71f171da6f65bc361b5fad51db74cf02d3e973347819a6518", 256'u16),
|
|
|
|
("1fa56cf25d4b46c2bf94e82355aa631717b63190785ac6bae545a88aadc304a9", 256'u16),
|
|
|
|
("3c38c503c0376f9b4adcbe935d5f4b890391741c764f61b03cd4d0d42deae002", 256'u16),
|
|
|
|
("3a54af3e9fa162bc8623cdf3e5d9b70bf30ade1d54cc3abea8659aba6cff471f", 256'u16),
|
|
|
|
("6799a02ea1999aefdcbcc4d3ff9544478be7365a328d0d0f37c26bd95ade0cda", 256'u16),
|
|
|
|
("e24a7bc9051058f918646b0f6e3d16884b2a55a15553b89bab910d55ebc36116", 256'u16)
|
2020-03-16 22:56:00 +00:00
|
|
|
]
|
|
|
|
|
2020-04-04 16:44:01 +00:00
|
|
|
let targetId = toNodeId(PublicKey.fromHex(targetKey)[])
|
2020-03-16 22:56:00 +00:00
|
|
|
|
|
|
|
for (key, d) in testValues:
|
2020-06-22 16:07:48 +00:00
|
|
|
let id = toNodeId(PrivateKey.fromHex(key)[].toPublicKey())
|
2021-10-07 14:03:12 +00:00
|
|
|
check logDistance(targetId, id) == d
|
2020-04-14 09:08:08 +00:00
|
|
|
|
|
|
|
test "Distance to id check":
|
|
|
|
const
|
|
|
|
targetId = "0x0000"
|
|
|
|
testValues = [ # possible id in that distance range
|
2021-07-13 08:05:46 +00:00
|
|
|
("0x0001", 1'u16),
|
|
|
|
("0x0002", 2'u16),
|
|
|
|
("0x0004", 3'u16),
|
|
|
|
("0x0008", 4'u16),
|
|
|
|
("0x0080", 8'u16),
|
|
|
|
("0x0100", 9'u16),
|
|
|
|
("0x8000", 16'u16)
|
2020-04-14 09:08:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for (id, d) in testValues:
|
|
|
|
check idAtDistance(parse(targetId, UInt256, 16), d) == parse(id, UInt256, 16)
|
|
|
|
|
|
|
|
test "Distance to id check with keys":
|
|
|
|
const
|
|
|
|
targetKey = "5d485bdcbe9bc89314a10ae9231e429d33853e3a8fa2af39f5f827370a2e4185e344ace5d16237491dad41f278f1d3785210d29ace76cd627b9147ee340b1125"
|
|
|
|
testValues = [ # possible id in that distance range
|
2021-07-13 08:05:46 +00:00
|
|
|
("9e5b34809116e3790b2258a45e7ef03b11af786503fb1a6d4b4a8ca021ad653c", 251'u16),
|
|
|
|
("925b34809116e3790b2258a45e7ef03b11af786503fb1a6d4b4a8ca021ad653c", 252'u16),
|
|
|
|
("8a5b34809116e3790b2258a45e7ef03b11af786503fb1a6d4b4a8ca021ad653c", 253'u16),
|
|
|
|
("ba5b34809116e3790b2258a45e7ef03b11af786503fb1a6d4b4a8ca021ad653c", 254'u16),
|
|
|
|
("da5b34809116e3790b2258a45e7ef03b11af786503fb1a6d4b4a8ca021ad653c", 255'u16),
|
|
|
|
("1a5b34809116e3790b2258a45e7ef03b11af786503fb1a6d4b4a8ca021ad653c", 256'u16)
|
2020-04-14 09:08:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
let targetId = toNodeId(PublicKey.fromHex(targetKey)[])
|
|
|
|
|
|
|
|
for (id, d) in testValues:
|
|
|
|
check idAtDistance(targetId, d) == parse(id, UInt256, 16)
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
asyncTest "FindNode Test":
|
2021-07-13 08:05:46 +00:00
|
|
|
const dist = 253'u16
|
2020-04-15 21:34:36 +00:00
|
|
|
let
|
|
|
|
mainNodeKey = PrivateKey.fromHex(
|
|
|
|
"a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")[]
|
|
|
|
testNodeKey = PrivateKey.fromHex(
|
|
|
|
"a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a618")[]
|
2020-07-07 08:56:26 +00:00
|
|
|
mainNode = initDiscoveryNode(rng, mainNodeKey, localAddress(20301))
|
|
|
|
testNode = initDiscoveryNode(rng, testNodeKey, localAddress(20302))
|
2020-04-15 21:34:36 +00:00
|
|
|
# logarithmic distance between mainNode and testNode is 256
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let nodes = nodesAtDistance(mainNode.localNode, rng[], dist, 10)
|
2020-04-15 21:34:36 +00:00
|
|
|
for n in nodes:
|
2020-06-30 11:35:15 +00:00
|
|
|
discard mainNode.addSeenNode(n) # for testing only!
|
|
|
|
|
|
|
|
# ping in one direction to add, ping in the other to update seen.
|
|
|
|
check (await testNode.ping(mainNode.localNode)).isOk()
|
|
|
|
check (await mainNode.ping(testNode.localNode)).isOk()
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
# Get ENR of the node itself
|
|
|
|
var discovered =
|
2021-07-13 08:05:46 +00:00
|
|
|
await findNode(testNode, mainNode.localNode, @[0'u16])
|
2020-04-15 21:34:36 +00:00
|
|
|
check:
|
2020-05-28 08:19:36 +00:00
|
|
|
discovered.isOk
|
|
|
|
discovered[].len == 1
|
|
|
|
discovered[][0] == mainNode.localNode
|
2020-04-15 21:34:36 +00:00
|
|
|
# Get ENRs of nodes added at provided logarithmic distance
|
|
|
|
discovered =
|
2020-09-30 09:26:06 +00:00
|
|
|
await findNode(testNode, mainNode.localNode, @[dist])
|
2020-05-28 08:19:36 +00:00
|
|
|
check discovered.isOk
|
|
|
|
check discovered[].len == 10
|
2020-04-15 21:34:36 +00:00
|
|
|
for n in nodes:
|
2020-05-28 08:19:36 +00:00
|
|
|
check discovered[].contains(n)
|
2020-04-15 21:34:36 +00:00
|
|
|
|
2020-09-30 07:43:51 +00:00
|
|
|
# Too high logarithmic distance, should return no nodes.
|
2020-04-15 21:34:36 +00:00
|
|
|
discovered =
|
2021-07-13 08:05:46 +00:00
|
|
|
await findNode(testNode, mainNode.localNode, @[high(uint16)])
|
2020-09-30 07:43:51 +00:00
|
|
|
check:
|
|
|
|
discovered.isOk
|
|
|
|
discovered[].len == 0
|
|
|
|
|
|
|
|
# Logarithmic distance of 256 should only return the testNode
|
|
|
|
discovered =
|
2021-07-13 08:05:46 +00:00
|
|
|
await findNode(testNode, mainNode.localNode, @[256'u16])
|
2020-04-15 21:34:36 +00:00
|
|
|
check:
|
2020-05-28 08:19:36 +00:00
|
|
|
discovered.isOk
|
|
|
|
discovered[].len == 1
|
|
|
|
discovered[][0] == testNode.localNode
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
# Empty bucket
|
|
|
|
discovered =
|
2021-07-13 08:05:46 +00:00
|
|
|
await findNode(testNode, mainNode.localNode, @[254'u16])
|
2020-05-28 08:19:36 +00:00
|
|
|
check discovered.isOk
|
|
|
|
check discovered[].len == 0
|
2020-04-15 21:34:36 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let moreNodes = nodesAtDistance(mainNode.localNode, rng[], dist, 10)
|
2020-04-15 21:34:36 +00:00
|
|
|
for n in moreNodes:
|
2020-06-30 11:35:15 +00:00
|
|
|
discard mainNode.addSeenNode(n) # for testing only!
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
# Full bucket
|
|
|
|
discovered =
|
2020-09-30 09:26:06 +00:00
|
|
|
await findNode(testNode, mainNode.localNode, @[dist])
|
2020-05-28 08:19:36 +00:00
|
|
|
check discovered.isOk
|
|
|
|
check discovered[].len == 16
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
await mainNode.closeWait()
|
|
|
|
await testNode.closeWait()
|
|
|
|
|
|
|
|
asyncTest "FindNode with test table":
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let mainNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301))
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
# Generate 1000 random nodes and add to our main node's routing table
|
|
|
|
for i in 0..<1000:
|
2020-07-07 08:56:26 +00:00
|
|
|
discard mainNode.addSeenNode(generateNode(PrivateKey.random(rng[]))) # for testing only!
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
neighbours = mainNode.neighbours(mainNode.localNode.id)
|
|
|
|
closest = neighbours[0]
|
2021-10-07 14:03:12 +00:00
|
|
|
closestDistance = logDistance(closest.id, mainNode.localNode.id)
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
debug "Closest neighbour", closestDistance, id=closest.id.toHex()
|
|
|
|
|
|
|
|
let
|
2020-07-07 08:56:26 +00:00
|
|
|
testNode = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302),
|
2020-04-15 21:34:36 +00:00
|
|
|
@[mainNode.localNode.record])
|
2020-09-30 09:26:06 +00:00
|
|
|
discovered = await findNode(testNode, mainNode.localNode,
|
|
|
|
@[closestDistance])
|
2020-04-15 21:34:36 +00:00
|
|
|
|
2020-05-28 08:19:36 +00:00
|
|
|
check discovered.isOk
|
|
|
|
check closest in discovered[]
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
await mainNode.closeWait()
|
|
|
|
await testNode.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Lookup targets":
|
|
|
|
const
|
|
|
|
nodeCount = 17
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let bootNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301))
|
2020-04-15 21:34:36 +00:00
|
|
|
bootNode.start()
|
|
|
|
|
|
|
|
var nodes = newSeqOfCap[discv5_protocol.Protocol](nodeCount)
|
|
|
|
nodes.add(bootNode)
|
|
|
|
for i in 1 ..< nodeCount:
|
2020-07-07 08:56:26 +00:00
|
|
|
nodes.add(initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301 + i),
|
2020-04-15 21:34:36 +00:00
|
|
|
@[bootNode.localNode.record]))
|
2020-06-30 11:35:15 +00:00
|
|
|
|
|
|
|
# Make sure all nodes have "seen" each other by forcing pings
|
|
|
|
for n in nodes:
|
|
|
|
for t in nodes:
|
|
|
|
if n != t:
|
2020-09-25 09:44:34 +00:00
|
|
|
let pong = await n.ping(t.localNode)
|
|
|
|
check pong.isOk()
|
|
|
|
if pong.isErr():
|
|
|
|
echo pong.error
|
|
|
|
# check (await n.ping(t.localNode)).isOk()
|
2020-06-30 11:35:15 +00:00
|
|
|
|
|
|
|
for i in 1 ..< nodeCount:
|
2020-04-15 21:34:36 +00:00
|
|
|
nodes[i].start()
|
|
|
|
|
|
|
|
for i in 0..<nodeCount-1:
|
|
|
|
let target = nodes[i]
|
|
|
|
let discovered = await nodes[nodeCount-1].lookup(target.localNode.id)
|
|
|
|
debug "Lookup result", target = target.localNode, discovered
|
2020-12-15 12:24:57 +00:00
|
|
|
check discovered[0] == target.localNode
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
for node in nodes:
|
|
|
|
await node.closeWait()
|
2020-04-20 11:50:22 +00:00
|
|
|
|
|
|
|
asyncTest "Resolve target":
|
|
|
|
let
|
2020-07-07 08:56:26 +00:00
|
|
|
mainNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301))
|
|
|
|
lookupNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
targetKey = PrivateKey.random(rng[])
|
2020-04-20 11:50:22 +00:00
|
|
|
targetAddress = localAddress(20303)
|
2020-07-07 08:56:26 +00:00
|
|
|
targetNode = initDiscoveryNode(rng, targetKey, targetAddress)
|
2020-04-20 11:50:22 +00:00
|
|
|
targetId = targetNode.localNode.id
|
|
|
|
|
|
|
|
var targetSeqNum = targetNode.localNode.record.seqNum
|
|
|
|
|
2020-07-17 20:28:03 +00:00
|
|
|
# Populate routing table with target through a ping. Next, close target and
|
|
|
|
# see if resolve works (only local getNode).
|
2020-04-20 11:50:22 +00:00
|
|
|
block:
|
|
|
|
let pong = await targetNode.ping(mainNode.localNode)
|
2020-05-28 08:19:36 +00:00
|
|
|
check pong.isOk()
|
2020-04-20 11:50:22 +00:00
|
|
|
await targetNode.closeWait()
|
|
|
|
let n = await mainNode.resolve(targetId)
|
|
|
|
check:
|
2020-05-28 08:19:36 +00:00
|
|
|
n.isSome()
|
2020-04-20 11:50:22 +00:00
|
|
|
n.get().id == targetId
|
|
|
|
n.get().record.seqNum == targetSeqNum
|
2020-07-17 20:28:03 +00:00
|
|
|
# Node will be removed because of failed findNode request.
|
2020-04-20 11:50:22 +00:00
|
|
|
|
|
|
|
# Bring target back online, update seqNum in ENR, check if we get the
|
|
|
|
# updated ENR.
|
|
|
|
block:
|
2020-06-30 11:35:15 +00:00
|
|
|
targetNode.open()
|
2020-09-10 12:49:48 +00:00
|
|
|
# Request the target ENR and manually add it to the routing table.
|
|
|
|
# Ping for handshake based ENR passing will not work as our previous
|
|
|
|
# session will still be in the LRU cache.
|
2021-07-13 08:05:46 +00:00
|
|
|
let nodes = await mainNode.findNode(targetNode.localNode, @[0'u16])
|
2020-09-10 12:49:48 +00:00
|
|
|
check:
|
|
|
|
nodes.isOk()
|
|
|
|
nodes[].len == 1
|
|
|
|
mainNode.addNode(nodes[][0])
|
2020-07-08 09:45:58 +00:00
|
|
|
|
2020-04-20 11:50:22 +00:00
|
|
|
targetSeqNum.inc()
|
2020-07-08 09:45:58 +00:00
|
|
|
# need to add something to get the enr sequence number incremented
|
2020-07-08 11:13:29 +00:00
|
|
|
let update = targetNode.updateRecord({"addsomefield": @[byte 1]})
|
2020-07-08 09:45:58 +00:00
|
|
|
check update.isOk()
|
|
|
|
|
2020-07-17 20:28:03 +00:00
|
|
|
var n = mainNode.getNode(targetId)
|
|
|
|
check:
|
|
|
|
n.isSome()
|
|
|
|
n.get().id == targetId
|
|
|
|
n.get().record.seqNum == targetSeqNum - 1
|
|
|
|
|
|
|
|
n = await mainNode.resolve(targetId)
|
2020-04-20 11:50:22 +00:00
|
|
|
check:
|
2020-05-28 08:19:36 +00:00
|
|
|
n.isSome()
|
2020-04-20 11:50:22 +00:00
|
|
|
n.get().id == targetId
|
|
|
|
n.get().record.seqNum == targetSeqNum
|
|
|
|
|
2020-07-17 20:28:03 +00:00
|
|
|
# Add the updated version
|
2020-11-26 17:20:15 +00:00
|
|
|
discard mainNode.addNode(n.get())
|
2020-07-17 20:28:03 +00:00
|
|
|
|
|
|
|
# Update seqNum in ENR again, ping lookupNode to be added in routing table,
|
2020-04-20 11:50:22 +00:00
|
|
|
# close targetNode, resolve should lookup, check if we get updated ENR.
|
|
|
|
block:
|
|
|
|
targetSeqNum.inc()
|
2020-07-08 11:13:29 +00:00
|
|
|
let update = targetNode.updateRecord({"addsomefield": @[byte 2]})
|
2020-10-19 19:04:53 +00:00
|
|
|
check update.isOk()
|
2020-04-20 11:50:22 +00:00
|
|
|
|
2020-06-30 11:35:15 +00:00
|
|
|
# ping node so that its ENR gets added
|
|
|
|
check (await targetNode.ping(lookupNode.localNode)).isOk()
|
|
|
|
# ping node so that it becomes "seen" and thus will be forwarded on a
|
|
|
|
# findNode request
|
|
|
|
check (await lookupNode.ping(targetNode.localNode)).isOk()
|
2020-04-20 11:50:22 +00:00
|
|
|
await targetNode.closeWait()
|
|
|
|
|
2020-07-17 20:28:03 +00:00
|
|
|
check mainNode.addNode(lookupNode.localNode.record)
|
2020-04-20 11:50:22 +00:00
|
|
|
let n = await mainNode.resolve(targetId)
|
|
|
|
check:
|
2020-05-28 08:19:36 +00:00
|
|
|
n.isSome()
|
2020-04-20 11:50:22 +00:00
|
|
|
n.get().id == targetId
|
|
|
|
n.get().record.seqNum == targetSeqNum
|
|
|
|
|
|
|
|
await mainNode.closeWait()
|
|
|
|
await lookupNode.closeWait()
|
2020-06-11 19:24:52 +00:00
|
|
|
|
|
|
|
asyncTest "Random nodes with enr field filter":
|
|
|
|
let
|
2020-07-07 08:56:26 +00:00
|
|
|
lookupNode = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301))
|
2020-06-11 19:24:52 +00:00
|
|
|
targetFieldPair = toFieldPair("test", @[byte 1,2,3,4])
|
2020-07-07 08:56:26 +00:00
|
|
|
targetNode = generateNode(PrivateKey.random(rng[]), localEnrFields = [targetFieldPair])
|
2020-06-11 19:24:52 +00:00
|
|
|
otherFieldPair = toFieldPair("test", @[byte 1,2,3,4,5])
|
2020-07-07 08:56:26 +00:00
|
|
|
otherNode = generateNode(PrivateKey.random(rng[]), localEnrFields = [otherFieldPair])
|
|
|
|
anotherNode = generateNode(PrivateKey.random(rng[]))
|
2020-06-11 19:24:52 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
lookupNode.addNode(targetNode)
|
|
|
|
lookupNode.addNode(otherNode)
|
|
|
|
lookupNode.addNode(anotherNode)
|
|
|
|
|
|
|
|
let discovered = lookupNode.randomNodes(10)
|
|
|
|
check discovered.len == 3
|
|
|
|
let discoveredFiltered = lookupNode.randomNodes(10,
|
|
|
|
("test", @[byte 1,2,3,4]))
|
|
|
|
check discoveredFiltered.len == 1 and discoveredFiltered.contains(targetNode)
|
2024-10-24 15:24:53 +00:00
|
|
|
let discoveredEmpty = lookupNode.randomNodes(10,
|
|
|
|
proc(n: Node) : bool = false)
|
|
|
|
check discoveredEmpty.len == 0
|
2020-06-11 19:24:52 +00:00
|
|
|
|
|
|
|
await lookupNode.closeWait()
|
2020-07-08 11:13:29 +00:00
|
|
|
|
|
|
|
test "New protocol with enr":
|
|
|
|
let
|
|
|
|
privKey = PrivateKey.random(rng[])
|
2024-06-18 16:09:27 +00:00
|
|
|
ip = Opt.some(parseIpAddress("127.0.0.1"))
|
2020-07-08 11:13:29 +00:00
|
|
|
port = Port(20301)
|
2024-06-18 16:09:27 +00:00
|
|
|
node = newProtocol(privKey, ip, Opt.some(port), Opt.some(port), bindPort = port,
|
2021-03-02 16:13:29 +00:00
|
|
|
rng = rng)
|
2024-06-18 16:09:27 +00:00
|
|
|
noUpdatesNode = newProtocol(privKey, ip, Opt.some(port), Opt.some(port),
|
|
|
|
bindPort = port, rng = rng, previousRecord = Opt.some(node.getRecord()))
|
|
|
|
updatesNode = newProtocol(privKey, ip, Opt.some(port), Opt.some(Port(20302)),
|
2021-03-02 16:13:29 +00:00
|
|
|
bindPort = port, rng = rng,
|
2024-06-18 16:09:27 +00:00
|
|
|
previousRecord = Opt.some(noUpdatesNode.getRecord()))
|
|
|
|
moreUpdatesNode = newProtocol(privKey, ip, Opt.some(port), Opt.some(port),
|
2021-03-02 16:13:29 +00:00
|
|
|
bindPort = port, rng = rng, localEnrFields = {"addfield": @[byte 0]},
|
2024-06-18 16:09:27 +00:00
|
|
|
previousRecord = Opt.some(updatesNode.getRecord()))
|
2020-07-08 11:13:29 +00:00
|
|
|
check:
|
|
|
|
node.getRecord().seqNum == 1
|
2024-06-24 12:57:39 +00:00
|
|
|
noUpdatesNode.getRecord().seqNum == 2
|
|
|
|
updatesNode.getRecord().seqNum == 3
|
|
|
|
moreUpdatesNode.getRecord().seqNum == 4
|
2020-07-08 11:13:29 +00:00
|
|
|
|
|
|
|
# Defect (for now?) on incorrect key use
|
|
|
|
expect ResultDefect:
|
|
|
|
let incorrectKeyUpdates = newProtocol(PrivateKey.random(rng[]),
|
2024-06-18 16:09:27 +00:00
|
|
|
ip, Opt.some(port), Opt.some(port), bindPort = port, rng = rng,
|
|
|
|
previousRecord = Opt.some(updatesNode.getRecord()))
|
2020-07-15 13:27:22 +00:00
|
|
|
|
2020-07-17 20:28:03 +00:00
|
|
|
asyncTest "Update node record with revalidate":
|
|
|
|
let
|
|
|
|
mainNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301))
|
|
|
|
testNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
testNodeId = testNode.localNode.id
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Get node with current ENR in routing table.
|
|
|
|
# Handshake will get done here.
|
|
|
|
(await testNode.ping(mainNode.localNode)).isOk()
|
|
|
|
testNode.updateRecord({"test" : @[byte 1]}).isOk()
|
|
|
|
testNode.localNode.record.seqNum == 2
|
|
|
|
|
|
|
|
# Get the node from routing table, seqNum should still be 1.
|
|
|
|
var n = mainNode.getNode(testNodeId)
|
|
|
|
check:
|
|
|
|
n.isSome()
|
|
|
|
n.get.record.seqNum == 1
|
|
|
|
|
|
|
|
# This should not do a handshake and thus the new ENR must come from the
|
|
|
|
# findNode(0)
|
|
|
|
await mainNode.revalidateNode(n.get)
|
|
|
|
|
|
|
|
# Get the node from routing table, and check if record got updated.
|
|
|
|
n = mainNode.getNode(testNodeId)
|
|
|
|
check:
|
|
|
|
n.isSome()
|
|
|
|
n.get.record.seqNum == 2
|
|
|
|
|
|
|
|
await mainNode.closeWait()
|
|
|
|
await testNode.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Update node record with handshake":
|
|
|
|
let
|
|
|
|
mainNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301))
|
|
|
|
testNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
testNodeId = testNode.localNode.id
|
|
|
|
|
|
|
|
# Add the node (from the record, so new node!) so no handshake is done yet.
|
|
|
|
check: mainNode.addNode(testNode.localNode.record)
|
|
|
|
|
|
|
|
check:
|
|
|
|
testNode.updateRecord({"test" : @[byte 1]}).isOk()
|
|
|
|
testNode.localNode.record.seqNum == 2
|
|
|
|
|
|
|
|
# Get the node from routing table, seqNum should still be 1.
|
|
|
|
var n = mainNode.getNode(testNodeId)
|
|
|
|
check:
|
|
|
|
n.isSome()
|
|
|
|
n.get.record.seqNum == 1
|
|
|
|
|
|
|
|
# This should do a handshake and update the ENR through that.
|
|
|
|
check (await testNode.ping(mainNode.localNode)).isOk()
|
|
|
|
|
|
|
|
# Get the node from routing table, and check if record got updated.
|
|
|
|
n = mainNode.getNode(testNodeId)
|
|
|
|
check:
|
|
|
|
n.isSome()
|
|
|
|
n.get.record.seqNum == 2
|
|
|
|
|
|
|
|
await mainNode.closeWait()
|
|
|
|
await testNode.closeWait()
|
|
|
|
|
2023-02-21 08:34:26 +00:00
|
|
|
test "Verify records of Nodes message - global src":
|
2020-07-15 13:27:22 +00:00
|
|
|
let
|
|
|
|
port = Port(9000)
|
2023-02-21 08:34:26 +00:00
|
|
|
srcRecord = enr.Record.init(1, PrivateKey.random(rng[]),
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(parseIpAddress("11.12.13.14")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2024-06-27 14:18:21 +00:00
|
|
|
srcNode = Node.fromRecord(srcRecord)
|
2020-07-15 13:27:22 +00:00
|
|
|
pk = PrivateKey.random(rng[])
|
2023-02-21 08:34:26 +00:00
|
|
|
targetDistance = @[logDistance(srcNode.id, pk.toPublicKey().toNodeId())]
|
2021-09-13 12:20:26 +00:00
|
|
|
limit = 16
|
2020-07-15 13:27:22 +00:00
|
|
|
|
|
|
|
block: # Duplicates
|
|
|
|
let
|
|
|
|
record = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("12.13.14.15")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2020-07-15 13:27:22 +00:00
|
|
|
|
|
|
|
# Exact duplicates
|
|
|
|
var records = @[record, record]
|
2023-02-21 08:34:26 +00:00
|
|
|
var nodes = verifyNodesRecords(records, srcNode, limit, targetDistance)
|
2020-07-15 13:27:22 +00:00
|
|
|
check nodes.len == 1
|
|
|
|
|
|
|
|
# Node id duplicates
|
|
|
|
let recordSameId = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("212.13.14.15")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2020-07-15 13:27:22 +00:00
|
|
|
records.add(recordSameId)
|
2023-02-21 08:34:26 +00:00
|
|
|
nodes = verifyNodesRecords(records, srcNode, limit, targetDistance)
|
2020-07-15 13:27:22 +00:00
|
|
|
check nodes.len == 1
|
|
|
|
|
|
|
|
block: # No address
|
|
|
|
let
|
|
|
|
recordNoAddress = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.none(IpAddress), Opt.some(port), Opt.some(port))[]
|
2020-07-15 13:27:22 +00:00
|
|
|
records = [recordNoAddress]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords(records, srcNode, limit, targetDistance)
|
|
|
|
check test.len == 0
|
|
|
|
|
|
|
|
block: # Invalid address - any local
|
|
|
|
let
|
|
|
|
recordInvalidAddress = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("0.0.0.0")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2023-02-21 08:34:26 +00:00
|
|
|
records = [recordInvalidAddress]
|
|
|
|
test = verifyNodesRecords(records, srcNode, limit, targetDistance)
|
2020-07-15 13:27:22 +00:00
|
|
|
check test.len == 0
|
|
|
|
|
|
|
|
block: # Invalid address - site local
|
|
|
|
let
|
|
|
|
recordInvalidAddress = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("10.1.2.3")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2020-07-15 13:27:22 +00:00
|
|
|
records = [recordInvalidAddress]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords(records, srcNode, limit, targetDistance)
|
2020-07-15 13:27:22 +00:00
|
|
|
check test.len == 0
|
|
|
|
|
|
|
|
block: # Invalid address - loopback
|
|
|
|
let
|
|
|
|
recordInvalidAddress = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("127.0.0.1")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2020-07-15 13:27:22 +00:00
|
|
|
records = [recordInvalidAddress]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords(records, srcNode, limit, targetDistance)
|
2020-07-15 13:27:22 +00:00
|
|
|
check test.len == 0
|
|
|
|
|
|
|
|
block: # Invalid distance
|
|
|
|
let
|
|
|
|
recordInvalidDistance = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("12.13.14.15")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2020-07-15 13:27:22 +00:00
|
|
|
records = [recordInvalidDistance]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords(records, srcNode, limit, @[0'u16])
|
2020-07-15 13:27:22 +00:00
|
|
|
check test.len == 0
|
2020-11-13 11:34:49 +00:00
|
|
|
|
2021-09-13 12:20:26 +00:00
|
|
|
block: # Invalid distance but distance validation is disabled
|
|
|
|
let
|
|
|
|
recordInvalidDistance = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("12.13.14.15")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2021-09-13 12:20:26 +00:00
|
|
|
records = [recordInvalidDistance]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords(records, srcNode, limit)
|
|
|
|
check test.len == 1
|
|
|
|
|
|
|
|
test "Verify records of Nodes message - loopback src":
|
|
|
|
let
|
|
|
|
port = Port(9000)
|
|
|
|
srcRecord = enr.Record.init(1, PrivateKey.random(rng[]),
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(parseIpAddress("127.0.0.0")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2024-06-27 14:18:21 +00:00
|
|
|
srcNode = Node.fromRecord(srcRecord)
|
2023-02-21 08:34:26 +00:00
|
|
|
pk = PrivateKey.random(rng[])
|
|
|
|
targetDistance = @[logDistance(srcNode.id, pk.toPublicKey().toNodeId())]
|
|
|
|
limit = 16
|
|
|
|
|
|
|
|
block: # valid address - lo with lo src
|
|
|
|
let
|
|
|
|
record = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("127.0.0.1")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords([record], srcNode, limit, targetDistance)
|
|
|
|
check test.len == 1
|
|
|
|
|
|
|
|
block: # valid address - global with lo src
|
|
|
|
let
|
|
|
|
record = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("1.2.3.4")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords([record], srcNode, limit, targetDistance)
|
|
|
|
check test.len == 1
|
|
|
|
|
|
|
|
test "Verify records of Nodes message - site local src":
|
|
|
|
let
|
|
|
|
port = Port(9000)
|
|
|
|
srcRecord = enr.Record.init(1, PrivateKey.random(rng[]),
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(parseIpAddress("192.168.1.1")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2024-06-27 14:18:21 +00:00
|
|
|
srcNode = Node.fromRecord(srcRecord)
|
2023-02-21 08:34:26 +00:00
|
|
|
pk = PrivateKey.random(rng[])
|
|
|
|
targetDistance = @[logDistance(srcNode.id, pk.toPublicKey().toNodeId())]
|
|
|
|
limit = 16
|
|
|
|
|
|
|
|
block: # valid address - site local with site local src
|
|
|
|
let
|
|
|
|
record = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("192.168.1.2")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords([record], srcNode, limit, targetDistance)
|
|
|
|
check test.len == 1
|
|
|
|
|
|
|
|
block: # valid address - global with site local src
|
|
|
|
let
|
|
|
|
record = enr.Record.init(
|
2024-06-18 16:09:27 +00:00
|
|
|
1, pk, Opt.some(parseIpAddress("1.2.3.4")),
|
|
|
|
Opt.some(port), Opt.some(port))[]
|
2023-02-21 08:34:26 +00:00
|
|
|
test = verifyNodesRecords([record], srcNode, limit, targetDistance)
|
2021-09-13 12:20:26 +00:00
|
|
|
check test.len == 1
|
|
|
|
|
2021-09-10 15:26:22 +00:00
|
|
|
test "Calculate lookup distances":
|
2022-11-15 11:36:16 +00:00
|
|
|
let rng = newRng()
|
|
|
|
let node = generateNode(PrivateKey.random(rng[]))
|
|
|
|
|
2021-09-10 15:26:22 +00:00
|
|
|
# Log distance between zeros is zero
|
|
|
|
let dist = lookupDistances(u256(0), u256(0))
|
|
|
|
check dist == @[0'u16, 1, 2]
|
|
|
|
|
|
|
|
# Log distance between zero and one is one
|
|
|
|
let dist1 = lookupDistances(u256(0), u256(1))
|
|
|
|
check dist1 == @[1'u16, 2, 3]
|
|
|
|
|
2022-11-15 11:36:16 +00:00
|
|
|
# Ensure that distances are in expected order
|
|
|
|
let dist2 = lookupDistances(node.id, idAtDistance(node.id, 128))
|
|
|
|
check dist2 == @[128'u16, 129, 127]
|
|
|
|
|
|
|
|
# Test target distance of 256
|
|
|
|
let dist3 = lookupDistances(node.id, idAtDistance(node.id, 256))
|
|
|
|
check dist3 == @[256'u16, 255, 254]
|
|
|
|
|
|
|
|
# Test target distance of 255
|
|
|
|
let dist4 = lookupDistances(node.id, idAtDistance(node.id, 255))
|
|
|
|
check dist4 == @[255'u16, 256, 254]
|
|
|
|
|
|
|
|
# Test target distance of 254
|
|
|
|
let dist5 = lookupDistances(node.id, idAtDistance(node.id, 254))
|
|
|
|
check dist5 == @[254'u16, 255, 253]
|
|
|
|
|
2020-11-13 11:34:49 +00:00
|
|
|
asyncTest "Handshake cleanup: different ids":
|
|
|
|
# Node to test the handshakes on.
|
|
|
|
let receiveNode = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
|
|
|
|
# Create random packets with same ip but different node ids
|
|
|
|
# and "receive" them on receiveNode
|
|
|
|
let a = localAddress(20303)
|
|
|
|
for i in 0 ..< 5:
|
|
|
|
let
|
|
|
|
privKey = PrivateKey.random(rng[])
|
|
|
|
enrRec = enr.Record.init(1, privKey,
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(parseIpAddress("127.0.0.1")), Opt.some(Port(9000)),
|
|
|
|
Opt.some(Port(9000))).expect("Properly initialized private key")
|
2024-06-27 14:18:21 +00:00
|
|
|
sendNode = Node.fromRecord(enrRec)
|
2020-11-13 11:34:49 +00:00
|
|
|
var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5))
|
|
|
|
|
|
|
|
let (packet, _) = encodeMessagePacket(rng[], codec,
|
|
|
|
receiveNode.localNode.id, receiveNode.localNode.address.get(), @[])
|
|
|
|
receiveNode.receive(a, packet)
|
|
|
|
|
|
|
|
# Checking different nodeIds but same address
|
|
|
|
check receiveNode.codec.handshakes.len == 5
|
|
|
|
# TODO: Could get rid of the sleep by storing the timeout future of the
|
|
|
|
# handshake
|
2022-08-05 13:19:16 +00:00
|
|
|
await sleepAsync(defaultHandshakeTimeout)
|
2020-11-13 11:34:49 +00:00
|
|
|
# Checking handshake cleanup
|
|
|
|
check receiveNode.codec.handshakes.len == 0
|
|
|
|
|
|
|
|
await receiveNode.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Handshake cleanup: different ips":
|
|
|
|
# Node to test the handshakes on.
|
|
|
|
let receiveNode = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
|
|
|
|
# Create random packets with same node ids but different ips
|
|
|
|
# and "receive" them on receiveNode
|
|
|
|
let
|
|
|
|
privKey = PrivateKey.random(rng[])
|
|
|
|
enrRec = enr.Record.init(1, privKey,
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(parseIpAddress("127.0.0.1")), Opt.some(Port(9000)),
|
|
|
|
Opt.some(Port(9000))).expect("Properly initialized private key")
|
2024-06-27 14:18:21 +00:00
|
|
|
sendNode = Node.fromRecord(enrRec)
|
2020-11-13 11:34:49 +00:00
|
|
|
var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5))
|
|
|
|
for i in 0 ..< 5:
|
|
|
|
let a = localAddress(20303 + i)
|
|
|
|
let (packet, _) = encodeMessagePacket(rng[], codec,
|
|
|
|
receiveNode.localNode.id, receiveNode.localNode.address.get(), @[])
|
|
|
|
receiveNode.receive(a, packet)
|
|
|
|
|
|
|
|
# Checking different nodeIds but same address
|
|
|
|
check receiveNode.codec.handshakes.len == 5
|
|
|
|
# TODO: Could get rid of the sleep by storing the timeout future of the
|
|
|
|
# handshake
|
2022-08-05 13:19:16 +00:00
|
|
|
await sleepAsync(defaultHandshakeTimeout)
|
2020-11-13 11:34:49 +00:00
|
|
|
# Checking handshake cleanup
|
|
|
|
check receiveNode.codec.handshakes.len == 0
|
|
|
|
|
|
|
|
await receiveNode.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Handshake duplicates":
|
|
|
|
# Node to test the handshakes on.
|
|
|
|
let receiveNode = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
|
|
|
|
# Create random packets with same node ids and same ips
|
|
|
|
# and "receive" them on receiveNode
|
|
|
|
let
|
|
|
|
a = localAddress(20303)
|
|
|
|
privKey = PrivateKey.random(rng[])
|
|
|
|
enrRec = enr.Record.init(1, privKey,
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(parseIpAddress("127.0.0.1")), Opt.some(Port(9000)),
|
|
|
|
Opt.some(Port(9000))).expect("Properly initialized private key")
|
2024-06-27 14:18:21 +00:00
|
|
|
sendNode = Node.fromRecord(enrRec)
|
2020-11-13 11:34:49 +00:00
|
|
|
var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5))
|
|
|
|
|
|
|
|
var firstRequestNonce: AESGCMNonce
|
|
|
|
for i in 0 ..< 5:
|
|
|
|
let (packet, requestNonce) = encodeMessagePacket(rng[], codec,
|
|
|
|
receiveNode.localNode.id, receiveNode.localNode.address.get(), @[])
|
|
|
|
receiveNode.receive(a, packet)
|
|
|
|
if i == 0:
|
|
|
|
firstRequestNonce = requestNonce
|
|
|
|
|
|
|
|
# Check handshake duplicates
|
|
|
|
check receiveNode.codec.handshakes.len == 1
|
|
|
|
# Check if it is for the first packet that a handshake is stored
|
2021-12-20 12:14:50 +00:00
|
|
|
let key = HandshakeKey(nodeId: sendNode.id, address: a)
|
2020-11-13 11:34:49 +00:00
|
|
|
check receiveNode.codec.handshakes[key].whoareyouData.requestNonce ==
|
|
|
|
firstRequestNonce
|
|
|
|
|
|
|
|
await receiveNode.closeWait()
|
2021-05-20 07:49:46 +00:00
|
|
|
|
|
|
|
asyncTest "Talkreq no protocol":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
2022-02-03 14:51:08 +00:00
|
|
|
talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
|
2021-05-20 07:49:46 +00:00
|
|
|
@[byte 0x01], @[])
|
|
|
|
|
|
|
|
check:
|
|
|
|
talkresp.isOk()
|
2021-07-30 14:04:14 +00:00
|
|
|
talkresp.get().len == 0
|
2021-05-20 07:49:46 +00:00
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Talkreq echo protocol":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
talkProtocol = "echo".toBytes()
|
|
|
|
|
2022-05-02 14:49:19 +00:00
|
|
|
proc handler(
|
|
|
|
protocol: TalkProtocol, request: seq[byte],
|
2023-08-30 15:44:05 +00:00
|
|
|
fromId: NodeId, fromUdpAddress: Address,
|
|
|
|
node: Opt[Node]):
|
2023-05-10 13:50:04 +00:00
|
|
|
seq[byte] {.gcsafe, raises: [].} =
|
2021-05-20 07:49:46 +00:00
|
|
|
request
|
|
|
|
|
2021-05-31 09:15:54 +00:00
|
|
|
let echoProtocol = TalkProtocol(protocolHandler: handler)
|
|
|
|
|
|
|
|
check node2.registerTalkProtocol(talkProtocol, echoProtocol).isOk()
|
2022-02-03 14:51:08 +00:00
|
|
|
let talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
|
2021-05-20 07:49:46 +00:00
|
|
|
talkProtocol, "hello".toBytes())
|
|
|
|
|
|
|
|
check:
|
|
|
|
talkresp.isOk()
|
2021-07-30 14:04:14 +00:00
|
|
|
talkresp.get() == "hello".toBytes()
|
2021-05-20 07:49:46 +00:00
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Talkreq register protocols":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
talkProtocol = "echo".toBytes()
|
|
|
|
|
2022-05-02 14:49:19 +00:00
|
|
|
proc handler(
|
|
|
|
protocol: TalkProtocol, request: seq[byte],
|
2023-08-30 15:44:05 +00:00
|
|
|
fromId: NodeId, fromUdpAddress: Address,
|
|
|
|
node: Opt[Node]):
|
2023-05-10 13:50:04 +00:00
|
|
|
seq[byte] {.gcsafe, raises: [].} =
|
2021-05-20 07:49:46 +00:00
|
|
|
request
|
|
|
|
|
2021-05-31 09:15:54 +00:00
|
|
|
let echoProtocol = TalkProtocol(protocolHandler: handler)
|
|
|
|
|
2021-05-20 07:49:46 +00:00
|
|
|
check:
|
2021-05-31 09:15:54 +00:00
|
|
|
node2.registerTalkProtocol(talkProtocol, echoProtocol).isOk()
|
|
|
|
node2.registerTalkProtocol(talkProtocol, echoProtocol).isErr()
|
|
|
|
node2.registerTalkProtocol("test".toBytes(), echoProtocol).isOk()
|
2021-05-20 07:49:46 +00:00
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
2022-05-02 14:49:19 +00:00
|
|
|
|
|
|
|
asyncTest "Max packet size: Request":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
talkProtocol = "echo".toBytes()
|
|
|
|
|
|
|
|
proc handler(
|
|
|
|
protocol: TalkProtocol, request: seq[byte],
|
2023-08-30 15:44:05 +00:00
|
|
|
fromId: NodeId, fromUdpAddress: Address,
|
|
|
|
node: Opt[Node]):
|
2023-05-10 13:50:04 +00:00
|
|
|
seq[byte] {.gcsafe, raises: [].} =
|
2022-05-02 14:49:19 +00:00
|
|
|
request
|
|
|
|
|
|
|
|
let echoProtocol = TalkProtocol(protocolHandler: handler)
|
|
|
|
|
|
|
|
check node2.registerTalkProtocol(talkProtocol, echoProtocol).isOk()
|
|
|
|
# Do a ping first so a session is created, that makes the next message to
|
|
|
|
# be an ordinary message and more easy to reverse calculate packet sizes for
|
|
|
|
# than for a handshake message.
|
|
|
|
check (await node1.ping(node2.localNode)).isOk()
|
|
|
|
|
|
|
|
block: # 1172 = 1280 - 103 - 4 - 1 = max - talkreq - "echo" - rlp blob
|
|
|
|
let talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
|
|
|
|
talkProtocol, repeat(byte 6, 1172))
|
|
|
|
|
|
|
|
check:
|
|
|
|
talkresp.isOk()
|
|
|
|
|
|
|
|
block: # > 1280 -> should fail
|
|
|
|
let talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
|
|
|
|
talkProtocol, repeat(byte 6, 1173))
|
|
|
|
|
|
|
|
check:
|
|
|
|
talkresp.isErr()
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Max packet size: Response":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
talkProtocol = "echo".toBytes()
|
|
|
|
|
|
|
|
proc handler(
|
|
|
|
protocol: TalkProtocol, request: seq[byte],
|
2023-08-30 15:44:05 +00:00
|
|
|
fromId: NodeId, fromUdpAddress: Address,
|
|
|
|
node: Opt[Node]):
|
2023-05-10 13:50:04 +00:00
|
|
|
seq[byte] {.gcsafe, raises: [].} =
|
2022-05-02 14:49:19 +00:00
|
|
|
# Return the request + same protocol id + 2 bytes, to make it 1 byte
|
|
|
|
# bigger than the request
|
|
|
|
request & "echo12".toBytes()
|
|
|
|
|
|
|
|
let echoProtocol = TalkProtocol(protocolHandler: handler)
|
|
|
|
|
|
|
|
check node2.registerTalkProtocol(talkProtocol, echoProtocol).isOk()
|
|
|
|
# Do a ping first so a session is created, that makes the next message to
|
|
|
|
# be an ordinary message and more easy to reverse calculate packet sizes for
|
|
|
|
# than for a handshake message.
|
|
|
|
check (await node1.ping(node2.localNode)).isOk()
|
|
|
|
|
|
|
|
block: # 1171 -> response will be 1 byte bigger thus this should pass
|
|
|
|
let talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
|
|
|
|
talkProtocol, repeat(byte 6, 1171))
|
|
|
|
|
|
|
|
check:
|
|
|
|
talkresp.isOk()
|
|
|
|
|
|
|
|
block: # 1172 -> response will be 1 byte bigger thus this should fail
|
|
|
|
let talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
|
|
|
|
talkProtocol, repeat(byte 6, 1172))
|
|
|
|
|
|
|
|
check:
|
|
|
|
talkresp.isErr()
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|