# Nimbus # Copyright (c) 2021 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. import std/sequtils, unittest2, testutils, confutils, chronos, eth/p2p/discoveryv5/random2, eth/keys, ../rpc/portal_rpc_client type PortalTestnetConf* = object nodeCount* {. defaultValue: 17 desc: "Number of nodes to test" name: "node-count" .}: int rpcAddress* {. desc: "Listening address of the JSON-RPC service for all nodes" defaultValue: "127.0.0.1" name: "rpc-address" }: string baseRpcPort* {. defaultValue: 7000 desc: "Port of the JSON-RPC service of the bootstrap (first) node" name: "base-rpc-port" .}: uint16 proc connectToRpcServers(config: PortalTestnetConf): Future[seq[RpcClient]] {.async.} = var clients: seq[RpcClient] for i in 0..= (min(config.nodeCount - 1, 16)) # grab a random node its `NodeInfo` and lookup that node from all nodes. let randomNodeInfo = sample(rng[], nodeInfos) for client in clients: var enr: Record try: enr = await client.discv5_lookupEnr(randomNodeInfo.nodeId) except CatchableError as e: echo e.msg check enr == randomNodeInfo.nodeENR asyncTest "Portal State - Random node lookup from each node": let clients = await connectToRpcServers(config) var nodeInfos: seq[NodeInfo] for client in clients: let nodeInfo = await client.portal_state_nodeInfo() nodeInfos.add(nodeInfo) for client in clients: try: discard await client.portal_state_addEnrs(nodeInfos.map( proc(x: NodeInfo): Record = x.nodeENR)) except CatchableError as e: # Call shouldn't fail, unless there are json rpc server/client issues echo e.msg raise e for client in clients: let routingTableInfo = await client.portal_state_routingTableInfo() var start: seq[NodeId] let nodes = foldl(routingTableInfo.buckets, a & b, start) check nodes.len >= (min(config.nodeCount - 1, 16)) # grab a random node its `NodeInfo` and lookup that node from all nodes. let randomNodeInfo = sample(rng[], nodeInfos) for client in clients: var enr: Record try: enr = await client.portal_state_lookupEnr(randomNodeInfo.nodeId) except CatchableError as e: echo e.msg # TODO: For state network this occasionally fails. It might be because the # distance function is not used in all locations, or perhaps it just # doesn't converge to the target always with this distance function. To be # further investigated. skip() # check enr == randomNodeInfo.nodeENR asyncTest "Portal History - Random node lookup from each node": let clients = await connectToRpcServers(config) var nodeInfos: seq[NodeInfo] for client in clients: let nodeInfo = await client.portal_history_nodeInfo() nodeInfos.add(nodeInfo) for client in clients: try: discard await client.portal_history_addEnrs(nodeInfos.map( proc(x: NodeInfo): Record = x.nodeENR)) except CatchableError as e: # Call shouldn't fail, unless there are json rpc server/client issues echo e.msg raise e for client in clients: let routingTableInfo = await client.portal_history_routingTableInfo() var start: seq[NodeId] let nodes = foldl(routingTableInfo.buckets, a & b, start) check nodes.len >= (min(config.nodeCount - 1, 16)) # grab a random node its `NodeInfo` and lookup that node from all nodes. let randomNodeInfo = sample(rng[], nodeInfos) for client in clients: var enr: Record try: enr = await client.portal_history_lookupEnr(randomNodeInfo.nodeId) except CatchableError as e: echo e.msg check enr == randomNodeInfo.nodeENR