2023-05-30 11:47: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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-06-02 10:04:29 +00:00
|
|
|
std/sequtils,
|
2023-05-30 11:47:47 +00:00
|
|
|
eth/[common, trie/nibbles],
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-06-20 13:26:25 +00:00
|
|
|
./aristo_desc
|
2023-05-30 11:47:47 +00:00
|
|
|
|
|
|
|
# Info snippet (just a reminder to keep somewhere)
|
|
|
|
#
|
|
|
|
# Extension of a compact encoded as prefixed sequence of nibbles (i.e.
|
|
|
|
# half bytes with 4 bits.)
|
|
|
|
#
|
|
|
|
# pfx | bits | vertex type | layout
|
|
|
|
# ----+ -----+-------------+----------------------------------------
|
|
|
|
# 0 | 0000 | extension | @[<pfx, ignored>, nibble-pair, ..]
|
|
|
|
# 1 | 0001 | extension | @[<pfx, first-nibble>, nibble-pair, ..]
|
|
|
|
# 2 | 0010 | leaf | @[<pfx, ignored>, nibble-pair, ..]
|
|
|
|
# 3 | 0011 | leaf | @[<pfx, first-nibble>, nibble-pair, ..]
|
|
|
|
#
|
|
|
|
# where the `ignored` part is typically expected a zero nibble.
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathPfxPad*(pfx: NibblesSeq; dblNibble: static[byte]): NibblesSeq
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathAsBlob*(keyOrTag: HashKey|PathID): Blob =
|
|
|
|
keyOrTag.to(NibblesSeq).hexPrefixEncode(isLeaf=true)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathToKey*(partPath: NibblesSeq): Result[HashKey,AristoError] =
|
2023-05-30 11:47:47 +00:00
|
|
|
var key: ByteArray32
|
|
|
|
if partPath.len == 64:
|
|
|
|
# Trailing dummy nibbles (aka no nibbles) force a nibble seq reorg
|
|
|
|
let path = (partPath & EmptyNibbleSeq).getBytes()
|
|
|
|
(addr key[0]).copyMem(unsafeAddr path[0], 32)
|
2023-06-12 18:16:03 +00:00
|
|
|
return ok(key.HashKey)
|
2023-05-30 11:47:47 +00:00
|
|
|
err(PathExpected64Nibbles)
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathToKey*(
|
2023-09-15 15:23:53 +00:00
|
|
|
partPath: openArray[byte];
|
|
|
|
): Result[HashKey,AristoError] =
|
2023-05-30 11:47:47 +00:00
|
|
|
let (isLeaf,pathSegment) = partPath.hexPrefixDecode
|
|
|
|
if isleaf:
|
|
|
|
return pathSegment.pathToKey()
|
|
|
|
err(PathExpectedLeaf)
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathToTag*(partPath: NibblesSeq): Result[PathID,AristoError] =
|
|
|
|
## Nickname `tag` for `PathID`
|
|
|
|
if partPath.len <= 64:
|
|
|
|
return ok PathID(
|
|
|
|
pfx: UInt256.fromBytesBE partPath.pathPfxPad(0).getBytes(),
|
|
|
|
length: partPath.len.uint8)
|
|
|
|
err(PathAtMost64Nibbles)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
# --------------------
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathPfxPad*(pfx: NibblesSeq; dblNibble: static[byte]): NibblesSeq =
|
2023-06-02 10:04:29 +00:00
|
|
|
## Extend (or cut) the argument nibbles sequence `pfx` for generating a
|
2023-06-09 11:17:37 +00:00
|
|
|
## `NibblesSeq` with exactly 64 nibbles, the equivalent of a path key.
|
2023-06-02 10:04:29 +00:00
|
|
|
##
|
|
|
|
## This function must be handled with some care regarding a meaningful value
|
|
|
|
## for the `dblNibble` argument. Currently, only static values `0` and `255`
|
|
|
|
## are allowed for padding. This is checked at compile time.
|
|
|
|
static:
|
|
|
|
doAssert dblNibble == 0 or dblNibble == 255
|
|
|
|
|
|
|
|
let padLen = 64 - pfx.len
|
|
|
|
if 0 <= padLen:
|
2023-06-09 11:17:37 +00:00
|
|
|
result = pfx & dblNibble.repeat(padlen div 2).mapIt(it.byte).initNibbleRange
|
2023-06-02 10:04:29 +00:00
|
|
|
if (padLen and 1) == 1:
|
2023-06-09 11:17:37 +00:00
|
|
|
result = result & @[dblNibble.byte].initNibbleRange.slice(1)
|
2023-06-02 10:04:29 +00:00
|
|
|
else:
|
|
|
|
let nope = seq[byte].default.initNibbleRange
|
2023-06-09 11:17:37 +00:00
|
|
|
result = pfx.slice(0,64) & nope # nope forces re-alignment
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
func pathPfxPadKey*(pfx: NibblesSeq; dblNibble: static[byte]): HashKey =
|
2023-06-09 11:17:37 +00:00
|
|
|
## Variant of `pathPfxPad()`.
|
|
|
|
##
|
|
|
|
## Extend (or cut) the argument nibbles sequence `pfx` for generating a
|
2023-06-12 18:16:03 +00:00
|
|
|
## `HashKey`.
|
2023-06-09 11:17:37 +00:00
|
|
|
let bytes = pfx.pathPfxPad(dblNibble).getBytes
|
2023-06-02 10:04:29 +00:00
|
|
|
(addr result.ByteArray32[0]).copyMem(unsafeAddr bytes[0], bytes.len)
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|