2023-08-07 17:45:23 +00:00
|
|
|
# nimbus-eth1
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-08-07 17:45:23 +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.
|
|
|
|
|
|
|
|
## Aristo DB -- Transaction interface
|
|
|
|
## ==================================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-03-22 17:31:56 +00:00
|
|
|
std/tables,
|
2023-08-07 17:45:23 +00:00
|
|
|
results,
|
2023-12-19 12:39:23 +00:00
|
|
|
"."/[aristo_desc, aristo_filter, aristo_get, aristo_layers, aristo_hashify]
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2024-03-20 15:15:56 +00:00
|
|
|
func isTop*(tx: AristoTxRef): bool {.gcsafe.}
|
|
|
|
func level*(db: AristoDbRef): int {.gcsafe.}
|
|
|
|
proc txBegin*(db: AristoDbRef): Result[AristoTxRef,AristoError] {.gcsafe.}
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func getDbDescFromTopTx(tx: AristoTxRef): Result[AristoDbRef,AristoError] =
|
|
|
|
if not tx.isTop():
|
|
|
|
return err(TxNotTopTx)
|
|
|
|
let db = tx.db
|
|
|
|
if tx.level != db.stack.len:
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxStackGarbled)
|
2023-08-11 17:23:57 +00:00
|
|
|
ok db
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
proc getTxUid(db: AristoDbRef): uint =
|
2023-08-11 17:23:57 +00:00
|
|
|
if db.txUidGen == high(uint):
|
|
|
|
db.txUidGen = 0
|
2023-08-07 17:45:23 +00:00
|
|
|
db.txUidGen.inc
|
|
|
|
db.txUidGen
|
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
iterator txWalk(tx: AristoTxRef): (AristoTxRef,LayerRef,AristoError) =
|
|
|
|
## Walk down the transaction chain.
|
|
|
|
let db = tx.db
|
|
|
|
var tx = tx
|
2024-03-21 10:45:57 +00:00
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
block body:
|
|
|
|
# Start at top layer if tx refers to that
|
|
|
|
if tx.level == db.stack.len:
|
|
|
|
if tx.txUid != db.top.txUid:
|
|
|
|
yield (tx,db.top,TxStackGarbled)
|
|
|
|
break body
|
2024-03-21 10:45:57 +00:00
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
# Yield the top level
|
|
|
|
yield (tx,db.top,AristoError(0))
|
|
|
|
|
|
|
|
# Walk down the transaction stack
|
|
|
|
for level in (tx.level-1).countDown(1):
|
|
|
|
tx = tx.parent
|
|
|
|
if tx.isNil or tx.level != level:
|
|
|
|
yield (tx,LayerRef(nil),TxStackGarbled)
|
|
|
|
break body
|
|
|
|
|
|
|
|
var layer = db.stack[level]
|
|
|
|
if tx.txUid != layer.txUid:
|
|
|
|
yield (tx,layer,TxStackGarbled)
|
|
|
|
break body
|
|
|
|
|
|
|
|
yield (tx,layer,AristoError(0))
|
2024-03-21 10:45:57 +00:00
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func txTop*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Getter, returns top level transaction if there is any.
|
|
|
|
if db.txRef.isNil:
|
|
|
|
err(TxNoPendingTx)
|
|
|
|
else:
|
|
|
|
ok(db.txRef)
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func isTop*(tx: AristoTxRef): bool =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Getter, returns `true` if the argument `tx` referes to the current top
|
|
|
|
## level transaction.
|
|
|
|
tx.db.txRef == tx and tx.db.top.txUid == tx.txUid
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func level*(tx: AristoTxRef): int =
|
|
|
|
## Getter, positive nesting level of transaction argument `tx`
|
|
|
|
tx.level
|
|
|
|
|
|
|
|
func level*(db: AristoDbRef): int =
|
|
|
|
## Getter, non-negative nesting level (i.e. number of pending transactions)
|
|
|
|
if not db.txRef.isNil:
|
|
|
|
result = db.txRef.level
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func to*(tx: AristoTxRef; T: type[AristoDbRef]): T =
|
|
|
|
## Getter, retrieves the parent database descriptor from argument `tx`
|
2023-08-07 17:45:23 +00:00
|
|
|
tx.db
|
|
|
|
|
2024-03-20 15:15:56 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
proc forkTx*(
|
|
|
|
tx: AristoTxRef; # Transaction descriptor
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[AristoDbRef,AristoError] =
|
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
|
|
|
## Clone a transaction into a new DB descriptor accessing the same backend
|
|
|
|
## database (if any) as the argument `db`. The new descriptor is linked to
|
2023-09-11 20:38:49 +00:00
|
|
|
## the transaction parent and is fully functional as a forked instance (see
|
|
|
|
## comments on `aristo_desc.reCentre()` for details.)
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
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
|
|
|
## Input situation:
|
|
|
|
## ::
|
|
|
|
## tx -> db0 with tx is top transaction, tx.level > 0
|
|
|
|
##
|
|
|
|
## Output situation:
|
|
|
|
## ::
|
|
|
|
## tx -> db0 \
|
|
|
|
## > share the same backend
|
|
|
|
## tx1 -> db1 /
|
|
|
|
##
|
|
|
|
## where `tx.level > 0`, `db1.level == 1` and `db1` is returned. The
|
|
|
|
## transaction `tx1` can be retrieved via `db1.txTop()`.
|
|
|
|
##
|
2023-09-11 20:38:49 +00:00
|
|
|
## The new DB descriptor will contain a copy of the argument transaction
|
|
|
|
## `tx` as top layer of level 1 (i.e. this is he only transaction.) Rolling
|
|
|
|
## back will end up at the backend layer (incl. backend filter.)
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
2023-09-18 20:20:28 +00:00
|
|
|
## If the arguent flag `dontHashify` is passed `true`, the clone descriptor
|
|
|
|
## will *NOT* be hashified right after construction.
|
|
|
|
##
|
2023-09-11 20:38:49 +00:00
|
|
|
## Use `aristo_desc.forget()` to clean up this descriptor.
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
|
|
|
let db = tx.db
|
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
# Verify `tx` argument
|
2023-08-17 13:42:01 +00:00
|
|
|
if db.txRef == tx:
|
2023-12-19 12:39:23 +00:00
|
|
|
if db.top.txUid != tx.txUid:
|
|
|
|
return err(TxArgStaleTx)
|
|
|
|
elif db.stack.len <= tx.level:
|
2023-08-17 13:42:01 +00:00
|
|
|
return err(TxArgStaleTx)
|
2023-12-19 12:39:23 +00:00
|
|
|
elif db.stack[tx.level].txUid != tx.txUid:
|
2023-08-17 13:42:01 +00:00
|
|
|
return err(TxArgStaleTx)
|
|
|
|
|
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
|
|
|
# Provide new empty stack layer
|
2023-08-17 13:42:01 +00:00
|
|
|
let stackLayer = block:
|
|
|
|
let rc = db.getIdgBE()
|
|
|
|
if rc.isOk:
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
LayerRef(
|
|
|
|
delta: LayerDeltaRef(),
|
|
|
|
final: LayerFinalRef(vGen: rc.value))
|
2023-08-17 13:42:01 +00:00
|
|
|
elif rc.error == GetIdgNotFound:
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
LayerRef.init()
|
2023-08-17 13:42:01 +00:00
|
|
|
else:
|
|
|
|
return err(rc.error)
|
|
|
|
|
|
|
|
# Set up clone associated to `db`
|
2024-03-20 15:15:56 +00:00
|
|
|
let txClone = ? db.fork(noToplayer = true, noFilter = false)
|
|
|
|
txClone.top = db.layersCc tx.level # Provide tx level 1 stack
|
|
|
|
txClone.stack = @[stackLayer] # Zero level stack
|
2023-12-19 12:39:23 +00:00
|
|
|
txClone.top.txUid = 1
|
2023-09-11 20:38:49 +00:00
|
|
|
txClone.txUidGen = 1
|
2023-08-17 13:42:01 +00:00
|
|
|
|
|
|
|
# Install transaction similar to `tx` on clone
|
|
|
|
txClone.txRef = AristoTxRef(
|
|
|
|
db: txClone,
|
|
|
|
txUid: 1,
|
|
|
|
level: 1)
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
if not dontHashify:
|
2024-02-22 08:24:58 +00:00
|
|
|
txClone.hashify().isOkOr:
|
2023-09-18 20:20:28 +00:00
|
|
|
discard txClone.forget()
|
2023-12-19 12:39:23 +00:00
|
|
|
return err(error[1])
|
2023-09-18 20:20:28 +00:00
|
|
|
|
2023-08-17 13:42:01 +00:00
|
|
|
ok(txClone)
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
proc forkTop*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[AristoDbRef,AristoError] =
|
|
|
|
## Variant of `forkTx()` for the top transaction if there is any. Otherwise
|
|
|
|
## the top layer is cloned, and an empty transaction is set up. After
|
|
|
|
## successful fork the returned descriptor has transaction level 1.
|
|
|
|
##
|
|
|
|
## Use `aristo_desc.forget()` to clean up this descriptor.
|
|
|
|
##
|
|
|
|
if db.txRef.isNil:
|
|
|
|
let dbClone = ? db.fork(noToplayer=true, noFilter=false)
|
|
|
|
dbClone.top = db.layersCc # Is a deep copy
|
|
|
|
|
|
|
|
if not dontHashify:
|
|
|
|
dbClone.hashify().isOkOr:
|
|
|
|
discard dbClone.forget()
|
|
|
|
return err(error[1])
|
|
|
|
|
|
|
|
discard dbClone.txBegin
|
|
|
|
return ok(dbClone)
|
|
|
|
# End if()
|
|
|
|
|
|
|
|
db.txRef.forkTx dontHashify
|
|
|
|
|
|
|
|
|
|
|
|
proc forkBase*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[AristoDbRef,AristoError] =
|
|
|
|
## Variant of `forkTx()`, sort of the opposite of `forkTop()`. This is the
|
|
|
|
## equivalent of top layer forking after all tranactions have been rolled
|
|
|
|
## back.
|
|
|
|
##
|
|
|
|
## Use `aristo_desc.forget()` to clean up this descriptor.
|
|
|
|
##
|
|
|
|
if not db.txRef.isNil:
|
|
|
|
let dbClone = ? db.fork(noToplayer=true, noFilter=false)
|
|
|
|
dbClone.top = db.layersCc 0
|
|
|
|
|
|
|
|
if not dontHashify:
|
|
|
|
dbClone.hashify().isOkOr:
|
|
|
|
discard dbClone.forget()
|
|
|
|
return err(error[1])
|
|
|
|
|
|
|
|
discard dbClone.txBegin
|
|
|
|
return ok(dbClone)
|
|
|
|
# End if()
|
|
|
|
|
|
|
|
db.forkTop dontHashify
|
|
|
|
|
|
|
|
|
2024-03-21 10:45:57 +00:00
|
|
|
proc forkWith*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
vid: VertexID; # Pivot vertex (typically `VertexID(1)`)
|
|
|
|
key: HashKey; # Hash key of pivot verte
|
2024-03-22 17:31:56 +00:00
|
|
|
dontHashify = true; # Process/fix MPT hashes
|
2024-03-21 10:45:57 +00:00
|
|
|
): Result[AristoDbRef,AristoError] =
|
|
|
|
## Find the transaction where the vertex with ID `vid` exists and has the
|
|
|
|
## Merkle hash key `key`. If there is no transaction available, search in
|
|
|
|
## the filter and then in the backend.
|
|
|
|
##
|
|
|
|
## If the above procedure succeeds, a new descriptor is forked with exactly
|
|
|
|
## one transaction which contains the all the bottom layers up until the
|
|
|
|
## layer where the `(vid,key)` pair is found. In case the pair was found on
|
|
|
|
## the filter or the backend, this transaction is empty.
|
|
|
|
##
|
|
|
|
if not vid.isValid or
|
|
|
|
not key.isValid:
|
|
|
|
return err(TxArgsUseless)
|
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
if db.txRef.isNil:
|
|
|
|
# Try `(vid,key)` on top layer
|
|
|
|
let topKey = db.top.delta.kMap.getOrVoid vid
|
|
|
|
if topKey == key:
|
|
|
|
return db.forkTop dontHashify
|
2024-03-21 10:45:57 +00:00
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
else:
|
|
|
|
# Find `(vid,key)` on transaction layers
|
|
|
|
for (tx,layer,error) in db.txRef.txWalk:
|
|
|
|
if error != AristoError(0):
|
|
|
|
return err(error)
|
|
|
|
if layer.delta.kMap.getOrVoid(vid) == key:
|
|
|
|
return tx.forkTx dontHashify
|
|
|
|
|
|
|
|
# Try bottom layer
|
|
|
|
let botKey = db.stack[0].delta.kMap.getOrVoid vid
|
|
|
|
if botKey == key:
|
|
|
|
return db.forkBase dontHashify
|
|
|
|
|
|
|
|
# Try `(vid,key)` on filter
|
2024-03-21 10:45:57 +00:00
|
|
|
if not db.roFilter.isNil:
|
|
|
|
let roKey = db.roFilter.kMap.getOrVoid vid
|
|
|
|
if roKey == key:
|
|
|
|
let rc = db.fork(noFilter = false)
|
|
|
|
if rc.isOk:
|
|
|
|
discard rc.value.txBegin
|
|
|
|
return rc
|
|
|
|
|
2024-03-22 17:31:56 +00:00
|
|
|
# Try `(vid,key)` on unfiltered backend
|
2024-03-21 10:45:57 +00:00
|
|
|
block:
|
|
|
|
let beKey = db.getKeyUBE(vid).valueOr: VOID_HASH_KEY
|
|
|
|
if beKey == key:
|
|
|
|
let rc = db.fork(noFilter = true)
|
|
|
|
if rc.isOk:
|
|
|
|
discard rc.value.txBegin
|
|
|
|
return rc
|
|
|
|
|
|
|
|
err(TxNotFound)
|
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: Transaction frame
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
proc txBegin*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Starts a new transaction.
|
|
|
|
##
|
|
|
|
## Example:
|
|
|
|
## ::
|
|
|
|
## proc doSomething(db: AristoDbRef) =
|
|
|
|
## let tx = db.begin
|
|
|
|
## defer: tx.rollback()
|
|
|
|
## ... continue using db ...
|
|
|
|
## tx.commit()
|
|
|
|
##
|
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
|
|
|
if db.level != db.stack.len:
|
|
|
|
return err(TxStackGarbled)
|
2023-08-11 17:23:57 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
db.stack.add db.top
|
|
|
|
db.top = LayerRef(
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
delta: LayerDeltaRef(),
|
|
|
|
final: db.top.final.dup,
|
2023-12-19 12:39:23 +00:00
|
|
|
txUid: db.getTxUid)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
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
|
|
|
db.txRef = AristoTxRef(
|
|
|
|
db: db,
|
|
|
|
txUid: db.top.txUid,
|
|
|
|
parent: db.txRef,
|
|
|
|
level: db.stack.len)
|
2023-08-11 17:23:57 +00:00
|
|
|
|
|
|
|
ok db.txRef
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc rollback*(
|
|
|
|
tx: AristoTxRef; # Top transaction on database
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Given a *top level* handle, this function discards all database operations
|
|
|
|
## performed for this transactio. The previous transaction is returned if
|
|
|
|
## there was any.
|
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
let db = ? tx.getDbDescFromTopTx()
|
2023-08-07 17:45:23 +00:00
|
|
|
|
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
|
|
|
# Roll back to previous layer.
|
|
|
|
db.top = db.stack[^1]
|
|
|
|
db.stack.setLen(db.stack.len-1)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
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
|
|
|
db.txRef = db.txRef.parent
|
|
|
|
ok()
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
|
|
|
|
proc commit*(
|
|
|
|
tx: AristoTxRef; # Top transaction on database
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Given a *top level* handle, this function accepts all database operations
|
|
|
|
## performed through this handle and merges it to the previous layer. The
|
|
|
|
## previous transaction is returned if there was any.
|
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
let db = ? tx.getDbDescFromTopTx()
|
2024-02-22 08:24:58 +00:00
|
|
|
db.hashify().isOkOr:
|
2023-12-19 12:39:23 +00:00
|
|
|
return err(error[1])
|
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
# Pop layer from stack and merge database top layer onto it
|
|
|
|
let merged = block:
|
|
|
|
if db.top.delta.sTab.len == 0 and
|
2024-02-22 08:24:58 +00:00
|
|
|
db.top.delta.kMap.len == 0:
|
2023-12-20 16:19:00 +00:00
|
|
|
# Avoid `layersMergeOnto()`
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
db.top.delta = db.stack[^1].delta
|
2023-12-20 16:19:00 +00:00
|
|
|
db.stack.setLen(db.stack.len-1)
|
|
|
|
db.top
|
|
|
|
else:
|
|
|
|
let layer = db.stack[^1]
|
|
|
|
db.stack.setLen(db.stack.len-1)
|
2024-02-22 08:24:58 +00:00
|
|
|
db.top.layersMergeOnto layer[]
|
2023-12-20 16:19:00 +00:00
|
|
|
layer
|
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
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
# Install `merged` stack top layer and update stack
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top = merged
|
|
|
|
db.txRef = tx.parent
|
|
|
|
if 0 < db.stack.len:
|
|
|
|
db.txRef.txUid = db.getTxUid
|
|
|
|
db.top.txUid = db.txRef.txUid
|
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
|
|
|
ok()
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc collapse*(
|
2023-08-11 17:23:57 +00:00
|
|
|
tx: AristoTxRef; # Top transaction on database
|
|
|
|
commit: bool; # Commit if `true`, otherwise roll back
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Iterated application of `commit()` or `rollback()` performing the
|
|
|
|
## something similar to
|
|
|
|
## ::
|
2023-08-11 17:23:57 +00:00
|
|
|
## while true:
|
|
|
|
## discard tx.commit() # ditto for rollback()
|
|
|
|
## if db.topTx.isErr: break
|
|
|
|
## tx = db.topTx.value
|
2023-08-07 17:45:23 +00:00
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
let db = ? tx.getDbDescFromTopTx()
|
|
|
|
|
|
|
|
if commit:
|
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
|
|
|
# For commit, hashify the current layer if requested and install it
|
2024-02-22 08:24:58 +00:00
|
|
|
db.hashify().isOkOr:
|
2023-12-19 12:39:23 +00:00
|
|
|
return err(error[1])
|
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
|
|
|
|
|
|
|
db.top.txUid = 0
|
|
|
|
db.stack.setLen(0)
|
|
|
|
db.txRef = AristoTxRef(nil)
|
|
|
|
ok()
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: save database
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
proc stow*(
|
2023-08-11 17:23:57 +00:00
|
|
|
db: AristoDbRef; # Database
|
|
|
|
persistent = false; # Stage only unless `true`
|
|
|
|
chunkedMpt = false; # Partial data (e.g. from `snap`)
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## If there is no backend while the `persistent` argument is set `true`,
|
2023-09-12 18:45:12 +00:00
|
|
|
## the function returns immediately with an error. The same happens if there
|
2023-08-17 13:42:01 +00:00
|
|
|
## is a pending transaction.
|
2023-08-11 17:23:57 +00:00
|
|
|
##
|
|
|
|
## The function then merges the data from the top layer cache into the
|
2023-08-10 20:01:28 +00:00
|
|
|
## backend stage area. After that, the top layer cache is cleared.
|
|
|
|
##
|
2023-08-11 17:23:57 +00:00
|
|
|
## Staging the top layer cache might fail withh a partial MPT when it is
|
|
|
|
## set up from partial MPT chunks as it happens with `snap` sync processing.
|
|
|
|
## In this case, the `chunkedMpt` argument must be set `true` (see alse
|
|
|
|
## `fwdFilter`.)
|
|
|
|
##
|
|
|
|
## If the argument `persistent` is set `true`, all the staged data are merged
|
|
|
|
## into the physical backend database and the staged data area is cleared.
|
2023-08-10 20:01:28 +00:00
|
|
|
##
|
2023-08-17 13:42:01 +00:00
|
|
|
if not db.txRef.isNil:
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxPendingTx)
|
2023-08-17 13:42:01 +00:00
|
|
|
if 0 < db.stack.len:
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxStackGarbled)
|
2023-09-11 20:38:49 +00:00
|
|
|
if persistent and not db.canResolveBackendFilter():
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxBackendNotWritable)
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2024-04-19 18:37:27 +00:00
|
|
|
# Updatre Merkle hashes (unless disabled)
|
2024-02-22 08:24:58 +00:00
|
|
|
db.hashify().isOkOr:
|
2023-12-19 12:39:23 +00:00
|
|
|
return err(error[1])
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
let fwd = db.fwdFilter(db.top, chunkedMpt).valueOr:
|
|
|
|
return err(error[1])
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
if fwd.isValid:
|
|
|
|
# Merge `top` layer into `roFilter`
|
2023-12-19 12:39:23 +00:00
|
|
|
db.merge(fwd).isOkOr:
|
|
|
|
return err(error[1])
|
2024-04-19 18:37:27 +00:00
|
|
|
|
|
|
|
# Special treatment for `snap` proofs (aka `chunkedMpt`)
|
2024-04-03 15:48:35 +00:00
|
|
|
let final =
|
|
|
|
if chunkedMpt: LayerFinalRef(fRpp: db.top.final.fRpp)
|
|
|
|
else: LayerFinalRef()
|
2024-04-19 18:37:27 +00:00
|
|
|
|
|
|
|
# New empty top layer (probably with `snap` proofs and `vGen` carry over)
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
db.top = LayerRef(
|
|
|
|
delta: LayerDeltaRef(),
|
2024-04-03 15:48:35 +00:00
|
|
|
final: final)
|
2024-02-01 21:27:48 +00:00
|
|
|
if db.roFilter.isValid:
|
|
|
|
db.top.final.vGen = db.roFilter.vGen
|
|
|
|
else:
|
|
|
|
let rc = db.getIdgUBE()
|
|
|
|
if rc.isOk:
|
|
|
|
db.top.final.vGen = rc.value
|
|
|
|
else:
|
|
|
|
# It is OK if there was no `Idg`. Otherwise something serious happened
|
|
|
|
# and there is no way to recover easily.
|
|
|
|
doAssert rc.error == GetIdgNotFound
|
2023-08-17 13:42:01 +00:00
|
|
|
|
|
|
|
if persistent:
|
2024-04-19 18:37:27 +00:00
|
|
|
# Merge `roFiler` into persistent tables
|
2023-09-15 15:23:53 +00:00
|
|
|
? db.resolveBackendFilter()
|
2023-08-18 19:46:55 +00:00
|
|
|
db.roFilter = FilterRef(nil)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2024-04-19 18:37:27 +00:00
|
|
|
# Special treatment for `snap` proofs (aka `chunkedMpt`)
|
2024-04-03 15:48:35 +00:00
|
|
|
let final =
|
|
|
|
if chunkedMpt: LayerFinalRef(vGen: db.vGen, fRpp: db.top.final.fRpp)
|
|
|
|
else: LayerFinalRef(vGen: db.vGen)
|
2024-04-19 18:37:27 +00:00
|
|
|
|
|
|
|
# New empty top layer (probably with `snap` proofs carry over)
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top = LayerRef(
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
delta: LayerDeltaRef(),
|
2024-04-03 15:48:35 +00:00
|
|
|
final: final,
|
2023-12-19 12:39:23 +00:00
|
|
|
txUid: db.top.txUid)
|
2023-08-07 17:45:23 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|