2023-05-30 12:47:47 +01:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-30 12:47:47 +01: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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-22 22:33:37 +02:00
|
|
|
eth/common,
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2024-09-19 10:39:06 +02:00
|
|
|
stew/arraybuf,
|
2023-06-12 19:16:03 +01:00
|
|
|
"."/[aristo_desc, aristo_get]
|
2023-05-30 12:47:47 +01:00
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
const
|
|
|
|
HikeAcceptableStopsNotFound* = {
|
|
|
|
HikeBranchTailEmpty,
|
|
|
|
HikeBranchMissingEdge,
|
|
|
|
HikeLeafUnexpected,
|
|
|
|
HikeNoLegs}
|
|
|
|
## When trying to find a leaf vertex the Patricia tree, there are several
|
|
|
|
## conditions where the search stops which do not constitute a problem
|
|
|
|
## with the trie (aka sysetm error.)
|
|
|
|
|
2023-05-30 12:47:47 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-22 22:33:37 +02:00
|
|
|
func getNibblesImpl(hike: Hike; start = 0; maxLen = high(int)): NibblesBuf =
|
2023-05-30 12:47:47 +01:00
|
|
|
## May be needed for partial rebuild, as well
|
|
|
|
for n in start ..< min(hike.legs.len, maxLen):
|
|
|
|
let leg = hike.legs[n]
|
|
|
|
case leg.wp.vtx.vType:
|
|
|
|
of Branch:
|
2024-09-13 18:55:17 +02:00
|
|
|
result = result & leg.wp.vtx.pfx & NibblesBuf.nibble(leg.nibble.byte)
|
2023-05-30 12:47:47 +01:00
|
|
|
of Leaf:
|
2024-09-13 18:55:17 +02:00
|
|
|
result = result & leg.wp.vtx.pfx
|
2023-05-30 12:47:47 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
func to*(rc: Result[Hike,(VertexID,AristoError,Hike)]; T: type Hike): T =
|
2023-09-15 16:23:53 +01:00
|
|
|
## Extract `Hike` from either ok ot error part of argument `rc`.
|
2024-02-08 16:32:16 +00:00
|
|
|
if rc.isOk: rc.value else: rc.error[2]
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-06-22 22:33:37 +02:00
|
|
|
func to*(hike: Hike; T: type NibblesBuf): T =
|
2023-05-30 12:47:47 +01:00
|
|
|
## Convert back
|
|
|
|
hike.getNibblesImpl() & hike.tail
|
|
|
|
|
2024-06-22 22:33:37 +02:00
|
|
|
func legsTo*(hike: Hike; T: type NibblesBuf): T =
|
2023-05-30 12:47:47 +01:00
|
|
|
## Convert back
|
|
|
|
hike.getNibblesImpl()
|
|
|
|
|
2024-06-22 22:33:37 +02:00
|
|
|
func legsTo*(hike: Hike; numLegs: int; T: type NibblesBuf): T =
|
2023-12-04 20:39:26 +00:00
|
|
|
## variant of `legsTo()`
|
|
|
|
hike.getNibblesImpl(0, numLegs)
|
|
|
|
|
2023-05-30 12:47:47 +01:00
|
|
|
# --------
|
|
|
|
|
2024-07-01 14:07:39 +02:00
|
|
|
proc step*(
|
2024-07-04 15:46:52 +02:00
|
|
|
path: NibblesBuf, rvid: RootedVertexID, db: AristoDbRef
|
2024-07-01 14:07:39 +02:00
|
|
|
): Result[(VertexRef, NibblesBuf, VertexID), AristoError] =
|
|
|
|
# Fetch next vertex
|
2024-07-18 09:13:56 +02:00
|
|
|
let (vtx, _) = db.getVtxRc(rvid).valueOr:
|
2024-07-01 14:07:39 +02:00
|
|
|
if error != GetVtxNotFound:
|
|
|
|
return err(error)
|
|
|
|
|
2024-09-19 10:39:06 +02:00
|
|
|
if rvid.root == rvid.vid:
|
|
|
|
return err(HikeNoLegs)
|
2024-07-01 14:07:39 +02:00
|
|
|
# The vertex ID `vid` was a follow up from a parent vertex, but there is
|
|
|
|
# no child vertex on the database. So `vid` is a dangling link which is
|
|
|
|
# allowed only if there is a partial trie (e.g. with `snap` sync.)
|
|
|
|
return err(HikeDanglingEdge)
|
|
|
|
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
# This must be the last vertex, so there cannot be any `tail` left.
|
2024-09-13 18:55:17 +02:00
|
|
|
if path.len != path.sharedPrefixLen(vtx.pfx):
|
2024-07-01 14:07:39 +02:00
|
|
|
return err(HikeLeafUnexpected)
|
|
|
|
|
|
|
|
ok (vtx, NibblesBuf(), VertexID(0))
|
|
|
|
|
|
|
|
of Branch:
|
|
|
|
# There must be some more data (aka `tail`) after a `Branch` vertex.
|
2024-09-13 18:55:17 +02:00
|
|
|
if path.len <= vtx.pfx.len:
|
2024-07-01 14:07:39 +02:00
|
|
|
return err(HikeBranchTailEmpty)
|
|
|
|
|
|
|
|
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 11:42:04 +01:00
|
|
|
nibble = path[vtx.pfx.len]
|
|
|
|
nextVid = vtx.bVid(nibble)
|
2024-07-01 14:07:39 +02:00
|
|
|
|
|
|
|
if not nextVid.isValid:
|
|
|
|
return err(HikeBranchMissingEdge)
|
|
|
|
|
2024-09-13 18:55:17 +02:00
|
|
|
ok (vtx, path.slice(vtx.pfx.len + 1), nextVid)
|
2024-07-01 14:07:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
iterator stepUp*(
|
|
|
|
path: NibblesBuf; # Partial path
|
|
|
|
root: VertexID; # Start vertex
|
|
|
|
db: AristoDbRef; # Database
|
|
|
|
): Result[VertexRef, AristoError] =
|
|
|
|
## For the argument `path`, iterate over the logest possible path in the
|
|
|
|
## argument database `db`.
|
|
|
|
var
|
|
|
|
path = path
|
|
|
|
next = root
|
|
|
|
vtx: VertexRef
|
|
|
|
block iter:
|
|
|
|
while true:
|
2024-07-04 15:46:52 +02:00
|
|
|
(vtx, path, next) = step(path, (root, next), db).valueOr:
|
2024-07-01 14:07:39 +02:00
|
|
|
yield Result[VertexRef, AristoError].err(error)
|
|
|
|
break iter
|
|
|
|
|
|
|
|
yield Result[VertexRef, AristoError].ok(vtx)
|
|
|
|
|
|
|
|
if path.len == 0:
|
|
|
|
break
|
|
|
|
|
2023-05-30 12:47:47 +01:00
|
|
|
proc hikeUp*(
|
2024-06-22 22:33:37 +02:00
|
|
|
path: NibblesBuf; # Partial path
|
2023-05-30 12:47:47 +01:00
|
|
|
root: VertexID; # Start vertex
|
2023-07-04 19:24:03 +01:00
|
|
|
db: AristoDbRef; # Database
|
2024-09-19 10:39:06 +02:00
|
|
|
leaf: Opt[VertexRef];
|
|
|
|
hike: var Hike;
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2023-05-30 12:47:47 +01:00
|
|
|
## For the argument `path`, find and return the logest possible path in the
|
2024-09-19 10:39:06 +02:00
|
|
|
## argument database `db` - this may result in a partial match in which case
|
|
|
|
## hike.tail will be non-empty.
|
|
|
|
##
|
|
|
|
## If a leaf is given, it gets used for the "last" leg of the hike.
|
|
|
|
hike.root = root
|
|
|
|
hike.tail = path
|
|
|
|
hike.legs.setLen(0)
|
2023-05-30 12:47:47 +01:00
|
|
|
|
2023-06-12 14:48:47 +01:00
|
|
|
if not root.isValid:
|
2024-09-19 10:39:06 +02:00
|
|
|
return err((VertexID(0),HikeRootMissing))
|
2023-10-27 22:36:51 +01:00
|
|
|
if path.len == 0:
|
2024-09-19 10:39:06 +02:00
|
|
|
return err((VertexID(0),HikeEmptyPath))
|
2023-09-15 16:23:53 +01:00
|
|
|
|
|
|
|
var vid = root
|
2024-05-03 17:38:17 +00:00
|
|
|
while true:
|
2024-09-19 10:39:06 +02:00
|
|
|
if leaf.isSome() and leaf[].isValid and path == leaf[].pfx:
|
|
|
|
hike.legs.add Leg(wp: VidVtxPair(vid: vid, vtx: leaf[]), nibble: -1)
|
|
|
|
reset(hike.tail)
|
|
|
|
break
|
|
|
|
|
2024-07-04 15:46:52 +02:00
|
|
|
let (vtx, path, next) = step(hike.tail, (root, vid), db).valueOr:
|
2024-09-19 10:39:06 +02:00
|
|
|
return err((vid,error))
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-07-01 14:07:39 +02:00
|
|
|
let wp = VidVtxPair(vid:vid, vtx:vtx)
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-07-01 14:07:39 +02:00
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
|
|
|
hike.legs.add Leg(wp: wp, nibble: -1)
|
|
|
|
hike.tail = path
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-07-01 14:07:39 +02:00
|
|
|
break
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-07-01 14:07:39 +02:00
|
|
|
of Branch:
|
2024-09-13 18:55:17 +02:00
|
|
|
hike.legs.add Leg(wp: wp, nibble: int8 hike.tail[vtx.pfx.len])
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-07-01 14:07:39 +02:00
|
|
|
hike.tail = path
|
|
|
|
vid = next
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-09-19 10:39:06 +02:00
|
|
|
ok()
|
2023-09-15 16:23:53 +01:00
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
proc hikeUp*(
|
|
|
|
lty: LeafTie;
|
|
|
|
db: AristoDbRef;
|
2024-09-19 10:39:06 +02:00
|
|
|
leaf: Opt[VertexRef];
|
|
|
|
hike: var Hike
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2023-05-30 12:47:47 +01:00
|
|
|
## Variant of `hike()`
|
2024-09-19 10:39:06 +02:00
|
|
|
lty.path.to(NibblesBuf).hikeUp(lty.root, db, leaf, hike)
|
2023-05-30 12:47:47 +01:00
|
|
|
|
2024-06-27 19:21:01 +00:00
|
|
|
proc hikeUp*(
|
|
|
|
path: openArray[byte];
|
|
|
|
root: VertexID;
|
|
|
|
db: AristoDbRef;
|
2024-09-19 10:39:06 +02:00
|
|
|
leaf: Opt[VertexRef];
|
|
|
|
hike: var Hike
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2024-06-27 19:21:01 +00:00
|
|
|
## Variant of `hike()`
|
2024-09-19 10:39:06 +02:00
|
|
|
NibblesBuf.fromBytes(path).hikeUp(root, db, leaf, hike)
|
2024-06-27 19:21:01 +00:00
|
|
|
|
2024-07-03 10:14:26 +02:00
|
|
|
proc hikeUp*(
|
2024-10-01 21:03:10 +00:00
|
|
|
path: Hash32;
|
2024-07-03 10:14:26 +02:00
|
|
|
root: VertexID;
|
|
|
|
db: AristoDbRef;
|
2024-09-19 10:39:06 +02:00
|
|
|
leaf: Opt[VertexRef];
|
|
|
|
hike: var Hike
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2024-07-03 10:14:26 +02:00
|
|
|
## Variant of `hike()`
|
2024-09-19 10:39:06 +02:00
|
|
|
NibblesBuf.fromBytes(path.data).hikeUp(root, db, leaf, hike)
|
2024-07-03 10:14:26 +02:00
|
|
|
|
2023-05-30 12:47:47 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|