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
|
|
|
|
## ==============================================
|
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-07-03 20:21:57 +00:00
|
|
|
std/typetraits,
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
./aristo_delete/delete_subtree,
|
2024-09-19 08:39:06 +00:00
|
|
|
"."/[aristo_desc, aristo_fetch, aristo_get, aristo_hike, aristo_layers]
|
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
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
Pre-allocate vids for branches (#2882)
Each branch node may have up to 16 sub-items - currently, these are
given VertexID based when they are first needed leading to a
mostly-random order of vertexid for each subitem.
Here, we pre-allocate all 16 vertex ids such that when a branch subitem
is filled, it already has a vertexid waiting for it. This brings several
important benefits:
* subitems are sorted and "close" in their id sequencing - this means
that when rocksdb stores them, they are likely to end up in the same
data block thus improving read efficiency
* because the ids are consequtive, we can store just the starting id and
a bitmap representing which subitems are in use - this reduces disk
space usage for branches allowing more of them fit into a single disk
read, further improving disk read and caching performance - disk usage
at block 18M is down from 84 to 78gb!
* the in-memory footprint of VertexRef reduced allowing more instances
to fit into caches and less memory to be used overall.
Because of the increased locality of reference, it turns out that we no
longer need to iterate over the entire database to efficiently generate
the hash key database because the normal computation is now faster -
this significantly benefits "live" chain processing as well where each
dirtied key must be accompanied by a read of all branch subitems next to
it - most of the performance benefit in this branch comes from this
locality-of-reference improvement.
On a sample resync, there's already ~20% improvement with later blocks
seeing increasing benefit (because the trie is deeper in later blocks
leading to more benefit from branch read perf improvements)
```
blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s
Time (total): -36h44m48s, -19.27%
```
Note: clients need to be resynced as the PR changes the on-disk format
R.I.P. little bloom filter - your life in the repo was short but
valuable
2024-12-04 10:42:04 +00:00
|
|
|
proc branchStillNeeded(vtx: VertexRef, removed: int8): Result[int8,void] =
|
2023-06-30 22:22:33 +00:00
|
|
|
## Returns the nibble if there is only one reference left.
|
Pre-allocate vids for branches (#2882)
Each branch node may have up to 16 sub-items - currently, these are
given VertexID based when they are first needed leading to a
mostly-random order of vertexid for each subitem.
Here, we pre-allocate all 16 vertex ids such that when a branch subitem
is filled, it already has a vertexid waiting for it. This brings several
important benefits:
* subitems are sorted and "close" in their id sequencing - this means
that when rocksdb stores them, they are likely to end up in the same
data block thus improving read efficiency
* because the ids are consequtive, we can store just the starting id and
a bitmap representing which subitems are in use - this reduces disk
space usage for branches allowing more of them fit into a single disk
read, further improving disk read and caching performance - disk usage
at block 18M is down from 84 to 78gb!
* the in-memory footprint of VertexRef reduced allowing more instances
to fit into caches and less memory to be used overall.
Because of the increased locality of reference, it turns out that we no
longer need to iterate over the entire database to efficiently generate
the hash key database because the normal computation is now faster -
this significantly benefits "live" chain processing as well where each
dirtied key must be accompanied by a read of all branch subitems next to
it - most of the performance benefit in this branch comes from this
locality-of-reference improvement.
On a sample resync, there's already ~20% improvement with later blocks
seeing increasing benefit (because the trie is deeper in later blocks
leading to more benefit from branch read perf improvements)
```
blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s
Time (total): -36h44m48s, -19.27%
```
Note: clients need to be resynced as the PR changes the on-disk format
R.I.P. little bloom filter - your life in the repo was short but
valuable
2024-12-04 10:42:04 +00:00
|
|
|
var nibble = -1'i8
|
|
|
|
for n in 0'i8 .. 15'i8:
|
2024-09-20 08:31:29 +00:00
|
|
|
if n == removed:
|
|
|
|
continue
|
|
|
|
|
Pre-allocate vids for branches (#2882)
Each branch node may have up to 16 sub-items - currently, these are
given VertexID based when they are first needed leading to a
mostly-random order of vertexid for each subitem.
Here, we pre-allocate all 16 vertex ids such that when a branch subitem
is filled, it already has a vertexid waiting for it. This brings several
important benefits:
* subitems are sorted and "close" in their id sequencing - this means
that when rocksdb stores them, they are likely to end up in the same
data block thus improving read efficiency
* because the ids are consequtive, we can store just the starting id and
a bitmap representing which subitems are in use - this reduces disk
space usage for branches allowing more of them fit into a single disk
read, further improving disk read and caching performance - disk usage
at block 18M is down from 84 to 78gb!
* the in-memory footprint of VertexRef reduced allowing more instances
to fit into caches and less memory to be used overall.
Because of the increased locality of reference, it turns out that we no
longer need to iterate over the entire database to efficiently generate
the hash key database because the normal computation is now faster -
this significantly benefits "live" chain processing as well where each
dirtied key must be accompanied by a read of all branch subitems next to
it - most of the performance benefit in this branch comes from this
locality-of-reference improvement.
On a sample resync, there's already ~20% improvement with later blocks
seeing increasing benefit (because the trie is deeper in later blocks
leading to more benefit from branch read perf improvements)
```
blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s
Time (total): -36h44m48s, -19.27%
```
Note: clients need to be resynced as the PR changes the on-disk format
R.I.P. little bloom filter - your life in the repo was short but
valuable
2024-12-04 10:42:04 +00:00
|
|
|
if vtx.bVid(uint8 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-06-30 22:22:33 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
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
|
2024-09-19 08:39:06 +00:00
|
|
|
): Result[VertexRef,AristoError] =
|
|
|
|
## Removes the last node in the hike and returns the updated leaf in case
|
|
|
|
## a branch collapsed
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2024-06-18 19:30:01 +00:00
|
|
|
# Remove leaf entry
|
2024-09-19 08:39:06 +00:00
|
|
|
let lf = hike.legs[^1].wp
|
2023-06-30 22:22:33 +00:00
|
|
|
if lf.vtx.vType != Leaf:
|
2024-07-14 17:12:10 +00:00
|
|
|
return err(DelLeafExpexted)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
db.layersResVtx((hike.root, lf.vid))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
if hike.legs.len == 1:
|
|
|
|
# This was the last node in the trie, meaning we don't have any branches or
|
|
|
|
# leaves to update
|
|
|
|
return ok(nil)
|
|
|
|
|
2024-09-20 08:31:29 +00:00
|
|
|
if hike.legs[^2].wp.vtx.vType != Branch:
|
2024-09-19 08:39:06 +00:00
|
|
|
return err(DelBranchExpexted)
|
|
|
|
|
2024-09-20 08:31:29 +00:00
|
|
|
# Get current `Branch` vertex `br`
|
|
|
|
let
|
|
|
|
br = hike.legs[^2].wp
|
|
|
|
nbl = br.vtx.branchStillNeeded(hike.legs[^2].nibble).valueOr:
|
|
|
|
return err(DelBranchWithoutRefs)
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
# Clear all Merkle hash keys up to the root key
|
|
|
|
for n in 0 .. hike.legs.len - 2:
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
let wp = hike.legs[n].wp
|
|
|
|
db.layersResKey((hike.root, wp.vid), wp.vtx)
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
if 0 <= nbl:
|
2024-09-20 08:31:29 +00:00
|
|
|
# Branch has only one entry - move that entry to where the branch was and
|
|
|
|
# update its path
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
# Get child vertex (there must be one after a `Branch` node)
|
|
|
|
let
|
Pre-allocate vids for branches (#2882)
Each branch node may have up to 16 sub-items - currently, these are
given VertexID based when they are first needed leading to a
mostly-random order of vertexid for each subitem.
Here, we pre-allocate all 16 vertex ids such that when a branch subitem
is filled, it already has a vertexid waiting for it. This brings several
important benefits:
* subitems are sorted and "close" in their id sequencing - this means
that when rocksdb stores them, they are likely to end up in the same
data block thus improving read efficiency
* because the ids are consequtive, we can store just the starting id and
a bitmap representing which subitems are in use - this reduces disk
space usage for branches allowing more of them fit into a single disk
read, further improving disk read and caching performance - disk usage
at block 18M is down from 84 to 78gb!
* the in-memory footprint of VertexRef reduced allowing more instances
to fit into caches and less memory to be used overall.
Because of the increased locality of reference, it turns out that we no
longer need to iterate over the entire database to efficiently generate
the hash key database because the normal computation is now faster -
this significantly benefits "live" chain processing as well where each
dirtied key must be accompanied by a read of all branch subitems next to
it - most of the performance benefit in this branch comes from this
locality-of-reference improvement.
On a sample resync, there's already ~20% improvement with later blocks
seeing increasing benefit (because the trie is deeper in later blocks
leading to more benefit from branch read perf improvements)
```
blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s
Time (total): -36h44m48s, -19.27%
```
Note: clients need to be resynced as the PR changes the on-disk format
R.I.P. little bloom filter - your life in the repo was short but
valuable
2024-12-04 10:42:04 +00:00
|
|
|
vid = br.vtx.bVid(uint8 nbl)
|
2024-09-19 08:39:06 +00:00
|
|
|
nxt = db.getVtx (hike.root, vid)
|
|
|
|
if not nxt.isValid:
|
|
|
|
return err(DelVidStaleVtx)
|
|
|
|
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
db.layersResVtx((hike.root, vid))
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
let vtx =
|
|
|
|
case nxt.vType
|
|
|
|
of Leaf:
|
|
|
|
VertexRef(
|
|
|
|
vType: Leaf,
|
|
|
|
pfx: br.vtx.pfx & NibblesBuf.nibble(nbl.byte) & nxt.pfx,
|
|
|
|
lData: nxt.lData)
|
|
|
|
|
|
|
|
of Branch:
|
|
|
|
VertexRef(
|
|
|
|
vType: Branch,
|
|
|
|
pfx: br.vtx.pfx & NibblesBuf.nibble(nbl.byte) & nxt.pfx,
|
Pre-allocate vids for branches (#2882)
Each branch node may have up to 16 sub-items - currently, these are
given VertexID based when they are first needed leading to a
mostly-random order of vertexid for each subitem.
Here, we pre-allocate all 16 vertex ids such that when a branch subitem
is filled, it already has a vertexid waiting for it. This brings several
important benefits:
* subitems are sorted and "close" in their id sequencing - this means
that when rocksdb stores them, they are likely to end up in the same
data block thus improving read efficiency
* because the ids are consequtive, we can store just the starting id and
a bitmap representing which subitems are in use - this reduces disk
space usage for branches allowing more of them fit into a single disk
read, further improving disk read and caching performance - disk usage
at block 18M is down from 84 to 78gb!
* the in-memory footprint of VertexRef reduced allowing more instances
to fit into caches and less memory to be used overall.
Because of the increased locality of reference, it turns out that we no
longer need to iterate over the entire database to efficiently generate
the hash key database because the normal computation is now faster -
this significantly benefits "live" chain processing as well where each
dirtied key must be accompanied by a read of all branch subitems next to
it - most of the performance benefit in this branch comes from this
locality-of-reference improvement.
On a sample resync, there's already ~20% improvement with later blocks
seeing increasing benefit (because the trie is deeper in later blocks
leading to more benefit from branch read perf improvements)
```
blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s
Time (total): -36h44m48s, -19.27%
```
Note: clients need to be resynced as the PR changes the on-disk format
R.I.P. little bloom filter - your life in the repo was short but
valuable
2024-12-04 10:42:04 +00:00
|
|
|
startVid: nxt.startVid,
|
|
|
|
used: nxt.used)
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
# Put the new vertex at the id of the obsolete branch
|
|
|
|
db.layersPutVtx((hike.root, br.vid), vtx)
|
|
|
|
|
|
|
|
if vtx.vType == Leaf:
|
|
|
|
ok(vtx)
|
|
|
|
else:
|
|
|
|
ok(nil)
|
|
|
|
else:
|
2024-09-20 08:31:29 +00:00
|
|
|
# Clear the removed leaf from the branch (that still contains other children)
|
|
|
|
let brDup = br.vtx.dup
|
Pre-allocate vids for branches (#2882)
Each branch node may have up to 16 sub-items - currently, these are
given VertexID based when they are first needed leading to a
mostly-random order of vertexid for each subitem.
Here, we pre-allocate all 16 vertex ids such that when a branch subitem
is filled, it already has a vertexid waiting for it. This brings several
important benefits:
* subitems are sorted and "close" in their id sequencing - this means
that when rocksdb stores them, they are likely to end up in the same
data block thus improving read efficiency
* because the ids are consequtive, we can store just the starting id and
a bitmap representing which subitems are in use - this reduces disk
space usage for branches allowing more of them fit into a single disk
read, further improving disk read and caching performance - disk usage
at block 18M is down from 84 to 78gb!
* the in-memory footprint of VertexRef reduced allowing more instances
to fit into caches and less memory to be used overall.
Because of the increased locality of reference, it turns out that we no
longer need to iterate over the entire database to efficiently generate
the hash key database because the normal computation is now faster -
this significantly benefits "live" chain processing as well where each
dirtied key must be accompanied by a read of all branch subitems next to
it - most of the performance benefit in this branch comes from this
locality-of-reference improvement.
On a sample resync, there's already ~20% improvement with later blocks
seeing increasing benefit (because the trie is deeper in later blocks
leading to more benefit from branch read perf improvements)
```
blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s
Time (total): -36h44m48s, -19.27%
```
Note: clients need to be resynced as the PR changes the on-disk format
R.I.P. little bloom filter - your life in the repo was short but
valuable
2024-12-04 10:42:04 +00:00
|
|
|
discard brDup.setUsed(uint8 hike.legs[^2].nibble, false)
|
2024-09-20 08:31:29 +00:00
|
|
|
db.layersPutVtx((hike.root, br.vid), brDup)
|
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
ok(nil)
|
2023-06-02 19:21:46 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc deleteAccountRecord*(
|
2024-06-18 19:30:01 +00:00
|
|
|
db: AristoDbRef;
|
2024-10-01 21:03:10 +00:00
|
|
|
accPath: Hash32;
|
2024-06-18 19:30:01 +00:00
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Delete the account leaf entry addressed by the argument `path`. If this
|
|
|
|
## leaf entry referres to a storage tree, this one will be deleted as well.
|
2024-02-01 21:27:48 +00:00
|
|
|
##
|
2024-09-19 08:39:06 +00:00
|
|
|
var accHike: Hike
|
|
|
|
db.fetchAccountHike(accPath, accHike).isOkOr:
|
|
|
|
if error == FetchAccInaccessible:
|
|
|
|
return err(DelPathNotFound)
|
|
|
|
return err(error)
|
2024-06-18 19:30:01 +00:00
|
|
|
let
|
2024-09-19 08:39:06 +00:00
|
|
|
stoID = accHike.legs[^1].wp.vtx.lData.stoID
|
2024-02-01 21:27:48 +00:00
|
|
|
|
2024-06-18 19:30:01 +00:00
|
|
|
# Delete storage tree if present
|
|
|
|
if stoID.isValid:
|
2024-08-14 08:54:44 +00:00
|
|
|
? db.delStoTreeImpl((stoID.vid, stoID.vid), accPath)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
let otherLeaf = ?db.deleteImpl(accHike)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
db.layersPutAccLeaf(accPath, nil)
|
2024-07-12 13:08:26 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
if otherLeaf.isValid:
|
|
|
|
db.layersPutAccLeaf(
|
2024-09-29 12:37:09 +00:00
|
|
|
Hash32(getBytes(NibblesBuf.fromBytes(accPath.data).replaceSuffix(otherLeaf.pfx))),
|
2024-09-19 08:39:06 +00:00
|
|
|
otherLeaf)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
ok()
|
2024-06-18 19:30:01 +00:00
|
|
|
|
|
|
|
proc deleteGenericData*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
|
|
|
path: openArray[byte];
|
|
|
|
): Result[bool,AristoError] =
|
|
|
|
## Delete the leaf data entry addressed by the argument `path`. The MPT
|
|
|
|
## sub-tree the leaf data entry is subsumed under is passed as argument
|
|
|
|
## `root` which must be greater than `VertexID(1)` and smaller than
|
|
|
|
## `LEAST_FREE_VID`.
|
2024-02-08 16:32:16 +00:00
|
|
|
##
|
2024-06-18 19:30:01 +00:00
|
|
|
## The return value is `true` if the argument `path` deleted was the last
|
|
|
|
## one and the tree does not exist anymore.
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
2024-06-18 19:30:01 +00:00
|
|
|
# Verify that `root` is neither an accounts tree nor a strorage tree.
|
|
|
|
if not root.isValid:
|
|
|
|
return err(DelRootVidMissing)
|
|
|
|
elif root == VertexID(1):
|
|
|
|
return err(DelAccRootNotAccepted)
|
|
|
|
elif LEAST_FREE_VID <= root.distinctBase:
|
|
|
|
return err(DelStoRootNotAccepted)
|
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
var hike: Hike
|
|
|
|
path.hikeUp(root, db, Opt.none(VertexRef), hike).isOkOr:
|
2024-06-18 19:30:01 +00:00
|
|
|
if error[1] in HikeAcceptableStopsNotFound:
|
|
|
|
return err(DelPathNotFound)
|
|
|
|
return err(error[1])
|
2023-06-02 19:21:46 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
discard ?db.deleteImpl(hike)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
ok(not db.getVtx((root, root)).isValid)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
|
|
|
proc deleteGenericTree*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2024-06-18 19:30:01 +00:00
|
|
|
root: VertexID; # Root vertex
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Variant of `deleteGenericData()` for purging the whole MPT sub-tree.
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
2024-06-18 19:30:01 +00:00
|
|
|
# Verify that `root` is neither an accounts tree nor a strorage tree.
|
|
|
|
if not root.isValid:
|
|
|
|
return err(DelRootVidMissing)
|
|
|
|
elif root == VertexID(1):
|
|
|
|
return err(DelAccRootNotAccepted)
|
|
|
|
elif LEAST_FREE_VID <= root.distinctBase:
|
|
|
|
return err(DelStoRootNotAccepted)
|
|
|
|
|
|
|
|
db.delSubTreeImpl root
|
|
|
|
|
|
|
|
proc deleteStorageData*(
|
2023-09-15 15:23:53 +00:00
|
|
|
db: AristoDbRef;
|
2024-10-01 21:03:10 +00:00
|
|
|
accPath: Hash32; # Implies storage data tree
|
|
|
|
stoPath: Hash32;
|
2024-06-18 19:30:01 +00:00
|
|
|
): Result[bool,AristoError] =
|
|
|
|
## For a given account argument `accPath`, this function deletes the
|
2024-06-27 19:21:01 +00:00
|
|
|
## argument `stoPath` from the associated storage tree (if any, at all.) If
|
|
|
|
## the if the argument `stoPath` deleted was the last one on the storage tree,
|
2024-06-18 19:30:01 +00:00
|
|
|
## account leaf referred to by `accPath` will be updated so that it will
|
|
|
|
## not refer to a storage tree anymore. In the latter case only the function
|
|
|
|
## will return `true`.
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
mixPath = mixUp(accPath, stoPath)
|
|
|
|
stoLeaf = db.cachedStoLeaf(mixPath)
|
|
|
|
|
|
|
|
if stoLeaf == Opt.some(nil):
|
|
|
|
return err(DelPathNotFound)
|
|
|
|
|
|
|
|
var accHike: Hike
|
|
|
|
db.fetchAccountHike(accPath, accHike).isOkOr:
|
|
|
|
if error == FetchAccInaccessible:
|
|
|
|
return err(DelStoAccMissing)
|
|
|
|
return err(error)
|
|
|
|
|
2024-06-18 19:30:01 +00:00
|
|
|
let
|
|
|
|
wpAcc = accHike.legs[^1].wp
|
2024-06-27 09:01:26 +00:00
|
|
|
stoID = wpAcc.vtx.lData.stoID
|
2024-06-18 19:30:01 +00:00
|
|
|
|
|
|
|
if not stoID.isValid:
|
|
|
|
return err(DelStoRootMissing)
|
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
let stoNibbles = NibblesBuf.fromBytes(stoPath.data)
|
|
|
|
var stoHike: Hike
|
|
|
|
stoNibbles.hikeUp(stoID.vid, db, stoLeaf, stoHike).isOkOr:
|
2024-06-18 19:30:01 +00:00
|
|
|
if error[1] in HikeAcceptableStopsNotFound:
|
|
|
|
return err(DelPathNotFound)
|
|
|
|
return err(error[1])
|
|
|
|
|
2024-06-28 18:43:04 +00:00
|
|
|
# Mark account path Merkle keys for update
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersResKeys accHike
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
let otherLeaf = ?db.deleteImpl(stoHike)
|
|
|
|
db.layersPutStoLeaf(mixPath, nil)
|
2024-07-14 17:12:10 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
if otherLeaf.isValid:
|
|
|
|
let leafMixPath = mixUp(
|
|
|
|
accPath,
|
2024-09-29 12:37:09 +00:00
|
|
|
Hash32(getBytes(stoNibbles.replaceSuffix(otherLeaf.pfx))))
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersPutStoLeaf(leafMixPath, otherLeaf)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
# If there was only one item (that got deleted), update the account as well
|
|
|
|
if stoHike.legs.len > 1:
|
2024-06-18 19:30:01 +00:00
|
|
|
return ok(false)
|
|
|
|
|
|
|
|
# De-register the deleted storage tree from the account record
|
|
|
|
let leaf = wpAcc.vtx.dup # Dup on modify
|
2024-08-07 13:28:01 +00:00
|
|
|
leaf.lData.stoID.isValid = false
|
2024-07-14 10:02:05 +00:00
|
|
|
db.layersPutAccLeaf(accPath, leaf)
|
2024-07-04 13:46:52 +00:00
|
|
|
db.layersPutVtx((accHike.root, wpAcc.vid), leaf)
|
2024-06-18 19:30:01 +00:00
|
|
|
ok(true)
|
|
|
|
|
|
|
|
proc deleteStorageTree*(
|
|
|
|
db: AristoDbRef; # Database, top layer
|
2024-10-01 21:03:10 +00:00
|
|
|
accPath: Hash32; # Implies storage data tree
|
2024-06-18 19:30:01 +00:00
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Variant of `deleteStorageData()` for purging the whole storage tree
|
|
|
|
## associated to the account argument `accPath`.
|
|
|
|
##
|
2024-09-19 08:39:06 +00:00
|
|
|
var accHike: Hike
|
|
|
|
db.fetchAccountHike(accPath, accHike).isOkOr:
|
|
|
|
if error == FetchAccInaccessible:
|
|
|
|
return err(DelStoAccMissing)
|
|
|
|
return err(error)
|
|
|
|
|
2024-06-18 19:30:01 +00:00
|
|
|
let
|
|
|
|
wpAcc = accHike.legs[^1].wp
|
2024-06-27 09:01:26 +00:00
|
|
|
stoID = wpAcc.vtx.lData.stoID
|
2024-06-18 19:30:01 +00:00
|
|
|
|
|
|
|
if not stoID.isValid:
|
|
|
|
return err(DelStoRootMissing)
|
|
|
|
|
2024-06-28 18:43:04 +00:00
|
|
|
# Mark account path Merkle keys for update
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersResKeys accHike
|
2024-06-18 19:30:01 +00:00
|
|
|
|
2024-08-14 08:54:44 +00:00
|
|
|
? db.delStoTreeImpl((stoID.vid, stoID.vid), accPath)
|
2024-06-18 19:30:01 +00:00
|
|
|
|
|
|
|
# De-register the deleted storage tree from the accounts record
|
|
|
|
let leaf = wpAcc.vtx.dup # Dup on modify
|
2024-08-07 13:28:01 +00:00
|
|
|
leaf.lData.stoID.isValid = false
|
2024-07-14 10:02:05 +00:00
|
|
|
db.layersPutAccLeaf(accPath, leaf)
|
2024-07-04 13:46:52 +00:00
|
|
|
db.layersPutVtx((accHike.root, wpAcc.vid), leaf)
|
2024-06-18 19:30:01 +00:00
|
|
|
ok()
|
2023-06-02 19:21:46 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|