replace all unittest usage with unittest2

- test_graphql
- test_rpc
- test_hexstrings

fixes #691
This commit is contained in:
jangko 2021-06-21 16:54:55 +07:00
parent 41c4d186b5
commit 3a826b8406
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 232 additions and 204 deletions

32
tests/asynctest.nim Normal file
View File

@ -0,0 +1,32 @@
## nim-ws
## Copyright (c) 2021 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import unittest2
export unittest2 except suite, test
template suite*(name, body) =
suite name:
template setup(setupBody) {.used.} =
setup:
let asyncproc = proc {.async.} = setupBody
waitFor asyncproc()
template teardown(teardownBody) {.used.} =
teardown:
let asyncproc = proc {.async.} = teardownBody
waitFor asyncproc()
let suiteproc = proc = body # Avoids GcUnsafe2 warnings with chronos
suiteproc()
template test*(name, body) =
test name:
let asyncproc = proc {.async.} = body
waitFor asyncproc()

View File

@ -7,7 +7,7 @@
# Separated from main tests for brevity # Separated from main tests for brevity
import unittest, ../../nimbus/rpc/hexstrings, json import unittest2, ../../nimbus/rpc/hexstrings, json
proc doHexStrTests* = proc doHexStrTests* =
suite "[RPC] Hex quantity": suite "[RPC] Hex quantity":
@ -54,7 +54,7 @@ proc doHexStrTests* =
source = "0x0123" source = "0x0123"
x = hexQuantityStr source x = hexQuantityStr source
check %x != %source check %x != %source
suite "[RPC] Hex data": suite "[RPC] Hex data":
test "Zero value": test "Zero value":
expect ValueError: expect ValueError:

View File

@ -8,8 +8,8 @@
# those terms. # those terms.
import import
std/[os, json, unittest], std/[os, json],
stew/byteutils, stew/byteutils, unittest2,
eth/[p2p, common, trie/db, rlp, trie], eth/[p2p, common, trie/db, rlp, trie],
eth/p2p/rlpx_protocols/eth_protocol, eth/p2p/rlpx_protocols/eth_protocol,
graphql, ../nimbus/graphql/ethapi, graphql/test_common, graphql, ../nimbus/graphql/ethapi, graphql/test_common,

View File

