nim-eth/tests/p2p/p2p_test_helper.nim

50 lines
1.7 KiB
Nim
Raw Normal View History

2019-04-05 08:13:22 +00:00
import
unittest, chronos, nimcrypto, strutils,
eth/[keys, p2p], eth/p2p/[discovery, enode]
2019-04-05 08:13:22 +00:00
var nextPort = 30303
proc localAddress*(port: int): Address =
2019-04-05 08:13:22 +00:00
let port = Port(port)
result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1"))
proc startDiscoveryNode*(privKey: PrivateKey, address: Address,
2019-04-05 08:13:22 +00:00
bootnodes: seq[ENode]): Future[DiscoveryProtocol] {.async.} =
result = newDiscoveryProtocol(privKey, address, bootnodes)
result.open()
await result.bootstrap()
proc setupBootNode*(): Future[ENode] {.async.} =
let
bootNodeKey = newPrivateKey()
bootNodeAddr = localAddress(30301)
bootNode = await startDiscoveryNode(bootNodeKey, bootNodeAddr, @[])
result = initENode(bootNodeKey.getPublicKey, bootNodeAddr)
proc setupTestNode*(capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
let keys1 = newKeyPair()
result = newEthereumNode(keys1, localAddress(nextPort), 1, nil,
addAllCapabilities = false)
nextPort.inc
for capability in capabilities:
result.addCapability capability
template asyncTest*(name, body: untyped) =
test name:
proc scenario {.async.} = body
waitFor scenario()
2019-10-01 10:00:20 +00:00
proc packData*(payload: openArray[byte], pk: PrivateKey): seq[byte] =
let
2019-10-01 10:00:20 +00:00
payloadSeq = @payload
signature = @(pk.signMessage(payload).getRaw())
2019-10-01 10:00:20 +00:00
msgHash = keccak256.digest(signature & payloadSeq)
result = @(msgHash.data) & signature & payloadSeq
template sourceDir*: string = currentSourcePath.rsplit(DirSep, 1)[0]
proc recvMsgMock*(msg: openArray[byte]): tuple[msgId: int, msgData: Rlp] =
var rlp = rlpFromBytes(@msg.toRange)
2019-10-22 15:00:19 +00:00
let msgId = rlp.read(int32)
return (msgId.int, rlp)