logos-delivery/tests/test_helpers.nim
Prem Chaitanya Prathi ba39ee4a37
fix(tests): libp2p v2.0.0 API migrations across test suite
Three small classes of fix across 12 test files (uncovered by a local
`nim c` sweep of tests/all_tests_common.nim + tests/all_tests_waku.nim):

1. `rng` no longer auto-calls when used as an argument: libp2p v2.0.0
   exports its own `rng` symbol (SwitchBuilder field), so Nim's resolver
   sees the testlib `common.rng` template ambiguously and stops auto-
   calling it. Add explicit `()`:
   - tests/test_helpers.nim
   - tests/waku_filter_v2/waku_filter_utils.nim
   - tests/waku_lightpush/lightpush_utils.nim
   - tests/waku_lightpush_legacy/lightpush_utils.nim
   - tests/node/test_wakunode_filter.nim
   - tests/waku_store/store_utils.nim

2. `PeerId.random()` zero-arg form removed in v2.0.0; new signature
   takes `rng: Rng`. Add `newRng()` + `libp2p/crypto/crypto` import:
   - tests/common/test_requestratelimiter.nim
   - tests/common/test_ratelimit_setting.nim

3. `some()` ambiguity vs `Opt.some` — files that don't directly
   `import std/options` get `Opt.some` template only, breaking calls
   intended for `Option[T]`. Add `std/options` to imports:
   - tests/waku_store/test_wakunode_store.nim
   - tests/waku_relay/test_wakunode_relay.nim

4. Two more API removals:
   - tests/testlib/wakunode.nim — `builders.MaxConnections` was removed;
     use `DefaultMaxConnections` from `libp2p/connmanager`.
   - tests/waku_rln_relay/utils_onchain.nim — `keys.PrivateKey.random
     (rng[])` `rng` ambiguous between testlib/common and eth/keys
     exports; qualify as `common.rng()[]`.

There are still more test-compile errors past this point — this is
incremental progress, not a complete v2.0.0 test-suite migration.
2026-06-04 18:55:26 +05:30

50 lines
1.6 KiB
Nim

import chronos, bearssl/rand, eth/[keys, p2p]
import libp2p/crypto/crypto
var nextPort = 30303
proc localAddress*(port: int): Address =
let port = Port(port)
result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1"))
proc setupTestNode*(
rng: ref HmacDrbgContext, capabilities: varargs[ProtocolInfo, `protocolInfo`]
): EthereumNode =
let
keys1 = keys.KeyPair.random(rng[])
address = localAddress(nextPort)
result = newEthereumNode(
keys1,
address,
NetworkId(1),
addAllCapabilities = false,
bindUdpPort = address.udpPort, # Assume same as external
bindTcpPort = address.tcpPort, # Assume same as external
rng = rng(),
)
nextPort.inc
for capability in capabilities:
result.addCapability capability
# Copied from here: https://github.com/status-im/nim-libp2p/blob/d522537b19a532bc4af94fcd146f779c1f23bad0/tests/helpers.nim#L28
type RngWrap = object
rng: ref rand.HmacDrbgContext
var rngVar: RngWrap
proc getRng(): ref rand.HmacDrbgContext =
# 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:
# libp2p v2.0.0: crypto.newRng() returns the new `Rng` wrapper type;
# construct an HmacDrbgContext directly so the field type stays as
# `ref HmacDrbgContext` (what bearssl-style consumers expect).
rngVar.rng = HmacDrbgContext.new()
rngVar.rng
template rng*(): ref rand.HmacDrbgContext =
getRng()