nim-eth/tests/p2p/p2p_test_helper.nim

36 lines
1.0 KiB
Nim
Raw Normal View History

2019-04-05 08:13:22 +00:00
import
std/strutils,
2022-06-17 20:45:37 +00:00
chronos,
../../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"))
2019-04-05 08:13:22 +00:00
proc setupTestNode*(
2022-06-17 20:45:37 +00:00
rng: ref HmacDrbgContext,
capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode {.gcsafe.} =
# Don't create new RNG every time in production code!
let keys1 = KeyPair.random(rng[])
var node = newEthereumNode(
keys1, localAddress(nextPort), NetworkId(1), nil,
addAllCapabilities = false,
bindUdpPort = Port(nextPort), bindTcpPort = Port(nextPort),
rng = rng)
2019-04-05 08:13:22 +00:00
nextPort.inc
for capability in capabilities:
node.addCapability capability
node
2019-04-05 08:13:22 +00:00
template sourceDir*: string = currentSourcePath.rsplit(DirSep, 1)[0]
proc recvMsgMock*(msg: openArray[byte]): tuple[msgId: int, msgData: Rlp] =
var rlp = rlpFromBytes(msg)
2019-10-22 15:00:19 +00:00
let msgId = rlp.read(int32)
return (msgId.int, rlp)