From ab5c763a8404fbf36c66f82690f3867f0e20d45f Mon Sep 17 00:00:00 2001 From: jangko Date: Tue, 21 Jul 2020 00:16:59 +0700 Subject: [PATCH] move rng to configuration --- nimbus/config.nim | 9 +++++++-- nimbus/random_keys.nim | 13 +++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/nimbus/config.nim b/nimbus/config.nim index 589dd8efb..1887449eb 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -11,7 +11,7 @@ import parseopt, strutils, macros, os, times, json, stew/[byteutils], chronos, eth/[keys, common, p2p, net/nat], chronicles, nimcrypto/hash, eth/p2p/bootnodes, eth/p2p/rlpx_protocols/whisper_protocol, - ./db/select_backend, ./random_keys, + ./db/select_backend, eth/keys, ./vm/interpreter/vm_forks const @@ -150,6 +150,9 @@ type debug*: DebugConfiguration ## Debug configuration shh*: WhisperConfig ## Whisper configuration customGenesis*: CustomGenesisConfig ## Custom Genesis Configuration + # You should only create one instance of the RNG per application / library + # Ref is used so that it can be shared between components + rng*: ref BrHmacDrbgContext CustomGenesisConfig = object chainId*: uint @@ -832,6 +835,8 @@ proc getDefaultDataDir*(): string = proc initConfiguration(): NimbusConfiguration = ## Allocates and initializes `NimbusConfiguration` with default values result = new NimbusConfiguration + result.rng = newRng() + ## RPC defaults result.rpc.flags = {} result.rpc.binds = @[initTAddress("127.0.0.1:8545")] @@ -847,7 +852,7 @@ proc initConfiguration(): NimbusConfiguration = result.net.ident = NimbusIdent result.net.nat = NatAny result.net.protocols = defaultProtocols - result.net.nodekey = randomPrivateKey() + result.net.nodekey = random(PrivateKey, result.rng[]) const dataDir = getDefaultDataDir() diff --git a/nimbus/random_keys.nim b/nimbus/random_keys.nim index 1da90771e..2bad1a16f 100644 --- a/nimbus/random_keys.nim +++ b/nimbus/random_keys.nim @@ -1,14 +1,7 @@ -import eth/keys as ethkeys +import eth/keys, config -# You should only create one instance of the RNG per application / library -# Ref is used so that it can be shared between components - -var theRNG {.threadvar.}: ref BrHmacDrbgContext - -proc getRng*(): ref BrHmacDrbgContext {.gcsafe.} = - if theRNG.isNil: - theRNG = newRng() - theRNG +proc getRng*(): ref BrHmacDrbgContext = + getConfiguration().rng proc randomPrivateKey*(): PrivateKey = random(PrivateKey, getRng()[])