From b0b0b1372d4f409b1a3d6a73054ecbb1b8604975 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Wed, 30 May 2018 16:05:53 +0100 Subject: [PATCH] Split tests into standard client-server, rpc macro tests and ethtests --- tests/ethprocs.nim | 2 +- tests/stintJsonConverters.nim | 32 ++++++++++++++++++++++++++++++++ tests/testethcalls.nim | 20 ++++++++++++++++++-- tests/testrpcmacro.nim | 17 +---------------- 4 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 tests/stintJsonConverters.nim diff --git a/tests/ethprocs.nim b/tests/ethprocs.nim index f8a561c..6fa1bcb 100644 --- a/tests/ethprocs.nim +++ b/tests/ethprocs.nim @@ -1,4 +1,4 @@ -import ../rpcserver, nimcrypto, json, stint, strutils, ethtypes +import ../rpcserver, nimcrypto, json, stint, strutils, ethtypes, stintjsonconverters #[ For details on available RPC calls, see: https://github.com/ethereum/wiki/wiki/JSON-RPC diff --git a/tests/stintJsonConverters.nim b/tests/stintJsonConverters.nim new file mode 100644 index 0000000..ac9af61 --- /dev/null +++ b/tests/stintJsonConverters.nim @@ -0,0 +1,32 @@ +import json, stint +from ../rpcserver import expect + +template stintStr(n: UInt256|Int256): JsonNode = + var s = n.toHex + if s.len mod 2 != 0: s = "0" & s + s = "0x" & s + %s + +proc `%`*(n: UInt256): JsonNode = n.stintStr + +proc `%`*(n: Int256): JsonNode = n.stintStr + +# allows UInt256 to be passed as a json string +proc fromJson*(n: JsonNode, argName: string, result: var UInt256) = + # expects base 16 string, starting with "0x" + n.kind.expect(JString, argName) + let hexStr = n.getStr() + if hexStr.len > 64 + 2: # including "0x" + raise newException(ValueError, "Parameter \"" & argName & "\" value too long for UInt256: " & $hexStr.len) + result = hexStr.parse(StUint[256], 16) # TODO: Handle errors + +# allows ref UInt256 to be passed as a json string +proc fromJson*(n: JsonNode, argName: string, result: var ref UInt256) = + # expects base 16 string, starting with "0x" + n.kind.expect(JString, argName) + let hexStr = n.getStr() + if hexStr.len > 64 + 2: # including "0x" + raise newException(ValueError, "Parameter \"" & argName & "\" value too long for UInt256: " & $hexStr.len) + new result + result[] = hexStr.parse(StUint[256], 16) # TODO: Handle errors + diff --git a/tests/testethcalls.nim b/tests/testethcalls.nim index ea9e063..857762b 100644 --- a/tests/testethcalls.nim +++ b/tests/testethcalls.nim @@ -1,4 +1,4 @@ -import ../ rpcclient, ../ rpcserver +import ../ rpcclient, ../ rpcserver import unittest, asyncdispatch, json, tables from os import getCurrentDir, DirSep @@ -10,10 +10,18 @@ srv.address = "localhost" srv.port = Port(8546) # importing ethprocs creates the server rpc calls -import stint, ethtypes, ethprocs +import stint, ethtypes, ethprocs, stintJsonConverters # generate all client ethereum rpc calls createRpcSigs(sourceDir & DirSep & "ethcallsigs.nim") +srv.rpc("rpc.uint256param") do(i: UInt256): + let r = i + 1.stUint(256) + result = %r + +srv.rpc("rpc.testreturnuint256") do() -> UInt256: + let r: UInt256 = "0x1234567890abcdef".parse(UInt256, 16) + return r + asyncCheck srv.serve suite "Ethereum RPCs": @@ -21,6 +29,14 @@ suite "Ethereum RPCs": var client = newRpcClient() await client.connect("localhost", Port(8546)) + test "UInt256 param": + let r = waitFor rpcUInt256Param(%[%"0x1234567890"]) + check r == %"0x1234567891" + + test "Return UInt256": + let r = waitFor rpcTestReturnUInt256(%[]) + check r == %"0x1234567890abcdef" + test "Version": var response = waitFor client.web3_clientVersion() diff --git a/tests/testrpcmacro.nim b/tests/testrpcmacro.nim index b1e8e7f..4317fc3 100644 --- a/tests/testrpcmacro.nim +++ b/tests/testrpcmacro.nim @@ -1,4 +1,4 @@ -import unittest, ../ rpcserver, asyncdispatch, json, tables, stint +import unittest, ../ rpcserver, asyncdispatch, json, tables type # some nested types to check object parsing @@ -51,9 +51,6 @@ s.rpc("rpc.seqparam") do(a: string, s: seq[int]): s.rpc("rpc.objparam") do(a: string, obj: MyObject): result = %obj -s.rpc("rpc.uint256param") do(i: UInt256): - let r = i + 1.stUint(256) - result = %r s.rpc("rpc.returntypesimple") do(i: int) -> int: result = i @@ -65,10 +62,6 @@ s.rpc("rpc.returntypecomplex") do(i: int) -> Test2: s.rpc("rpc.testreturns") do() -> int: return 1234 -s.rpc("rpc.testreturnuint256") do() -> UInt256: - let r: UInt256 = "0x1234567890abcdef".parse(UInt256, 16) - return r - # Tests suite "Server types": @@ -108,10 +101,6 @@ suite "Server types": let r = waitfor rpcObjParam(%[%"abc", testObj]) check r == testObj - test "UInt256 param": - let r = waitFor rpcUInt256Param(%[%"0x1234567890"]) - check r == %"0x1234567891" - test "Simple return types": let inp = %99 @@ -128,10 +117,6 @@ suite "Server types": let r = waitFor rpcTestReturns(%[]) check r == %1234 - test "Return UInt256": - let r = waitFor rpcTestReturnUInt256(%[]) - check r == %"0x1234567890abcdef" - test "Runtime errors": expect ValueError: # root param not array