2019-01-06 19:19:48 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-07-26 21:48:01 +00:00
|
|
|
import
|
2019-07-07 10:12:01 +00:00
|
|
|
unittest, json, strformat, options, nimcrypto, stew/byteutils,
|
2019-04-24 20:45:29 +00:00
|
|
|
json_rpc/[rpcserver, rpcclient], eth/common as eth_common,
|
|
|
|
eth/[rlp, keys], eth/trie/db, eth/p2p/rlpx_protocols/eth_protocol,
|
2018-11-26 18:16:58 +00:00
|
|
|
../nimbus/rpc/[common, p2p, hexstrings, rpc_types],
|
2019-04-24 20:45:29 +00:00
|
|
|
../nimbus/[constants, vm_state, config, genesis],
|
2020-06-01 13:37:00 +00:00
|
|
|
../nimbus/db/[accounts_cache, db_chain, storage_types],
|
2018-11-12 18:26:09 +00:00
|
|
|
../nimbus/p2p/chain,
|
2019-04-24 20:45:29 +00:00
|
|
|
./rpcclient/test_hexstrings, ./test_helpers
|
|
|
|
|
|
|
|
from eth/p2p/rlpx_protocols/whisper_protocol import SymKey
|
2018-07-27 17:02:59 +00:00
|
|
|
|
2018-07-31 18:10:52 +00:00
|
|
|
# Perform checks for hex string validation
|
2018-07-27 17:02:59 +00:00
|
|
|
doHexStrTests()
|
2018-07-26 21:48:01 +00:00
|
|
|
|
|
|
|
from os import getCurrentDir, DirSep
|
|
|
|
from strutils import rsplit
|
|
|
|
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|
|
|
|
|
|
|
## Generate client convenience marshalling wrappers from forward declarations
|
2018-07-31 18:10:52 +00:00
|
|
|
## For testing, ethcallsigs needs to be kept in sync with ../nimbus/rpc/[common, p2p]
|
2018-07-26 21:48:01 +00:00
|
|
|
const sigPath = &"{sourceDir}{DirSep}rpcclient{DirSep}ethcallsigs.nim"
|
|
|
|
createRpcSigs(RpcSocketClient, sigPath)
|
|
|
|
|
2018-08-13 16:34:54 +00:00
|
|
|
proc toEthAddressStr(address: EthAddress): EthAddressStr =
|
|
|
|
result = ("0x" & address.toHex).ethAddressStr
|
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
proc doTests {.async.} =
|
2018-07-31 18:10:52 +00:00
|
|
|
# TODO: Include other transports such as Http
|
2019-04-24 20:45:29 +00:00
|
|
|
var ethNode = setupEthNode(eth)
|
2018-07-31 18:10:52 +00:00
|
|
|
let
|
2018-11-12 18:26:09 +00:00
|
|
|
emptyRlpHash = keccak256.digest(rlp.encode(""))
|
2018-07-31 18:10:52 +00:00
|
|
|
header = BlockHeader(stateRoot: emptyRlpHash)
|
|
|
|
var
|
2018-09-18 11:18:59 +00:00
|
|
|
chain = newBaseChainDB(newMemoryDb())
|
2019-02-14 15:20:41 +00:00
|
|
|
state = newBaseVMState(emptyRlpHash, header, chain)
|
2018-11-12 18:26:09 +00:00
|
|
|
ethNode.chain = newChain(chain)
|
2018-07-26 21:48:01 +00:00
|
|
|
|
2018-08-13 16:34:54 +00:00
|
|
|
let
|
|
|
|
balance = 100.u256
|
|
|
|
address: EthAddress = hexToByteArray[20]("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
|
2018-11-28 21:57:10 +00:00
|
|
|
conf = getConfiguration()
|
|
|
|
defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain)
|
2018-07-31 18:10:52 +00:00
|
|
|
state.mutateStateDB:
|
|
|
|
db.setBalance(address, balance)
|
2019-01-06 19:19:48 +00:00
|
|
|
doAssert(canonicalHeadHashKey().toOpenArray in state.chainDb.db)
|
2018-11-29 18:53:12 +00:00
|
|
|
|
2018-07-31 18:10:52 +00:00
|
|
|
# Create Ethereum RPCs
|
2019-01-06 19:19:48 +00:00
|
|
|
let RPC_PORT = 8545
|
2018-07-31 18:10:52 +00:00
|
|
|
var
|
2019-01-06 19:19:48 +00:00
|
|
|
rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT])
|
2018-07-31 18:10:52 +00:00
|
|
|
client = newRpcSocketClient()
|
|
|
|
setupCommonRpc(rpcServer)
|
2018-08-29 08:49:01 +00:00
|
|
|
setupEthRpc(ethNode, chain, rpcServer)
|
2018-07-31 18:10:52 +00:00
|
|
|
|
|
|
|
# Begin tests
|
|
|
|
rpcServer.start()
|
2019-05-16 20:20:17 +00:00
|
|
|
await client.connect("localhost", Port(RPC_PORT))
|
2018-07-31 18:10:52 +00:00
|
|
|
|
2019-01-06 19:19:48 +00:00
|
|
|
# TODO: add more tests here
|
2018-08-06 16:18:46 +00:00
|
|
|
suite "Remote Procedure Calls":
|
2018-11-26 18:16:58 +00:00
|
|
|
test "eth_call":
|
|
|
|
let
|
|
|
|
blockNum = state.blockheader.blockNumber
|
|
|
|
callParams = EthCall(value: some(100.u256))
|
2019-05-16 20:20:17 +00:00
|
|
|
r1 = await client.eth_call(callParams, "0x" & blockNum.toHex)
|
2019-01-06 19:19:48 +00:00
|
|
|
check r1 == "0x"
|
2018-07-31 18:10:52 +00:00
|
|
|
test "eth_getBalance":
|
2019-05-16 20:20:17 +00:00
|
|
|
let r2 = await client.eth_getBalance(ZERO_ADDRESS.toEthAddressStr, "0x0")
|
2019-01-06 19:19:48 +00:00
|
|
|
check r2 == 0
|
2018-07-31 18:10:52 +00:00
|
|
|
|
|
|
|
let blockNum = state.blockheader.blockNumber
|
2019-05-16 20:20:17 +00:00
|
|
|
let r3 = await client.eth_getBalance(address.toEthAddressStr, "0x" & blockNum.toHex)
|
2019-01-06 19:19:48 +00:00
|
|
|
check r3 == 0
|
2018-12-03 20:06:00 +00:00
|
|
|
test "eth_estimateGas":
|
|
|
|
let
|
|
|
|
call = EthCall()
|
|
|
|
blockNum = state.blockheader.blockNumber
|
2019-05-16 20:20:17 +00:00
|
|
|
r4 = await client.eth_estimateGas(call, "0x" & blockNum.toHex)
|
2019-01-06 19:19:48 +00:00
|
|
|
check r4 == 21_000
|
2018-07-31 18:10:52 +00:00
|
|
|
|
|
|
|
rpcServer.stop()
|
|
|
|
rpcServer.close()
|
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
waitFor doTests()
|