2023-06-02 19:21:46 +00:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-02 19:21:46 +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 -- Patricia Trie delete funcionality
|
|
|
|
## ==============================================
|
|
|
|
##
|
2024-02-07 14:29:57 +00:00
|
|
|
## Delete by `Hike` type chain of vertices.
|
2023-06-02 19:21:46 +00:00
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-02-08 16:32:16 +00:00
|
|
|
std/[sets, tables, typetraits],
|
2023-06-02 19:21:46 +00:00
|
|
|
chronicles,
|
|
|
|
eth/[common, trie/nibbles],
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-12-19 12:39:23 +00:00
|
|
|
"."/[aristo_desc, aristo_get, aristo_hike, aristo_layers, aristo_path,
|
2024-02-01 21:27:48 +00:00
|
|
|
aristo_utils, aristo_vid]
|
2023-06-02 19:21:46 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "aristo-delete"
|
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
type
|
|
|
|
SaveToVaeVidFn =
|
|
|
|
proc(err: AristoError): (VertexID,AristoError) {.gcsafe, raises: [].}
|
|
|
|
|
2023-06-02 19:21:46 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-30 22:22:33 +00:00
|
|
|
# Private heplers
|
2023-06-02 19:21:46 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
func toVae(err: AristoError): (VertexID,AristoError) =
|
|
|
|
## Map single error to error pair with dummy vertex
|
|
|
|
(VertexID(0),err)
|
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
func toVae(vid: VertexID): SaveToVaeVidFn =
|
|
|
|
## Map single error to error pair with argument vertex
|
|
|
|
result =
|
|
|
|
proc(err: AristoError): (VertexID,AristoError) =
|
|
|
|
return (vid,err)
|
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
func toVae(err: (VertexID,AristoError,Hike)): (VertexID,AristoError) =
|
|
|
|
(err[0], err[1])
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc branchStillNeeded(vtx: VertexRef): Result[int,void] =
|
|
|
|
## Returns the nibble if there is only one reference left.
|
|
|
|
var nibble = -1
|
2023-06-02 19:21:46 +00:00
|
|
|
for n in 0 .. 15:
|
2023-06-12 13:48:47 +00:00
|
|
|
if vtx.bVid[n].isValid:
|
2023-06-30 22:22:33 +00:00
|
|
|
if 0 <= nibble:
|
|
|
|
return ok(-1)
|
|
|
|
nibble = n
|
|
|
|
if 0 <= nibble:
|
|
|
|
return ok(nibble)
|
|
|
|
# Oops, degenerated branch node
|
|
|
|
err()
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
# -----------
|
|
|
|
|
|
|
|
proc nullifyKey(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-22 11:13:24 +00:00
|
|
|
vid: VertexID; # Vertex IDs to clear
|
|
|
|
) =
|
2023-12-12 17:47:41 +00:00
|
|
|
# Register for void hash (to be recompiled)
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersResLabel vid
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
proc disposeOfVtx(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-22 11:13:24 +00:00
|
|
|
vid: VertexID; # Vertex IDs to clear
|
|
|
|
) =
|
2023-06-02 19:21:46 +00:00
|
|
|
# Remove entry
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersResVtx vid
|
|
|
|
db.layersResLabel vid
|
|
|
|
db.vidDispose vid # Recycle ID
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc collapseBranch(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
hike: Hike; # Fully expanded path
|
|
|
|
nibble: byte; # Applicable link for `Branch` vertex
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Convert/merge vertices:
|
|
|
|
## ::
|
|
|
|
## current | becomes | condition
|
|
|
|
## | |
|
|
|
|
## ^3 ^2 | ^3 ^2 |
|
|
|
|
## -------------------+---------------------+------------------
|
|
|
|
## Branch <br> Branch | Branch <ext> Branch | 2 < legs.len (1)
|
|
|
|
## Ext <br> Branch | <ext> Branch | 2 < legs.len (2)
|
|
|
|
## <br> Branch | <ext> Branch | legs.len == 2 (3)
|
|
|
|
##
|
|
|
|
## Depending on whether the parent `par` is an extension, merge `br` into
|
|
|
|
## `par`. Otherwise replace `br` by an extension.
|
|
|
|
##
|
|
|
|
let br = hike.legs[^2].wp
|
|
|
|
|
|
|
|
var xt = VidVtxPair( # Rewrite `br`
|
|
|
|
vid: br.vid,
|
|
|
|
vtx: VertexRef(
|
|
|
|
vType: Extension,
|
|
|
|
ePfx: @[nibble].initNibbleRange.slice(1),
|
|
|
|
eVid: br.vtx.bVid[nibble]))
|
|
|
|
|
|
|
|
if 2 < hike.legs.len: # (1) or (2)
|
|
|
|
let par = hike.legs[^3].wp
|
|
|
|
case par.vtx.vType:
|
|
|
|
of Branch: # (1)
|
|
|
|
# Replace `br` (use `xt` as-is)
|
|
|
|
discard
|
|
|
|
|
|
|
|
of Extension: # (2)
|
|
|
|
# Merge `br` into ^3 (update `xt`)
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx xt.vid
|
2023-06-30 22:22:33 +00:00
|
|
|
xt.vid = par.vid
|
|
|
|
xt.vtx.ePfx = par.vtx.ePfx & xt.vtx.ePfx
|
|
|
|
|
|
|
|
of Leaf:
|
|
|
|
return err((par.vid,DelLeafUnexpected))
|
|
|
|
|
|
|
|
else: # (3)
|
|
|
|
# Replace `br` (use `xt` as-is)
|
|
|
|
discard
|
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(xt.vid, xt.vtx)
|
2023-06-30 22:22:33 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc collapseExt(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
hike: Hike; # Fully expanded path
|
|
|
|
nibble: byte; # Link for `Branch` vertex `^2`
|
|
|
|
vtx: VertexRef; # Follow up extension vertex (nibble)
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Convert/merge vertices:
|
|
|
|
## ::
|
|
|
|
## ^3 ^2 `vtx` | ^3 ^2 |
|
|
|
|
## --------------------+-----------------------+------------------
|
|
|
|
## Branch <br> Ext | Branch <ext> | 2 < legs.len (1)
|
|
|
|
## Ext <br> Ext | <ext> | 2 < legs.len (2)
|
|
|
|
## <br> Ext | <ext> | legs.len == 2 (3)
|
|
|
|
##
|
|
|
|
## Merge `vtx` into `br` and unlink `vtx`.
|
|
|
|
##
|
|
|
|
let br = hike.legs[^2].wp
|
|
|
|
|
|
|
|
var xt = VidVtxPair( # Merge `vtx` into `br`
|
|
|
|
vid: br.vid,
|
|
|
|
vtx: VertexRef(
|
|
|
|
vType: Extension,
|
|
|
|
ePfx: @[nibble].initNibbleRange.slice(1) & vtx.ePfx,
|
|
|
|
eVid: vtx.eVid))
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx br.vtx.bVid[nibble] # `vtx` is obsolete now
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
if 2 < hike.legs.len: # (1) or (2)
|
|
|
|
let par = hike.legs[^3].wp
|
|
|
|
case par.vtx.vType:
|
|
|
|
of Branch: # (1)
|
|
|
|
# Replace `br` by `^2 & vtx` (use `xt` as-is)
|
|
|
|
discard
|
|
|
|
|
|
|
|
of Extension: # (2)
|
|
|
|
# Replace ^3 by `^3 & ^2 & vtx` (update `xt`)
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx xt.vid
|
2023-06-30 22:22:33 +00:00
|
|
|
xt.vid = par.vid
|
|
|
|
xt.vtx.ePfx = par.vtx.ePfx & xt.vtx.ePfx
|
|
|
|
|
|
|
|
of Leaf:
|
|
|
|
return err((par.vid,DelLeafUnexpected))
|
|
|
|
|
|
|
|
else: # (3)
|
|
|
|
# Replace ^2 by `^2 & vtx` (use `xt` as-is)
|
|
|
|
discard
|
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(xt.vid, xt.vtx)
|
2023-06-30 22:22:33 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc collapseLeaf(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
hike: Hike; # Fully expanded path
|
|
|
|
nibble: byte; # Link for `Branch` vertex `^2`
|
|
|
|
vtx: VertexRef; # Follow up leaf vertex (from nibble)
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Convert/merge vertices:
|
|
|
|
## ::
|
|
|
|
## current | becomes | condition
|
|
|
|
## | |
|
|
|
|
## ^4 ^3 ^2 `vtx` | ^4 ^3 ^2 |
|
|
|
|
## -------------------------+----------------------------+------------------
|
|
|
|
## .. Branch <br> Leaf | .. Branch <Leaf> | 2 < legs.len (1)
|
|
|
|
## Branch Ext <br> Leaf | Branch <Leaf> | 3 < legs.len (2)
|
|
|
|
## Ext <br> Leaf | <Leaf> | legs.len == 3 (3)
|
|
|
|
## <br> Leaf | <Leaf> | legs.len == 2 (4)
|
|
|
|
##
|
|
|
|
## Merge `<br>` and `Leaf` replacing one and removing the other.
|
|
|
|
##
|
|
|
|
let br = hike.legs[^2].wp
|
|
|
|
|
|
|
|
var lf = VidVtxPair( # Merge `br` into `vtx`
|
|
|
|
vid: br.vtx.bVid[nibble],
|
|
|
|
vtx: VertexRef(
|
|
|
|
vType: Leaf,
|
|
|
|
lPfx: @[nibble].initNibbleRange.slice(1) & vtx.lPfx,
|
|
|
|
lData: vtx.lData))
|
2023-12-12 17:47:41 +00:00
|
|
|
db.nullifyKey lf.vid # `vtx` was modified
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
if 2 < hike.legs.len: # (1), (2), or (3)
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx br.vid # `br` is obsolete now
|
2023-06-30 22:22:33 +00:00
|
|
|
# Merge `br` into the leaf `vtx` and unlink `br`.
|
2023-12-04 20:39:26 +00:00
|
|
|
let par = hike.legs[^3].wp.dup # Writable vertex
|
2023-06-30 22:22:33 +00:00
|
|
|
case par.vtx.vType:
|
|
|
|
of Branch: # (1)
|
|
|
|
# Replace `vtx` by `^2 & vtx` (use `lf` as-is)
|
|
|
|
par.vtx.bVid[hike.legs[^3].nibble] = lf.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(par.vid, par.vtx)
|
|
|
|
db.layersPutVtx(lf.vid, lf.vtx)
|
2023-12-04 20:39:26 +00:00
|
|
|
# Make sure that there is a cache enty in case the leaf was pulled from
|
2024-02-08 16:32:16 +00:00
|
|
|
# the backend.
|
2023-12-04 20:39:26 +00:00
|
|
|
let
|
|
|
|
lfPath = hike.legsTo(hike.legs.len - 2, NibblesSeq) & lf.vtx.lPfx
|
|
|
|
tag = lfPath.pathToTag.valueOr:
|
|
|
|
return err((lf.vid,error))
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top.final.lTab[LeafTie(root: hike.root, path: tag)] = lf.vid
|
2023-06-30 22:22:33 +00:00
|
|
|
return ok()
|
|
|
|
|
|
|
|
of Extension: # (2) or (3)
|
|
|
|
# Merge `^3` into `lf` but keep the leaf vertex ID unchanged. This
|
|
|
|
# avoids some `lTab[]` registry update.
|
|
|
|
lf.vtx.lPfx = par.vtx.ePfx & lf.vtx.lPfx
|
|
|
|
|
|
|
|
if 3 < hike.legs.len: # (2)
|
|
|
|
# Grandparent exists
|
2023-12-04 20:39:26 +00:00
|
|
|
let gpr = hike.legs[^4].wp.dup # Writable vertex
|
2023-06-30 22:22:33 +00:00
|
|
|
if gpr.vtx.vType != Branch:
|
|
|
|
return err((gpr.vid,DelBranchExpexted))
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx par.vid # `par` is obsolete now
|
2023-06-30 22:22:33 +00:00
|
|
|
gpr.vtx.bVid[hike.legs[^4].nibble] = lf.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(gpr.vid, gpr.vtx)
|
|
|
|
db.layersPutVtx(lf.vid, lf.vtx)
|
2023-12-04 20:39:26 +00:00
|
|
|
# Make sure that there is a cache enty in case the leaf was pulled from
|
2024-02-08 16:32:16 +00:00
|
|
|
# the backend.
|
2023-12-04 20:39:26 +00:00
|
|
|
let
|
|
|
|
lfPath = hike.legsTo(hike.legs.len - 3, NibblesSeq) & lf.vtx.lPfx
|
|
|
|
tag = lfPath.pathToTag.valueOr:
|
|
|
|
return err((lf.vid,error))
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top.final.lTab[LeafTie(root: hike.root, path: tag)] = lf.vid
|
2023-06-30 22:22:33 +00:00
|
|
|
return ok()
|
|
|
|
|
|
|
|
# No grandparent, so ^3 is root vertex # (3)
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(par.vid, lf.vtx)
|
2023-06-30 22:22:33 +00:00
|
|
|
# Continue below
|
|
|
|
|
|
|
|
of Leaf:
|
|
|
|
return err((par.vid,DelLeafUnexpected))
|
|
|
|
|
|
|
|
else: # (4)
|
2023-12-12 17:47:41 +00:00
|
|
|
# Replace ^2 by `^2 & vtx` (use `lf` as-is) # `br` is root vertex
|
|
|
|
db.nullifyKey br.vid # root was changed
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(br.vid, lf.vtx)
|
2023-06-30 22:22:33 +00:00
|
|
|
# Continue below
|
|
|
|
|
|
|
|
# Common part for setting up `lf` as root vertex # Rest of (3) or (4)
|
|
|
|
let rc = lf.vtx.lPfx.pathToTag
|
|
|
|
if rc.isErr:
|
|
|
|
return err((br.vid,rc.error))
|
|
|
|
#
|
|
|
|
# No need to update the cache unless `lf` is present there. The leaf path
|
|
|
|
# as well as the value associated with the leaf path has not been changed.
|
|
|
|
let lfTie = LeafTie(root: hike.root, path: rc.value)
|
2024-02-01 21:27:48 +00:00
|
|
|
if db.lTab.hasKey lfTie:
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top.final.lTab[lfTie] = lf.vid
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# Clean up stale leaf vertex which has moved to root position
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx lf.vid
|
2023-12-04 20:39:26 +00:00
|
|
|
|
|
|
|
# If some `Leaf` vertex was installed as root, there must be a an extra
|
|
|
|
# `LeafTie` lookup entry.
|
|
|
|
let rootVtx = db.getVtx hike.root
|
|
|
|
if rootVtx.isValid and
|
|
|
|
rootVtx != hike.legs[0].wp.vtx and
|
|
|
|
rootVtx.vType == Leaf:
|
|
|
|
let tag = rootVtx.lPfx.pathToTag.valueOr:
|
|
|
|
return err((hike.root,error))
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top.final.lTab[LeafTie(root: hike.root, path: tag)] = hike.root
|
2023-12-04 20:39:26 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# -------------------------
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
proc delSubTree(
|
|
|
|
db: AristoDbRef; # Database, top layer
|
|
|
|
root: VertexID; # Root vertex
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Implementation of *delete* sub-trie.
|
|
|
|
if not root.isValid:
|
|
|
|
return err((root,DelSubTreeVoidRoot))
|
|
|
|
var
|
|
|
|
dispose = @[root]
|
|
|
|
rootVtx = db.getVtxRc(root).valueOr:
|
|
|
|
if error == GetVtxNotFound:
|
|
|
|
return ok()
|
|
|
|
return err((root,error))
|
|
|
|
follow = @[rootVtx]
|
|
|
|
|
|
|
|
# Collect list of nodes to delete
|
|
|
|
while 0 < follow.len:
|
|
|
|
var redo: seq[VertexRef]
|
|
|
|
for vtx in follow:
|
|
|
|
for vid in vtx.subVids:
|
|
|
|
let vtx = ? db.getVtxRc(vid).mapErr toVae(vid)
|
|
|
|
redo.add vtx
|
|
|
|
dispose.add vid
|
|
|
|
if SUB_TREE_DISPOSAL_MAX < dispose.len:
|
|
|
|
return err((VertexID(0),DelSubTreeTooBig))
|
|
|
|
redo.swap follow
|
|
|
|
|
|
|
|
# Mark nodes deleted
|
|
|
|
for vid in dispose:
|
|
|
|
db.disposeOfVtx vid
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
2023-06-02 19:21:46 +00:00
|
|
|
proc deleteImpl(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-02 19:21:46 +00:00
|
|
|
hike: Hike; # Fully expanded path
|
2023-06-12 13:48:47 +00:00
|
|
|
lty: LeafTie; # `Patricia Trie` path root-to-leaf
|
2024-02-08 16:32:16 +00:00
|
|
|
accPath: PathID; # Needed for accounts payload
|
2023-06-02 19:21:46 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Implementation of *delete* functionality.
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
if LEAST_FREE_VID <= lty.root.distinctBase:
|
|
|
|
db.registerAccount(lty.root, accPath).isOkOr:
|
|
|
|
return err((lty.root,error))
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
# Remove leaf entry on the top
|
|
|
|
let lf = hike.legs[^1].wp
|
|
|
|
if lf.vtx.vType != Leaf:
|
|
|
|
return err((lf.vid,DelLeafExpexted))
|
2023-12-19 12:39:23 +00:00
|
|
|
if lf.vid in db.pPrf:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((lf.vid, DelLeafLocked))
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# Will be needed at the end. Just detect an error early enouhh
|
|
|
|
let leafVidBe = block:
|
2023-08-11 17:23:57 +00:00
|
|
|
let rc = db.getVtxBE lf.vid
|
2023-08-07 17:45:23 +00:00
|
|
|
if rc.isErr:
|
|
|
|
if rc.error != GetVtxNotFound:
|
|
|
|
return err((lf.vid, rc.error))
|
|
|
|
VertexRef(nil)
|
|
|
|
else:
|
|
|
|
rc.value
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
db.disposeOfVtx lf.vid
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
if 1 < hike.legs.len:
|
|
|
|
# Get current `Branch` vertex `br`
|
2023-11-08 12:18:32 +00:00
|
|
|
let br = block:
|
|
|
|
var wp = hike.legs[^2].wp
|
|
|
|
wp.vtx = wp.vtx.dup # make sure that layers are not impliciteley modified
|
|
|
|
wp
|
2023-06-30 22:22:33 +00:00
|
|
|
if br.vtx.vType != Branch:
|
|
|
|
return err((br.vid,DelBranchExpexted))
|
|
|
|
|
|
|
|
# Unlink child vertex from structural table
|
|
|
|
br.vtx.bVid[hike.legs[^2].nibble] = VertexID(0)
|
2023-12-19 12:39:23 +00:00
|
|
|
db.layersPutVtx(br.vid, br.vtx)
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# Clear all keys up to the root key
|
|
|
|
for n in 0 .. hike.legs.len - 2:
|
|
|
|
let vid = hike.legs[n].wp.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
if vid in db.top.final.pPrf:
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid, DelBranchLocked))
|
2023-12-12 17:47:41 +00:00
|
|
|
db.nullifyKey vid
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
let nibble = block:
|
|
|
|
let rc = br.vtx.branchStillNeeded()
|
|
|
|
if rc.isErr:
|
|
|
|
return err((br.vid,DelBranchWithoutRefs))
|
|
|
|
rc.value
|
|
|
|
|
|
|
|
# Convert to `Extension` or `Leaf` vertex
|
|
|
|
if 0 <= nibble:
|
|
|
|
# Get child vertex (there must be one after a `Branch` node)
|
|
|
|
let nxt = block:
|
|
|
|
let vid = br.vtx.bVid[nibble]
|
|
|
|
VidVtxPair(vid: vid, vtx: db.getVtx vid)
|
|
|
|
if not nxt.vtx.isValid:
|
|
|
|
return err((nxt.vid, DelVidStaleVtx))
|
|
|
|
|
|
|
|
# Collapse `Branch` vertex `br` depending on `nxt` vertex type
|
2023-09-11 20:38:49 +00:00
|
|
|
case nxt.vtx.vType:
|
|
|
|
of Branch:
|
|
|
|
? db.collapseBranch(hike, nibble.byte)
|
|
|
|
of Extension:
|
|
|
|
? db.collapseExt(hike, nibble.byte, nxt.vtx)
|
|
|
|
of Leaf:
|
|
|
|
? db.collapseLeaf(hike, nibble.byte, nxt.vtx)
|
2023-06-22 11:13:24 +00:00
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
# Make sure that there is a cache entry so the hasher can label this path
|
|
|
|
# at a later state.
|
|
|
|
db.top.final.lTab[lty] = VertexID(0)
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
# Delete dependent leaf node storage tree if there is any
|
|
|
|
let data = lf.vtx.lData
|
|
|
|
if data.pType == AccountData:
|
|
|
|
let vid = data.account.storageID
|
|
|
|
if vid.isValid:
|
|
|
|
return db.delSubTree vid
|
|
|
|
|
2023-06-02 19:21:46 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
proc delete*(
|
|
|
|
db: AristoDbRef; # Database, top layer
|
|
|
|
root: VertexID; # Root vertex
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Delete sub-trie below `root`. The maximum supported sub-tree size is
|
|
|
|
## `SUB_TREE_DISPOSAL_MAX`. Larger tries must be disposed by walk-deleting
|
|
|
|
## leaf nodes using `left()` or `right()` traversal functions.
|
|
|
|
##
|
|
|
|
## Caveat:
|
|
|
|
## There is no way to quickly verify that the `root` argument is isolated.
|
|
|
|
## Deleting random sub-trees might lead to an inconsistent database.
|
|
|
|
##
|
|
|
|
db.delSubTree root
|
|
|
|
|
2023-06-02 19:21:46 +00:00
|
|
|
proc delete*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
hike: Hike; # Fully expanded chain of vertices
|
2024-02-08 16:32:16 +00:00
|
|
|
accPath: PathID; # Needed for accounts payload
|
2023-06-02 19:21:46 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2024-02-01 21:27:48 +00:00
|
|
|
## Delete argument `hike` chain of vertices from the database.
|
|
|
|
##
|
2024-02-08 16:32:16 +00:00
|
|
|
## For a `hike.root` with `VertexID` greater than `LEAST_FREE_VID`, the
|
|
|
|
## sub-tree generated by `payload.root` is considered a storage trie linked
|
|
|
|
## to an account leaf referred to by a valid `accPath` (i.e. different from
|
|
|
|
## `VOID_PATH_ID`.) In that case, an account must exists. If there is payload
|
|
|
|
## of type `AccountData`, its `storageID` field must be unset or equal to the
|
|
|
|
## `hike.root` vertex ID.
|
|
|
|
##
|
2024-02-01 21:27:48 +00:00
|
|
|
## Note:
|
|
|
|
## If the leaf node has an account payload referring to a storage sub-trie,
|
|
|
|
## this one will be deleted as well.
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
2023-06-02 19:21:46 +00:00
|
|
|
# Need path in order to remove it from `lTab[]`
|
2023-09-11 20:38:49 +00:00
|
|
|
let lty = LeafTie(
|
|
|
|
root: hike.root,
|
|
|
|
path: ? hike.to(NibblesSeq).pathToTag().mapErr toVae)
|
2024-02-08 16:32:16 +00:00
|
|
|
db.deleteImpl(hike, lty, accPath)
|
2023-06-02 19:21:46 +00:00
|
|
|
|
|
|
|
proc delete*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
lty: LeafTie; # `Patricia Trie` path root-to-leaf
|
2024-02-08 16:32:16 +00:00
|
|
|
accPath: PathID; # Needed for accounts payload
|
2023-06-02 19:21:46 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Variant of `delete()`
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
2024-02-08 16:32:16 +00:00
|
|
|
db.deleteImpl(? lty.hikeUp(db).mapErr toVae, lty, accPath)
|
2023-09-15 15:23:53 +00:00
|
|
|
|
|
|
|
proc delete*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
2023-09-18 20:20:28 +00:00
|
|
|
path: openArray[byte];
|
2024-02-08 16:32:16 +00:00
|
|
|
accPath: PathID; # Needed for accounts payload
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2023-12-04 20:39:26 +00:00
|
|
|
## Variant of `delete()`
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
2024-02-08 16:32:16 +00:00
|
|
|
let rc = path.initNibbleRange.hikeUp(root, db)
|
|
|
|
if rc.isOk:
|
|
|
|
return db.delete(rc.value, accPath)
|
|
|
|
if rc.error[1] in HikeAcceptableStopsNotFound:
|
|
|
|
return err((rc.error[0], DelPathNotFound))
|
|
|
|
err((rc.error[0],rc.error[1]))
|
2023-06-02 19:21:46 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|