2023-05-11 14:25:29 +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-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.
|
|
|
|
|
|
|
|
## Aristo DB -- a Patricia Trie with labeled edges
|
|
|
|
## ===============================================
|
|
|
|
##
|
2023-08-17 13:42:01 +00:00
|
|
|
## These data structures allow to overlay the *Patricia Trie* with *Merkel
|
2023-05-11 14:25:29 +00:00
|
|
|
## Trie* hashes. See the `README.md` in the `aristo` folder for documentation.
|
2023-05-14 17:43:01 +00:00
|
|
|
##
|
|
|
|
## Some semantic explanations;
|
|
|
|
##
|
2023-06-12 18:16:03 +00:00
|
|
|
## * HashKey, NodeRef etc. refer to the standard/legacy `Merkle Patricia Tree`
|
2023-05-14 17:43:01 +00:00
|
|
|
## * VertexID, VertexRef, etc. refer to the `Aristo Trie`
|
|
|
|
##
|
2023-05-11 14:25:29 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-08-17 13:42:01 +00:00
|
|
|
std/[hashes, sets, tables],
|
2023-06-12 18:16:03 +00:00
|
|
|
eth/common,
|
2023-09-11 20:38:49 +00:00
|
|
|
results,
|
2023-06-12 13:48:47 +00:00
|
|
|
./aristo_constants,
|
2024-09-13 13:47:50 +00:00
|
|
|
./aristo_desc/[desc_error, desc_identifiers, desc_nibbles, desc_structural],
|
|
|
|
minilru
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
from ./aristo_desc/desc_backend
|
2023-08-18 19:46:55 +00:00
|
|
|
import BackendRef
|
2023-05-30 21:21:15 +00:00
|
|
|
|
2023-08-17 13:42:01 +00:00
|
|
|
# Not auto-exporting backend
|
2023-05-30 21:21:15 +00:00
|
|
|
export
|
2024-07-18 07:13:56 +00:00
|
|
|
tables, aristo_constants, desc_error, desc_identifiers, desc_nibbles,
|
2024-09-13 13:47:50 +00:00
|
|
|
desc_structural, minilru, common
|
2024-07-03 15:58:25 +00:00
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
type
|
2023-08-07 17:45:23 +00:00
|
|
|
AristoTxRef* = ref object
|
|
|
|
## Transaction descriptor
|
|
|
|
db*: AristoDbRef ## Database descriptor
|
|
|
|
parent*: AristoTxRef ## Previous transaction
|
|
|
|
txUid*: uint ## Unique ID among transactions
|
2023-08-11 17:23:57 +00:00
|
|
|
level*: int ## Stack index for this transaction
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
AristoDbRef* = ref object
|
2023-08-17 13:42:01 +00:00
|
|
|
## Three tier database object supporting distributed instances.
|
2023-08-18 19:46:55 +00:00
|
|
|
top*: LayerRef ## Database working layer, mutable
|
|
|
|
stack*: seq[LayerRef] ## Stashed immutable parent layers
|
2024-07-18 21:32:32 +00:00
|
|
|
balancer*: LayerRef ## Balance out concurrent backend access
|
2023-08-18 19:46:55 +00:00
|
|
|
backend*: BackendRef ## Backend database (may well be `nil`)
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
txRef*: AristoTxRef ## Latest active transaction
|
|
|
|
txUidGen*: uint ## Tx-relative unique number generator
|
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
accLeaves*: LruCache[Hash32, VertexRef]
|
2024-07-12 13:08:26 +00:00
|
|
|
## Account path to payload cache - accounts are frequently accessed by
|
|
|
|
## account path when contracts interact with them - this cache ensures
|
|
|
|
## that we don't have to re-traverse the storage trie for every such
|
|
|
|
## interaction
|
|
|
|
## TODO a better solution would probably be to cache this in a type
|
|
|
|
## exposed to the high-level API
|
2024-07-03 15:58:25 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
stoLeaves*: LruCache[Hash32, VertexRef]
|
2024-07-14 17:12:10 +00:00
|
|
|
## Mixed account/storage path to payload cache - same as above but caches
|
|
|
|
## the full lookup of storage slots
|
|
|
|
|
2024-07-29 20:15:17 +00:00
|
|
|
# Debugging data below, might go away in future
|
|
|
|
xMap*: Table[HashKey,RootedVertexID] ## For pretty printing/debugging
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
Leg* = object
|
|
|
|
## For constructing a `VertexPath`
|
|
|
|
wp*: VidVtxPair ## Vertex ID and data ref
|
|
|
|
nibble*: int8 ## Next vertex selector for `Branch` (if any)
|
|
|
|
|
|
|
|
Hike* = object
|
|
|
|
## Trie traversal path
|
|
|
|
root*: VertexID ## Handy for some fringe cases
|
|
|
|
legs*: ArrayBuf[NibblesBuf.high + 1, Leg] ## Chain of vertices and IDs
|
|
|
|
tail*: NibblesBuf ## Portion of non completed path
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-12 13:48:47 +00:00
|
|
|
# Public helpers
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
template mixUp*(accPath, stoPath: Hash32): Hash32 =
|
2024-07-14 17:12:10 +00:00
|
|
|
# Insecure but fast way of mixing the values of two hashes, for the purpose
|
2024-10-01 21:03:10 +00:00
|
|
|
# of quick lookups - this is certainly not a good idea for general Hash32
|
2024-07-14 17:12:10 +00:00
|
|
|
# values but account paths are generated from accounts which would be hard
|
|
|
|
# to create pre-images for, for the purpose of collisions with a particular
|
|
|
|
# storage slot
|
2024-10-01 21:03:10 +00:00
|
|
|
var v {.noinit.}: Hash32
|
2024-07-14 17:12:10 +00:00
|
|
|
for i in 0..<v.data.len:
|
|
|
|
# `+` wraps leaving all bits used
|
|
|
|
v.data[i] = accPath.data[i] + stoPath.data[i]
|
|
|
|
v
|
|
|
|
|
2023-06-22 11:13:24 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,VertexRef]; w: W): VertexRef =
|
2023-06-12 13:48:47 +00:00
|
|
|
tab.getOrDefault(w, VertexRef(nil))
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,NodeRef]; w: W): NodeRef =
|
|
|
|
tab.getOrDefault(w, NodeRef(nil))
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,HashKey]; w: W): HashKey =
|
|
|
|
tab.getOrDefault(w, VOID_HASH_KEY)
|
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,RootedVertexID]; w: W): RootedVertexID =
|
|
|
|
tab.getOrDefault(w, default(RootedVertexID))
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,HashSet[RootedVertexID]]; w: W): HashSet[RootedVertexID] =
|
|
|
|
tab.getOrDefault(w, default(HashSet[RootedVertexID]))
|
2023-11-08 12:18:32 +00:00
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# --------
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func isValid*(vtx: VertexRef): bool =
|
2024-05-23 15:37:51 +00:00
|
|
|
vtx != VertexRef(nil)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func isValid*(nd: NodeRef): bool =
|
2023-06-12 13:48:47 +00:00
|
|
|
nd != NodeRef(nil)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
func isValid*(pid: PathID): bool =
|
|
|
|
pid != VOID_PATH_ID
|
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
func isValid*(layer: LayerRef): bool =
|
|
|
|
layer != LayerRef(nil)
|
2023-08-21 18:18:06 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func isValid*(root: Hash32): bool =
|
2023-11-08 12:18:32 +00:00
|
|
|
root != EMPTY_ROOT_HASH
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func isValid*(key: HashKey): bool =
|
2024-10-01 21:03:10 +00:00
|
|
|
assert key.len != 32 or key.to(Hash32).isValid
|
2024-02-22 08:24:58 +00:00
|
|
|
0 < key.len
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func isValid*(vid: VertexID): bool =
|
2023-06-12 13:48:47 +00:00
|
|
|
vid != VertexID(0)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
func isValid*(rvid: RootedVertexID): bool =
|
|
|
|
rvid.vid.isValid and rvid.root.isValid
|
|
|
|
|
|
|
|
func isValid*(sqv: HashSet[RootedVertexID]): bool =
|
|
|
|
sqv.len > 0
|
2023-11-08 12:18:32 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, miscellaneous
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-17 13:42:01 +00:00
|
|
|
# Hash set helper
|
|
|
|
func hash*(db: AristoDbRef): Hash =
|
|
|
|
## Table/KeyedQueue/HashSet mixin
|
|
|
|
cast[pointer](db).hash
|
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
iterator rstack*(db: AristoDbRef): LayerRef =
|
|
|
|
# Stack in reverse order
|
|
|
|
for i in 0..<db.stack.len:
|
|
|
|
yield db.stack[db.stack.len - i - 1]
|
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
proc deltaAtLevel*(db: AristoDbRef, level: int): LayerRef =
|
2024-07-18 07:13:56 +00:00
|
|
|
if level == 0:
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top
|
2024-07-18 07:13:56 +00:00
|
|
|
elif level > 0:
|
|
|
|
doAssert level <= db.stack.len
|
2024-07-18 21:32:32 +00:00
|
|
|
db.stack[^level]
|
2024-07-18 07:13:56 +00:00
|
|
|
elif level == -1:
|
|
|
|
doAssert db.balancer != nil
|
|
|
|
db.balancer
|
|
|
|
elif level == -2:
|
|
|
|
nil
|
|
|
|
else:
|
|
|
|
raiseAssert "Unknown level " & $level
|
|
|
|
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|