2024-03-15 23:08:47 +00:00
|
|
|
import chronos, bearssl/rand, eth/[keys, p2p]
|
2020-05-01 12:43:25 +00:00
|
|
|
|
2020-08-27 02:44:09 +00:00
|
|
|
import libp2p/crypto/crypto
|
|
|
|
|
2020-05-01 12:43:25 +00:00
|
|
|
var nextPort = 30303
|
|
|
|
|
|
|
|
proc localAddress*(port: int): Address =
|
|
|
|
let port = Port(port)
|
2024-03-15 23:08:47 +00:00
|
|
|
result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1"))
|
2020-05-01 12:43:25 +00:00
|
|
|
|
2020-07-13 10:08:03 +00:00
|
|
|
proc setupTestNode*(
|
2024-03-15 23:08:47 +00:00
|
|
|
rng: ref HmacDrbgContext, capabilities: varargs[ProtocolInfo, `protocolInfo`]
|
|
|
|
): EthereumNode =
|
2022-09-07 15:31:27 +00:00
|
|
|
let
|
|
|
|
keys1 = keys.KeyPair.random(rng[])
|
|
|
|
address = localAddress(nextPort)
|
2024-03-15 23:08:47 +00:00
|
|
|
result = newEthereumNode(
|
|
|
|
keys1,
|
|
|
|
address,
|
|
|
|
NetworkId(1),
|
|
|
|
addAllCapabilities = false,
|
|
|
|
bindUdpPort = address.udpPort, # Assume same as external
|
|
|
|
bindTcpPort = address.tcpPort, # Assume same as external
|
|
|
|
rng = rng,
|
|
|
|
)
|
2020-05-01 12:43:25 +00:00
|
|
|
nextPort.inc
|
|
|
|
for capability in capabilities:
|
|
|
|
result.addCapability capability
|
|
|
|
|
2020-08-27 02:44:09 +00:00
|
|
|
# Copied from here: https://github.com/status-im/nim-libp2p/blob/d522537b19a532bc4af94fcd146f779c1f23bad0/tests/helpers.nim#L28
|
|
|
|
type RngWrap = object
|
2022-09-07 15:31:27 +00:00
|
|
|
rng: ref rand.HmacDrbgContext
|
2020-08-27 02:44:09 +00:00
|
|
|
|
|
|
|
var rngVar: RngWrap
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
proc getRng(): ref rand.HmacDrbgContext =
|
2020-08-27 02:44:09 +00:00
|
|
|
# TODO if `rngVar` is a threadvar like it should be, there are random and
|
|
|
|
# spurious compile failures on mac - this is not gcsafe but for the
|
|
|
|
# purpose of the tests, it's ok as long as we only use a single thread
|
|
|
|
{.gcsafe.}:
|
|
|
|
if rngVar.rng.isNil:
|
|
|
|
rngVar.rng = crypto.newRng()
|
|
|
|
rngVar.rng
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
template rng*(): ref rand.HmacDrbgContext =
|
2020-08-27 02:44:09 +00:00
|
|
|
getRng()
|