2020-05-01 12:43:25 +00:00
|
|
|
import
|
2020-08-27 02:44:09 +00:00
|
|
|
unittest, chronos, bearssl,
|
2020-05-01 12:43:25 +00:00
|
|
|
eth/[keys, p2p]
|
|
|
|
|
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)
|
|
|
|
result = Address(udpPort: port, tcpPort: port,
|
|
|
|
ip: parseIpAddress("127.0.0.1"))
|
|
|
|
|
2020-07-13 10:08:03 +00:00
|
|
|
proc setupTestNode*(
|
|
|
|
rng: ref BrHmacDrbgContext,
|
|
|
|
capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
|
2020-08-27 02:44:09 +00:00
|
|
|
let keys1 = keys.KeyPair.random(rng[])
|
2020-05-01 12:43:25 +00:00
|
|
|
result = newEthereumNode(keys1, localAddress(nextPort), 1, nil,
|
2020-07-13 10:08:03 +00:00
|
|
|
addAllCapabilities = false, rng = rng)
|
2020-05-01 12:43:25 +00:00
|
|
|
nextPort.inc
|
|
|
|
for capability in capabilities:
|
|
|
|
result.addCapability capability
|
|
|
|
|
|
|
|
template asyncTest*(name, body: untyped) =
|
|
|
|
test name:
|
|
|
|
proc scenario {.async.} = body
|
|
|
|
waitFor scenario()
|
|
|
|
|
|
|
|
template procSuite*(name, body: untyped) =
|
|
|
|
proc suitePayload =
|
|
|
|
suite name:
|
|
|
|
body
|
|
|
|
|
|
|
|
suitePayload()
|
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
|
|
|
|
rng: ref BrHmacDrbgContext
|
|
|
|
|
|
|
|
var rngVar: RngWrap
|
|
|
|
|
|
|
|
proc getRng(): ref BrHmacDrbgContext =
|
|
|
|
# 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
|
|
|
|
|
|
|
|
template rng*(): ref BrHmacDrbgContext =
|
|
|
|
getRng()
|