2021-08-27 16:04:55 +00:00
|
|
|
# Nimbus - Portal Network
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-08-27 16:04:55 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2024-02-28 17:31:45 +00:00
|
|
|
chronos,
|
|
|
|
testutils/unittests,
|
2024-09-18 07:46:50 +00:00
|
|
|
json_rpc/rpcserver,
|
2024-02-28 17:31:45 +00:00
|
|
|
json_rpc/clients/httpclient,
|
|
|
|
stint,
|
|
|
|
eth/p2p/discoveryv5/enr,
|
|
|
|
eth/keys,
|
2021-08-27 16:04:55 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2022-01-06 08:06:05 +00:00
|
|
|
../rpc/rpc_discovery_api,
|
|
|
|
./test_helpers
|
2021-08-27 16:04:55 +00:00
|
|
|
|
|
|
|
type TestCase = ref object
|
2022-01-06 08:06:05 +00:00
|
|
|
localDiscovery: discv5_protocol.Protocol
|
2024-09-18 07:46:50 +00:00
|
|
|
server: RpcHttpServer
|
2021-08-27 16:04:55 +00:00
|
|
|
client: RpcHttpClient
|
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
proc setupTest(rng: ref HmacDrbgContext): Future[TestCase] {.async.} =
|
2022-01-06 08:06:05 +00:00
|
|
|
let
|
2021-08-27 16:04:55 +00:00
|
|
|
localSrvAddress = "127.0.0.1"
|
2024-04-25 10:22:21 +00:00
|
|
|
localSrvPort = 0 # let the OS choose a port
|
2021-08-27 16:04:55 +00:00
|
|
|
ta = initTAddress(localSrvAddress, localSrvPort)
|
2024-02-28 17:31:45 +00:00
|
|
|
localDiscoveryNode =
|
|
|
|
initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302))
|
2021-08-27 16:04:55 +00:00
|
|
|
client = newRpcHttpClient()
|
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
let rpcHttpServer = RpcHttpServer.new()
|
|
|
|
rpcHttpServer.addHttpServer(ta, maxRequestBodySize = 4 * 1_048_576)
|
2021-08-27 16:04:55 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcHttpServer.installDiscoveryApiHandlers(localDiscoveryNode)
|
2021-08-27 16:04:55 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcHttpServer.start()
|
|
|
|
await client.connect(localSrvAddress, rpcHttpServer.localAddress[0].port, false)
|
|
|
|
return
|
|
|
|
TestCase(localDiscovery: localDiscoveryNode, server: rpcHttpServer, client: client)
|
2021-08-27 16:04:55 +00:00
|
|
|
|
2022-09-10 19:00:27 +00:00
|
|
|
proc stop(testCase: TestCase) {.async.} =
|
|
|
|
await testCase.server.stop()
|
|
|
|
await testCase.server.closeWait()
|
|
|
|
await testCase.localDiscovery.closeWait()
|
2021-08-27 16:04:55 +00:00
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
procSuite "Discovery RPC":
|
2021-08-27 16:04:55 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
asyncTest "Get local node info":
|
|
|
|
let tc = await setupTest(rng)
|
2024-01-13 01:41:57 +00:00
|
|
|
let jsonBytes = await tc.client.call("discv5_nodeInfo", %[])
|
|
|
|
let resp = JrpcConv.decode(jsonBytes.string, JsonNode)
|
2021-08-27 16:04:55 +00:00
|
|
|
|
|
|
|
check:
|
2021-11-24 07:45:55 +00:00
|
|
|
resp.contains("nodeId")
|
|
|
|
resp["nodeId"].kind == JString
|
2022-12-13 18:22:36 +00:00
|
|
|
resp.contains("enr")
|
|
|
|
resp["enr"].kind == JString
|
2021-08-27 16:04:55 +00:00
|
|
|
|
2021-11-24 07:45:55 +00:00
|
|
|
let nodeId = resp["nodeId"].getStr()
|
2022-12-13 18:22:36 +00:00
|
|
|
let nodeEnr = resp["enr"].getStr()
|
2021-08-27 16:04:55 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
nodeEnr == tc.localDiscovery.localNode.record.toURI()
|
|
|
|
nodeId == "0x" & tc.localDiscovery.localNode.id.toHex()
|
|
|
|
|
|
|
|
waitFor tc.stop()
|