2023-06-02 10:04:29 +00:00
|
|
|
# nimbus-eth1
|
2024-02-29 21:10:24 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-02 10:04:29 +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 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
|
2024-11-03 00:11:24 +00:00
|
|
|
std/[tables],
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2024-06-27 09:01:26 +00:00
|
|
|
"."/[aristo_desc, aristo_fetch, aristo_get, aristo_hike, aristo_path]
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
proc `<=`(a, b: NibblesBuf): bool =
|
2023-06-02 10:04:29 +00:00
|
|
|
## 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
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
proc `<`(a, b: NibblesBuf): bool =
|
2023-06-02 10:04:29 +00:00
|
|
|
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:
|
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-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:
|
2024-11-01 19:06:26 +00:00
|
|
|
for n in maxInx.countdown 0:
|
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-02 10:04:29 +00:00
|
|
|
return n
|
|
|
|
-1
|
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
# ------------------
|
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
proc toLeafTiePayload(hike: Hike): (LeafTie,LeafPayload) =
|
2023-07-12 23:03:14 +00:00
|
|
|
## Shortcut for iterators. This function will gloriously crash unless the
|
|
|
|
## `hike` argument is complete.
|
2024-06-22 20:33:37 +00:00
|
|
|
(LeafTie(root: hike.root, path: hike.to(NibblesBuf).pathToTag.value),
|
2023-07-12 23:03:14 +00:00
|
|
|
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
|
2024-07-04 13:46:52 +00:00
|
|
|
vtx = db.getVtx (hike.root, vid)
|
2023-06-02 10:04:29 +00:00
|
|
|
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 Branch:
|
|
|
|
when doLeast:
|
|
|
|
leg.nibble = vtx.branchNibbleMin 0
|
|
|
|
else:
|
|
|
|
leg.nibble = vtx.branchNibbleMax 15
|
|
|
|
if 0 <= leg.nibble:
|
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 = vtx.bVid(uint8 leg.nibble)
|
2024-07-04 13:46:52 +00:00
|
|
|
vtx = db.getVtx (hike.root, 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.
|
2024-06-22 20:33:37 +00:00
|
|
|
proc accept(p: Hike; pfx: NibblesBuf): bool =
|
2023-06-02 10:04:29 +00:00
|
|
|
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
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
proc toHike(pfx: NibblesBuf, root: VertexID, db: AristoDbRef): Hike =
|
2023-06-02 10:04:29 +00:00
|
|
|
when doLeast:
|
2024-09-19 08:39:06 +00:00
|
|
|
discard pfx.pathPfxPad(0).hikeUp(root, db, Opt.none(VertexRef), result)
|
2023-06-02 10:04:29 +00:00
|
|
|
else:
|
2024-09-19 08:39:06 +00:00
|
|
|
discard pfx.pathPfxPad(255).hikeUp(root, db, Opt.none(VertexRef), result)
|
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
|
|
|
|
2024-08-01 10:41:20 +00:00
|
|
|
let rootVtx = db.getVtx (hike.root, hike.root)
|
|
|
|
if rootVtx.isValid:
|
2023-06-02 10:04:29 +00:00
|
|
|
block fail:
|
2024-06-22 20:33:37 +00:00
|
|
|
var pfx: NibblesBuf
|
2024-08-01 10:41:20 +00:00
|
|
|
case rootVtx.vType:
|
2023-06-02 10:04:29 +00:00
|
|
|
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
|
2024-08-01 10:41:20 +00:00
|
|
|
let n = rootVtx.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))
|
2024-09-13 16:55:17 +00:00
|
|
|
pfx = rootVtx.pfx & NibblesBuf.nibble(n.byte)
|
No ext update (#2494)
* Imported/rebase from `no-ext`, PR #2485
Store extension nodes together with the branch
Extension nodes must be followed by a branch - as such, it makes sense
to store the two together both in the database and in memory:
* fewer reads, writes and updates to traverse the tree
* simpler logic for maintaining the node structure
* less space used, both memory and storage, because there are fewer
nodes overall
There is also a downside: hashes can no longer be cached for an
extension - instead, only the extension+branch hash can be cached - this
seems like a fine tradeoff since computing it should be fast.
TODO: fix commented code
* Fix merge functions and `toNode()`
* Update `merkleSignCommit()` prototype
why:
Result is always a 32bit hash
* Update short Merkle hash key generation
details:
Ethereum reference MPTs use Keccak hashes as node links if the size of
an RLP encoded node is at least 32 bytes. Otherwise, the RLP encoded
node value is used as a pseudo node link (rather than a hash.) This is
specified in the yellow paper, appendix D.
Different to the `Aristo` implementation, the reference MPT would not
store such a node on the key-value database. Rather the RLP encoded node value is stored instead of a node link in a parent node
is stored as a node link on the parent database.
Only for the root hash, the top level node is always referred to by the
hash.
* Fix/update `Extension` sections
why:
Were commented out after removal of a dedicated `Extension` type which
left the system disfunctional.
* Clean up unused error codes
* Update unit tests
* Update docu
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
2024-07-16 19:47:59 +00:00
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
of Leaf:
|
2024-09-13 16:55:17 +00:00
|
|
|
pfx = rootVtx.pfx
|
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
|
|
|
|
2024-08-01 10:41:20 +00:00
|
|
|
# Pathological case: matching `rootVtx` which is a leaf
|
|
|
|
if hike.legs.len == 0 and hike.tail.len == 0:
|
2024-09-19 08:39:06 +00:00
|
|
|
var ret = Hike(root: hike.root)
|
|
|
|
ret.legs.add Leg(
|
2024-08-01 10:41:20 +00:00
|
|
|
nibble: -1,
|
|
|
|
wp: VidVtxPair(
|
|
|
|
vid: hike.root,
|
2024-09-19 08:39:06 +00:00
|
|
|
vtx: rootVtx))
|
|
|
|
return ok ret
|
2024-08-01 10:41:20 +00:00
|
|
|
|
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
|
2024-06-22 20:33:37 +00:00
|
|
|
proc beyond(p: Hike; pfx: NibblesBuf): bool =
|
2023-06-02 10:04:29 +00:00
|
|
|
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
|
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 = top.wp.vtx.bVid(uint8 top.nibble)
|
2024-07-04 13:46:52 +00:00
|
|
|
vtx = db.getVtx (hike.root, 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
|
|
|
|
2024-09-13 16:55:17 +00:00
|
|
|
let pfx =
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
vtx.pfx
|
|
|
|
of Branch:
|
|
|
|
vtx.pfx & NibblesBuf.nibble(vtx.branchBorderNibble.byte)
|
2023-06-02 10:04:29 +00:00
|
|
|
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
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
proc accept(p: Hike; pfx: NibblesBuf): bool =
|
2023-06-02 10:04:29 +00:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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:
|
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
|
|
|
let vid = top.wp.vtx.bVid(uint8 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
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
let vtx = db.getVtx (hike.root, 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:
|
2024-09-13 16:55:17 +00:00
|
|
|
if uHike.accept vtx.pfx:
|
2023-06-02 10:04:29 +00:00
|
|
|
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(
|
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
|
|
|
step.wp.vtx.bVid(uint8 n), db, hikeLenMax, doLeast=moveRight)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
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`
|
2024-06-22 20:33:37 +00:00
|
|
|
uHike.tail = NibblesBuf.nibble(top.nibble.byte) & uHike.tail
|
2023-06-02 10:04:29 +00:00
|
|
|
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
|
2024-09-19 08:39:06 +00:00
|
|
|
var hike: Hike
|
|
|
|
discard lty.hikeUp(db, Opt.none(VertexRef), hike)
|
|
|
|
hike = ?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))
|
2024-06-22 20:33:37 +00:00
|
|
|
let rc = hike.legsTo(NibblesBuf).pathToTag
|
2023-06-02 10:04:29 +00:00
|
|
|
if rc.isOk:
|
2023-11-08 12:18:32 +00:00
|
|
|
return ok rc.value
|
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
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
iterator rightPairs*(
|
2023-07-12 23:03:14 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
|
|
|
start = low(LeafTie); # Before or at first value
|
2024-07-14 10:02:05 +00:00
|
|
|
): (LeafTie,LeafPayload) =
|
2023-07-12 23:03:14 +00:00
|
|
|
## Traverse the sub-trie implied by the argument `start` with increasing
|
|
|
|
## order.
|
2024-09-19 08:39:06 +00:00
|
|
|
var hike: Hike
|
|
|
|
discard start.hikeUp(db, Opt.none(VertexRef), hike)
|
|
|
|
var rc = hike.right db
|
2024-11-01 19:06:26 +00:00
|
|
|
while rc.isOk:
|
2023-07-12 23:03:14 +00:00
|
|
|
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:
|
2024-09-13 16:55:17 +00:00
|
|
|
let tail = hike.legs[^1].wp.vtx.pfx
|
2023-07-12 23:03:14 +00:00
|
|
|
if 0 < tail.len:
|
|
|
|
let topNibble = tail[tail.len - 1]
|
|
|
|
if topNibble < 15:
|
2024-06-22 20:33:37 +00:00
|
|
|
let newNibble = NibblesBuf.nibble(topNibble+1)
|
2023-07-12 23:03:14 +00:00
|
|
|
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:
|
2024-06-22 20:33:37 +00:00
|
|
|
let dblNibble = NibblesBuf.fromBytes([((nxtNibble+1) shl 4) + 0])
|
2023-07-12 23:03:14 +00:00
|
|
|
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
|
2024-09-19 08:39:06 +00:00
|
|
|
discard key.next.hikeUp(db, Opt.none(VertexRef), hike)
|
2023-07-12 23:03:14 +00:00
|
|
|
|
|
|
|
rc = hike.right db
|
|
|
|
# End while
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
iterator rightPairsAccount*(
|
|
|
|
db: AristoDbRef; # Database layer
|
|
|
|
start = low(PathID); # Before or at first value
|
|
|
|
): (PathID,AristoAccount) =
|
|
|
|
## Variant of `rightPairs()` for accounts tree
|
|
|
|
for (lty,pyl) in db.rightPairs LeafTie(root: VertexID(1), path: start):
|
|
|
|
yield (lty.path, pyl.account)
|
|
|
|
|
|
|
|
iterator rightPairsStorage*(
|
|
|
|
db: AristoDbRef; # Database layer
|
2024-10-01 21:03:10 +00:00
|
|
|
accPath: Hash32; # Account the storage data belong to
|
2024-06-27 09:01:26 +00:00
|
|
|
start = low(PathID); # Before or at first value
|
2024-07-05 10:15:48 +00:00
|
|
|
): (PathID,UInt256) =
|
2024-06-27 09:01:26 +00:00
|
|
|
## Variant of `rightPairs()` for a storage tree
|
|
|
|
block body:
|
|
|
|
let stoID = db.fetchStorageID(accPath).valueOr:
|
|
|
|
break body
|
|
|
|
if stoID.isValid:
|
|
|
|
for (lty,pyl) in db.rightPairs LeafTie(root: stoID, path: start):
|
2024-07-05 10:15:48 +00:00
|
|
|
yield (lty.path, pyl.stoData)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
2023-07-12 23:03:14 +00:00
|
|
|
# ----------------
|
|
|
|
|
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
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
iterator leftPairs*(
|
2023-07-12 23:03:14 +00:00
|
|
|
db: AristoDbRef; # Database layer
|
|
|
|
start = high(LeafTie); # Before or at first value
|
2024-07-14 10:02:05 +00:00
|
|
|
): (LeafTie,LeafPayload) =
|
2023-07-12 23:03:14 +00:00
|
|
|
## 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
|
2024-09-19 08:39:06 +00:00
|
|
|
hike: Hike
|
|
|
|
discard start.hikeUp(db, Opt.none(VertexRef), hike)
|
|
|
|
|
|
|
|
var rc = hike.left db
|
2024-11-01 19:06:26 +00:00
|
|
|
while rc.isOk:
|
2023-07-12 23:03:14 +00:00
|
|
|
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:
|
2024-09-13 16:55:17 +00:00
|
|
|
let tail = hike.legs[^1].wp.vtx.pfx
|
2023-07-12 23:03:14 +00:00
|
|
|
if 0 < tail.len:
|
|
|
|
let topNibble = tail[tail.len - 1]
|
|
|
|
if 0 < topNibble:
|
2024-06-22 20:33:37 +00:00
|
|
|
let newNibble = NibblesBuf.nibble(topNibble - 1)
|
2023-07-12 23:03:14 +00:00
|
|
|
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:
|
2024-06-22 20:33:37 +00:00
|
|
|
let dblNibble = NibblesBuf.fromBytes([((nxtNibble-1) shl 4) + 15])
|
2023-07-12 23:03:14 +00:00
|
|
|
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
|
2024-09-19 08:39:06 +00:00
|
|
|
discard key.prev.hikeUp(db, Opt.none(VertexRef), 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)
|
|
|
|
|
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
|
|
|
let vid = top.wp.vtx.bVid(uint8 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
|
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
let vtx = db.getVtx (hike.root, 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:
|
2024-09-13 16:55:17 +00:00
|
|
|
return ok(vtx.pfx < hike.tail)
|
2023-06-02 10:04:29 +00:00
|
|
|
of Branch:
|
|
|
|
return ok(vtx.branchNibbleMin(hike.tail[0].int8) < 0)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|