2018-07-26 21:48:01 +00:00
|
|
|
import
|
2018-07-31 18:10:52 +00:00
|
|
|
unittest, json, strformat, nimcrypto, rlp,
|
|
|
|
json_rpc/[rpcserver, rpcclient],
|
2018-08-13 16:34:54 +00:00
|
|
|
../nimbus/rpc/[common, p2p, hexstrings],
|
2018-07-31 18:10:52 +00:00
|
|
|
../nimbus/constants,
|
|
|
|
../nimbus/nimbus/[account, vm_state, config],
|
|
|
|
../nimbus/db/[state_db, db_chain], eth_common, byteutils,
|
|
|
|
eth_trie/[memDb, types],
|
|
|
|
eth_p2p, eth_keys
|
2018-07-27 17:02:59 +00:00
|
|
|
import rpcclient/test_hexstrings
|
|
|
|
|
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-07-31 18:10:52 +00:00
|
|
|
proc setupEthNode: EthereumNode =
|
|
|
|
var
|
|
|
|
conf = getConfiguration()
|
|
|
|
keypair: KeyPair
|
|
|
|
keypair.seckey = conf.net.nodekey
|
|
|
|
keypair.pubkey = conf.net.nodekey.getPublicKey()
|
2018-07-26 21:48:01 +00:00
|
|
|
|
2018-07-31 18:10:52 +00:00
|
|
|
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")
|
2018-07-26 21:48:01 +00:00
|
|
|
|
2018-08-13 16:34:54 +00:00
|
|
|
proc toEthAddressStr(address: EthAddress): EthAddressStr =
|
|
|
|
result = ("0x" & address.toHex).ethAddressStr
|
|
|
|
|
2018-07-31 18:10:52 +00:00
|
|
|
proc doTests =
|
|
|
|
# TODO: Include other transports such as Http
|
|
|
|
var ethNode = setupEthNode()
|
|
|
|
let
|
|
|
|
emptyRlpHash = keccak256.digest(rlp.encode("").toOpenArray)
|
|
|
|
header = BlockHeader(stateRoot: emptyRlpHash)
|
|
|
|
var
|
|
|
|
chain = newBaseChainDB(trieDB newMemDB())
|
|
|
|
state = newBaseVMState(header, chain)
|
|
|
|
ethNode.chain = 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-07-31 18:10:52 +00:00
|
|
|
state.mutateStateDB:
|
|
|
|
db.setBalance(address, balance)
|
2018-07-26 21:48:01 +00:00
|
|
|
|
2018-07-31 18:10:52 +00:00
|
|
|
# Create Ethereum RPCs
|
|
|
|
var
|
|
|
|
rpcServer = newRpcSocketServer(["localhost:8545"])
|
|
|
|
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()
|
|
|
|
waitFor client.connect("localhost", Port(8545))
|
|
|
|
|
2018-08-06 16:18:46 +00:00
|
|
|
suite "Remote Procedure Calls":
|
2018-07-31 18:10:52 +00:00
|
|
|
test "eth_getBalance":
|
|
|
|
expect ValueError:
|
|
|
|
# check error is raised on null address
|
2018-08-13 16:34:54 +00:00
|
|
|
var r = waitFor client.eth_getBalance(ZERO_ADDRESS.toEthAddressStr, "0x0")
|
2018-07-31 18:10:52 +00:00
|
|
|
|
|
|
|
let blockNum = state.blockheader.blockNumber
|
2018-08-13 16:34:54 +00:00
|
|
|
var r = waitFor client.eth_getBalance(address.toEthAddressStr, "0x" & blockNum.toHex)
|
2018-07-31 18:10:52 +00:00
|
|
|
echo r
|
|
|
|
|
|
|
|
rpcServer.stop()
|
|
|
|
rpcServer.close()
|
|
|
|
|
2018-08-29 08:49:01 +00:00
|
|
|
doTests()
|