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
|
|
|
|
## ===============================================
|
|
|
|
##
|
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-06-12 13:48:47 +00:00
|
|
|
./aristo_constants,
|
2023-08-25 22:53:59 +00:00
|
|
|
./aristo_desc/[desc_error, desc_identifiers, desc_structural]
|
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
|
2023-08-25 22:53:59 +00:00
|
|
|
aristo_constants, desc_error, desc_identifiers, desc_structural
|
2023-06-12 13:48:47 +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
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
DudesRef* = ref object
|
2023-08-17 13:42:01 +00:00
|
|
|
case rwOk*: bool
|
|
|
|
of true:
|
|
|
|
roDudes*: HashSet[AristoDbRef] ## Read-only peers
|
|
|
|
else:
|
|
|
|
rwDb*: AristoDbRef ## Link to writable descriptor
|
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
AristoDbRef* = ref AristoDbObj
|
|
|
|
AristoDbObj* = 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
|
|
|
|
roFilter*: FilterRef ## Apply read filter (locks writing)
|
|
|
|
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
|
2023-08-18 19:46:55 +00:00
|
|
|
dudes*: DudesRef ## Related DB descriptors
|
2023-08-07 17:45:23 +00:00
|
|
|
|
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-08-17 13:42:01 +00:00
|
|
|
AristoDbAction* = proc(db: AristoDbRef) {.gcsafe, raises: [CatchableError].}
|
|
|
|
## Generic call back function/closure.
|
|
|
|
|
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-08-21 18:18:06 +00:00
|
|
|
func isValid*(filter: FilterRef): bool =
|
|
|
|
filter != FilterRef(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-08-25 22:53:59 +00:00
|
|
|
func isValid*(qid: QueueID): bool =
|
|
|
|
qid != QueueID(0)
|
2023-08-18 19:46:55 +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
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
# Note that the below `init()` function cannot go into `desc_identifiers` as
|
|
|
|
# this would result in a circular import.
|
2023-06-20 13:26:25 +00:00
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|