2023-06-12 13:48:47 +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 -- 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-06-20 13:26:25 +00:00
|
|
|
stint
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
type
|
2023-06-20 13:26:25 +00:00
|
|
|
ByteArray32* = array[32,byte]
|
|
|
|
## Used for 32 byte hash components repurposed as Merkle hash labels.
|
|
|
|
|
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`.
|
|
|
|
|
|
|
|
HashID* = distinct UInt256
|
|
|
|
## Variant of a `Hash256` object that can be used in a order relation
|
|
|
|
## (i.e. it can be sorted.) Among temporary conversions for sorting, the
|
|
|
|
## `HashID` type is consistently used for addressing leaf vertices (see
|
|
|
|
## below `LeafTie`.)
|
|
|
|
|
|
|
|
HashKey* = distinct ByteArray32
|
|
|
|
## Dedicated `Hash256` object variant that is used for labelling the
|
|
|
|
## vertices of the `Patricia Trie` in order to make it a
|
|
|
|
## `Merkle Patricia Tree`.
|
|
|
|
|
|
|
|
# ----------
|
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-06-12 18:16:03 +00:00
|
|
|
path*: HashID ## 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.
|
|
|
|
key*: HashKey ## Merkle hash tacked to a vertex.
|
|
|
|
|
|
|
|
static:
|
|
|
|
# Not that there is no doubt about this ...
|
|
|
|
doAssert HashKey.default.ByteArray32.initNibbleRange.len == 64
|
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
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `HashID` scalar data model
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func u256*(lp: HashID): UInt256 = lp.UInt256
|
|
|
|
func low*(T: type HashID): T = low(UInt256).T
|
|
|
|
func high*(T: type HashID): T = high(UInt256).T
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `+`*(a: HashID; b: UInt256): HashID = (a.u256+b).HashID
|
|
|
|
func `-`*(a: HashID; b: UInt256): HashID = (a.u256-b).HashID
|
|
|
|
func `-`*(a, b: HashID): UInt256 = (a.u256 - b.u256)
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `==`*(a, b: HashID): bool = a.u256 == b.u256
|
|
|
|
func `<=`*(a, b: HashID): bool = a.u256 <= b.u256
|
|
|
|
func `<`*(a, b: HashID): bool = a.u256 < b.u256
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func cmp*(x, y: HashID): int = cmp(x.UInt256, y.UInt256)
|
2023-06-12 18:16:03 +00:00
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `LeafTie`
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func high*(_: type LeafTie; root = VertexID(1)): LeafTie =
|
|
|
|
## Highest possible `LeafTie` object for given root vertex.
|
|
|
|
LeafTie(root: root, path: high(HashID))
|
|
|
|
|
|
|
|
func low*(_: type LeafTie; root = VertexID(1)): LeafTie =
|
|
|
|
## Lowest possible `LeafTie` object for given root vertex.
|
|
|
|
LeafTie(root: root, path: low(HashID))
|
|
|
|
|
|
|
|
func `+`*(lty: LeafTie, n: int): LeafTie =
|
|
|
|
## Return a `LeafTie` object with incremented path field. This function
|
|
|
|
## will not check for a path field overflow. Neither it will verify that
|
|
|
|
## the argument `n` is non-negative.
|
|
|
|
LeafTie(root: lty.root, path: HashID(lty.path.u256 + n.u256))
|
|
|
|
|
|
|
|
func `-`*(lty: LeafTie, n: int): LeafTie =
|
|
|
|
## Return a `LeafTie` object with decremented path field. This function
|
|
|
|
## will not check for a path field underflow. Neither it will verify that
|
|
|
|
## the argument `n` is non-negative.
|
|
|
|
LeafTie(root: lty.root, path: HashID(lty.path.u256 - n.u256))
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: Conversions between `HashID`, `HashKey`, `Hash256`
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(hid: HashID; T: type Hash256): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
result.data = hid.UInt256.toBytesBE
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
func to*(hid: HashID; T: type HashKey): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
hid.UInt256.toBytesBE.T
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(key: HashKey; T: type HashID): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
UInt256.fromBytesBE(key.ByteArray32).T
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(key: HashKey; T: type Hash256): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
T(data: ByteArray32(key))
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(hash: Hash256; T: type HashKey): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
hash.data.T
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(key: Hash256; T: type HashID): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
key.data.HashKey.to(T)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: Miscellaneous mappings
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(key: HashKey; T: type Blob): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Representation of a `HashKey` as `Blob` (preserving full information)
|
|
|
|
key.ByteArray32.toSeq
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
func to*(hid: HashID; T: type Blob): T =
|
|
|
|
## Representation of a `HashID` as `Blob` (preserving full information)
|
|
|
|
hid.UInt256.toBytesBE.toSeq
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(key: HashKey; T: type NibblesSeq): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Representation of a `HashKey` as `NibbleSeq` (preserving full information)
|
|
|
|
key.ByteArray32.initNibbleRange()
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(hid: HashID; T: type NibblesSeq): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Representation of a `HashKey` as `NibbleSeq` (preserving full information)
|
|
|
|
ByteArray32(hid.to(HashKey)).initNibbleRange()
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func to*(n: SomeUnsignedInt|UInt256; T: type HashID): T =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Representation of a scalar as `HashID` (preserving full information)
|
|
|
|
n.u256.T
|
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
func digestTo*(data: openArray[byte]; T: type HashKey): T =
|
|
|
|
## Keccak hash of a `Blob` like argument, represented as a `HashKey`
|
2023-06-12 18:16:03 +00:00
|
|
|
keccakHash(data).data.T
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `Tables` and `Rlp` support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func hash*(a: HashID): Hash =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Table/KeyedQueue mixin
|
|
|
|
a.to(HashKey).ByteArray32.hash
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func hash*(a: HashKey): Hash =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Table/KeyedQueue mixin
|
|
|
|
a.ByteArray32.hash
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `==`*(a, b: HashKey): bool =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Table/KeyedQueue mixin
|
|
|
|
a.ByteArray32 == b.ByteArray32
|
|
|
|
|
2023-07-05 20:27:48 +00:00
|
|
|
func read*[T: HashID|HashKey](
|
|
|
|
rlp: var Rlp;
|
|
|
|
W: type T;
|
|
|
|
): T
|
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2023-06-12 18:16:03 +00:00
|
|
|
rlp.read(Hash256).to(T)
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func append*(writer: var RlpWriter, val: HashID|HashKey) =
|
2023-06-12 18:16:03 +00:00
|
|
|
writer.append(val.to(Hash256))
|
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers: `LeafTie` scalar data model
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `<`*(a, b: LeafTie): bool =
|
2023-06-12 13:48:47 +00:00
|
|
|
a.root < b.root or (a.root == b.root and a.path < b.path)
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `==`*(a, b: LeafTie): bool =
|
2023-06-12 13:48:47 +00:00
|
|
|
a.root == b.root and a.path == b.path
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func cmp*(a, b: LeafTie): int =
|
2023-06-12 13:48:47 +00:00
|
|
|
if a < b: -1 elif a == b: 0 else: 1
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `$`*(a: LeafTie): string =
|
2023-06-12 13:48:47 +00:00
|
|
|
let w = $a.root.uint64.toHex & ":" & $a.path.Uint256.toHex
|
|
|
|
w.strip(leading=true, trailing=false, chars={'0'}).toLowerAscii
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Miscellaneous helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `$`*(hid: HashID): string =
|
2023-06-12 18:16:03 +00:00
|
|
|
if hid == high(HashID):
|
|
|
|
"2^256-1"
|
|
|
|
elif hid == 0.u256.HashID:
|
|
|
|
"0"
|
|
|
|
elif hid == 2.u256.pow(255).HashID:
|
|
|
|
"2^255" # 800...
|
|
|
|
elif hid == 2.u256.pow(254).HashID:
|
|
|
|
"2^254" # 400..
|
|
|
|
elif hid == 2.u256.pow(253).HashID:
|
|
|
|
"2^253" # 200...
|
|
|
|
elif hid == 2.u256.pow(251).HashID:
|
|
|
|
"2^252" # 100...
|
|
|
|
else:
|
|
|
|
hid.UInt256.toHex
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
func `$`*(key: HashKey): string =
|
2023-06-12 18:16:03 +00:00
|
|
|
$key.to(HashID)
|
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|