From f0637cfbfccddeaa392e63a950cb9d0fd78de27b Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Fri, 27 Aug 2021 18:04:55 +0200 Subject: [PATCH] Add rpc for local node info (#810) * Add rpc for local node info * Group let statement --- fluffy/fluffy.nim | 3 +- fluffy/rpc/discovery_api.nim | 27 +++++++++++ fluffy/tests/all_fluffy_tests.nim | 3 +- fluffy/tests/test_content_network.nim | 3 ++ fluffy/tests/test_discovery_rpc.nim | 65 +++++++++++++++++++++++++++ fluffy/tests/test_helpers.nim | 6 +++ fluffy/tests/test_portal.nim | 6 --- 7 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 fluffy/rpc/discovery_api.nim create mode 100644 fluffy/tests/test_discovery_rpc.nim diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index ac7dff8cb..e8dc44fa3 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -13,7 +13,7 @@ import json_rpc/rpcproxy, eth/keys, eth/net/nat, eth/p2p/discoveryv5/protocol as discv5_protocol, - ./conf, ./network/state/portal_protocol, ./rpc/eth_api, ./rpc/bridge_client + ./conf, ./network/state/portal_protocol, ./rpc/[eth_api, bridge_client, discovery_api] proc initializeBridgeClient(maybeUri: Option[string]): Option[BridgeClient] = try: @@ -70,6 +70,7 @@ proc run(config: PortalConf) {.raises: [CatchableError, Defect].} = let ta = initTAddress(config.rpcAddress, config.rpcPort) var rpcHttpServerWithProxy = RpcProxy.new([ta], config.proxyUri) rpcHttpServerWithProxy.installEthApiHandlers() + rpcHttpServerWithProxy.installDiscoveryApiHandlers(d) # TODO for now we can only proxy to local node (or remote one without ssl) to make it possible # to call infura https://github.com/status-im/nim-json-rpc/pull/101 needs to get merged for http client to support https/ waitFor rpcHttpServerWithProxy.start() diff --git a/fluffy/rpc/discovery_api.nim b/fluffy/rpc/discovery_api.nim new file mode 100644 index 000000000..e42c3f4f6 --- /dev/null +++ b/fluffy/rpc/discovery_api.nim @@ -0,0 +1,27 @@ +# 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. + +{.push raises: [Defect].} + +import + json_rpc/[rpcproxy, rpcserver], stint, + eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/enr + +type + NodeInfoResponse = object + node_id: string + enr: string + +proc installDiscoveryApiHandlers*(rpcServerWithProxy: var RpcProxy, discovery: discv5_protocol.Protocol) + {.raises: [Defect, CatchableError].} = + + # https://ddht.readthedocs.io/en/latest/jsonrpc.html#discv5-nodeinfo + rpcServerWithProxy.rpc("discv5_nodeInfo") do() -> NodeInfoResponse: + let localNodeId = "0x" & discovery.localNode.id.toHex() + let localNodeEnr = discovery.localNode.record.toURI() + return NodeInfoResponse(node_id: localNodeId, enr: localNodeEnr) + diff --git a/fluffy/tests/all_fluffy_tests.nim b/fluffy/tests/all_fluffy_tests.nim index af3069052..cad531a0a 100644 --- a/fluffy/tests/all_fluffy_tests.nim +++ b/fluffy/tests/all_fluffy_tests.nim @@ -12,7 +12,8 @@ import ../../test_macro import ./test_portal_encoding, ./test_portal, - ./test_content_network + ./test_content_network, + ./test_discovery_rpc cliBuilder: import diff --git a/fluffy/tests/test_content_network.nim b/fluffy/tests/test_content_network.nim index 471ad86e3..11ed0ca19 100644 --- a/fluffy/tests/test_content_network.nim +++ b/fluffy/tests/test_content_network.nim @@ -9,6 +9,7 @@ import std/os, testutils/unittests, eth/[keys, trie/db, trie/hexary, ssz/ssz_serialization], + eth/p2p/discoveryv5/protocol as discv5_protocol, ../../nimbus/[genesis, chain_config, db/db_chain], ../network/state/portal_protocol, ../network/state/content, ./test_helpers @@ -73,3 +74,5 @@ procSuite "Content Network": let hash = hexary.keccak(foundContent.get().payload.asSeq()) check hash.data == key + await node1.closeWait() + await node2.closeWait() diff --git a/fluffy/tests/test_discovery_rpc.nim b/fluffy/tests/test_discovery_rpc.nim new file mode 100644 index 000000000..9cd171348 --- /dev/null +++ b/fluffy/tests/test_discovery_rpc.nim @@ -0,0 +1,65 @@ +# 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, + ../rpc/discovery_api, ./test_helpers + +type TestCase = ref object + localDiscovery: discv5_protocol.Protocol + server: RpcProxy + client: RpcHttpClient + +proc setupTest(rng: ref BrHmacDrbgContext): Future[TestCase] {.async.} = + let + 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() + await client.connect(localSrvAddress, Port(localSrvPort)) + return TestCase(localDiscovery: localDiscoveryNode, server: rpcHttpServerWithProxy, client: client) + +proc stop(t: TestCase) {.async.} = + await t.server.stop() + await t.server.closeWait() + await t.localDiscovery.closeWait() + +procSuite "Discovery Rpc": + let rng = newRng() + + asyncTest "Get local node info": + let tc = await setupTest(rng) + let resp = await tc.client.call("discv5_nodeInfo", %[]) + + check: + resp.contains("node_id") + resp["node_id"].kind == JString + resp.contains("enr") + resp["enr"].kind == JString + + let nodeId = resp["node_id"].getStr() + let nodeEnr = resp["enr"].getStr() + + check: + nodeEnr == tc.localDiscovery.localNode.record.toURI() + nodeId == "0x" & tc.localDiscovery.localNode.id.toHex() + + waitFor tc.stop() diff --git a/fluffy/tests/test_helpers.nim b/fluffy/tests/test_helpers.nim index df2751aa0..de47a4912 100644 --- a/fluffy/tests/test_helpers.nim +++ b/fluffy/tests/test_helpers.nim @@ -34,3 +34,9 @@ proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, rng = rng) result.open() + +proc random*(T: type UInt256, rng: var BrHmacDrbgContext): T = + var key: UInt256 + brHmacDrbgGenerate(addr rng, addr key, csize_t(sizeof(key))) + + key diff --git a/fluffy/tests/test_portal.nim b/fluffy/tests/test_portal.nim index 8f1da3449..51d374190 100644 --- a/fluffy/tests/test_portal.nim +++ b/fluffy/tests/test_portal.nim @@ -14,12 +14,6 @@ import ../network/state/portal_protocol, ./test_helpers -proc random(T: type UInt256, rng: var BrHmacDrbgContext): T = - var key: UInt256 - brHmacDrbgGenerate(addr rng, addr key, csize_t(sizeof(key))) - - key - type Default2NodeTest = ref object node1: discv5_protocol.Protocol node2: discv5_protocol.Protocol