2021-08-27 16:04:55 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
chronos, testutils/unittests, stew/shims/net,
|
|
|
|
json_rpc/[rpcproxy, rpcserver], json_rpc/clients/httpclient,
|
|
|
|
stint,eth/p2p/discoveryv5/enr, eth/keys,
|
|
|
|
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
|
2021-08-27 16:04:55 +00:00
|
|
|
server: RpcProxy
|
|
|
|
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"
|
|
|
|
localSrvPort = 8545
|
|
|
|
ta = initTAddress(localSrvAddress, localSrvPort)
|
|
|
|
localDiscoveryNode = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
fakeProxyConfig = getHttpClientConfig("http://127.0.0.1:8546")
|
|
|
|
client = newRpcHttpClient()
|
|
|
|
|
|
|
|
var rpcHttpServerWithProxy = RpcProxy.new([ta], fakeProxyConfig)
|
|
|
|
|
|
|
|
rpcHttpServerWithProxy.installDiscoveryApiHandlers(localDiscoveryNode)
|
|
|
|
|
|
|
|
await rpcHttpServerWithProxy.start()
|
2021-11-30 15:04:24 +00:00
|
|
|
await client.connect(localSrvAddress, Port(localSrvPort), false)
|
2021-08-27 16:04:55 +00:00
|
|
|
return TestCase(localDiscovery: localDiscoveryNode, server: rpcHttpServerWithProxy, client: client)
|
|
|
|
|
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)
|
|
|
|
let resp = await tc.client.call("discv5_nodeInfo", %[])
|
|
|
|
|
|
|
|
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()
|