2023-06-02 10:04: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 -- Patricia Trie traversal
|
|
|
|
## ====================================
|
|
|
|
##
|
|
|
|
## This module provides tools to visit leaf vertices in a monotone order,
|
|
|
|
## increasing or decreasing. These tools are intended for
|
|
|
|
## * boundary proof verification
|
|
|
|
## * step along leaf vertices in sorted order
|
|
|
|
## * tree/trie consistency checks when debugging
|
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/tables,
|
|
|
|
eth/[common, trie/nibbles],
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-06-12 13:48:47 +00:00
|
|
|
"."/[aristo_desc, aristo_get, aristo_hike, aristo_path]
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `<=`(a, b: NibblesSeq): bool =
|
|
|
|
## Compare nibbles, different lengths are padded to the right with zeros
|
|
|
|
let abMin = min(a.len, b.len)
|
|
|
|
for n in 0 ..< abMin:
|
|
|
|
if a[n] < b[n]:
|
|
|
|
return true
|
|
|
|
if b[n] < a[n]:
|
|
|
|
return false
|
|
|
|
# otherwise a[n] == b[n]
|
|
|
|
|
|
|
|
# Assuming zero for missing entries
|
|
|
|
if b.len < a.len:
|
|
|
|
for n in abMin + 1 ..< a.len:
|
|
|
|
if 0 < a[n]:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
|
|
|
proc `<`(a, b: NibblesSeq): bool =
|
|
|
|
not (b <= a)
|
|
|
|
|
|
|
|
# ------------------
|
|
|
|
|
|
|
|
proc branchNibbleMin*(vtx: VertexRef; minInx: int8): int8 =
|
|
|
|
## Find the least index for an argument branch `vtx` link with index
|
|
|
|
## greater or equal the argument `nibble`.
|
|
|
|
if vtx.vType == Branch:
|
|
|
|
for n in minInx .. 15:
|
2023-06-12 13:48:47 +00:00
|
|
|
if vtx.bVid[n].isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
return n
|
|
|
|
-1
|
|
|
|
|
|
|
|
proc branchNibbleMax*(vtx: VertexRef; maxInx: int8): int8 =
|
|
|
|
## Find the greatest index for an argument branch `vtx` link with index
|
|
|
|
## less or equal the argument `nibble`.
|
|
|
|
if vtx.vType == Branch:
|
|
|
|
for n in maxInx.countDown 0:
|
2023-06-12 13:48:47 +00:00
|
|
|
if vtx.bVid[n].isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
return n
|
|
|
|
-1
|
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
# ------------------
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
proc toLeafTiePayload(hike: Hike): (LeafTie,PayloadRef) =
|
2023-07-12 23:03:14 +00:00
|
|
|
## Shortcut for iterators. This function will gloriously crash unless the
|
|
|
|
## `hike` argument is complete.
|
|
|
|
(LeafTie(root: hike.root, path: hike.to(NibblesSeq).pathToTag.value),
|
|
|
|
hike.legs[^1].wp.vtx.lData)
|
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc complete(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-06-02 10:04:29 +00:00
|
|
|
vid: VertexID; # Start ID
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-02 10:04:29 +00:00
|
|
|
hikeLenMax: static[int]; # Beware of loops (if any)
|
|
|
|
doLeast: static[bool]; # Direction: *least* or *most*
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 10:04:29 +00:00
|
|
|
## Extend `hike` using least or last vertex without recursion.
|
2023-06-30 22:22:33 +00:00
|
|
|
if not vid.isValid:
|
|
|
|
return err((VertexID(0),NearbyVidInvalid))
|
2023-06-02 10:04:29 +00:00
|
|
|
var
|
|
|
|
vid = vid
|
|
|
|
vtx = db.getVtx vid
|
|
|
|
uHike = Hike(root: hike.root, legs: hike.legs)
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vtx.isValid:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,GetVtxNotFound))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
while uHike.legs.len < hikeLenMax:
|
|
|
|
var leg = Leg(wp: VidVtxPair(vid: vid, vtx: vtx), nibble: -1)
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
uHike.legs.add leg
|
2023-06-30 22:22:33 +00:00
|
|
|
return ok(uHike) # done
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
of Extension:
|
|
|
|
vid = vtx.eVid
|
2023-06-12 13:48:47 +00:00
|
|
|
if vid.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
vtx = db.getVtx vid
|
2023-06-12 13:48:47 +00:00
|
|
|
if vtx.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
uHike.legs.add leg
|
|
|
|
continue
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,NearbyExtensionError)) # Oops, no way
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
of Branch:
|
|
|
|
when doLeast:
|
|
|
|
leg.nibble = vtx.branchNibbleMin 0
|
|
|
|
else:
|
|
|
|
leg.nibble = vtx.branchNibbleMax 15
|
|
|
|
if 0 <= leg.nibble:
|
|
|
|
vid = vtx.bVid[leg.nibble]
|
|
|
|
vtx = db.getVtx vid
|
2023-06-12 13:48:47 +00:00
|
|
|
if vtx.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
uHike.legs.add leg
|
|
|
|
continue
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((leg.wp.vid,NearbyBranchError)) # Oops, no way
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
err((VertexID(0),NearbyNestingTooDeep))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc zeroAdjust(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-02 10:04:29 +00:00
|
|
|
doLeast: static[bool]; # Direction: *least* or *most*
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Adjust empty argument path to the first vertex entry to the right. Ths
|
2023-06-02 10:04:29 +00:00
|
|
|
## applies is the argument `hike` is before the first entry in the database.
|
|
|
|
## The result is a hike which is aligned with the first entry.
|
|
|
|
proc accept(p: Hike; pfx: NibblesSeq): bool =
|
|
|
|
when doLeast:
|
|
|
|
p.tail <= pfx
|
|
|
|
else:
|
|
|
|
pfx <= p.tail
|
|
|
|
|
|
|
|
proc branchBorderNibble(w: VertexRef; n: int8): int8 =
|
|
|
|
when doLeast:
|
|
|
|
w.branchNibbleMin n
|
|
|
|
else:
|
|
|
|
w.branchNibbleMax n
|
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
proc toHike(pfx: NibblesSeq, root: VertexID, db: AristoDbRef): Hike =
|
2023-06-02 10:04:29 +00:00
|
|
|
when doLeast:
|
2023-09-15 15:23:53 +00:00
|
|
|
pfx.pathPfxPad(0).hikeUp(root, db).to(Hike)
|
2023-06-02 10:04:29 +00:00
|
|
|
else:
|
2023-09-15 15:23:53 +00:00
|
|
|
pfx.pathPfxPad(255).hikeUp(root, db).to(Hike)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
if 0 < hike.legs.len:
|
2023-06-30 22:22:33 +00:00
|
|
|
return ok(hike)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
let root = db.getVtx hike.root
|
2023-06-12 13:48:47 +00:00
|
|
|
if root.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
block fail:
|
|
|
|
var pfx: NibblesSeq
|
|
|
|
case root.vType:
|
|
|
|
of Branch:
|
|
|
|
# Find first non-dangling link and assign it
|
2023-10-27 21:36:51 +00:00
|
|
|
let nibbleID = block:
|
|
|
|
when doLeast:
|
|
|
|
if hike.tail.len == 0: 0i8
|
|
|
|
else: hike.tail[0].int8
|
|
|
|
else:
|
|
|
|
if hike.tail.len == 0:
|
|
|
|
break fail
|
|
|
|
hike.tail[0].int8
|
|
|
|
let n = root.branchBorderNibble nibbleID
|
2023-06-02 10:04:29 +00:00
|
|
|
if n < 0:
|
|
|
|
# Before or after the database range
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((hike.root,NearbyBeyondRange))
|
2023-06-02 10:04:29 +00:00
|
|
|
pfx = @[n.byte].initNibbleRange.slice(1)
|
|
|
|
|
|
|
|
of Extension:
|
|
|
|
let ePfx = root.ePfx
|
2023-06-12 18:16:03 +00:00
|
|
|
# Must be followed by a branch vertex
|
2023-10-27 21:36:51 +00:00
|
|
|
if not hike.accept ePfx:
|
2023-06-02 10:04:29 +00:00
|
|
|
break fail
|
|
|
|
let vtx = db.getVtx root.eVid
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vtx.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
break fail
|
|
|
|
pfx = ePfx
|
|
|
|
|
|
|
|
of Leaf:
|
|
|
|
pfx = root.lPfx
|
2023-10-27 21:36:51 +00:00
|
|
|
if not hike.accept pfx:
|
2023-06-02 10:04:29 +00:00
|
|
|
# Before or after the database range
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((hike.root,NearbyBeyondRange))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
var newHike = pfx.toHike(hike.root, db)
|
|
|
|
if 0 < newHike.legs.len:
|
2023-06-30 22:22:33 +00:00
|
|
|
return ok(newHike)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
err((VertexID(0),NearbyEmptyHike))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc finalise(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-02 19:21:46 +00:00
|
|
|
moveRight: static[bool]; # Direction of next vertex
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 10:04:29 +00:00
|
|
|
## Handle some pathological cases after main processing failed
|
|
|
|
proc beyond(p: Hike; pfx: NibblesSeq): bool =
|
|
|
|
when moveRight:
|
|
|
|
pfx < p.tail
|
|
|
|
else:
|
|
|
|
p.tail < pfx
|
|
|
|
|
|
|
|
proc branchBorderNibble(w: VertexRef): int8 =
|
|
|
|
when moveRight:
|
|
|
|
w.branchNibbleMax 15
|
|
|
|
else:
|
|
|
|
w.branchNibbleMin 0
|
|
|
|
|
|
|
|
# Just for completeness (this case should have been handled, already)
|
|
|
|
if hike.legs.len == 0:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((VertexID(0),NearbyEmptyHike))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
# Check whether the path is beyond the database range
|
|
|
|
if 0 < hike.tail.len: # nothing to compare against, otherwise
|
|
|
|
let top = hike.legs[^1]
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# Note that only a `Branch` vertices has a non-zero nibble
|
2023-06-02 10:04:29 +00:00
|
|
|
if 0 <= top.nibble and top.nibble == top.wp.vtx.branchBorderNibble:
|
2023-06-12 18:16:03 +00:00
|
|
|
# Check the following up vertex
|
2023-06-30 22:22:33 +00:00
|
|
|
let
|
|
|
|
vid = top.wp.vtx.bVid[top.nibble]
|
|
|
|
vtx = db.getVtx vid
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vtx.isValid:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,NearbyDanglingLink))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
var pfx: NibblesSeq
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
pfx = vtx.lPfx
|
|
|
|
of Extension:
|
|
|
|
pfx = vtx.ePfx
|
|
|
|
of Branch:
|
|
|
|
pfx = @[vtx.branchBorderNibble.byte].initNibbleRange.slice(1)
|
|
|
|
if hike.beyond pfx:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,NearbyBeyondRange))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
# Pathological cases
|
|
|
|
# * finalise right: nfffff.. for n < f or
|
|
|
|
# * finalise left: n00000.. for 0 < n
|
|
|
|
if hike.legs[0].wp.vtx.vType == Branch or
|
|
|
|
(1 < hike.legs.len and hike.legs[1].wp.vtx.vType == Branch):
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((VertexID(0),NearbyFailed)) # no more vertices
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
err((hike.legs[^1].wp.vid,NearbyUnexpectedVtx)) # error
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc nearbyNext(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-02 10:04:29 +00:00
|
|
|
hikeLenMax: static[int]; # Beware of loops (if any)
|
2023-06-02 19:21:46 +00:00
|
|
|
moveRight: static[bool]; # Direction of next vertex
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 10:04:29 +00:00
|
|
|
## Unified implementation of `nearbyRight()` and `nearbyLeft()`.
|
|
|
|
proc accept(nibble: int8): bool =
|
|
|
|
## Accept `nibble` unless on boundaty dependent on `moveRight`
|
|
|
|
when moveRight:
|
|
|
|
nibble < 15
|
|
|
|
else:
|
|
|
|
0 < nibble
|
|
|
|
|
|
|
|
proc accept(p: Hike; pfx: NibblesSeq): bool =
|
|
|
|
when moveRight:
|
|
|
|
p.tail <= pfx
|
|
|
|
else:
|
|
|
|
pfx <= p.tail
|
|
|
|
|
|
|
|
proc branchNibbleNext(w: VertexRef; n: int8): int8 =
|
|
|
|
when moveRight:
|
|
|
|
w.branchNibbleMin(n + 1)
|
|
|
|
else:
|
|
|
|
w.branchNibbleMax(n - 1)
|
|
|
|
|
|
|
|
# Some easy cases
|
2023-09-11 20:38:49 +00:00
|
|
|
let hike = ? hike.zeroAdjust(db, doLeast=moveRight)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
if hike.legs[^1].wp.vtx.vType == Extension:
|
|
|
|
let vid = hike.legs[^1].wp.vtx.eVid
|
|
|
|
return hike.complete(vid, db, hikeLenMax, doLeast=moveRight)
|
|
|
|
|
|
|
|
var
|
|
|
|
uHike = hike
|
|
|
|
start = true
|
|
|
|
while 0 < uHike.legs.len:
|
|
|
|
let top = uHike.legs[^1]
|
|
|
|
case top.wp.vtx.vType:
|
|
|
|
of Leaf:
|
2023-06-30 22:22:33 +00:00
|
|
|
return ok(uHike)
|
2023-06-02 10:04:29 +00:00
|
|
|
of Branch:
|
|
|
|
if top.nibble < 0 or uHike.tail.len == 0:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((top.wp.vid,NearbyUnexpectedVtx))
|
2023-06-02 10:04:29 +00:00
|
|
|
of Extension:
|
|
|
|
uHike.tail = top.wp.vtx.ePfx & uHike.tail
|
|
|
|
uHike.legs.setLen(uHike.legs.len - 1)
|
|
|
|
continue
|
|
|
|
|
|
|
|
var
|
|
|
|
step = top
|
|
|
|
let
|
|
|
|
uHikeLen = uHike.legs.len # in case of backtracking
|
|
|
|
uHikeTail = uHike.tail # in case of backtracking
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
# Look ahead checking next vertex
|
2023-06-02 10:04:29 +00:00
|
|
|
if start:
|
|
|
|
let vid = top.wp.vtx.bVid[top.nibble]
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vid.isValid:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((top.wp.vid,NearbyDanglingLink)) # error
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
let vtx = db.getVtx vid
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vtx.isValid:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,GetVtxNotFound)) # error
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
|
|
|
if uHike.accept vtx.lPfx:
|
|
|
|
return uHike.complete(vid, db, hikeLenMax, doLeast=moveRight)
|
|
|
|
of Extension:
|
|
|
|
if uHike.accept vtx.ePfx:
|
|
|
|
return uHike.complete(vid, db, hikeLenMax, doLeast=moveRight)
|
|
|
|
of Branch:
|
|
|
|
let nibble = uHike.tail[0].int8
|
|
|
|
if start and accept nibble:
|
2023-06-12 18:16:03 +00:00
|
|
|
# Step down and complete with a branch link on the child vertex
|
2023-06-02 10:04:29 +00:00
|
|
|
step = Leg(wp: VidVtxPair(vid: vid, vtx: vtx), nibble: nibble)
|
|
|
|
uHike.legs.add step
|
|
|
|
|
|
|
|
# Find the next item to the right/left of the current top entry
|
|
|
|
let n = step.wp.vtx.branchNibbleNext step.nibble
|
|
|
|
if 0 <= n:
|
|
|
|
uHike.legs[^1].nibble = n
|
|
|
|
return uHike.complete(
|
|
|
|
step.wp.vtx.bVid[n], db, hikeLenMax, doLeast=moveRight)
|
|
|
|
|
|
|
|
if start:
|
|
|
|
# Retry without look ahead
|
|
|
|
start = false
|
|
|
|
|
|
|
|
# Restore `uPath` (pop temporary extra step)
|
|
|
|
if uHikeLen < uHike.legs.len:
|
|
|
|
uHike.legs.setLen(uHikeLen)
|
|
|
|
uHike.tail = uHikeTail
|
|
|
|
else:
|
2023-06-12 18:16:03 +00:00
|
|
|
# Pop current `Branch` vertex on top and append nibble to `tail`
|
2023-06-02 10:04:29 +00:00
|
|
|
uHike.tail = @[top.nibble.byte].initNibbleRange.slice(1) & uHike.tail
|
|
|
|
uHike.legs.setLen(uHike.legs.len - 1)
|
|
|
|
# End while
|
|
|
|
|
|
|
|
# Handle some pathological cases
|
2023-06-30 22:22:33 +00:00
|
|
|
hike.finalise(db, moveRight)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc nearbyNextLeafTie(
|
2023-06-12 13:48:47 +00:00
|
|
|
lty: LeafTie; # Some `Patricia Trie` path
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-02 10:04:29 +00:00
|
|
|
hikeLenMax: static[int]; # Beware of loops (if any)
|
2023-06-09 11:17:37 +00:00
|
|
|
moveRight:static[bool]; # Direction of next vertex
|
2023-10-27 21:36:51 +00:00
|
|
|
): Result[PathID,(VertexID,AristoError)] =
|
2023-09-11 20:38:49 +00:00
|
|
|
## Variant of `nearbyNext()`, convenience wrapper
|
2023-09-15 15:23:53 +00:00
|
|
|
let hike = ? lty.hikeUp(db).to(Hike).nearbyNext(db, hikeLenMax, moveRight)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
if 0 < hike.legs.len:
|
|
|
|
if hike.legs[^1].wp.vtx.vType != Leaf:
|
|
|
|
return err((hike.legs[^1].wp.vid,NearbyLeafExpected))
|
2023-06-02 10:04:29 +00:00
|
|
|
let rc = hike.legsTo(NibblesSeq).pathToKey
|
|
|
|
if rc.isOk:
|
2023-10-27 21:36:51 +00:00
|
|
|
return ok rc.value.to(PathID)
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((VertexID(0),rc.error))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
err((VertexID(0),NearbyLeafExpected))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, moving and right boundary proof
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc right*(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Extends the maximally extended argument vertices `hike` to the right (i.e.
|
2023-06-02 10:04:29 +00:00
|
|
|
## with non-decreasing path value). This function does not backtrack if
|
|
|
|
## there are dangling links in between. It will return an error in that case.
|
|
|
|
##
|
2023-06-12 18:16:03 +00:00
|
|
|
## If there is no more leaf vertices to the right of the argument `hike`, the
|
2023-06-02 10:04:29 +00:00
|
|
|
## particular error code `NearbyBeyondRange` is returned.
|
|
|
|
##
|
|
|
|
## This code is intended to be used for verifying a left-bound proof to
|
2023-06-12 18:16:03 +00:00
|
|
|
## verify that there is no leaf vertex *right* of a boundary path value.
|
2023-06-02 10:04:29 +00:00
|
|
|
hike.nearbyNext(db, 64, moveRight=true)
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc right*(
|
2023-06-12 13:48:47 +00:00
|
|
|
lty: LeafTie; # Some `Patricia Trie` path
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[LeafTie,(VertexID,AristoError)] =
|
2023-10-27 21:36:51 +00:00
|
|
|
## Variant of `nearbyRight()` working with a `LeafTie` argument instead
|
2023-06-02 10:04:29 +00:00
|
|
|
## of a `Hike`.
|
2023-09-11 20:38:49 +00:00
|
|
|
ok LeafTie(
|
|
|
|
root: lty.root,
|
|
|
|
path: ? lty.nearbyNextLeafTie(db, 64, moveRight=true))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
iterator right*(
|
|
|
|
db: AristoDbRef; # Database layer
|
|
|
|
start = low(LeafTie); # Before or at first value
|
|
|
|
): (LeafTie,PayloadRef) =
|
|
|
|
## Traverse the sub-trie implied by the argument `start` with increasing
|
|
|
|
## order.
|
|
|
|
var
|
2023-09-15 15:23:53 +00:00
|
|
|
hike = start.hikeUp(db).to(Hike)
|
2023-07-12 23:03:14 +00:00
|
|
|
rc = hike.right db
|
|
|
|
while rc.isOK:
|
|
|
|
hike = rc.value
|
2023-10-27 21:36:51 +00:00
|
|
|
let (key, pyl) = hike.toLeafTiePayload
|
2023-07-12 23:03:14 +00:00
|
|
|
yield (key, pyl)
|
2023-10-27 21:36:51 +00:00
|
|
|
if high(PathID) <= key.path:
|
2023-07-12 23:03:14 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Increment `key` by one and update `hike`. In many cases, the current
|
|
|
|
# `hike` can be modified and re-used which saves some database lookups.
|
2023-10-27 21:36:51 +00:00
|
|
|
block reuseHike:
|
2023-07-12 23:03:14 +00:00
|
|
|
let tail = hike.legs[^1].wp.vtx.lPfx
|
|
|
|
if 0 < tail.len:
|
|
|
|
let topNibble = tail[tail.len - 1]
|
|
|
|
if topNibble < 15:
|
|
|
|
let newNibble = @[topNibble+1].initNibbleRange.slice(1)
|
|
|
|
hike.tail = tail.slice(0, tail.len - 1) & newNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 21:36:51 +00:00
|
|
|
break reuseHike
|
2023-07-12 23:03:14 +00:00
|
|
|
if 1 < tail.len:
|
|
|
|
let nxtNibble = tail[tail.len - 2]
|
|
|
|
if nxtNibble < 15:
|
|
|
|
let dblNibble = @[((nxtNibble+1) shl 4) + 0].initNibbleRange
|
|
|
|
hike.tail = tail.slice(0, tail.len - 2) & dblNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 21:36:51 +00:00
|
|
|
break reuseHike
|
2023-07-12 23:03:14 +00:00
|
|
|
# Fall back to default method
|
2023-10-27 21:36:51 +00:00
|
|
|
hike = key.next.hikeUp(db).to(Hike)
|
2023-07-12 23:03:14 +00:00
|
|
|
|
|
|
|
rc = hike.right db
|
|
|
|
# End while
|
|
|
|
|
|
|
|
# ----------------
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc left*(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 10:04:29 +00:00
|
|
|
## Similar to `nearbyRight()`.
|
|
|
|
##
|
|
|
|
## This code is intended to be used for verifying a right-bound proof to
|
2023-06-12 18:16:03 +00:00
|
|
|
## verify that there is no leaf vertex *left* to a boundary path value.
|
2023-06-02 10:04:29 +00:00
|
|
|
hike.nearbyNext(db, 64, moveRight=false)
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc left*(
|
2023-06-12 13:48:47 +00:00
|
|
|
lty: LeafTie; # Some `Patricia Trie` path
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[LeafTie,(VertexID,AristoError)] =
|
2023-10-27 21:36:51 +00:00
|
|
|
## Similar to `nearbyRight()` for `LeafTie` argument instead of a `Hike`.
|
2023-09-11 20:38:49 +00:00
|
|
|
ok LeafTie(
|
|
|
|
root: lty.root,
|
|
|
|
path: ? lty.nearbyNextLeafTie(db, 64, moveRight=false))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
iterator left*(
|
|
|
|
db: AristoDbRef; # Database layer
|
|
|
|
start = high(LeafTie); # Before or at first value
|
|
|
|
): (LeafTie,PayloadRef) =
|
|
|
|
## Traverse the sub-trie implied by the argument `start` with decreasing
|
|
|
|
## order. It will stop at any error. In order to reproduce an error, one
|
|
|
|
## can run the function `left()` on the last returned `LiefTie` item with
|
|
|
|
## the `path` field decremented by `1`.
|
|
|
|
var
|
2023-09-15 15:23:53 +00:00
|
|
|
hike = start.hikeUp(db).to(Hike)
|
2023-07-12 23:03:14 +00:00
|
|
|
rc = hike.left db
|
|
|
|
while rc.isOK:
|
|
|
|
hike = rc.value
|
2023-10-27 21:36:51 +00:00
|
|
|
let (key, pyl) = hike.toLeafTiePayload
|
2023-07-12 23:03:14 +00:00
|
|
|
yield (key, pyl)
|
2023-10-27 21:36:51 +00:00
|
|
|
if key.path <= low(PathID):
|
2023-07-12 23:03:14 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Decrement `key` by one and update `hike`. In many cases, the current
|
|
|
|
# `hike` can be modified and re-used which saves some database lookups.
|
2023-10-27 21:36:51 +00:00
|
|
|
block reuseHike:
|
2023-07-12 23:03:14 +00:00
|
|
|
let tail = hike.legs[^1].wp.vtx.lPfx
|
|
|
|
if 0 < tail.len:
|
|
|
|
let topNibble = tail[tail.len - 1]
|
|
|
|
if 0 < topNibble:
|
|
|
|
let newNibble = @[topNibble - 1].initNibbleRange.slice(1)
|
|
|
|
hike.tail = tail.slice(0, tail.len - 1) & newNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 21:36:51 +00:00
|
|
|
break reuseHike
|
2023-07-12 23:03:14 +00:00
|
|
|
if 1 < tail.len:
|
|
|
|
let nxtNibble = tail[tail.len - 2]
|
|
|
|
if 0 < nxtNibble:
|
|
|
|
let dblNibble = @[((nxtNibble-1) shl 4) + 15].initNibbleRange
|
|
|
|
hike.tail = tail.slice(0, tail.len - 2) & dblNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 21:36:51 +00:00
|
|
|
break reuseHike
|
2023-07-12 23:03:14 +00:00
|
|
|
# Fall back to default method
|
2023-10-27 21:36:51 +00:00
|
|
|
hike = key.prev.hikeUp(db).to(Hike)
|
2023-07-12 23:03:14 +00:00
|
|
|
|
|
|
|
rc = hike.left db
|
|
|
|
# End while
|
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public debugging helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc rightMissing*(
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
2023-06-02 10:04:29 +00:00
|
|
|
): Result[bool,AristoError] =
|
2023-06-12 18:16:03 +00:00
|
|
|
## Returns `true` if the maximally extended argument vertex `hike` is the
|
|
|
|
## right most on the hexary trie database. It verifies that there is no more
|
2023-06-02 10:04:29 +00:00
|
|
|
## leaf entry to the right of the argument `hike`. This function is an
|
2023-06-12 18:16:03 +00:00
|
|
|
## alternative to
|
2023-06-02 10:04:29 +00:00
|
|
|
## ::
|
|
|
|
## let rc = path.nearbyRight(db)
|
|
|
|
## if rc.isOk:
|
|
|
|
## # not at the end => false
|
|
|
|
## ...
|
|
|
|
## elif rc.error != NearbyBeyondRange:
|
|
|
|
## # problem with database => error
|
|
|
|
## ...
|
|
|
|
## else:
|
2023-06-12 18:16:03 +00:00
|
|
|
## # no nore vertices => true
|
2023-06-02 10:04:29 +00:00
|
|
|
## ...
|
|
|
|
## and is intended mainly for debugging.
|
|
|
|
if hike.legs.len == 0:
|
|
|
|
return err(NearbyEmptyHike)
|
|
|
|
if 0 < hike.tail.len:
|
|
|
|
return err(NearbyPathTailUnexpected)
|
|
|
|
|
|
|
|
let top = hike.legs[^1]
|
|
|
|
if top.wp.vtx.vType != Branch or top.nibble < 0:
|
|
|
|
return err(NearbyBranchError)
|
|
|
|
|
|
|
|
let vid = top.wp.vtx.bVid[top.nibble]
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vid.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
return err(NearbyDanglingLink) # error
|
|
|
|
|
|
|
|
let vtx = db.getVtx vid
|
2023-06-12 13:48:47 +00:00
|
|
|
if not vtx.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
return err(GetVtxNotFound) # error
|
|
|
|
|
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
|
|
|
return ok(vtx.lPfx < hike.tail)
|
|
|
|
of Extension:
|
|
|
|
return ok(vtx.ePfx < hike.tail)
|
|
|
|
of Branch:
|
|
|
|
return ok(vtx.branchNibbleMin(hike.tail[0].int8) < 0)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|