@ -6,7 +6,7 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
unittest, json, strformat, strutils, options, tables, os, asynctest, json, strformat, strutils, options, tables, os,
nimcrypto, stew/byteutils, times, nimcrypto, stew/byteutils, times,
json_rpc/[rpcserver, rpcclient], eth/common as eth_common, json_rpc/[rpcserver, rpcclient], eth/common as eth_common,
eth/[rlp, keys], eth/trie/db, eth/p2p/rlpx_protocols/eth_protocol, eth/[rlp, keys], eth/trie/db, eth/p2p/rlpx_protocols/eth_protocol,
@ -114,8 +114,7 @@ proc setupEnv(chain: BaseChainDB, signer, ks2: EthAddress, conf: NimbusConfigura
blockHash: header.hash blockHash: header.hash
) )
suite "Remote Procedure Calls":
proc doTests {.async.} =
# TODO: Include other transports such as Http # TODO: Include other transports such as Http
var var
ethNode = setupEthNode(eth) ethNode = setupEthNode(eth)
@ -154,252 +153,249 @@ proc doTests {.async.} =
# Begin tests # Begin tests
rpcServer.start() rpcServer.start()
await client.connect("localhost", Port(RPC_PORT)) waitFor client.connect("localhost", Port(RPC_PORT))
# TODO: add more tests here # TODO: add more tests here
suite "Remote Procedure Calls": test "web3_clientVersion":
test "web3_clientVersion": let res = await client.web3_clientVersion()
let res = await client.web3_clientVersion() check res == NimbusIdent
check res == NimbusIdent
test "web3_sha3": test "web3_sha3":
expect ValueError: expect ValueError:
discard await client.web3_sha3(NimbusName.HexDataStr) discard await client.web3_sha3(NimbusName.HexDataStr)
let data = "0x" & byteutils.toHex(NimbusName.toOpenArrayByte(0, NimbusName.len-1)) let data = "0x" & byteutils.toHex(NimbusName.toOpenArrayByte(0, NimbusName.len-1))
let res = await client.web3_sha3(data.hexDataStr) let res = await client.web3_sha3(data.hexDataStr)
let rawdata = nimcrypto.fromHex(data[2 .. ^1]) let rawdata = nimcrypto.fromHex(data[2 .. ^1])
let hash = "0x" & $keccak_256.digest(rawdata) let hash = "0x" & $keccak_256.digest(rawdata)
check hash == res check hash == res
test "net_version": test "net_version":
let res = await client.net_version() let res = await client.net_version()
check res == $conf.net.networkId check res == $conf.net.networkId
test "net_listening": test "net_listening":
let res = await client.net_listening() let res = await client.net_listening()
let listening = ethNode.peerPool.connectedNodes.len < conf.net.maxPeers let listening = ethNode.peerPool.connectedNodes.len < conf.net.maxPeers
check res == listening check res == listening
test "net_peerCount": test "net_peerCount":
let res = await client.net_peerCount() let res = await client.net_peerCount()
let peerCount = ethNode.peerPool.connectedNodes.len let peerCount = ethNode.peerPool.connectedNodes.len
check isValidHexQuantity res.string check isValidHexQuantity res.string
check res == encodeQuantity(peerCount.uint) check res == encodeQuantity(peerCount.uint)
test "eth_protocolVersion": test "eth_protocolVersion":
let res = await client.eth_protocolVersion() let res = await client.eth_protocolVersion()
check res == $eth_protocol.protocolVersion check res == $eth_protocol.protocolVersion
test "eth_syncing": test "eth_syncing":
let res = await client.eth_syncing() let res = await client.eth_syncing()
if res.kind == JBool: if res.kind == JBool:
let syncing = ethNode.peerPool.connectedNodes.len > 0 let syncing = ethNode.peerPool.connectedNodes.len > 0
check res.getBool() == syncing check res.getBool() == syncing
else: else:
check res.kind == JObject check res.kind == JObject
check chain.startingBlock == UInt256.fromHex(res["startingBlock"].getStr()) check chain.startingBlock == UInt256.fromHex(res["startingBlock"].getStr())
check chain.currentBlock == UInt256.fromHex(res["currentBlock"].getStr()) check chain.currentBlock == UInt256.fromHex(res["currentBlock"].getStr())
check chain.highestBlock == UInt256.fromHex(res["highestBlock"].getStr()) check chain.highestBlock == UInt256.fromHex(res["highestBlock"].getStr())
test "eth_coinbase": test "eth_coinbase":
let res = await client.eth_coinbase() let res = await client.eth_coinbase()
# currently we don't have miner # currently we don't have miner
check isValidEthAddress(res.string) check isValidEthAddress(res.string)
check res == ethAddressStr(EthAddress.default) check res == ethAddressStr(EthAddress.default)
test "eth_mining": test "eth_mining":
let res = await client.eth_mining() let res = await client.eth_mining()
# currently we don't have miner # currently we don't have miner
check res == false check res == false
test "eth_hashrate": test "eth_hashrate":
let res = await client.eth_hashrate() let res = await client.eth_hashrate()
# currently we don't have miner # currently we don't have miner
check res == encodeQuantity(0.uint) check res == encodeQuantity(0.uint)
test "eth_gasPrice": test "eth_gasPrice":
let res = await client.eth_gasPrice() let res = await client.eth_gasPrice()
check res.string == "0x47E" check res.string == "0x47E"
test "eth_accounts": test "eth_accounts":
let res = await client.eth_accounts() let res = await client.eth_accounts()
check signer.ethAddressStr in res check signer.ethAddressStr in res
check ks2.ethAddressStr in res check ks2.ethAddressStr in res
check ks3.ethAddressStr in res check ks3.ethAddressStr in res
test "eth_blockNumber": test "eth_blockNumber":
let res = await client.eth_blockNumber() let res = await client.eth_blockNumber()
check res.string == "0x1" check res.string == "0x1"
test "eth_getBalance": test "eth_getBalance":
let a = await client.eth_getBalance(ethAddressStr("0xfff33a3bd36abdbd412707b8e310d6011454a7ae"), "0x0") let a = await client.eth_getBalance(ethAddressStr("0xfff33a3bd36abdbd412707b8e310d6011454a7ae"), "0x0")
check a.string == "0x1b1ae4d6e2ef5000000" check a.string == "0x1b1ae4d6e2ef5000000"
let b = await client.eth_getBalance(ethAddressStr("0xfff4bad596633479a2a29f9a8b3f78eefd07e6ee"), "0x0") let b = await client.eth_getBalance(ethAddressStr("0xfff4bad596633479a2a29f9a8b3f78eefd07e6ee"), "0x0")
check b.string == "0x56bc75e2d63100000" check b.string == "0x56bc75e2d63100000"
let c = await client.eth_getBalance(ethAddressStr("0xfff7ac99c8e4feb60c9750054bdc14ce1857f181"), "0x0") let c = await client.eth_getBalance(ethAddressStr("0xfff7ac99c8e4feb60c9750054bdc14ce1857f181"), "0x0")
check c.string == "0x3635c9adc5dea00000" check c.string == "0x3635c9adc5dea00000"
test "eth_getStorageAt": test "eth_getStorageAt":
let res = await client.eth_getStorageAt(ethAddressStr("0xfff33a3bd36abdbd412707b8e310d6011454a7ae"), hexQuantityStr "0x0", "0x0") let res = await client.eth_getStorageAt(ethAddressStr("0xfff33a3bd36abdbd412707b8e310d6011454a7ae"), hexQuantityStr "0x0", "0x0")
check hexDataStr(0.u256).string == hexDataStr(res).string check hexDataStr(0.u256).string == hexDataStr(res).string
test "eth_getTransactionCount": test "eth_getTransactionCount":
let res = await client.eth_getTransactionCount(ethAddressStr("0xfff7ac99c8e4feb60c9750054bdc14ce1857f181"), "0x0") let res = await client.eth_getTransactionCount(ethAddressStr("0xfff7ac99c8e4feb60c9750054bdc14ce1857f181"), "0x0")
check res.string == "0x0" check res.string == "0x0"
test "eth_getBlockTransactionCountByHash": test "eth_getBlockTransactionCountByHash":
let hash = chain.getBlockHash(0.toBlockNumber) let hash = chain.getBlockHash(0.toBlockNumber)
let res = await client.eth_getBlockTransactionCountByHash(hash) let res = await client.eth_getBlockTransactionCountByHash(hash)
check res.string == "0x0" check res.string == "0x0"
test "eth_getBlockTransactionCountByNumber": test "eth_getBlockTransactionCountByNumber":
let res = await client.eth_getBlockTransactionCountByNumber("0x0") let res = await client.eth_getBlockTransactionCountByNumber("0x0")
check res.string == "0x0" check res.string == "0x0"
test "eth_getUncleCountByBlockHash": test "eth_getUncleCountByBlockHash":
let hash = chain.getBlockHash(0.toBlockNumber) let hash = chain.getBlockHash(0.toBlockNumber)
let res = await client.eth_getUncleCountByBlockHash(hash) let res = await client.eth_getUncleCountByBlockHash(hash)
check res.string == "0x0" check res.string == "0x0"
test "eth_getUncleCountByBlockNumber": test "eth_getUncleCountByBlockNumber":
let res = await client.eth_getUncleCountByBlockNumber("0x0") let res = await client.eth_getUncleCountByBlockNumber("0x0")
check res.string == "0x0" check res.string == "0x0"
test "eth_getCode": test "eth_getCode":
let res = await client.eth_getCode(ethAddressStr("0xfff7ac99c8e4feb60c9750054bdc14ce1857f181"), "0x0") let res = await client.eth_getCode(ethAddressStr("0xfff7ac99c8e4feb60c9750054bdc14ce1857f181"), "0x0")
check res.string == "0x" check res.string == "0x"
test "eth_sign": test "eth_sign":
let msg = "hello world" let msg = "hello world"
let msgHex = hexDataStr(msg.toOpenArrayByte(0, msg.len-1)) let msgHex = hexDataStr(msg.toOpenArrayByte(0, msg.len-1))
expect ValueError: expect ValueError:
discard await client.eth_sign(ethAddressStr(ks2), msgHex) discard await client.eth_sign(ethAddressStr(ks2), msgHex)
let res = await client.eth_sign(ethAddressStr(signer), msgHex) let res = await client.eth_sign(ethAddressStr(signer), msgHex)
let sig = Signature.fromHex(res.string).tryGet() let sig = Signature.fromHex(res.string).tryGet()
# now let us try to verify signature # now let us try to verify signature
let msgData = "\x19Ethereum Signed Message:\n" & $msg.len & msg let msgData = "\x19Ethereum Signed Message:\n" & $msg.len & msg
let msgDataHex = hexDataStr(msgData.toOpenArrayByte(0, msgData.len-1)) let msgDataHex = hexDataStr(msgData.toOpenArrayByte(0, msgData.len-1))
let sha3Data = await client.web3_sha3(msgDataHex) let sha3Data = await client.web3_sha3(msgDataHex)
let msgHash = hexToByteArray[32](sha3Data) let msgHash = hexToByteArray[32](sha3Data)
let pubkey = recover(sig, SkMessage(msgHash)).tryGet() let pubkey = recover(sig, SkMessage(msgHash)).tryGet()
let recoveredAddr = pubkey.toCanonicalAddress() let recoveredAddr = pubkey.toCanonicalAddress()
check recoveredAddr == signer # verified check recoveredAddr == signer # verified
test "eth_signTransaction, eth_sendTransaction, eth_sendRawTransaction": test "eth_signTransaction, eth_sendTransaction, eth_sendRawTransaction":
var unsignedTx = TxSend( var unsignedTx = TxSend(
source: ethAddressStr(signer), source: ethAddressStr(signer),
to: ethAddressStr(ks2).some, to: ethAddressStr(ks2).some,
gas: encodeQuantity(100000'u).some, gas: encodeQuantity(100000'u).some,
gasPrice: none(HexQuantityStr), gasPrice: none(HexQuantityStr),
value: encodeQuantity(100'u).some, value: encodeQuantity(100'u).some,
data: HexDataStr("0x"), data: HexDataStr("0x"),
nonce: none(HexQuantityStr) nonce: none(HexQuantityStr)
) )
let signedTxHex = await client.eth_signTransaction(unsignedTx) let signedTxHex = await client.eth_signTransaction(unsignedTx)
let signedTx = rlp.decode(hexToSeqByte(signedTxHex.string), Transaction) let signedTx = rlp.decode(hexToSeqByte(signedTxHex.string), Transaction)
check signer == signedTx.getSender() # verified check signer == signedTx.getSender() # verified
let hashAhex = await client.eth_sendTransaction(unsignedTx) let hashAhex = await client.eth_sendTransaction(unsignedTx)
let hashBhex = await client.eth_sendRawTransaction(signedTxHex) let hashBhex = await client.eth_sendRawTransaction(signedTxHex)
check hashAhex.string == hashBhex.string check hashAhex.string == hashBhex.string
test "eth_call": test "eth_call":
var ec = EthCall( var ec = EthCall(
source: ethAddressStr(signer).some, source: ethAddressStr(signer).some,
to: ethAddressStr(ks2).some, to: ethAddressStr(ks2).some,
gas: encodeQuantity(100000'u).some, gas: encodeQuantity(100000'u).some,
gasPrice: none(HexQuantityStr), gasPrice: none(HexQuantityStr),
value: encodeQuantity(100'u).some value: encodeQuantity(100'u).some
) )
let res = await client.eth_call(ec, "latest") let res = await client.eth_call(ec, "latest")
check hexToByteArray[4](res.string) == hexToByteArray[4]("deadbeef") check hexToByteArray[4](res.string) == hexToByteArray[4]("deadbeef")
test "eth_estimateGas": test "eth_estimateGas":
var ec = EthCall( var ec = EthCall(
source: ethAddressStr(signer).some, source: ethAddressStr(signer).some,
to: ethAddressStr(ks3).some, to: ethAddressStr(ks3).some,
gas: encodeQuantity(42000'u).some, gas: encodeQuantity(42000'u).some,
gasPrice: encodeQuantity(100'u).some, gasPrice: encodeQuantity(100'u).some,
value: encodeQuantity(100'u).some value: encodeQuantity(100'u).some
) )
let res = await client.eth_estimateGas(ec, "latest") let res = await client.eth_estimateGas(ec, "latest")
check hexToInt(res.string, int) == 21000 check hexToInt(res.string, int) == 21000
test "eth_getBlockByHash": test "eth_getBlockByHash":
let res = await client.eth_getBlockByHash(env.blockHash, true) let res = await client.eth_getBlockByHash(env.blockHash, true)
check res.isSome check res.isSome
check res.get().hash.get() == env.blockHash check res.get().hash.get() == env.blockHash
let res2 = await client.eth_getBlockByHash(env.txHash, true) let res2 = await client.eth_getBlockByHash(env.txHash, true)
check res2.isNone check res2.isNone
test "eth_getBlockByNumber": test "eth_getBlockByNumber":
let res = await client.eth_getBlockByNumber("latest", true) let res = await client.eth_getBlockByNumber("latest", true)
check res.isSome check res.isSome
check res.get().hash.get() == env.blockHash check res.get().hash.get() == env.blockHash
let res2 = await client.eth_getBlockByNumber($1, true) let res2 = await client.eth_getBlockByNumber($1, true)
check res2.isNone check res2.isNone
test "eth_getTransactionByHash": test "eth_getTransactionByHash":
let res = await client.eth_getTransactionByHash(env.txHash) let res = await client.eth_getTransactionByHash(env.txHash)
check res.isSome check res.isSome
check res.get().blockNumber.get().string.hexToInt(int) == 1 check res.get().blockNumber.get().string.hexToInt(int) == 1
let res2 = await client.eth_getTransactionByHash(env.blockHash) let res2 = await client.eth_getTransactionByHash(env.blockHash)
check res2.isNone check res2.isNone
test "eth_getTransactionByBlockHashAndIndex": test "eth_getTransactionByBlockHashAndIndex":
let res = await client.eth_getTransactionByBlockHashAndIndex(env.blockHash, encodeQuantity(0)) let res = await client.eth_getTransactionByBlockHashAndIndex(env.blockHash, encodeQuantity(0))
check res.isSome check res.isSome
check res.get().blockNumber.get().string.hexToInt(int) == 1 check res.get().blockNumber.get().string.hexToInt(int) == 1
let res2 = await client.eth_getTransactionByBlockHashAndIndex(env.blockHash, encodeQuantity(3)) let res2 = await client.eth_getTransactionByBlockHashAndIndex(env.blockHash, encodeQuantity(3))
check res2.isNone check res2.isNone
let res3 = await client.eth_getTransactionByBlockHashAndIndex(env.txHash, encodeQuantity(3)) let res3 = await client.eth_getTransactionByBlockHashAndIndex(env.txHash, encodeQuantity(3))
check res3.isNone check res3.isNone
test "eth_getTransactionByBlockNumberAndIndex": test "eth_getTransactionByBlockNumberAndIndex":
let res = await client.eth_getTransactionByBlockNumberAndIndex("latest", encodeQuantity(1)) let res = await client.eth_getTransactionByBlockNumberAndIndex("latest", encodeQuantity(1))
check res.isSome check res.isSome
check res.get().blockNumber.get().string.hexToInt(int) == 1 check res.get().blockNumber.get().string.hexToInt(int) == 1
let res2 = await client.eth_getTransactionByBlockNumberAndIndex("latest", encodeQuantity(3)) let res2 = await client.eth_getTransactionByBlockNumberAndIndex("latest", encodeQuantity(3))
check res2.isNone check res2.isNone
test "eth_getTransactionReceipt": test "eth_getTransactionReceipt":
let res = await client.eth_getTransactionReceipt(env.txHash) let res = await client.eth_getTransactionReceipt(env.txHash)
check res.isSome check res.isSome
check res.get().blockNumber.string.hexToInt(int) == 1 check res.get().blockNumber.string.hexToInt(int) == 1
let res2 = await client.eth_getTransactionReceipt(env.blockHash) let res2 = await client.eth_getTransactionReceipt(env.blockHash)
check res2.isNone check res2.isNone
test "eth_getUncleByBlockHashAndIndex": test "eth_getUncleByBlockHashAndIndex":
let res = await client.eth_getUncleByBlockHashAndIndex(env.blockHash, encodeQuantity(0)) let res = await client.eth_getUncleByBlockHashAndIndex(env.blockHash, encodeQuantity(0))
check res.isSome check res.isSome
check res.get().number.get().string.hexToInt(int) == 1 check res.get().number.get().string.hexToInt(int) == 1
let res2 = await client.eth_getUncleByBlockHashAndIndex(env.blockHash, encodeQuantity(1)) let res2 = await client.eth_getUncleByBlockHashAndIndex(env.blockHash, encodeQuantity(1))
check res2.isNone check res2.isNone
let res3 = await client.eth_getUncleByBlockHashAndIndex(env.txHash, encodeQuantity(0)) let res3 = await client.eth_getUncleByBlockHashAndIndex(env.txHash, encodeQuantity(0))
check res3.isNone check res3.isNone
test "eth_getUncleByBlockNumberAndIndex": test "eth_getUncleByBlockNumberAndIndex":
let res = await client.eth_getUncleByBlockNumberAndIndex("latest", encodeQuantity(0)) let res = await client.eth_getUncleByBlockNumberAndIndex("latest", encodeQuantity(0))
check res.isSome check res.isSome
check res.get().number.get().string.hexToInt(int) == 1 check res.get().number.get().string.hexToInt(int) == 1
let res2 = await client.eth_getUncleByBlockNumberAndIndex("latest", encodeQuantity(1)) let res2 = await client.eth_getUncleByBlockNumberAndIndex("latest", encodeQuantity(1))
check res2.isNone check res2.isNone
rpcServer.stop() rpcServer.stop()
rpcServer.close() rpcServer.close()
waitFor doTests()