Split tests into standard client-server, rpc macro tests and ethtests

This commit is contained in:
coffeepots 2018-05-30 16:05:53 +01:00
parent 31d12246bc
commit 92bae923c9
4 changed files with 52 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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