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-06-12 13:48:47 +00:00
|
|
|
std/[sets, tables],
|
2023-06-12 18:16:03 +00:00
|
|
|
eth/common,
|
2023-06-12 13:48:47 +00:00
|
|
|
./aristo_constants,
|
|
|
|
./aristo_desc/[
|
|
|
|
aristo_error, aristo_types_backend,
|
|
|
|
aristo_types_identifiers, aristo_types_structural]
|
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-06-09 11:17:37 +00:00
|
|
|
AristoLayerRef* = ref object
|
|
|
|
## Hexary trie database layer structures. Any layer holds the full
|
|
|
|
## change relative to the backend.
|
|
|
|
sTab*: Table[VertexID,VertexRef] ## Structural vertex table
|
2023-06-12 13:48:47 +00:00
|
|
|
lTab*: Table[LeafTie,VertexID] ## Direct access, path to leaf vertex
|
|
|
|
kMap*: Table[VertexID,HashLabel] ## Merkle hash key mapping
|
2023-06-22 11:13:24 +00:00
|
|
|
pAmk*: Table[HashLabel,VertexID] ## Reverse `kMap` entries, hash key lookup
|
2023-06-09 11:17:37 +00:00
|
|
|
pPrf*: HashSet[VertexID] ## Locked vertices (proof nodes)
|
2023-05-30 21:21:15 +00:00
|
|
|
vGen*: seq[VertexID] ## Unique vertex ID generator
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
AristoDb* = object
|
|
|
|
## Set of database layers, supporting transaction frames
|
2023-06-22 11:13:24 +00:00
|
|
|
top*: AristoLayerRef ## Database working layer, mutable
|
|
|
|
stack*: seq[AristoLayerRef] ## Stashed immutable parent layers
|
2023-06-09 11:17:37 +00:00
|
|
|
backend*: AristoBackendRef ## Backend database (may well be `nil`)
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# Debugging data below, might go away in future
|
2023-06-12 13:48:47 +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-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-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
|
|
|
|
# ------------------------------------------------------------------------------
|