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-eth1
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[algorithm, sequtils, strutils, tables],
|
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
stew/byteutils,
|
|
|
|
./kvt_desc,
|
|
|
|
./kvt_desc/desc_backend,
|
|
|
|
./kvt_init/[memory_db, memory_only, rocks_db]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc squeeze(s: string; hex = false; ignLen = false): string =
|
|
|
|
## For long strings print `begin..end` only
|
|
|
|
if hex:
|
|
|
|
let n = (s.len + 1) div 2
|
|
|
|
result = if s.len < 20: s else: s[0 .. 5] & ".." & s[s.len-8 .. ^1]
|
|
|
|
if not ignLen:
|
|
|
|
result &= "[" & (if 0 < n: "#" & $n else: "") & "]"
|
|
|
|
elif s.len <= 30:
|
|
|
|
result = s
|
|
|
|
else:
|
|
|
|
result = if (s.len and 1) == 0: s[0 ..< 8] else: "0" & s[0 ..< 7]
|
|
|
|
if not ignLen:
|
|
|
|
result &= "..(" & $s.len & ")"
|
|
|
|
result &= ".." & s[s.len-16 .. ^1]
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
#proc stripZeros(a: string): string =
|
|
|
|
# a.strip(leading=true, trailing=false, chars={'0'})
|
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
|
|
|
|
|
|
|
proc toPfx(indent: int; offset = 0): string =
|
|
|
|
if 0 < indent+offset: "\n" & " ".repeat(indent+offset) else: ""
|
|
|
|
|
|
|
|
|
|
|
|
func getOrVoid*(tab: Table[Blob,uint64]; w: Blob): uint64 =
|
|
|
|
tab.getOrDefault(w, 0u64)
|
|
|
|
|
|
|
|
func getOrVoid*(tab: Table[uint64,Blob]; w: uint64): Blob =
|
|
|
|
tab.getOrDefault(w, EmptyBlob)
|
|
|
|
|
|
|
|
func isValid*(id: uint64): bool =
|
|
|
|
0 < id
|
|
|
|
|
|
|
|
|
|
|
|
proc keyID(key: Blob; db = KvtDbRef(nil)): uint64 =
|
|
|
|
if key.len == 0:
|
|
|
|
return 0
|
|
|
|
elif db.isNil:
|
|
|
|
return high(uint64)
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
ctr = db.getCentre
|
|
|
|
id = ctr.xMap.getOrVoid key
|
|
|
|
if id.isValid:
|
|
|
|
id
|
|
|
|
else:
|
|
|
|
# Save new ID
|
|
|
|
ctr.xIdGen.inc
|
|
|
|
ctr.xMap[key] = db.xIdGen
|
|
|
|
ctr.pAmx[db.xIdGen] = key
|
|
|
|
ctr.xIdGen
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
#proc keyBlob(id: uint64; db = KvtDbRef(nil)): Blob =
|
|
|
|
# if 0 < id and not db.isNil:
|
|
|
|
# result = db.getCentre.pAmx.getOrVoid id
|
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
|
|
|
|
|
|
|
|
|
|
|
proc ppID(id: uint64): string =
|
|
|
|
"$" & (if id == 0: "ø" else: $id)
|
|
|
|
|
|
|
|
proc ppKey(key: Blob; db = KvtDbRef(nil)): string =
|
|
|
|
if key.len == 0:
|
|
|
|
0.ppID
|
|
|
|
elif db.isNil:
|
|
|
|
key[0 .. 0].toHex & "-" &
|
|
|
|
key[1 ..< key.len].toHex.squeeze(hex=true,ignLen=(key.len==33))
|
|
|
|
else:
|
|
|
|
key.keyID(db).ppID
|
|
|
|
|
|
|
|
proc ppValue(data: Blob): string =
|
|
|
|
data.toHex.squeeze(hex=true)
|
|
|
|
|
|
|
|
proc ppTab(tab: Table[Blob,Blob]; db = KvtDbRef(nil); indent = 4): string =
|
|
|
|
result = "{"
|
|
|
|
if db.isNil:
|
|
|
|
let keys = tab.keys.toSeq.sorted
|
|
|
|
result &= keys.mapIt((it, tab.getOrVoid it))
|
|
|
|
.mapIt("(" & it[0].ppKey & "," & it[1].ppValue & ")")
|
|
|
|
.join(indent.toPfx(1))
|
|
|
|
else:
|
|
|
|
let keys = tab.keys.toSeq.mapIt(it.keyID db).sorted
|
|
|
|
result &= keys.mapIt((it, tab.getOrVoid db.pAmx.getOrVoid(it)))
|
|
|
|
.mapIt("(" & it[0].ppID & "," & it[1].ppValue & ")")
|
|
|
|
.join(indent.toPfx(1))
|
|
|
|
result &= "}"
|
|
|
|
|
|
|
|
proc ppMap(tab: Table[uint64,Blob]; indent = 4): string =
|
|
|
|
let keys = tab.keys.toSeq.sorted
|
|
|
|
"{" &
|
|
|
|
keys.mapIt((it, tab.getOrVoid it))
|
|
|
|
.mapIt("(" & it[0].ppID & "," & it[1].ppKey & ")")
|
|
|
|
.join(indent.toPfx(1)) &
|
|
|
|
"}"
|
|
|
|
|
|
|
|
proc ppBe[T](be: T; db: KvtDbRef; indent: int): string =
|
|
|
|
## Walk over backend table
|
|
|
|
let
|
|
|
|
pfx1 = indent.toPfx(1)
|
|
|
|
pfx2 = indent.toPfx(2)
|
|
|
|
pfx3 = indent.toPfx(3)
|
|
|
|
data = be.walk.toSeq.mapIt(
|
|
|
|
$(1+it[0]) & "(" & it[1].ppKey(db) & "," & it[2].ppValue & ")"
|
|
|
|
).join(pfx3)
|
|
|
|
spc = if 0 < data.len: pfx2 else: " "
|
|
|
|
"<" & $be.kind & ">" & pfx1 & "tab" & spc & "{" & data & "}"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc pp*(layer: LayerRef; db: KvtDbRef; indent = 4): string =
|
|
|
|
let
|
|
|
|
tLen = layer.tab.len
|
|
|
|
info = "tab(" & $tLen & ")"
|
|
|
|
pfx1 = indent.toPfx(1)
|
|
|
|
pfx2 = if 0 < tLen: indent.toPfx(2) else: " "
|
|
|
|
"<layer>" & pfx1 & info & pfx2 & layer.tab.ppTab(db,indent+2)
|
|
|
|
|
|
|
|
|
|
|
|
proc pp*(
|
|
|
|
be: BackendRef;
|
|
|
|
db: KvtDbRef;
|
|
|
|
indent = 4;
|
|
|
|
): string =
|
|
|
|
case be.kind:
|
|
|
|
of BackendMemory:
|
|
|
|
result &= be.MemBackendRef.ppBe(db, indent)
|
|
|
|
of BackendRocksDB:
|
|
|
|
result &= be.RdbBackendRef.ppBe(db, indent)
|
|
|
|
of BackendVoid:
|
|
|
|
result &= "<NoBackend>"
|
|
|
|
|
|
|
|
proc pp*(
|
|
|
|
db: KvtDbRef;
|
|
|
|
backendOk = false;
|
|
|
|
keysOk = false;
|
|
|
|
indent = 4;
|
|
|
|
): string =
|
|
|
|
let
|
|
|
|
pfx = indent.toPfx
|
|
|
|
pfx1 = indent.toPfx(1)
|
|
|
|
result = db.top.pp(db, indent=indent)
|
|
|
|
if backendOk:
|
|
|
|
result &= pfx & db.backend.pp(db, indent=indent)
|
|
|
|
if keysOk:
|
|
|
|
result &= pfx & "<keys>" & pfx1 & db.getCentre.pAmx.ppMap(indent=indent+1)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|