nim-eth/tests/p2p/p2p_test_helper.nim

46 lines
1.5 KiB
Nim
Raw Normal View History

# nim-eth
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at
# https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at
# https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
2019-04-05 08:13:22 +00:00
import
std/strutils,
2022-06-17 20:45:37 +00:00
chronos,
../../eth/p2p, ../../eth/common/keys, ../../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),
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: uint, msgData: Rlp] =
var rlp = rlpFromBytes(msg)
let msgId = rlp.read(uint32)
return (msgId.uint, rlp)