2023-10-03 11:56:13 +00:00
|
|
|
# Nimbus - Types, data structures and shared utilities used in network sync
|
|
|
|
#
|
Core db and aristo updates for destructor and tx logic (#1894)
* Disable `TransactionID` related functions from `state_db.nim`
why:
Functions `getCommittedStorage()` and `updateOriginalRoot()` from
the `state_db` module are nowhere used. The emulation of a legacy
`TransactionID` type functionality is administratively expensive to
provide by `Aristo` (the legacy DB version is only partially
implemented, anyway).
As there is no other place where `TransactionID`s are used, they will
not be provided by the `Aristo` variant of the `CoreDb`. For the
legacy DB API, nothing will change.
* Fix copyright headers in source code
* Get rid of compiler warning
* Update Aristo code, remove unused `merge()` variant, export `hashify()`
why:
Adapt to upcoming `CoreDb` wrapper
* Remove synced tx feature from `Aristo`
why:
+ This feature allowed to synchronise transaction methods like begin,
commit, and rollback for a group of descriptors.
+ The feature is over engineered and not needed for `CoreDb`, neither
is it complete (some convergence features missing.)
* Add debugging helpers to `Kvt`
also:
Update database iterator, add count variable yield argument similar
to `Aristo`.
* Provide optional destructors for `CoreDb` API
why;
For the upcoming Aristo wrapper, this allows to control when certain
smart destruction and update can take place. The auto destructor works
fine in general when the storage/cache strategy is known and acceptable
when creating descriptors.
* Add update option for `CoreDb` API function `hash()`
why;
The hash function is typically used to get the state root of the MPT.
Due to lazy hashing, this might be not available on the `Aristo` DB.
So the `update` function asks for re-hashing the gurrent state changes
if needed.
* Update API tracking log mode: `info` => `debug
* Use shared `Kvt` descriptor in new Ledger API
why:
No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
|
|
|
# Copyright (c) 2023 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
|
|
|
|
std/[os, strformat, strutils],
|
|
|
|
chronicles,
|
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
unittest2,
|
2023-11-08 12:18:32 +00:00
|
|
|
../../nimbus/db/core_db/persistent,
|
2023-10-25 14:03:09 +00:00
|
|
|
../../nimbus/core/chain,
|
2023-10-03 11:56:13 +00:00
|
|
|
./replay/pp,
|
2023-10-25 14:03:09 +00:00
|
|
|
./test_coredb/[coredb_test_xx, test_chainsync]
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
baseDir = [".", "..", ".."/"..", $DirSep]
|
|
|
|
repoDir = [".", "tests", "nimbus-eth1-blobs"]
|
|
|
|
subDir = ["replay", "test_coredb"]
|
|
|
|
|
|
|
|
# Reference file for finding some database directory base
|
|
|
|
sampleDirRefFile = "coredb_test_xx.nim"
|
|
|
|
|
|
|
|
# Standard test sample
|
|
|
|
bChainCapture = bulkTest0
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc findFilePath(
|
|
|
|
file: string;
|
|
|
|
baseDir: openArray[string] = baseDir;
|
|
|
|
repoDir: openArray[string] = repoDir;
|
|
|
|
subDir: openArray[string] = subDir;
|
|
|
|
): Result[string,void] =
|
|
|
|
for dir in baseDir:
|
|
|
|
if dir.dirExists:
|
|
|
|
for repo in repoDir:
|
|
|
|
if (dir / repo).dirExists:
|
|
|
|
for sub in subDir:
|
|
|
|
if (dir / repo / sub).dirExists:
|
|
|
|
let path = dir / repo / sub / file
|
|
|
|
if path.fileExists:
|
|
|
|
return ok(path)
|
|
|
|
echo "*** File not found \"", file, "\"."
|
|
|
|
err()
|
|
|
|
|
|
|
|
proc getTmpDir(sampleDir = sampleDirRefFile): string =
|
|
|
|
sampleDir.findFilePath.value.splitFile.dir
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc setErrorLevel {.used.} =
|
|
|
|
discard
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.ERROR)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc openLegacyDB(
|
|
|
|
persistent: bool;
|
|
|
|
path: string;
|
|
|
|
network: NetworkId;
|
|
|
|
): CommonRef =
|
|
|
|
let coreDB = if not persistent: newCoreDbRef LegacyDbMemory
|
|
|
|
else: newCoreDbRef(LegacyDbPersistent, path)
|
|
|
|
result = CommonRef.new(
|
|
|
|
db = coreDB,
|
|
|
|
networkId = network,
|
|
|
|
params = network.networkParams)
|
|
|
|
result.initializeEmptyDb
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# 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;
|
|
|
|
persistent = true;
|
|
|
|
) =
|
|
|
|
## Test legacy backend database
|
|
|
|
let
|
|
|
|
fileInfo = capture.file.splitFile.name.split(".")[0]
|
|
|
|
filePath = capture.file.findFilePath(baseDir,repoDir).value
|
|
|
|
baseDir = getTmpDir() / capture.name & "-legacy"
|
|
|
|
dbDir = if persistent: baseDir / "tmp" else: ""
|
|
|
|
sayPersistent = if persistent: "persistent DB" else: "mem DB only"
|
|
|
|
numBlocks = capture.numBlocks
|
|
|
|
numBlocksInfo = if numBlocks == high(int): "" else: $numBlocks & " "
|
|
|
|
|
|
|
|
defer:
|
|
|
|
if persistent: baseDir.flushDbDir
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
suite "CoreDB and LedgerRef API"&
|
2023-10-03 11:56:13 +00:00
|
|
|
&", capture={fileInfo}, {sayPersistent}":
|
|
|
|
|
2023-10-25 14:03:09 +00:00
|
|
|
test &"Ledger API, {numBlocksInfo} blocks":
|
2023-10-03 11:56:13 +00:00
|
|
|
let
|
|
|
|
com = openLegacyDB(persistent, dbDir, capture.network)
|
|
|
|
defer:
|
|
|
|
com.db.finish(flush = true)
|
|
|
|
if persistent: dbDir.flushDbDir
|
|
|
|
|
2023-10-25 14:03:09 +00:00
|
|
|
check noisy.testChainSync(filePath, com, numBlocks)
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Main function(s)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc coreDbMain*(noisy = defined(debug)) =
|
2023-11-08 12:18:32 +00:00
|
|
|
noisy.chainSyncRunner()
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
const
|
|
|
|
noisy = defined(debug) or true
|
2023-10-18 19:27:22 +00:00
|
|
|
persDb = true and false
|
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`.
|
2023-10-18 19:27:22 +00:00
|
|
|
var testList = @[bulkTest0] # This test is superseded by `bulkTest1` and `2`
|
Core db and aristo updates for destructor and tx logic (#1894)
* Disable `TransactionID` related functions from `state_db.nim`
why:
Functions `getCommittedStorage()` and `updateOriginalRoot()` from
the `state_db` module are nowhere used. The emulation of a legacy
`TransactionID` type functionality is administratively expensive to
provide by `Aristo` (the legacy DB version is only partially
implemented, anyway).
As there is no other place where `TransactionID`s are used, they will
not be provided by the `Aristo` variant of the `CoreDb`. For the
legacy DB API, nothing will change.
* Fix copyright headers in source code
* Get rid of compiler warning
* Update Aristo code, remove unused `merge()` variant, export `hashify()`
why:
Adapt to upcoming `CoreDb` wrapper
* Remove synced tx feature from `Aristo`
why:
+ This feature allowed to synchronise transaction methods like begin,
commit, and rollback for a group of descriptors.
+ The feature is over engineered and not needed for `CoreDb`, neither
is it complete (some convergence features missing.)
* Add debugging helpers to `Kvt`
also:
Update database iterator, add count variable yield argument similar
to `Aristo`.
* Provide optional destructors for `CoreDb` API
why;
For the upcoming Aristo wrapper, this allows to control when certain
smart destruction and update can take place. The auto destructor works
fine in general when the storage/cache strategy is known and acceptable
when creating descriptors.
* Add update option for `CoreDb` API function `hash()`
why;
The hash function is typically used to get the state root of the MPT.
Due to lazy hashing, this might be not available on the `Aristo` DB.
So the `update` function asks for re-hashing the gurrent state changes
if needed.
* Update API tracking log mode: `info` => `debug
* Use shared `Kvt` descriptor in new Ledger API
why:
No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
|
|
|
#testList = @[failSample0]
|
2023-10-25 14:03:09 +00:00
|
|
|
when true and false:
|
2023-10-18 19:27:22 +00:00
|
|
|
testList = @[bulkTest2, bulkTest3]
|
2023-10-12 20:10:04 +00:00
|
|
|
|
|
|
|
for n,capture in testList:
|
2023-11-08 12:18:32 +00:00
|
|
|
noisy.chainSyncRunner(capture=capture, persistent=persDb)
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|