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
|
|
|
# Nimbus
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-11 14:25:29 +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.
|
|
|
|
|
|
|
|
## Re-invented implementation for Merkle Patricia Tree named as Aristo Trie
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[os, strformat, strutils],
|
|
|
|
chronicles,
|
2023-08-21 14:58:30 +00:00
|
|
|
eth/common,
|
|
|
|
results,
|
2023-05-11 14:25:29 +00:00
|
|
|
unittest2,
|
2024-06-07 10:56:31 +00:00
|
|
|
../nimbus/db/aristo/aristo_desc,
|
2024-08-15 12:31:07 +00:00
|
|
|
./replay/pp,
|
No ext update (#2494)
* Imported/rebase from `no-ext`, PR #2485
Store extension nodes together with the branch
Extension nodes must be followed by a branch - as such, it makes sense
to store the two together both in the database and in memory:
* fewer reads, writes and updates to traverse the tree
* simpler logic for maintaining the node structure
* less space used, both memory and storage, because there are fewer
nodes overall
There is also a downside: hashes can no longer be cached for an
extension - instead, only the extension+branch hash can be cached - this
seems like a fine tradeoff since computing it should be fast.
TODO: fix commented code
* Fix merge functions and `toNode()`
* Update `merkleSignCommit()` prototype
why:
Result is always a 32bit hash
* Update short Merkle hash key generation
details:
Ethereum reference MPTs use Keccak hashes as node links if the size of
an RLP encoded node is at least 32 bytes. Otherwise, the RLP encoded
node value is used as a pseudo node link (rather than a hash.) This is
specified in the yellow paper, appendix D.
Different to the `Aristo` implementation, the reference MPT would not
store such a node on the key-value database. Rather the RLP encoded node value is stored instead of a node link in a parent node
is stored as a node link on the parent database.
Only for the root hash, the top level node is always referred to by the
hash.
* Fix/update `Extension` sections
why:
Were commented out after removal of a dedicated `Extension` type which
left the system disfunctional.
* Clean up unused error codes
* Update unit tests
* Update docu
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
2024-07-16 19:47:59 +00:00
|
|
|
./test_aristo/test_blobify,
|
2024-08-06 11:29:26 +00:00
|
|
|
./test_aristo/test_merge_proof,
|
|
|
|
./test_aristo/test_portal_proof,
|
|
|
|
./test_aristo/test_short_keys,
|
2024-08-15 12:31:07 +00:00
|
|
|
./test_aristo/[
|
|
|
|
test_balancer, test_helpers, test_samples_xx, test_tx,
|
|
|
|
undump_accounts, undump_storages]
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
baseDir = [".", "..", ".."/"..", $DirSep]
|
|
|
|
repoDir = [".", "tests", "nimbus-eth1-blobs"]
|
2024-06-06 09:29:38 +00:00
|
|
|
subDir = ["replay", "test_aristo", "replay"/"snap"]
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Reference file for finding the database directory
|
|
|
|
sampleDirRefFile = "sample0.txt.gz"
|
|
|
|
|
|
|
|
# Standard test samples
|
|
|
|
accSample = snapTest0
|
2023-06-02 10:04:29 +00:00
|
|
|
storSample = snapTest4
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# 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 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)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Test Runners: accounts and accounts storages
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-22 11:13:24 +00:00
|
|
|
proc accountsRunner(
|
|
|
|
noisy = true;
|
|
|
|
sample = accSample;
|
|
|
|
cmpBackends = true;
|
2023-09-11 20:38:49 +00:00
|
|
|
persistent = true;
|
2023-06-22 11:13:24 +00:00
|
|
|
) =
|
2023-05-30 21:21:15 +00:00
|
|
|
let
|
2023-06-02 10:04:29 +00:00
|
|
|
accLst = sample.to(seq[UndumpAccounts]).to(seq[ProofTrieData])
|
2023-05-30 21:21:15 +00:00
|
|
|
fileInfo = sample.file.splitPath.tail.replace(".txt.gz","")
|
2023-06-20 13:26:25 +00:00
|
|
|
baseDir = getTmpDir() / sample.name & "-accounts"
|
2023-09-11 20:38:49 +00:00
|
|
|
dbDir = if persistent: baseDir / "tmp" else: ""
|
2023-09-12 18:45:12 +00:00
|
|
|
isPersistent = if persistent: "persistent DB" else: "mem DB only"
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
defer:
|
|
|
|
try: baseDir.removeDir except CatchableError: discard
|
2023-05-30 21:21:15 +00:00
|
|
|
|
2024-08-06 11:29:26 +00:00
|
|
|
suite &"Aristo: accounts data dump from {fileInfo}, {isPersistent}":
|
2023-05-30 21:21:15 +00:00
|
|
|
|
|
|
|
test &"Merge {accLst.len} proof & account lists to database":
|
2024-08-06 11:29:26 +00:00
|
|
|
check noisy.testMergeProofAndKvpList(accLst, dbDir)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
test &"Delete accounts database successively, {accLst.len} lists":
|
|
|
|
check noisy.testTxMergeAndDeleteOneByOne(accLst, dbDir)
|
|
|
|
|
|
|
|
test &"Delete accounts database sub-trees, {accLst.len} lists":
|
|
|
|
check noisy.testTxMergeAndDeleteSubTree(accLst, dbDir)
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2024-07-17 19:27:33 +00:00
|
|
|
test &"Distributed backend balancers {accLst.len} entries":
|
|
|
|
check noisy.testBalancer(accLst, dbDir)
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
proc storagesRunner(
|
|
|
|
noisy = true;
|
|
|
|
sample = storSample;
|
2023-06-22 11:13:24 +00:00
|
|
|
cmpBackends = true;
|
2023-09-11 20:38:49 +00:00
|
|
|
persistent = true;
|
2023-06-02 10:04:29 +00:00
|
|
|
) =
|
|
|
|
let
|
|
|
|
stoLst = sample.to(seq[UndumpStorages]).to(seq[ProofTrieData])
|
|
|
|
fileInfo = sample.file.splitPath.tail.replace(".txt.gz","")
|
2023-06-20 13:26:25 +00:00
|
|
|
baseDir = getTmpDir() / sample.name & "-storage"
|
2023-09-11 20:38:49 +00:00
|
|
|
dbDir = if persistent: baseDir / "tmp" else: ""
|
2023-09-12 18:45:12 +00:00
|
|
|
isPersistent = if persistent: "persistent DB" else: "mem DB only"
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
defer:
|
|
|
|
try: baseDir.removeDir except CatchableError: discard
|
2023-05-30 21:21:15 +00:00
|
|
|
|
2024-08-06 11:29:26 +00:00
|
|
|
suite &"Aristo: storages data dump from {fileInfo}, {isPersistent}":
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2024-08-06 11:29:26 +00:00
|
|
|
test &"Merge {stoLst.len} proof & slot lists to database":
|
|
|
|
check noisy.testMergeProofAndKvpList(stoLst, dbDir, fileInfo)
|
2023-06-22 11:13:24 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
test &"Delete storage database successively, {stoLst.len} lists":
|
|
|
|
check noisy.testTxMergeAndDeleteOneByOne(stoLst, dbDir)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
test &"Delete storage database sub-trees, {stoLst.len} lists":
|
|
|
|
check noisy.testTxMergeAndDeleteSubTree(stoLst, dbDir)
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2024-07-17 19:27:33 +00:00
|
|
|
test &"Distributed backend balancers {stoLst.len} entries":
|
|
|
|
check noisy.testBalancer(stoLst, dbDir)
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Main function(s)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc aristoMain*(noisy = defined(debug)) =
|
2024-03-15 14:20:00 +00:00
|
|
|
noisy.storagesRunner()
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
const
|
|
|
|
noisy = defined(debug) or true
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
setErrorLevel()
|
|
|
|
|
2024-04-03 15:48:35 +00:00
|
|
|
when true and false:
|
|
|
|
# Verify Problem with the database for production test
|
2024-06-03 20:10:35 +00:00
|
|
|
noisy.aristoMain()
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-05-30 21:21:15 +00:00
|
|
|
when true: # and false:
|
No ext update (#2494)
* Imported/rebase from `no-ext`, PR #2485
Store extension nodes together with the branch
Extension nodes must be followed by a branch - as such, it makes sense
to store the two together both in the database and in memory:
* fewer reads, writes and updates to traverse the tree
* simpler logic for maintaining the node structure
* less space used, both memory and storage, because there are fewer
nodes overall
There is also a downside: hashes can no longer be cached for an
extension - instead, only the extension+branch hash can be cached - this
seems like a fine tradeoff since computing it should be fast.
TODO: fix commented code
* Fix merge functions and `toNode()`
* Update `merkleSignCommit()` prototype
why:
Result is always a 32bit hash
* Update short Merkle hash key generation
details:
Ethereum reference MPTs use Keccak hashes as node links if the size of
an RLP encoded node is at least 32 bytes. Otherwise, the RLP encoded
node value is used as a pseudo node link (rather than a hash.) This is
specified in the yellow paper, appendix D.
Different to the `Aristo` implementation, the reference MPT would not
store such a node on the key-value database. Rather the RLP encoded node value is stored instead of a node link in a parent node
is stored as a node link on the parent database.
Only for the root hash, the top level node is always referred to by the
hash.
* Fix/update `Extension` sections
why:
Were commented out after removal of a dedicated `Extension` type which
left the system disfunctional.
* Clean up unused error codes
* Update unit tests
* Update docu
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
2024-07-16 19:47:59 +00:00
|
|
|
let persistent = false # or true
|
2023-09-11 20:38:49 +00:00
|
|
|
noisy.showElapsed("@snap_test_list"):
|
|
|
|
for n,sam in snapTestList:
|
2023-09-12 18:45:12 +00:00
|
|
|
noisy.accountsRunner(sam, persistent=persistent)
|
2023-09-11 20:38:49 +00:00
|
|
|
noisy.showElapsed("@snap_test_storage_list"):
|
|
|
|
for n,sam in snapTestStorageList:
|
2023-09-12 18:45:12 +00:00
|
|
|
noisy.accountsRunner(sam, persistent=persistent)
|
|
|
|
noisy.storagesRunner(sam, persistent=persistent)
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|