nimbus-eth2/tests/testutil.nim

110 lines
3.4 KiB
Nim
Raw Normal View History

# beacon_chain
# Copyright (c) 2018-2019 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * 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.
import
2020-10-12 20:49:05 +00:00
os, algorithm, strformat, stats, times, tables, std/monotimes, stew/endians2,
2020-03-10 04:00:19 +00:00
testutils/markdown_reports, chronicles,
../beacon_chain/[beacon_chain_db, extras],
../beacon_chain/ssz,
../beacon_chain/spec/[digest, beaconstate, datatypes, presets],
../beacon_chain/consensus_object_pools/blockchain_dag,
2020-10-12 20:49:05 +00:00
eth/db/[kvstore, kvstore_sqlite3],
testblockutil
export beacon_chain_db
2019-12-05 10:27:00 +00:00
type
TestDuration = tuple[duration: float, label: string]
func preset*(): string =
" [Preset: " & const_preset & ']'
2019-12-05 10:27:00 +00:00
# For state_sim
template withTimer*(duration: var float, body: untyped) =
let start = getMonoTime()
block:
body
duration = (getMonoTime() - start).inMicroseconds.float / 1000000.0
# For state_sim
template withTimerRet*(stats: var RunningStat, body: untyped): untyped =
let start = getMonoTime()
let tmp = block:
body
let stop = getMonoTime()
stats.push (stop - start).inMicroseconds.float / 1000000.0
tmp
2019-12-05 10:27:00 +00:00
var testTimes: seq[TestDuration]
2020-03-10 04:00:19 +00:00
var status = initOrderedTable[string, OrderedTable[string, Status]]()
var last: string
2019-12-05 10:27:00 +00:00
2020-03-10 04:00:19 +00:00
proc summarizeLongTests*(name: string) =
2019-12-05 10:27:00 +00:00
# TODO clean-up and make machine-readable/storable the output
# TODO this is too hard-coded and mostly a demo for using the
# timedTest wrapper template for unittest
sort(testTimes, system.cmp, SortOrder.Descending)
echo ""
echo "10 longest individual test durations"
echo "------------------------------------"
for i, item in testTimes:
echo &"{item.duration:6.2f}s for {item.label}"
if i >= 10:
break
2020-03-10 04:00:19 +00:00
status.sort do (a: (string, OrderedTable[string, Status]),
b: (string, OrderedTable[string, Status])) -> int: cmp(a[0], b[0])
generateReport(name & "-" & const_preset, status, width=90)
template suiteReport*(name, body) =
last = name
status[last] = initOrderedTable[string, Status]()
block: # namespacing
proc runSuite() =
suite name:
body
runSuite()
2020-03-10 04:00:19 +00:00
2019-12-05 10:27:00 +00:00
template timedTest*(name, body) =
var f: float
test name:
2020-03-10 04:00:19 +00:00
status[last][name] = Status.Fail
2019-12-05 10:27:00 +00:00
withTimer f:
body
2020-03-10 04:00:19 +00:00
status[last][name] = case testStatusIMPL
of OK: Status.OK
of FAILED: Status.Fail
of SKIPPED: Status.Skip
2019-12-05 10:27:00 +00:00
# TODO reached for a failed test; maybe defer or similar
# TODO noto thread-safe as-is
testTimes.add (f, name)
immutable validator database factoring (#2297) * initial immutable validator database factoring * remove changes from chain_dag: this abstraction properly belongs in beacon_chain_db * add merging mutable/immutable validator portions; individually test database roundtripping of immutable validators and states-sans-immutable-validators * update test summaries * use stew/assign2 instead of Nim assignment * add reading/writing of immutable validators in chaindag * remove unused import * replace chunked k/v store of immutable validators with per-row SQL table storage * use List instead of HashList * un-stub some ncli_db code so that it uses * switch HashArray to array; move BeaconStateNoImmutableValidators from datatypes to beacon_chain_db * begin only-mutable-part state storage * uncomment some assigns * work around https://github.com/nim-lang/Nim/issues/17253 * fix most of the issues/oversights; local sim runs again * fix test suite by adding missing beaconstate field to copy function * have ncli bench also store immutable validators * extract some immutable-validator-specific code from the beacon chain db module * add more rigorous database state roundtripping, with changing validator sets * adjust ncli_db to use new schema * simplify putState/getState by moving all immutable validator accounting into beacon state DB * remove redundant test case and move code to immutable-beacon-chain module * more efficient, but still brute-force, mutable+immutable validator merging * reuse BeaconState in getState * ensure HashList/HashArray caches are cleared when reusing getState buffers; add ncli_db and a unit test to verify this * HashList.clear() -> HashList.clearCache() * only copy incrementally necessary immutable validators * increase strictness of test cases and fix/work around resulting HashList cache invalidation issues * remove explanatory scaffolding * allow for storage of full (with all validators) states for backwards/forwards-compatibility * adjust DbSeq type usage * store full, with-validators, state every 64 epochs to enable reverting versions * reduce memory allocation and intermediate objects in state storage codepath * eliminate allocation/copying through intermediate BeaconStateNoImmutableValidators objects * skip benchmarking initial genesis-validator-heavy state store * always store new-style state and sometimes old-style state * document intent behind BeaconState/Validator type-punnery * more accurate failure message on SQLite in-memory database initialization failure
2021-03-15 14:11:51 +00:00
proc makeTestDB*(tailState: var BeaconState, tailBlock: SignedBeaconBlock): BeaconChainDB =
result = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
ChainDAGRef.preInit(result, tailState, tailState, tailBlock)
2019-12-05 10:27:00 +00:00
proc makeTestDB*(validators: Natural): BeaconChainDB =
let
genState = initialize_beacon_state_from_eth1(
defaultRuntimePreset,
Eth2Digest(),
0,
makeInitialDeposits(validators.uint64, flags = {skipBlsValidation}),
{skipBlsValidation})
genBlock = get_initial_beacon_block(genState[])
makeTestDB(genState[], genBlock)
2019-12-05 10:27:00 +00:00
export inMicroseconds