2023-06-12 13:48:47 +00:00
|
|
|
# nimbus-eth1
|
2023-11-08 16:52:25 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2023-06-12 13:48:47 +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 -- Identifier types
|
|
|
|
## =============================
|
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-06-20 13:26:25 +00:00
|
|
|
std/[sequtils, strutils, hashes],
|
2023-06-12 18:16:03 +00:00
|
|
|
eth/[common, trie/nibbles],
|
2023-11-08 16:52:25 +00:00
|
|
|
stew/byteutils,
|
2023-11-09 12:22:51 +00:00
|
|
|
chronicles,
|
2023-11-08 12:18:32 +00:00
|
|
|
results,
|
2023-06-20 13:26:25 +00:00
|
|
|
stint
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
type
|
2023-08-25 22:53:59 +00:00
|
|
|
QueueID* = distinct uint64
|
2023-08-21 18:18:06 +00:00
|
|
|
## Identifier used to tag filter logs stored on the backend.
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
FilterID* = distinct uint64
|
2023-09-05 13:57:20 +00:00
|
|
|
## Identifier used to identify a particular filter. It is generatied with
|
|
|
|
## the filter when stored to database.
|
2023-08-25 22:53:59 +00:00
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
VertexID* = distinct uint64
|
|
|
|
## Unique identifier for a vertex of the `Aristo Trie`. The vertex is the
|
|
|
|
## prefix tree (aka `Patricia Trie`) component. When augmented by hash
|
|
|
|
## keys, the vertex component will be called a node. On the persistent
|
|
|
|
## backend of the database, there is no other reference to the node than
|
2023-06-12 18:16:03 +00:00
|
|
|
## the very same `VertexID`.
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
HashKey* = object
|
|
|
|
## Ethereum MPTs use Keccak hashes as node links if the size of an RLP
|
|
|
|
## encoded node is of size at least 32 bytes. Otherwise, the RLP encoded
|
|
|
|
## node value is used as a pseudo node link (rather than a hash.) Such a
|
|
|
|
## node is nor stored on key-value database. Rather the RLP encoded node
|
|
|
|
## value is stored instead of a lode link in a parent node instead. Only
|
|
|
|
## for the root hash, the top level node is always referred to by the
|
|
|
|
## hash.
|
|
|
|
##
|
|
|
|
## This compaction feature needed an abstraction of the `HashKey` object
|
|
|
|
## which is either a `Hash256` or a `Blob` of length at most 31 bytes.
|
|
|
|
## This leaves two ways of representing an empty/void `HashKey` type.
|
|
|
|
## It may be available as an empty `Blob` of zero length, or the
|
|
|
|
## `Hash256` type of the Keccak hash of an empty `Blob` (see constant
|
|
|
|
## `EMPTY_ROOT_HASH`.)
|
|
|
|
##
|
|
|
|
case isHash: bool
|
|
|
|
of true:
|
|
|
|
key: Hash256 ## Merkle hash tacked to a vertex
|
|
|
|
else:
|
|
|
|
blob: Blob ## Optionally encoded small node data
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
PathID* = object
|
|
|
|
## Path into the `Patricia Trie`. This is a chain of maximal 64 nibbles
|
|
|
|
## (which is 32 bytes.) In most cases, the length is 64. So the path is
|
|
|
|
## encoded as a numeric value which is often easier to handle than a
|
|
|
|
## chain of nibbles.
|
|
|
|
##
|
|
|
|
## The path ID should be kept normalised, i.e.
|
|
|
|
## * 0 <= `length` <= 64
|
|
|
|
## * the unused trailing nibbles in `pfx` ar set to `0`
|
|
|
|
##
|
|
|
|
pfx*: UInt256
|
|
|
|
length*: uint8
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# ----------
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
LeafTie* = object
|
|
|
|
## Unique access key for a leaf vertex. It identifies a root vertex
|
|
|
|
## followed by a nibble path along the `Patricia Trie` down to a leaf
|
|
|
|
## vertex. So this implies an obvious injection from the set of `LeafTie`
|
|
|
|
## objects *into* the set of `VertexID` obvious (which is typically *into*
|
|
|
|
## only, not a bijection.)
|
|
|
|
##
|
|
|
|
## Note that `LeafTie` objects have no representation in the `Aristo Trie`.
|
|
|
|
## They are used temporarily and in caches or backlog tables.
|
|
|
|
root*: VertexID ## Root ID for the sub-trie
|
2023-10-27 21:36:51 +00:00
|
|
|
path*: PathID ## Path into the `Patricia Trie`
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
HashLabel* = object
|
|
|
|
## Merkle hash key uniquely associated with a vertex ID. As hashes in a
|
|
|
|
## `Merkle Patricia Tree` are unique only on a particular sub-trie, the
|
|
|
|
## hash key is paired with the top vertex of the relevant sub-trie. This
|
|
|
|
## construction is similar to the one of a `LeafTie` object.
|
|
|
|
##
|
2023-08-07 17:45:23 +00:00
|
|
|
## Note that `HashLabel` objects have no representation in the
|
|
|
|
## `Aristo Trie`. They are used temporarily and in caches or backlog
|
|
|
|
## tables.
|
2023-06-12 18:16:03 +00:00
|
|
|
root*: VertexID ## Root ID for the sub-trie.
|
2023-12-04 20:39:26 +00:00
|
|
|
key*: HashKey ## Merkle hash or encoded small node data
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-09 12:22:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Chronicles formatters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
chronicles.formatIt(VertexID): $it
|
|
|
|
chronicles.formatIt(QueueID): $it
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func to(lid: HashKey; T: type PathID): T =
|
|
|
|
## Helper to bowrrow certain properties from `PathID`
|
|
|
|
if lid.isHash:
|
|
|
|
PathID(pfx: UInt256.fromBytesBE lid.key.data, length: 64)
|
|
|
|
elif 0 < lid.blob.len:
|
|
|
|
doAssert lid.blob.len < 32
|
|
|
|
var a32: array[32,byte]
|
|
|
|
(addr a32[0]).copyMem(unsafeAddr lid.blob[0], lid.blob.len)
|
|
|
|
PathID(pfx: UInt256.fromBytesBE a32, length: 2 * lid.blob.len.uint8)
|
|
|
|
else:
|
|
|
|
PathID()
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `VertexID` scalar data model
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `<`*(a, b: VertexID): bool {.borrow.}
|
2023-06-30 22:22:33 +00:00
|
|
|
func `<=`*(a, b: VertexID): bool {.borrow.}
|
2023-06-20 13:26:25 +00:00
|
|
|
func `==`*(a, b: VertexID): bool {.borrow.}
|
|
|
|
func cmp*(a, b: VertexID): int {.borrow.}
|
2023-08-18 19:46:55 +00:00
|
|
|
func `$`*(a: VertexID): string {.borrow.}
|
2023-06-12 13:48:47 +00:00
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
func `==`*(a: VertexID; b: static[uint]): bool = (a == VertexID(b))
|
2023-06-12 13:48:47 +00:00
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
# Scalar model extension as in `IntervalSetRef[VertexID,uint64]`
|
|
|
|
func `+`*(a: VertexID; b: uint64): VertexID = (a.uint64+b).VertexID
|
|
|
|
func `-`*(a: VertexID; b: uint64): VertexID = (a.uint64-b).VertexID
|
|
|
|
func `-`*(a, b: VertexID): uint64 = (a.uint64 - b.uint64)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `QueueID` scalar data model
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func `<`*(a, b: QueueID): bool {.borrow.}
|
|
|
|
func `<=`*(a, b: QueueID): bool {.borrow.}
|
|
|
|
func `==`*(a, b: QueueID): bool {.borrow.}
|
|
|
|
func cmp*(a, b: QueueID): int {.borrow.}
|
|
|
|
func `$`*(a: QueueID): string {.borrow.}
|
|
|
|
|
|
|
|
func `==`*(a: QueueID; b: static[uint]): bool = (a == QueueID(b))
|
|
|
|
|
|
|
|
func `+`*(a: QueueID; b: uint64): QueueID = (a.uint64+b).QueueID
|
|
|
|
func `-`*(a: QueueID; b: uint64): QueueID = (a.uint64-b).QueueID
|
|
|
|
func `-`*(a, b: QueueID): uint64 = (a.uint64 - b.uint64)
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2023-08-21 18:18:06 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `FilterID` scalar data model
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
func `<`*(a, b: FilterID): bool {.borrow.}
|
|
|
|
func `<=`*(a, b: FilterID): bool {.borrow.}
|
2023-08-21 18:18:06 +00:00
|
|
|
func `==`*(a, b: FilterID): bool {.borrow.}
|
|
|
|
func `$`*(a: FilterID): string {.borrow.}
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
func `==`*(a: FilterID; b: static[uint]): bool = (a == FilterID(b))
|
|
|
|
|
|
|
|
func `+`*(a: FilterID; b: uint64): FilterID = (a.uint64+b).FilterID
|
|
|
|
func `-`*(a: FilterID; b: uint64): FilterID = (a.uint64-b).FilterID
|
|
|
|
func `-`*(a, b: FilterID): uint64 = (a.uint64 - b.uint64)
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-10-27 21:36:51 +00:00
|
|
|
# Public helpers: `PathID` ordered scalar data model
|
2023-06-12 18:16:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func high*(_: type PathID): PathID =
|
|
|
|
## Highest possible `PathID` object for given root vertex.
|
|
|
|
PathID(pfx: high(UInt256), length: 64)
|
|
|
|
|
|
|
|
func low*(_: type PathID): PathID =
|
|
|
|
## Lowest possible `PathID` object for given root vertex.
|
|
|
|
PathID()
|
|
|
|
|
|
|
|
func next*(pid: PathID): PathID =
|
|
|
|
## Return a `PathID` object with incremented path field. This function might
|
|
|
|
## return also a modified `length` field.
|
|
|
|
##
|
|
|
|
## The function returns the argument `pid` if it is already at its
|
|
|
|
## maximum value `high(PathID)`.
|
|
|
|
if pid.pfx == 0 and pid.length < 64:
|
|
|
|
PathID(length: pid.length + 1)
|
|
|
|
elif pid.pfx < high(UInt256):
|
|
|
|
PathID(pfx: pid.pfx + 1, length: 64)
|
|
|
|
else:
|
|
|
|
pid
|
|
|
|
|
|
|
|
func prev*(pid: PathID): PathID =
|
|
|
|
## Return a `PathID` object with decremented path field. This function might
|
|
|
|
## return also a modified `length` field.
|
|
|
|
##
|
|
|
|
## The function returns the argument `pid` if it is already at its
|
|
|
|
## minimum value `low(PathID)`.
|
|
|
|
if 0 < pid.pfx:
|
|
|
|
PathID(pfx: pid.pfx - 1, length: 64)
|
|
|
|
elif 0 < pid.length:
|
|
|
|
PathID(length: pid.length - 1)
|
|
|
|
else:
|
|
|
|
pid
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func `<`*(a, b: PathID): bool =
|
|
|
|
## This function assumes that the arguments `a` and `b` are normalised
|
|
|
|
## (see `normal()`.)
|
|
|
|
a.pfx < b.pfx or (a.pfx == b.pfx and a.length < b.length)
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func `<=`*(a, b: PathID): bool =
|
|
|
|
not (b < a)
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func `==`*(a, b: PathID): bool =
|
|
|
|
## This function assumes that the arguments `a` and `b` are normalised
|
|
|
|
## (see `normal()`.)
|
|
|
|
a.pfx == b.pfx and a.length == b.length
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func cmp*(a, b: PathID): int =
|
|
|
|
if a < b: -1 elif b < a: 1 else: 0
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `HashKey` ordered scalar data model
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func len*(lid: HashKey): int =
|
|
|
|
if lid.isHash: 32 else: lid.blob.len
|
|
|
|
|
|
|
|
func fromBytes*(T: type HashKey; data: openArray[byte]): Result[T,void] =
|
|
|
|
## Write argument `data` of length 0 or between 2 and 32 bytes as a `HashKey`.
|
|
|
|
##
|
|
|
|
## A function argument `data` of length 32 is used as-is.
|
|
|
|
##
|
|
|
|
## For a function argument `data` of length between 2 and 31, the first
|
|
|
|
## byte must be the start of an RLP encoded list, i.e. `0xc0 + len` where
|
|
|
|
## where `len` is one less as the `data` length.
|
|
|
|
##
|
|
|
|
if data.len == 32:
|
|
|
|
var lid: T
|
|
|
|
lid.isHash = true
|
|
|
|
(addr lid.key.data[0]).copyMem(unsafeAddr data[0], data.len)
|
|
|
|
return ok lid
|
|
|
|
if data.len == 0:
|
|
|
|
return ok HashKey()
|
|
|
|
if 1 < data.len and data.len < 32 and data[0].int == 0xbf + data.len:
|
|
|
|
return ok T(isHash: false, blob: @data)
|
|
|
|
err()
|
|
|
|
|
|
|
|
func `<`*(a, b: HashKey): bool =
|
|
|
|
## Slow, but useful for debug sorting
|
|
|
|
a.to(PathID) < b.to(PathID)
|
|
|
|
|
|
|
|
func `==`*(a, b: HashKey): bool =
|
|
|
|
if a.isHash != b.isHash:
|
|
|
|
false
|
|
|
|
elif a.isHash:
|
|
|
|
a.key == b.key
|
|
|
|
else:
|
|
|
|
a.blob == b.blob
|
|
|
|
|
|
|
|
func cmp*(a, b: HashKey): int =
|
|
|
|
## Slow, but useful for debug sorting
|
|
|
|
if a < b: -1 elif b < a: 1 else: 0
|
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-10-27 21:36:51 +00:00
|
|
|
# Public helpers: `LeafTie` ordered scalar data model
|
2023-07-12 23:03:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func high*(_: type LeafTie; root = VertexID(1)): LeafTie =
|
|
|
|
## Highest possible `LeafTie` object for given root vertex.
|
2023-10-27 21:36:51 +00:00
|
|
|
LeafTie(root: root, path: high(PathID))
|
2023-07-12 23:03:14 +00:00
|
|
|
|
|
|
|
func low*(_: type LeafTie; root = VertexID(1)): LeafTie =
|
|
|
|
## Lowest possible `LeafTie` object for given root vertex.
|
2023-10-27 21:36:51 +00:00
|
|
|
LeafTie(root: root, path: low(PathID))
|
2023-07-12 23:03:14 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func next*(lty: LeafTie): LeafTie =
|
|
|
|
## Return a `LeafTie` object with the `next()` path field.
|
|
|
|
LeafTie(root: lty.root, path: lty.path.next)
|
2023-07-12 23:03:14 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func prev*(lty: LeafTie): LeafTie =
|
|
|
|
## Return a `LeafTie` object with the `prev()` path field.
|
|
|
|
LeafTie(root: lty.root, path: lty.path.prev)
|
2023-07-12 23:03:14 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func `<`*(a, b: LeafTie): bool =
|
|
|
|
## This function assumes that the arguments `a` and `b` are normalised
|
|
|
|
## (see `normal()`.)
|
|
|
|
a.root < b.root or (a.root == b.root and a.path < b.path)
|
|
|
|
|
|
|
|
func `==`*(a, b: LeafTie): bool =
|
|
|
|
## This function assumes that the arguments `a` and `b` are normalised
|
|
|
|
## (see `normal()`.)
|
|
|
|
a.root == b.root and a.path == b.path
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func cmp*(a, b: LeafTie): int =
|
|
|
|
## This function assumes that the arguments `a` and `b` are normalised
|
|
|
|
## (see `normal()`.)
|
|
|
|
if a < b: -1 elif a == b: 0 else: 1
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: Reversible conversions between `PathID`, `HashKey`, etc.
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(key: HashKey; T: type Blob): T =
|
2023-11-08 12:18:32 +00:00
|
|
|
## Rewrite `HashKey` argument as `Blob` type of length between 0 and 32. A
|
|
|
|
## blob of length 32 is taken as a representation of a `HashKey` type while
|
|
|
|
## samller blobs are expected to represent an RLP encoded small node.
|
|
|
|
if key.isHash:
|
|
|
|
@(key.key.data)
|
|
|
|
else:
|
|
|
|
key.blob
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func `@`*(lid: HashKey): Blob =
|
|
|
|
## Variant of `to(Blob)`
|
|
|
|
lid.to(Blob)
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func to*(pid: PathID; T: type NibblesSeq): T =
|
2023-11-08 12:18:32 +00:00
|
|
|
## Representation of a `PathID` as `NibbleSeq` (preserving full information)
|
2023-11-24 22:16:21 +00:00
|
|
|
let nibbles = pid.pfx.toBytesBE.toSeq.initNibbleRange()
|
2023-10-27 21:36:51 +00:00
|
|
|
if pid.length < 64:
|
|
|
|
nibbles.slice(0, pid.length.int)
|
|
|
|
else:
|
|
|
|
nibbles
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func to*(lid: HashKey; T: type Hash256): T =
|
|
|
|
## Returns the `Hash236` key if available, otherwise the Keccak hash of
|
|
|
|
## the `Blob` version.
|
|
|
|
if lid.isHash:
|
|
|
|
lid.key
|
|
|
|
elif 0 < lid.blob.len:
|
|
|
|
lid.blob.keccakHash
|
|
|
|
else:
|
|
|
|
EMPTY_ROOT_HASH
|
|
|
|
|
|
|
|
func to*(key: Hash256; T: type HashKey): T =
|
|
|
|
## This is an efficient version of `HashKey.fromBytes(key.data).value`, not
|
|
|
|
## to be confused with `digestTo(HashKey)`.
|
|
|
|
T(isHash: true, key: key)
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func to*(n: SomeUnsignedInt|UInt256; T: type PathID): T =
|
|
|
|
## Representation of a scalar as `PathID` (preserving full information)
|
|
|
|
T(pfx: n.u256, length: 64)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: Miscellaneous mappings
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
func digestTo*(data: openArray[byte]; T: type HashKey): T =
|
2023-11-08 12:18:32 +00:00
|
|
|
## For argument `data` with length smaller than 32, import them as-is into
|
|
|
|
## the result. Otherwise import the Keccak hash of the argument `data`.
|
|
|
|
if data.len < 32:
|
|
|
|
result.blob = @data
|
|
|
|
else:
|
|
|
|
result.isHash = true
|
|
|
|
result.key = data.keccakHash
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func normal*(a: PathID): PathID =
|
|
|
|
## Normalise path ID representation
|
|
|
|
result = a
|
|
|
|
if 64 < a.length:
|
|
|
|
result.length = 64
|
|
|
|
elif a.length < 64:
|
|
|
|
result.pfx = a.pfx and not (1.u256 shl (4 * (64 - a.length))) - 1.u256
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `Tables` and `Rlp` support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func hash*(a: PathID): Hash =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Table/KeyedQueue mixin
|
2023-10-27 21:36:51 +00:00
|
|
|
var h: Hash = 0
|
|
|
|
h = h !& a.pfx.toBytesBE.hash
|
|
|
|
h = h !& a.length.hash
|
|
|
|
!$h
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func hash*(a: HashKey): Hash =
|
|
|
|
## Table/KeyedQueue mixin
|
|
|
|
var h: Hash = 0
|
|
|
|
if a.isHash:
|
|
|
|
h = h !& a.key.hash
|
|
|
|
else:
|
|
|
|
h = h !& a.blob.hash
|
|
|
|
!$h
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func hash*(lbl: HashLabel): Hash =
|
|
|
|
## Table/KeyedQueue/HashSet mixin
|
|
|
|
var h: Hash = 0
|
|
|
|
h = h !& lbl.root.hash
|
|
|
|
h = h !& lbl.key.hash
|
|
|
|
!$h
|
2023-06-12 18:16:03 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Miscellaneous helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func `$`*(key: Hash256): string =
|
|
|
|
let w = UInt256.fromBytesBE key.data
|
2023-10-27 21:36:51 +00:00
|
|
|
if w == high(UInt256):
|
2023-06-12 18:16:03 +00:00
|
|
|
"2^256-1"
|
2023-10-27 21:36:51 +00:00
|
|
|
elif w == 0.u256:
|
2023-06-12 18:16:03 +00:00
|
|
|
"0"
|
2023-10-27 21:36:51 +00:00
|
|
|
elif w == 2.u256.pow 255:
|
2023-06-12 18:16:03 +00:00
|
|
|
"2^255" # 800...
|
2023-10-27 21:36:51 +00:00
|
|
|
elif w == 2.u256.pow 254:
|
2023-06-12 18:16:03 +00:00
|
|
|
"2^254" # 400..
|
2023-10-27 21:36:51 +00:00
|
|
|
elif w == 2.u256.pow 253:
|
2023-06-12 18:16:03 +00:00
|
|
|
"2^253" # 200...
|
2023-10-27 21:36:51 +00:00
|
|
|
elif w == 2.u256.pow 251:
|
2023-06-12 18:16:03 +00:00
|
|
|
"2^252" # 100...
|
|
|
|
else:
|
2023-10-27 21:36:51 +00:00
|
|
|
w.toHex
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-11-08 16:52:25 +00:00
|
|
|
func `$`*(key: HashKey): string =
|
|
|
|
if key.isHash:
|
|
|
|
$key.key
|
|
|
|
else:
|
|
|
|
key.blob.toHex & "[#" & $key.blob.len & "]"
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func `$`*(a: PathID): string =
|
|
|
|
if a.pfx != 0:
|
2023-11-08 12:18:32 +00:00
|
|
|
var dgts = $a.pfx.toHex
|
|
|
|
if a.length < 64:
|
|
|
|
dgts = dgts[0 ..< a.length]
|
|
|
|
result = dgts.strip(
|
|
|
|
leading=true, trailing=false, chars={'0'})
|
2023-10-27 21:36:51 +00:00
|
|
|
elif a.length != 0:
|
|
|
|
result = "0"
|
|
|
|
if a.length < 64:
|
|
|
|
result &= "(" & $a.length & ")"
|
|
|
|
|
|
|
|
func `$`*(a: LeafTie): string =
|
|
|
|
if a.root != 0:
|
|
|
|
result = ($a.root.uint64.toHex).strip(
|
2023-11-08 12:18:32 +00:00
|
|
|
leading=true, trailing=false, chars={'0'})
|
2023-10-27 21:36:51 +00:00
|
|
|
else:
|
|
|
|
result = "0"
|
|
|
|
result &= ":" & $a.path
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|