2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-03-14 22:17:43 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-09-12 18:44:45 +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.
|
|
|
|
|
|
|
|
## In-memory backend for Kvt DB
|
|
|
|
## ============================
|
|
|
|
##
|
|
|
|
## The iterators provided here are currently available only by direct
|
|
|
|
## backend access
|
|
|
|
## ::
|
|
|
|
## import
|
|
|
|
## kvt/kvt_init,
|
|
|
|
## kvt/kvt_init/kvt_memory
|
|
|
|
##
|
|
|
|
## let rc = newKvtDbRef(BackendMemory)
|
|
|
|
## if rc.isOk:
|
|
|
|
## let be = rc.value.to(MemBackendRef)
|
|
|
|
## for (n, key, vtx) in be.walkVtx:
|
|
|
|
## ...
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-12-20 16:19:00 +00:00
|
|
|
std/tables,
|
2023-09-12 18:44:45 +00:00
|
|
|
chronicles,
|
|
|
|
eth/common,
|
|
|
|
results,
|
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
|
|
|
stew/byteutils,
|
2023-09-12 18:44:45 +00:00
|
|
|
../kvt_desc,
|
|
|
|
../kvt_desc/desc_backend,
|
|
|
|
./init_common
|
|
|
|
|
|
|
|
type
|
2024-03-14 22:17:43 +00:00
|
|
|
MemDbRef = ref object
|
|
|
|
## Database
|
|
|
|
tab: Table[Blob,Blob] ## Structural key-value table
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
MemBackendRef* = ref object of TypedBackendRef
|
|
|
|
## Inheriting table so access can be extended for debugging purposes
|
2024-03-14 22:17:43 +00:00
|
|
|
mdb: MemDbRef ## Database
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
MemPutHdlRef = ref object of TypedPutHdlRef
|
|
|
|
tab: Table[Blob,Blob]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template logTxt(info: static[string]): static[string] =
|
|
|
|
"MemoryDB " & info
|
|
|
|
|
|
|
|
|
|
|
|
proc newSession(db: MemBackendRef): MemPutHdlRef =
|
|
|
|
new result
|
|
|
|
result.TypedPutHdlRef.beginSession db
|
|
|
|
|
|
|
|
proc getSession(hdl: PutHdlRef; db: MemBackendRef): MemPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.verifySession db
|
|
|
|
hdl.MemPutHdlRef
|
|
|
|
|
|
|
|
proc endSession(hdl: PutHdlRef; db: MemBackendRef): MemPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.finishSession db
|
|
|
|
hdl.MemPutHdlRef
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: interface
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getKvpFn(db: MemBackendRef): GetKvpFn =
|
|
|
|
result =
|
2023-09-15 15:23:53 +00:00
|
|
|
proc(key: openArray[byte]): Result[Blob,KvtError] =
|
2023-09-12 18:44:45 +00:00
|
|
|
if key.len == 0:
|
|
|
|
return err(KeyInvalid)
|
2024-03-14 22:17:43 +00:00
|
|
|
let data = db.mdb.tab.getOrVoid @key
|
2023-09-12 18:44:45 +00:00
|
|
|
if data.isValid:
|
|
|
|
return ok(data)
|
|
|
|
err(GetNotFound)
|
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc putBegFn(db: MemBackendRef): PutBegFn =
|
|
|
|
result =
|
|
|
|
proc(): PutHdlRef =
|
|
|
|
db.newSession()
|
|
|
|
|
|
|
|
proc putKvpFn(db: MemBackendRef): PutKvpFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; kvps: openArray[(Blob,Blob)]) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error == KvtError(0):
|
|
|
|
for (k,v) in kvps:
|
|
|
|
if k.isValid:
|
|
|
|
hdl.tab[k] = v
|
|
|
|
else:
|
|
|
|
hdl.error = KeyInvalid
|
|
|
|
|
|
|
|
proc putEndFn(db: MemBackendRef): PutEndFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef): Result[void,KvtError] =
|
|
|
|
let hdl = hdl.endSession db
|
|
|
|
if hdl.error != KvtError(0):
|
|
|
|
debug logTxt "putEndFn: key/value failed", error=hdl.error
|
|
|
|
return err(hdl.error)
|
|
|
|
|
|
|
|
for (k,v) in hdl.tab.pairs:
|
2024-03-14 22:17:43 +00:00
|
|
|
db.mdb.tab[k] = v
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc closeFn(db: MemBackendRef): CloseFn =
|
|
|
|
result =
|
|
|
|
proc(ignore: bool) =
|
|
|
|
discard
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc memoryBackend*: BackendRef =
|
|
|
|
let db = MemBackendRef(
|
2024-03-14 22:17:43 +00:00
|
|
|
beKind: BackendMemory,
|
|
|
|
mdb: MemDbRef())
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
db.getKvpFn = getKvpFn db
|
|
|
|
|
|
|
|
db.putBegFn = putBegFn db
|
|
|
|
db.putKvpFn = putKvpFn db
|
|
|
|
db.putEndFn = putEndFn db
|
|
|
|
|
|
|
|
db.closeFn = closeFn db
|
|
|
|
|
|
|
|
db
|
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
proc dup*(db: MemBackendRef): MemBackendRef =
|
|
|
|
## Duplicate descriptor shell as needed for API debugging
|
|
|
|
new result
|
|
|
|
init_common.init(result[], db[])
|
|
|
|
result.mdb = db.mdb
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators (needs direct backend access)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator walk*(
|
|
|
|
be: MemBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[key: Blob, data: Blob] =
|
2023-09-12 18:44:45 +00:00
|
|
|
## Walk over all key-value pairs of the database.
|
2024-03-14 22:17:43 +00:00
|
|
|
for (key,data) in be.mdb.tab.pairs:
|
2023-12-20 16:19:00 +00:00
|
|
|
if data.isValid:
|
|
|
|
yield (key, data)
|
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
|
|
|
else:
|
2023-12-20 16:19:00 +00:00
|
|
|
debug logTxt "walk() skip empty", key
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|