mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-06-06 22:19:30 +00:00
libp2p v2.0.0's `crypto.newRng()` returns the new `Rng` wrapper type, which can't be assigned to a `ref HmacDrbgContext` field. The two test helper modules (tests/testlib/common.nim and tests/test_helpers.nim) both fall in this pattern; rest of the codebase migrated in the prior sweep but these two test-only files were missed because they don't compile during the wakunode2 / chat2mix builds — only when the test target is built (which CI does, locally we didn't until this PR). Same fix pattern as the other newRng → HmacDrbgContext.new() migrations.
36 lines
1.3 KiB
Nim
36 lines
1.3 KiB
Nim
import std/[times, random], bearssl/rand, libp2p/crypto/crypto
|
|
|
|
## Randomization
|
|
|
|
proc randomize*() =
|
|
## Initializes the default random number generator with the given seed.
|
|
## From: https://nim-lang.org/docs/random.html#randomize,int64
|
|
let now = getTime()
|
|
randomize(now.toUnix() * 1_000_000_000 + now.nanosecond)
|
|
|
|
## RNG
|
|
# Copied from here: https://github.com/status-im/nim-libp2p/blob/d522537b19a532bc4af94fcd146f779c1f23bad0/tests/helpers.nim#L28
|
|
|
|
type Rng = object
|
|
rng: ref HmacDrbgContext
|
|
|
|
# Typically having a module variable is considered bad design. This case should
|
|
# be considered as an exception and it should be used only in the tests.
|
|
var rngVar: Rng
|
|
|
|
proc getRng(): ref 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 HmacDrbgContext =
|
|
getRng()
|