2023-05-11 14:25:29 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
## Aristo DB -- a Patricia Trie with labeled edges
|
|
|
|
## ===============================================
|
|
|
|
##
|
|
|
|
## These data structures allows to overlay the *Patricia Trie* with *Merkel
|
|
|
|
## 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-10 20:01:28 +00:00
|
|
|
std/tables,
|
2023-06-12 18:16:03 +00:00
|
|
|
eth/common,
|
2023-06-12 13:48:47 +00:00
|
|
|
./aristo_constants,
|
|
|
|
./aristo_desc/[
|
2023-08-10 20:01:28 +00:00
|
|
|
aristo_error, aristo_types_identifiers, aristo_types_structural]
|
|
|
|
|
|
|
|
from ./aristo_desc/aristo_types_backend
|
|
|
|
import AristoBackendRef
|
2023-05-30 21:21:15 +00:00
|
|
|
|
|
|
|
export
|
2023-06-12 13:48:47 +00:00
|
|
|
# Not auto-exporting backend
|
|
|
|
aristo_constants, aristo_error, aristo_types_identifiers,
|
|
|
|
aristo_types_structural
|
|
|
|
|
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
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
AristoDbRef* = ref AristoDbObj
|
|
|
|
AristoDbObj* = object
|
2023-06-09 11:17:37 +00:00
|
|
|
## Set of database layers, supporting transaction frames
|
2023-07-04 18:24:03 +00:00
|
|
|
top*: AristoLayerRef ## Database working layer, mutable
|
|
|
|
stack*: seq[AristoLayerRef] ## Stashed immutable parent layers
|
2023-08-10 20:01:28 +00:00
|
|
|
roFilter*: AristoFilterRef ## Apply read filter (locks writing)
|
2023-07-04 18:24:03 +00:00
|
|
|
backend*: AristoBackendRef ## 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
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# Debugging data below, might go away in future
|
2023-07-04 18:24:03 +00:00
|
|
|
xMap*: Table[HashLabel,VertexID] ## For pretty printing, extends `pAmk`
|
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
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
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-06-22 11:13:24 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,HashLabel]; w: W): HashLabel =
|
2023-06-12 13:48:47 +00:00
|
|
|
tab.getOrDefault(w, VOID_HASH_LABEL)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,HashKey]; w: W): HashKey =
|
|
|
|
tab.getOrDefault(w, VOID_HASH_KEY)
|
|
|
|
|
2023-06-22 11:13:24 +00:00
|
|
|
func getOrVoid*[W](tab: Table[W,VertexID]; w: W): VertexID =
|
2023-06-12 13:48:47 +00:00
|
|
|
tab.getOrDefault(w, VertexID(0))
|
2023-06-09 11:17:37 +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 =
|
2023-06-12 13:48:47 +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
|
|
|
|
2023-07-05 13:50:11 +00:00
|
|
|
func isValid*(pld: PayloadRef): bool =
|
|
|
|
pld != PayloadRef(nil)
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func isValid*(key: HashKey): bool =
|
2023-06-12 18:16:03 +00:00
|
|
|
key != VOID_HASH_KEY
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func isValid*(lbl: HashLabel): bool =
|
2023-06-12 13:48:47 +00:00
|
|
|
lbl != VOID_HASH_LABEL
|
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
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, miscellaneous
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Note that the below `init()` function cannot go into
|
|
|
|
# `aristo_types_identifiers` as this would result in a circular import.
|
|
|
|
|
|
|
|
func init*(key: var HashKey; data: openArray[byte]): bool =
|
|
|
|
## Import argument `data` into `key` which must have length either `32`, or
|
|
|
|
## `0`. The latter case is equivalent to an all zero byte array of size `32`.
|
|
|
|
if data.len == 32:
|
|
|
|
(addr key.ByteArray32[0]).copyMem(unsafeAddr data[0], data.len)
|
|
|
|
return true
|
|
|
|
if data.len == 0:
|
|
|
|
key = VOID_HASH_KEY
|
|
|
|
return true
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|