2023-12-12 19:12:56 +00:00
|
|
|
# Nimbus
|
2024-02-02 20:23:04 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-10-03 11:56:13 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or
|
|
|
|
# distributed except according to those terms.
|
|
|
|
|
|
|
|
## Testing `CoreDB` wrapper implementation
|
|
|
|
|
|
|
|
import
|
2024-02-12 19:37:00 +00:00
|
|
|
std/[algorithm, os, strformat, strutils, times],
|
2023-10-03 11:56:13 +00:00
|
|
|
chronicles,
|
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
unittest2,
|
2023-12-12 19:12:56 +00:00
|
|
|
../../nimbus/db/[core_db/persistent, ledger],
|
2023-10-25 14:03:09 +00:00
|
|
|
../../nimbus/core/chain,
|
2023-10-03 11:56:13 +00:00
|
|
|
./replay/pp,
|
2023-12-12 17:47:41 +00:00
|
|
|
./test_coredb/[coredb_test_xx, test_chainsync, test_helpers]
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
const
|
2024-02-12 19:37:00 +00:00
|
|
|
# If `true`, this compile time option set up `unittest2` for manual parsing
|
|
|
|
unittest2DisableParamFiltering {.booldefine.} = false
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
baseDir = [".", "..", ".."/"..", $DirSep]
|
|
|
|
repoDir = [".", "tests", "nimbus-eth1-blobs"]
|
2024-02-09 13:30:07 +00:00
|
|
|
subDir = ["replay", "test_coredb", "custom-network", "customgenesis"]
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
# Reference file for finding some database directory base
|
|
|
|
sampleDirRefFile = "coredb_test_xx.nim"
|
|
|
|
|
2024-02-12 19:37:00 +00:00
|
|
|
dbTypeDefault = LegacyDbMemory
|
|
|
|
ldgTypeDefault = LegacyAccountsCache
|
|
|
|
|
2024-02-09 13:30:07 +00:00
|
|
|
let
|
2023-10-03 11:56:13 +00:00
|
|
|
# Standard test sample
|
|
|
|
bChainCapture = bulkTest0
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-02-12 19:37:00 +00:00
|
|
|
when unittest2DisableParamFiltering:
|
|
|
|
# Filter out local options and pass on the rest to `unittest2`
|
|
|
|
proc cmdLineConfig(): tuple[samples: seq[CaptureSpecs]] =
|
|
|
|
|
|
|
|
# Define sample list from the command line (if any)
|
|
|
|
const optPfx = "--sample=" # Custom option with sample list
|
|
|
|
|
|
|
|
proc parseError(s = "") =
|
|
|
|
let msg = if 0 < s.len: "Unsupported \"" & optPfx & "\" list item: " & s
|
|
|
|
else: "Empty \"" & optPfx & " list"
|
|
|
|
echo "*** ", getAppFilename().splitFile.name, ": ", msg
|
|
|
|
echo " Available: ", allSamples.mapIt(it.name).sorted.join(" ")
|
|
|
|
quit(99)
|
|
|
|
|
|
|
|
var other: seq[string] # Options for manual parsing by `unittest2`
|
|
|
|
|
|
|
|
for arg in commandLineParams():
|
|
|
|
if optPfx.len <= arg.len and arg[0 ..< optPfx.len] == optPfx:
|
|
|
|
for w in arg[optPfx.len ..< arg.len].split(",").mapIt(it.strip):
|
|
|
|
block findSample:
|
|
|
|
for sample in allSamples:
|
|
|
|
if w.cmpIgnoreCase(sample.name) == 0:
|
|
|
|
result.samples.add sample
|
|
|
|
break findSample
|
|
|
|
w.parseError()
|
|
|
|
if result.samples.len == 0:
|
|
|
|
parseError()
|
|
|
|
else:
|
|
|
|
other.add arg
|
|
|
|
|
|
|
|
# Setup `unittest2`
|
|
|
|
other.parseParameters
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Kill the compilation process iff the directive `cmdLineConfig()` is used
|
|
|
|
template cmdLineConfig(): untyped =
|
|
|
|
{.error: "cmdLineConfig() needs compiler option "&
|
|
|
|
" -d:unittest2DisableParamFiltering".}
|
|
|
|
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
proc findFilePath(
|
2024-02-02 20:23:04 +00:00
|
|
|
file: string;
|
|
|
|
baseDir: openArray[string] = baseDir;
|
|
|
|
repoDir: openArray[string] = repoDir;
|
|
|
|
subDir: openArray[string] = subDir;
|
|
|
|
): Result[string,void] =
|
2024-02-12 19:37:00 +00:00
|
|
|
file.findFilePathHelper(baseDir, repoDir, subDir)
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
proc getTmpDir(sampleDir = sampleDirRefFile): string =
|
|
|
|
sampleDir.findFilePath.value.splitFile.dir
|
|
|
|
|
2024-02-12 19:37:00 +00:00
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
proc flushDbDir(s: string) =
|
|
|
|
if s != "":
|
|
|
|
let dataDir = s / "nimbus"
|
|
|
|
if (dataDir / "data").dirExists:
|
|
|
|
# Typically under Windows: there might be stale file locks.
|
|
|
|
try: dataDir.removeDir except CatchableError: discard
|
|
|
|
block dontClearUnlessEmpty:
|
|
|
|
for w in s.walkDir:
|
|
|
|
break dontClearUnlessEmpty
|
|
|
|
try: s.removeDir except CatchableError: discard
|
|
|
|
|
|
|
|
# ----------------
|
|
|
|
|
|
|
|
proc setTraceLevel {.used.} =
|
|
|
|
discard
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.TRACE)
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc setDebugLevel {.used.} =
|
|
|
|
discard
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.DEBUG)
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
proc setErrorLevel {.used.} =
|
|
|
|
discard
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.ERROR)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc initRunnerDB(
|
2023-10-03 11:56:13 +00:00
|
|
|
path: string;
|
2024-02-09 13:30:07 +00:00
|
|
|
specs: CaptureSpecs;
|
2023-12-12 19:12:56 +00:00
|
|
|
dbType: CoreDbType;
|
|
|
|
ldgType: LedgerType;
|
2023-10-03 11:56:13 +00:00
|
|
|
): CommonRef =
|
2023-12-12 19:12:56 +00:00
|
|
|
let coreDB =
|
|
|
|
# Resolve for static `dbType`
|
|
|
|
case dbType:
|
|
|
|
of LegacyDbMemory: LegacyDbMemory.newCoreDbRef()
|
|
|
|
of LegacyDbPersistent: LegacyDbPersistent.newCoreDbRef path
|
|
|
|
of AristoDbMemory: AristoDbMemory.newCoreDbRef()
|
|
|
|
of AristoDbRocks: AristoDbRocks.newCoreDbRef path
|
|
|
|
of AristoDbVoid: AristoDbVoid.newCoreDbRef()
|
|
|
|
else: raiseAssert "Oops"
|
|
|
|
|
|
|
|
when false: # or true:
|
|
|
|
setDebugLevel()
|
|
|
|
coreDB.trackLegaApi = true
|
|
|
|
coreDB.trackNewApi = true
|
|
|
|
coreDB.localDbOnly = true
|
|
|
|
|
2024-02-09 13:30:07 +00:00
|
|
|
var
|
|
|
|
params: NetworkParams
|
|
|
|
networkId: NetworkId
|
|
|
|
if specs.builtIn:
|
|
|
|
networkId = specs.network
|
|
|
|
params = networkId.networkParams()
|
|
|
|
else:
|
|
|
|
doAssert specs.genesis.findFilePath.value.loadNetworkParams(params)
|
|
|
|
networkId = params.config.chainId.NetworkId
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
result = CommonRef.new(
|
|
|
|
db = coreDB,
|
2024-02-09 13:30:07 +00:00
|
|
|
networkId = networkId,
|
|
|
|
params = params,
|
2023-12-12 19:12:56 +00:00
|
|
|
ldgType = ldgType)
|
2024-02-02 20:23:04 +00:00
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
result.initializeEmptyDb
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
setErrorLevel()
|
|
|
|
coreDB.trackLegaApi = false
|
|
|
|
coreDB.trackNewApi = false
|
|
|
|
coreDB.localDbOnly = false
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Test Runners: accounts and accounts storages
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
proc chainSyncRunner(
|
2023-10-03 11:56:13 +00:00
|
|
|
noisy = true;
|
|
|
|
capture = bChainCapture;
|
2024-02-12 19:37:00 +00:00
|
|
|
dbType = CoreDbType(0);
|
|
|
|
ldgType = ldgTypeDefault;
|
2023-12-12 19:12:56 +00:00
|
|
|
enaLogging = false;
|
|
|
|
lastOneExtra = true;
|
2023-10-03 11:56:13 +00:00
|
|
|
) =
|
2024-02-09 13:30:07 +00:00
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
## Test backend database and ledger
|
2023-10-03 11:56:13 +00:00
|
|
|
let
|
2024-02-09 13:30:07 +00:00
|
|
|
fileInfo = capture.files[0].splitFile.name.split(".")[0]
|
|
|
|
filePaths = capture.files.mapIt(it.findFilePath(baseDir,repoDir).value)
|
2023-12-12 19:12:56 +00:00
|
|
|
baseDir = getTmpDir() / capture.name & "-chain-sync"
|
|
|
|
dbDir = baseDir / "tmp"
|
2023-10-03 11:56:13 +00:00
|
|
|
numBlocks = capture.numBlocks
|
2023-12-12 17:47:41 +00:00
|
|
|
numBlocksInfo = if numBlocks == high(int): "all" else: $numBlocks
|
2024-02-12 19:37:00 +00:00
|
|
|
|
|
|
|
dbType = block:
|
|
|
|
# Decreasing priority: dbType, capture.dbType, dbTypeDefault
|
|
|
|
var effDbType = dbTypeDefault
|
|
|
|
if dbType != CoreDbType(0):
|
|
|
|
effDbType = dbType
|
|
|
|
elif capture.dbType != CoreDbType(0):
|
|
|
|
effDbType = capture.dbType
|
|
|
|
effDbType
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
persistent = dbType in CoreDbPersistentTypes
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
defer:
|
|
|
|
if persistent: baseDir.flushDbDir
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
suite &"CoreDB and LedgerRef API on {fileInfo}, {dbType}, {ldgType}":
|
2023-10-03 11:56:13 +00:00
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
test &"Ledger API {ldgType}, {numBlocksInfo} blocks":
|
2023-10-03 11:56:13 +00:00
|
|
|
let
|
2024-02-09 13:30:07 +00:00
|
|
|
com = initRunnerDB(dbDir, capture, dbType, ldgType)
|
2023-10-03 11:56:13 +00:00
|
|
|
defer:
|
|
|
|
com.db.finish(flush = true)
|
2024-02-12 19:37:00 +00:00
|
|
|
#noisy.testChainSyncProfilingPrint numBlocks
|
2023-10-03 11:56:13 +00:00
|
|
|
if persistent: dbDir.flushDbDir
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
if noisy:
|
|
|
|
com.db.trackNewApi = true
|
|
|
|
com.db.trackNewApi = true
|
|
|
|
com.db.trackLedgerApi = true
|
2024-02-02 20:23:04 +00:00
|
|
|
com.db.localDbOnly = true
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2024-02-09 13:30:07 +00:00
|
|
|
check noisy.testChainSync(filePaths, com, numBlocks,
|
2023-12-12 19:12:56 +00:00
|
|
|
lastOneExtra=lastOneExtra, enaLogging=enaLogging)
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Main function(s)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc coreDbMain*(noisy = defined(debug)) =
|
2023-12-12 19:12:56 +00:00
|
|
|
noisy.chainSyncRunner(ldgType=LedgerCache)
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
const
|
|
|
|
noisy = defined(debug) or true
|
2024-02-12 19:37:00 +00:00
|
|
|
var
|
|
|
|
sampleList: seq[CaptureSpecs]
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
setErrorLevel()
|
|
|
|
|
2023-10-12 20:10:04 +00:00
|
|
|
# This one uses the readily available dump: `bulkTest0` and some huge replay
|
|
|
|
# dumps `bulkTest2`, `bulkTest3`, .. from the `nimbus-eth1-blobs` package.
|
|
|
|
# For specs see `tests/test_coredb/bulk_test_xx.nim`.
|
2024-02-12 19:37:00 +00:00
|
|
|
|
|
|
|
sampleList = cmdLineConfig().samples
|
|
|
|
if sampleList.len == 0:
|
|
|
|
sampleList = @[bulkTest0]
|
|
|
|
when true:
|
|
|
|
sampleList = @[bulkTest2, bulkTest3]
|
|
|
|
sampleList = @[ariTest1] # debugging
|
2023-10-12 20:10:04 +00:00
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
var state: (Duration, int)
|
2024-02-12 19:37:00 +00:00
|
|
|
for n,capture in sampleList:
|
2023-12-12 17:47:41 +00:00
|
|
|
noisy.profileSection("@testList #" & $n, state):
|
2023-12-12 19:12:56 +00:00
|
|
|
noisy.chainSyncRunner(
|
|
|
|
capture=capture,
|
2024-02-12 19:37:00 +00:00
|
|
|
#dbType = ...,
|
2023-12-12 19:12:56 +00:00
|
|
|
ldgType=LedgerCache,
|
2024-02-12 19:37:00 +00:00
|
|
|
#enaLogging = true
|
2023-12-12 19:12:56 +00:00
|
|
|
)
|
2023-12-12 17:47:41 +00:00
|
|
|
|
|
|
|
noisy.say "***", "total elapsed: ", state[0].pp, " sections: ", state[1]
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|