# # Ethereum P2P # (c) Copyright 2018 # Status Research & Development GmbH # # See the file "LICENSE", included in this # distribution, for details about the copyright. # {.used.} import std/sequtils, chronos, stew/byteutils, nimcrypto, testutils/unittests, ../../eth/keys, ../../eth/p2p/[discovery, kademlia, enode] proc localAddress(port: int): Address = let port = Port(port) result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1")) proc initDiscoveryNode(privKey: PrivateKey, address: Address, bootnodes: seq[ENode]): DiscoveryProtocol = let node = newDiscoveryProtocol(privKey, address, bootnodes) node.open() return node proc packData(payload: openArray[byte], pk: PrivateKey): seq[byte] = let payloadSeq = @payload signature = @(pk.sign(payload).toRaw()) msgHash = keccak256.digest(signature & payloadSeq) result = @(msgHash.data) & signature & payloadSeq proc nodeIdInNodes(id: NodeId, nodes: openarray[Node]): bool = for n in nodes: if id == n.id: return true procSuite "Discovery Tests": let bootNodeKey = PrivateKey.fromHex( "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")[] bootNodeAddr = localAddress(20301) bootENode = ENode(pubkey: bootNodeKey.toPublicKey(), address: bootNodeAddr) bootNode = initDiscoveryNode(bootNodeKey, bootNodeAddr, @[]) waitFor bootNode.bootstrap() asyncTest "Discover nodes": let nodeKeys = [ PrivateKey.fromHex( "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a618")[], PrivateKey.fromHex( "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a619")[], PrivateKey.fromHex( "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a620")[] ] var nodes: seq[DiscoveryProtocol] for i in 0..