2020-02-17 15:36:04 +00:00
|
|
|
import
|
2020-03-20 15:38:46 +00:00
|
|
|
random, unittest, chronos, sequtils, chronicles, tables, stint, options,
|
2020-02-26 22:15:14 +00:00
|
|
|
eth/[keys, rlp], eth/p2p/enode, eth/trie/db,
|
|
|
|
eth/p2p/discoveryv5/[discovery_db, enr, node, types, routing_table, encoding],
|
2020-02-17 15:36:04 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
|
|
|
./p2p_test_helper
|
|
|
|
|
2020-02-24 14:45:30 +00:00
|
|
|
proc initDiscoveryNode*(privKey: PrivateKey, address: Address,
|
2020-03-13 16:48:03 +00:00
|
|
|
bootstrapRecords: seq[Record]):
|
|
|
|
discv5_protocol.Protocol =
|
2020-02-17 15:36:04 +00:00
|
|
|
var db = DiscoveryDB.init(newMemoryDB())
|
2020-02-21 23:55:37 +00:00
|
|
|
result = newProtocol(privKey, db,
|
|
|
|
parseIpAddress("127.0.0.1"),
|
2020-03-13 16:48:03 +00:00
|
|
|
address.tcpPort, address.udpPort,
|
|
|
|
bootstrapRecords)
|
2020-02-17 15:36:04 +00:00
|
|
|
|
|
|
|
result.open()
|
|
|
|
|
|
|
|
proc nodeIdInNodes(id: NodeId, nodes: openarray[Node]): bool =
|
|
|
|
for n in nodes:
|
|
|
|
if id == n.id: return true
|
|
|
|
|
2020-02-27 21:36:42 +00:00
|
|
|
# Creating a random packet with specific nodeid each time
|
|
|
|
proc randomPacket(tag: PacketTag): seq[byte] =
|
|
|
|
var
|
|
|
|
authTag: AuthTag
|
|
|
|
msg: array[44, byte]
|
|
|
|
|
|
|
|
randomBytes(authTag)
|
|
|
|
randomBytes(msg)
|
|
|
|
result.add(tag)
|
|
|
|
result.add(rlp.encode(authTag))
|
|
|
|
result.add(msg)
|
|
|
|
|
2020-03-27 13:37:31 +00:00
|
|
|
proc generateNode(privKey = newPrivateKey(), port: int): Node =
|
|
|
|
let enr = enr.Record.init(1, privKey, some(localAddress(port)))
|
2020-03-20 15:38:46 +00:00
|
|
|
result = newNode(enr)
|
|
|
|
|
2020-02-17 15:36:04 +00:00
|
|
|
suite "Discovery v5 Tests":
|
2020-02-24 14:45:30 +00:00
|
|
|
asyncTest "Random nodes":
|
2020-02-17 15:36:04 +00:00
|
|
|
let
|
|
|
|
bootNodeKey = initPrivateKey("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")
|
2020-02-26 22:15:14 +00:00
|
|
|
bootNode = initDiscoveryNode(bootNodeKey, localAddress(20301), @[])
|
2020-02-17 15:36:04 +00:00
|
|
|
|
|
|
|
let nodeKeys = [
|
|
|
|
initPrivateKey("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a618"),
|
|
|
|
initPrivateKey("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a619"),
|
|
|
|
initPrivateKey("a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a620")
|
|
|
|
]
|
|
|
|
var nodeAddrs = newSeqOfCap[Address](nodeKeys.len)
|
|
|
|
for i in 0 ..< nodeKeys.len: nodeAddrs.add(localAddress(20302 + i))
|
|
|
|
|
|
|
|
var nodes = zip(nodeKeys, nodeAddrs).mapIt(
|
2020-03-23 21:09:16 +00:00
|
|
|
initDiscoveryNode(it[0], it[1], @[bootNode.localNode.record]))
|
2020-02-17 15:36:04 +00:00
|
|
|
nodes.add(bootNode)
|
|
|
|
|
|
|
|
for node in nodes:
|
|
|
|
let discovered = await node.lookupRandom()
|
2020-02-19 12:11:19 +00:00
|
|
|
check discovered.len < nodes.len
|
2020-02-24 14:45:30 +00:00
|
|
|
debug "Lookup from random id", node = node.localNode, discovered
|
2020-02-17 15:36:04 +00:00
|
|
|
|
|
|
|
# Check for each node if the other nodes shows up in the routing table
|
|
|
|
for i in nodes:
|
|
|
|
for j in nodes:
|
|
|
|
if j != i:
|
|
|
|
check(nodeIdInNodes(i.localNode.id, j.randomNodes(nodes.len - 1)))
|
2020-02-24 14:45:30 +00:00
|
|
|
|
|
|
|
for node in nodes:
|
|
|
|
await node.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Lookup targets":
|
|
|
|
const
|
2020-02-25 13:49:31 +00:00
|
|
|
nodeCount = 17
|
2020-02-24 14:45:30 +00:00
|
|
|
|
2020-02-26 22:15:14 +00:00
|
|
|
let bootNode = initDiscoveryNode(newPrivateKey(), localAddress(20301), @[])
|
2020-03-18 22:05:04 +00:00
|
|
|
bootNode.start()
|
2020-02-24 14:45:30 +00:00
|
|
|
|
|
|
|
var nodes = newSeqOfCap[discv5_protocol.Protocol](nodeCount)
|
|
|
|
nodes.add(bootNode)
|
|
|
|
for i in 1 ..< nodeCount:
|
|
|
|
nodes.add(initDiscoveryNode(newPrivateKey(), localAddress(20301 + i),
|
|
|
|
@[bootNode.localNode.record]))
|
2020-03-18 22:05:04 +00:00
|
|
|
nodes[i].start()
|
2020-02-24 14:45:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
# if lookUp would return ordered on distance we could check discovered[0]
|
|
|
|
check discovered.contains(target.localNode)
|
|
|
|
|
|
|
|
for node in nodes:
|
|
|
|
await node.closeWait()
|
2020-02-26 22:15:14 +00:00
|
|
|
|
2020-03-20 15:38:46 +00:00
|
|
|
asyncTest "FindNode with test table":
|
|
|
|
|
|
|
|
let mainNode = initDiscoveryNode(newPrivateKey(), localAddress(20301), @[])
|
|
|
|
|
|
|
|
# Generate 1000 random nodes and add to our main node's routing table
|
|
|
|
for i in 0..<1000:
|
2020-03-27 13:37:31 +00:00
|
|
|
mainNode.addNode(generateNode(port = 20302 + i))
|
2020-03-20 15:38:46 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
neighbours = mainNode.neighbours(mainNode.localNode.id)
|
|
|
|
closest = neighbours[0]
|
|
|
|
closestDistance = logDist(closest.id, mainNode.localNode.id)
|
|
|
|
|
|
|
|
debug "Closest neighbour", closestDistance, id=closest.id.toHex()
|
|
|
|
|
|
|
|
let
|
|
|
|
testNode = initDiscoveryNode(newPrivateKey(), localAddress(20302),
|
|
|
|
@[mainNode.localNode.record])
|
|
|
|
discovered = await discv5_protocol.findNode(testNode, mainNode.localNode,
|
|
|
|
closestDistance)
|
|
|
|
|
|
|
|
check closest in discovered
|
|
|
|
|
|
|
|
await mainNode.closeWait()
|
|
|
|
await testNode.closeWait()
|
|
|
|
|
|
|
|
asyncTest "GetNode":
|
|
|
|
# TODO: This could be tested in just a routing table only context
|
|
|
|
let
|
|
|
|
node = initDiscoveryNode(newPrivateKey(), localAddress(20302), @[])
|
2020-03-27 13:37:31 +00:00
|
|
|
targetNode = generateNode(port = 20303)
|
2020-03-20 15:38:46 +00:00
|
|
|
|
|
|
|
node.addNode(targetNode)
|
|
|
|
|
|
|
|
for i in 0..<1000:
|
2020-03-27 13:37:31 +00:00
|
|
|
node.addNode(generateNode(port = 20303 + i))
|
2020-03-20 15:38:46 +00:00
|
|
|
|
|
|
|
check node.getNode(targetNode.id) == targetNode
|
|
|
|
|
|
|
|
await node.closeWait()
|
|
|
|
|
2020-03-24 09:51:34 +00:00
|
|
|
asyncTest "Node deletion":
|
|
|
|
let
|
|
|
|
bootnode = initDiscoveryNode(newPrivateKey(), localAddress(20301), @[])
|
|
|
|
node1 = initDiscoveryNode(newPrivateKey(), localAddress(20302),
|
|
|
|
@[bootnode.localNode.record])
|
|
|
|
node2 = initDiscoveryNode(newPrivateKey(), localAddress(20303),
|
|
|
|
@[bootnode.localNode.record])
|
|
|
|
pong1 = await discv5_protocol.ping(node1, bootnode.localNode)
|
|
|
|
pong2 = await discv5_protocol.ping(node1, node2.localNode)
|
|
|
|
|
|
|
|
check pong1.isSome() and pong2.isSome()
|
|
|
|
|
|
|
|
await bootnode.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
|
|
|
await node1.revalidateNode(bootnode.localNode)
|
|
|
|
await node1.revalidateNode(node2.localNode)
|
|
|
|
|
|
|
|
check node1.getNode(bootnode.localNode.id) == bootnode.localNode
|
|
|
|
check node1.getNode(node2.localNode.id) == nil
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
|
|
|
|
|
2020-02-27 21:36:42 +00:00
|
|
|
asyncTest "Handshake cleanup":
|
2020-02-26 22:15:14 +00:00
|
|
|
let node = initDiscoveryNode(newPrivateKey(), localAddress(20302), @[])
|
2020-02-27 21:36:42 +00:00
|
|
|
var tag: PacketTag
|
2020-02-26 22:15:14 +00:00
|
|
|
let a = localAddress(20303)
|
2020-02-27 21:36:42 +00:00
|
|
|
|
2020-02-26 22:15:14 +00:00
|
|
|
for i in 0 ..< 5:
|
2020-02-27 21:36:42 +00:00
|
|
|
randomBytes(tag)
|
|
|
|
node.receive(a, randomPacket(tag))
|
2020-02-26 22:15:14 +00:00
|
|
|
|
2020-02-27 21:36:42 +00:00
|
|
|
# Checking different nodeIds but same address
|
2020-02-26 22:15:14 +00:00
|
|
|
check node.codec.handshakes.len == 5
|
2020-02-27 21:36:42 +00:00
|
|
|
# TODO: Could get rid of the sleep by storing the timeout future of the
|
|
|
|
# handshake
|
2020-02-26 22:15:14 +00:00
|
|
|
await sleepAsync(handshakeTimeout)
|
|
|
|
# Checking handshake cleanup
|
|
|
|
check node.codec.handshakes.len == 0
|
|
|
|
|
2020-02-27 21:36:42 +00:00
|
|
|
await node.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Handshake different address":
|
|
|
|
let node = initDiscoveryNode(newPrivateKey(), localAddress(20302), @[])
|
|
|
|
var tag: PacketTag
|
|
|
|
|
2020-02-26 22:15:14 +00:00
|
|
|
for i in 0 ..< 5:
|
2020-02-27 21:36:42 +00:00
|
|
|
let a = localAddress(20303 + i)
|
|
|
|
node.receive(a, randomPacket(tag))
|
|
|
|
|
|
|
|
check node.codec.handshakes.len == 5
|
|
|
|
|
|
|
|
await node.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Handshake duplicates":
|
|
|
|
let node = initDiscoveryNode(newPrivateKey(), localAddress(20302), @[])
|
|
|
|
var tag: PacketTag
|
|
|
|
let a = localAddress(20303)
|
|
|
|
|
|
|
|
for i in 0 ..< 5:
|
|
|
|
node.receive(a, randomPacket(tag))
|
2020-02-26 22:15:14 +00:00
|
|
|
|
|
|
|
# Checking handshake duplicates
|
|
|
|
check node.codec.handshakes.len == 1
|
|
|
|
|
2020-02-27 21:36:42 +00:00
|
|
|
# TODO: add check that gets the Whoareyou value and checks if its authTag
|
|
|
|
# is that of the first packet.
|
|
|
|
|
2020-02-26 22:15:14 +00:00
|
|
|
await node.closeWait()
|
2020-03-16 22:56:00 +00:00
|
|
|
|
|
|
|
test "Distance check":
|
|
|
|
const
|
|
|
|
targetId = "0x0000"
|
|
|
|
testValues = [
|
|
|
|
("0x0001", 1'u32),
|
|
|
|
("0x0002", 2'u32),
|
|
|
|
("0x0003", 2'u32),
|
|
|
|
("0x0004", 3'u32),
|
|
|
|
("0x0008", 4'u32),
|
|
|
|
("0x00ff", 8'u32),
|
|
|
|
("0x0100", 9'u32),
|
|
|
|
("0xf000", 16'u32)
|
|
|
|
]
|
|
|
|
|
|
|
|
for (id, d) in testValues:
|
|
|
|
check logDist(parse(targetId, UInt256, 16), parse(id, UInt256, 16)) == d
|
|
|
|
|
|
|
|
test "Distance check with keys":
|
|
|
|
const
|
|
|
|
targetKey = "5d485bdcbe9bc89314a10ae9231e429d33853e3a8fa2af39f5f827370a2e4185e344ace5d16237491dad41f278f1d3785210d29ace76cd627b9147ee340b1125"
|
|
|
|
testValues = [
|
|
|
|
("29738ba0c1a4397d6a65f292eee07f02df8e58d41594ba2be3cf84ce0fc58169", 251'u32),
|
|
|
|
("1c9b1cafbec00848d2c174b858219914b42a7d5c9359b1ca03fd650e8239ae94", 252'u32),
|
|
|
|
("2d0511ae9bf590166597eeab86b6f27b1ab761761eaea8965487b162f8703847", 253'u32),
|
|
|
|
("dec742079ec00ff4ec1284d7905bc3de2366f67a0769431fd16f80fd68c58a7c", 254'u32),
|
|
|
|
("da8645f90826e57228d9ea72aff84500060ad111a5d62e4af831ed8e4b5acfb8", 255'u32),
|
|
|
|
("8c5b422155d33ea8e9d46f71d1ad3e7b24cb40051413ffa1a81cff613d243ba9", 256'u32)
|
|
|
|
]
|
|
|
|
|
|
|
|
let targetId = toNodeId(initPublicKey(targetKey))
|
|
|
|
|
|
|
|
for (key, d) in testValues:
|
|
|
|
let id = toNodeId(initPrivateKey(key).getPublicKey())
|
|
|
|
check logDist(targetId, id) == d
|