From 7ab84641d69dde477b3e0a021dec53605846bd33 Mon Sep 17 00:00:00 2001 From: kdeme Date: Wed, 24 Apr 2019 22:45:29 +0200 Subject: [PATCH] Fix, improve and activate rpc test --- nimbus.nimble | 1 + tests/test_helpers.nim | 21 ++++++++++++++++--- tests/test_rpc.nim | 32 ++++++++-------------------- tests/test_rpc_whisper.nim | 43 ++++++++++++++++++++------------------ 4 files changed, 51 insertions(+), 46 deletions(-) diff --git a/nimbus.nimble b/nimbus.nimble index 589916bb1..eeaa4824e 100644 --- a/nimbus.nimble +++ b/nimbus.nimble @@ -34,6 +34,7 @@ proc test(name: string, lang = "c") = task test, "Run tests": test "all_tests" test "test_rpc" + test "test_rpc_whisper" task nimbus, "Build Nimbus": buildBinary "nimbus", "nimbus/", "-d:chronicles_log_level=TRACE" diff --git a/tests/test_helpers.nim b/tests/test_helpers.nim index bee13201c..4efd6101f 100644 --- a/tests/test_helpers.nim +++ b/tests/test_helpers.nim @@ -7,10 +7,9 @@ import os, macros, json, strformat, strutils, parseutils, ospaths, tables, - byteutils, eth/[common, keys, rlp], ranges/typedranges, - ../nimbus/[vm_state, constants], + byteutils, ranges/typedranges, net, eth/[common, keys, rlp, p2p], + ../nimbus/[vm_state, constants, config, transaction, utils], ../nimbus/db/[db_chain, state_db], - ../nimbus/[transaction, utils], ../nimbus/vm/interpreter/[gas_costs, vm_forks], ../tests/test_generalstate_failing @@ -293,3 +292,19 @@ proc getFixtureTransaction*(j: JsonNode, dataIndex, gasIndex, valueIndex: int): proc hashLogEntries*(logs: seq[Log]): string = toLowerAscii("0x" & $keccakHash(rlp.encode(logs))) + +proc setupEthNode*(capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode = + var + conf = getConfiguration() + keypair: KeyPair + keypair.seckey = conf.net.nodekey + keypair.pubkey = conf.net.nodekey.getPublicKey() + + var srvAddress: Address + srvAddress.ip = parseIpAddress("0.0.0.0") + srvAddress.tcpPort = Port(conf.net.bindPort) + srvAddress.udpPort = Port(conf.net.discPort) + result = newEthereumNode(keypair, srvAddress, conf.net.networkId, + nil, "nimbus 0.1.0", addAllCapabilities = false) + for capability in capabilities: + result.addCapability capability \ No newline at end of file diff --git a/tests/test_rpc.nim b/tests/test_rpc.nim index 87c5b79b1..47fccca41 100644 --- a/tests/test_rpc.nim +++ b/tests/test_rpc.nim @@ -6,16 +6,16 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - unittest, json, strformat, options, - nimcrypto, eth/[rlp, keys], eth/trie/db, eth/p2p as eth_p2p, - json_rpc/[rpcserver, rpcclient], + unittest, json, strformat, options, nimcrypto, byteutils, + json_rpc/[rpcserver, rpcclient], eth/common as eth_common, + eth/[rlp, keys], eth/trie/db, eth/p2p/rlpx_protocols/eth_protocol, ../nimbus/rpc/[common, p2p, hexstrings, rpc_types], - ../nimbus/constants, - ../nimbus/[vm_state, config], - ../nimbus/db/[state_db, db_chain, storage_types], eth/common as eth_common, byteutils, + ../nimbus/[constants, vm_state, config, genesis], + ../nimbus/db/[state_db, db_chain, storage_types], ../nimbus/p2p/chain, - ../nimbus/genesis, - ./rpcclient/test_hexstrings + ./rpcclient/test_hexstrings, ./test_helpers + +from eth/p2p/rlpx_protocols/whisper_protocol import SymKey # Perform checks for hex string validation doHexStrTests() @@ -29,26 +29,12 @@ template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0] const sigPath = &"{sourceDir}{DirSep}rpcclient{DirSep}ethcallsigs.nim" createRpcSigs(RpcSocketClient, sigPath) -proc setupEthNode: EthereumNode = - var - conf = getConfiguration() - keypair: KeyPair - keypair.seckey = conf.net.nodekey - keypair.pubkey = conf.net.nodekey.getPublicKey() - - var srvAddress: Address - srvAddress.ip = parseIpAddress("0.0.0.0") - srvAddress.tcpPort = Port(conf.net.bindPort) - srvAddress.udpPort = Port(conf.net.discPort) - result = newEthereumNode(keypair, srvAddress, conf.net.networkId, - nil, "nimbus 0.1.0") - proc toEthAddressStr(address: EthAddress): EthAddressStr = result = ("0x" & address.toHex).ethAddressStr proc doTests = # TODO: Include other transports such as Http - var ethNode = setupEthNode() + var ethNode = setupEthNode(eth) let emptyRlpHash = keccak256.digest(rlp.encode("")) header = BlockHeader(stateRoot: emptyRlpHash) diff --git a/tests/test_rpc_whisper.nim b/tests/test_rpc_whisper.nim index c5e22d0a0..01e18f0f0 100644 --- a/tests/test_rpc_whisper.nim +++ b/tests/test_rpc_whisper.nim @@ -1,8 +1,7 @@ import unittest, strformat, options, byteutils, json_rpc/[rpcserver, rpcclient], - eth/common as eth_common, eth/p2p as eth_p2p, - eth/[rlp, keys], eth/p2p/rlpx_protocols/whisper_protocol, - ../nimbus/rpc/[common, hexstrings, rpc_types, whisper], ../nimbus/config + eth/common as eth_common, eth/[rlp, keys], eth/p2p/rlpx_protocols/whisper_protocol, + ../nimbus/rpc/[common, hexstrings, rpc_types, whisper], ./test_helpers from os import DirSep from strutils import rsplit @@ -13,23 +12,8 @@ template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0] const sigPath = &"{sourceDir}{DirSep}rpcclient{DirSep}ethcallsigs.nim" createRpcSigs(RpcSocketClient, sigPath) -proc setupEthNode: EthereumNode = - var - conf = getConfiguration() - keypair: KeyPair - keypair.seckey = conf.net.nodekey - keypair.pubkey = conf.net.nodekey.getPublicKey() - - var srvAddress: Address - srvAddress.ip = parseIpAddress("0.0.0.0") - srvAddress.tcpPort = Port(conf.net.bindPort) - srvAddress.udpPort = Port(conf.net.discPort) - result = newEthereumNode(keypair, srvAddress, conf.net.networkId, - nil, "nimbus 0.1.0", addAllCapabilities = false) - result.addCapability Whisper - proc doTests = - var ethNode = setupEthNode() + var ethNode = setupEthNode(Whisper) # Create Ethereum RPCs let RPC_PORT = 8545 @@ -37,7 +21,6 @@ proc doTests = rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT]) client = newRpcSocketClient() let keys = newWhisperKeys() - setupCommonRPC(rpcServer) setupWhisperRPC(ethNode, keys, rpcServer) # Begin tests @@ -128,6 +111,20 @@ proc doTests = powTarget = 0.001 powTime = 1.0 + test "shh filter create and delete": + let + topic = topicStr.toTopic() + symKeyID = waitFor client.shh_newSymKey() + options = WhisperFilterOptions(symKeyID: some(symKeyID), + topics: some(@[topic])) + filterID = waitFor client.shh_newMessageFilter(options) + + check: + filterID.string.isValidIdentifier + waitFor(client.shh_deleteMessageFilter(filterID)) + expect Exception: + discard waitFor(client.shh_deleteMessageFilter(filterID)) + test "shh symKey post and filter loop": let topic = topicStr.toTopic() @@ -156,6 +153,8 @@ proc doTests = messages[0].padding.len > 0 messages[0].pow >= powTarget + waitFor(client.shh_deleteMessageFilter(filterID)) + test "shh asymKey post and filter loop": let topic = topicStr.toTopic() @@ -184,6 +183,8 @@ proc doTests = messages[0].padding.len > 0 messages[0].pow >= powTarget + waitFor(client.shh_deleteMessageFilter(filterID)) + test "shh signature in post and filter loop": let topic = topicStr.toTopic() @@ -216,6 +217,8 @@ proc doTests = messages[0].padding.len > 0 messages[0].pow >= powTarget + waitFor(client.shh_deleteMessageFilter(filterID)) + rpcServer.stop() rpcServer.close()