mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-12 07:14:20 +00:00
modernize simutils
(#5901)
`simutils.nim` is quite outdated w.r.t. code style. Apply the following: - Use string concatenation instead of `strformat` for simple cases - Catch `IOError` and `SerializationError` when loading/saving SSZ files - Catch `ValueError` for remaining `strformat` usage - Consistently use `chronicles` in `loadGenesis`
This commit is contained in:
parent
30b7c6153f
commit
403568cd2b
@ -5,6 +5,8 @@
|
|||||||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
||||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||||
|
|
||||||
|
{.push raises: [].}
|
||||||
|
|
||||||
import
|
import
|
||||||
stew/io2,
|
stew/io2,
|
||||||
../tests/testblockutil, ../tests/consensus_spec/os_ops,
|
../tests/testblockutil, ../tests/consensus_spec/os_ops,
|
||||||
@ -70,40 +72,64 @@ proc loadGenesis*(validators: Natural, validate: bool):
|
|||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
let
|
let
|
||||||
|
suffix = const_preset & "_" & $validators & "_" & SPEC_VERSION
|
||||||
genesisFn = genesisDir /
|
genesisFn = genesisDir /
|
||||||
&"genesis_{const_preset}_{validators}_{SPEC_VERSION}.ssz"
|
"genesis_" & suffix & ".ssz"
|
||||||
contractSnapshotFn = genesisDir /
|
contractSnapshotFn = genesisDir /
|
||||||
&"deposit_contract_snapshot_{const_preset}_{validators}_{SPEC_VERSION}.ssz"
|
"deposit_contract_snapshot_" & suffix & ".ssz"
|
||||||
const cfg = getSimulationConfig()
|
const cfg = getSimulationConfig()
|
||||||
|
|
||||||
if fileExists(genesisFn) and fileExists(contractSnapshotFn):
|
if fileExists(genesisFn) and fileExists(contractSnapshotFn):
|
||||||
let res = newClone(readSszForkedHashedBeaconState(
|
let res =
|
||||||
cfg, readAllBytes(genesisFn).tryGet()))
|
try:
|
||||||
|
newClone(readSszForkedHashedBeaconState(
|
||||||
|
cfg, readAllBytes(genesisFn).tryGet()))
|
||||||
|
except ResultError[IoErrorCode] as exc:
|
||||||
|
fatal "Genesis file failed to load",
|
||||||
|
fileName = genesisFn, exc = exc.msg
|
||||||
|
quit 1
|
||||||
|
except SerializationError as exc:
|
||||||
|
fatal "Genesis file malformed",
|
||||||
|
fileName = genesisFn, exc = exc.msg
|
||||||
|
quit 1
|
||||||
|
|
||||||
withState(res[]):
|
withState(res[]):
|
||||||
if forkyState.data.slot != GENESIS_SLOT:
|
if forkyState.data.slot != GENESIS_SLOT:
|
||||||
echo "Can only start from genesis state"
|
fatal "Can only start from genesis state"
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
if forkyState.data.validators.len != validators:
|
if forkyState.data.validators.len != validators:
|
||||||
echo &"Supplied genesis file has {forkyState.data.validators.len} validators, while {validators} where requested, running anyway"
|
fatal "Supplied genesis file has unexpected number of validators",
|
||||||
|
numExpectedValidators = validators,
|
||||||
|
numActualValidators = forkyState.data.validators.len
|
||||||
|
quit 1
|
||||||
|
|
||||||
echo &"Loaded {genesisFn}..."
|
info "Loaded genesis file", fileName = genesisFn
|
||||||
|
|
||||||
# TODO check that the private keys are EF test keys
|
# TODO check that the private keys are EF test keys
|
||||||
|
|
||||||
let contractSnapshot = SSZ.loadFile(contractSnapshotFn,
|
let contractSnapshot =
|
||||||
DepositTreeSnapshot)
|
try:
|
||||||
|
SSZ.loadFile(contractSnapshotFn, DepositTreeSnapshot)
|
||||||
|
except IOError as exc:
|
||||||
|
fatal "Deposit contract snapshot failed to load",
|
||||||
|
fileName = contractSnapshotFn, exc = exc.msg
|
||||||
|
quit 1
|
||||||
|
except SerializationError as exc:
|
||||||
|
fatal "Deposit contract snapshot malformed",
|
||||||
|
fileName = contractSnapshotFn, exc = exc.msg
|
||||||
|
quit 1
|
||||||
(res, contractSnapshot)
|
(res, contractSnapshot)
|
||||||
else:
|
else:
|
||||||
echo "Genesis file not found, making one up (use nimbus_beacon_node createTestnet to make one)"
|
warn "Genesis file not found, making one up",
|
||||||
|
hint = "use nimbus_beacon_node createTestnet to make one"
|
||||||
|
|
||||||
echo "Preparing validators..."
|
info "Preparing validators..."
|
||||||
let
|
let
|
||||||
flags = if validate: {} else: {skipBlsValidation}
|
flags = if validate: {} else: {skipBlsValidation}
|
||||||
deposits = makeInitialDeposits(validators.uint64, flags)
|
deposits = makeInitialDeposits(validators.uint64, flags)
|
||||||
|
|
||||||
echo "Generating Genesis..."
|
info "Generating Genesis..."
|
||||||
var merkleizer = init DepositsMerkleizer
|
var merkleizer = init DepositsMerkleizer
|
||||||
for d in deposits:
|
for d in deposits:
|
||||||
merkleizer.addChunk hash_tree_root(d).data
|
merkleizer.addChunk hash_tree_root(d).data
|
||||||
@ -117,34 +143,51 @@ proc loadGenesis*(validators: Natural, validate: bool):
|
|||||||
cfg, ZERO_HASH, 0, deposits,
|
cfg, ZERO_HASH, 0, deposits,
|
||||||
default(capella.ExecutionPayloadHeader), {skipBlsValidation})))
|
default(capella.ExecutionPayloadHeader), {skipBlsValidation})))
|
||||||
|
|
||||||
echo &"Saving to {genesisFn}..."
|
info "Saving genesis file", fileName = genesisFn
|
||||||
SSZ.saveFile(genesisFn, res.capellaData.data)
|
try:
|
||||||
echo &"Saving to {contractSnapshotFn}..."
|
SSZ.saveFile(genesisFn, res.capellaData.data)
|
||||||
SSZ.saveFile(contractSnapshotFn, contractSnapshot)
|
except IOError as exc:
|
||||||
|
fatal "Genesis file failed to save",
|
||||||
|
fileName = genesisFn, exc = exc.msg
|
||||||
|
quit 1
|
||||||
|
info "Saving deposit contract snapshot", fileName = contractSnapshotFn
|
||||||
|
try:
|
||||||
|
SSZ.saveFile(contractSnapshotFn, contractSnapshot)
|
||||||
|
except IOError as exc:
|
||||||
|
fatal "Deposit contract snapshot failed to save",
|
||||||
|
fileName = contractSnapshotFn, exc = exc.msg
|
||||||
|
quit 1
|
||||||
|
|
||||||
(res, contractSnapshot)
|
(res, contractSnapshot)
|
||||||
|
|
||||||
proc printTimers*[Timers: enum](
|
proc printTimers*[Timers: enum](
|
||||||
validate: bool,
|
validate: bool,
|
||||||
timers: array[Timers, RunningStat]
|
timers: array[Timers, RunningStat]) =
|
||||||
) =
|
func fmtTime(t: float): string =
|
||||||
func fmtTime(t: float): string = &"{t * 1000 :>12.3f}, "
|
try:
|
||||||
|
&"{t * 1000 :>12.3f}, "
|
||||||
|
except ValueError as exc:
|
||||||
|
raiseAssert "formatValue failed unexpectedly: " & $exc.msg
|
||||||
|
|
||||||
echo "All time are ms"
|
try:
|
||||||
echo &"{\"Average\" :>12}, {\"StdDev\" :>12}, {\"Min\" :>12}, " &
|
echo "All time are ms"
|
||||||
&"{\"Max\" :>12}, {\"Samples\" :>12}, {\"Test\" :>12}"
|
echo &"{\"Average\" :>12}, {\"StdDev\" :>12}, {\"Min\" :>12}, " &
|
||||||
|
&"{\"Max\" :>12}, {\"Samples\" :>12}, {\"Test\" :>12}"
|
||||||
|
|
||||||
if not validate:
|
if not validate:
|
||||||
echo "Validation is turned off meaning that no BLS operations are performed"
|
echo "Validation is turned off; no BLS operations are performed"
|
||||||
|
|
||||||
for t in Timers:
|
for t in Timers:
|
||||||
echo fmtTime(timers[t].mean), fmtTime(timers[t].standardDeviationS),
|
echo fmtTime(timers[t].mean), fmtTime(timers[t].standardDeviationS),
|
||||||
fmtTime(timers[t].min), fmtTime(timers[t].max), &"{timers[t].n :>12}, ",
|
fmtTime(timers[t].min), fmtTime(timers[t].max), &"{timers[t].n :>12}, ",
|
||||||
$t
|
$t
|
||||||
|
except ValueError as exc:
|
||||||
|
raiseAssert "formatValue failed unexpectedly: " & $exc.msg
|
||||||
|
|
||||||
proc printTimers*[Timers: enum](
|
proc printTimers*[Timers: enum](
|
||||||
state: ForkedHashedBeaconState, attesters: RunningStat, validate: bool,
|
state: ForkedHashedBeaconState, attesters: RunningStat, validate: bool,
|
||||||
timers: array[Timers, RunningStat]) =
|
timers: array[Timers, RunningStat]) =
|
||||||
echo "Validators: ", getStateField(state, validators).len, ", epoch length: ", SLOTS_PER_EPOCH
|
echo "Validators: ", getStateField(state, validators).len,
|
||||||
|
", epoch length: ", SLOTS_PER_EPOCH
|
||||||
echo "Validators per attestation (mean): ", attesters.mean
|
echo "Validators per attestation (mean): ", attesters.mean
|
||||||
printTimers(validate, timers)
|
printTimers(validate, timers)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user