move rng to configuration

This commit is contained in:
jangko 2020-07-21 00:16:59 +07:00
parent 45bbf65470
commit ab5c763a84
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 10 additions and 12 deletions

View File

@ -11,7 +11,7 @@ import
parseopt, strutils, macros, os, times, json, stew/[byteutils], parseopt, strutils, macros, os, times, json, stew/[byteutils],
chronos, eth/[keys, common, p2p, net/nat], chronicles, nimcrypto/hash, chronos, eth/[keys, common, p2p, net/nat], chronicles, nimcrypto/hash,
eth/p2p/bootnodes, eth/p2p/rlpx_protocols/whisper_protocol, eth/p2p/bootnodes, eth/p2p/rlpx_protocols/whisper_protocol,
./db/select_backend, ./random_keys, ./db/select_backend, eth/keys,
./vm/interpreter/vm_forks ./vm/interpreter/vm_forks
const const
@ -150,6 +150,9 @@ type
debug*: DebugConfiguration ## Debug configuration debug*: DebugConfiguration ## Debug configuration
shh*: WhisperConfig ## Whisper configuration shh*: WhisperConfig ## Whisper configuration
customGenesis*: CustomGenesisConfig ## Custom Genesis 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 CustomGenesisConfig = object
chainId*: uint chainId*: uint
@ -832,6 +835,8 @@ proc getDefaultDataDir*(): string =
proc initConfiguration(): NimbusConfiguration = proc initConfiguration(): NimbusConfiguration =
## Allocates and initializes `NimbusConfiguration` with default values ## Allocates and initializes `NimbusConfiguration` with default values
result = new NimbusConfiguration result = new NimbusConfiguration
result.rng = newRng()
## RPC defaults ## RPC defaults
result.rpc.flags = {} result.rpc.flags = {}
result.rpc.binds = @[initTAddress("127.0.0.1:8545")] result.rpc.binds = @[initTAddress("127.0.0.1:8545")]
@ -847,7 +852,7 @@ proc initConfiguration(): NimbusConfiguration =
result.net.ident = NimbusIdent result.net.ident = NimbusIdent
result.net.nat = NatAny result.net.nat = NatAny
result.net.protocols = defaultProtocols result.net.protocols = defaultProtocols
result.net.nodekey = randomPrivateKey() result.net.nodekey = random(PrivateKey, result.rng[])
const dataDir = getDefaultDataDir() const dataDir = getDefaultDataDir()

View File

@ -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 proc getRng*(): ref BrHmacDrbgContext =
# Ref is used so that it can be shared between components getConfiguration().rng
var theRNG {.threadvar.}: ref BrHmacDrbgContext
proc getRng*(): ref BrHmacDrbgContext {.gcsafe.} =
if theRNG.isNil:
theRNG = newRng()
theRNG
proc randomPrivateKey*(): PrivateKey = proc randomPrivateKey*(): PrivateKey =
random(PrivateKey, getRng()[]) random(PrivateKey, getRng()[])