2023-06-02 11:04:29 +01:00
|
|
|
# nimbus-eth1
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
# Copyright (c) 2023-2025 Status Research & Development GmbH
|
2023-06-02 11:04:29 +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.
|
|
|
|
|
|
|
|
## 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 07:11:24 +07:00
|
|
|
std/[tables],
|
2024-06-22 22:33:37 +02:00
|
|
|
eth/common,
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2024-06-27 09:01:26 +00:00
|
|
|
"."/[aristo_desc, aristo_fetch, aristo_get, aristo_hike, aristo_path]
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-22 22:33:37 +02:00
|
|
|
proc `<=`(a, b: NibblesBuf): bool =
|
2023-06-02 11:04:29 +01: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 22:33:37 +02:00
|
|
|
proc `<`(a, b: NibblesBuf): bool =
|
2023-06-02 11:04:29 +01: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 11:42:04 +01:00
|
|
|
if vtx.bVid(uint8 n).isValid:
|
2023-06-02 11:04:29 +01: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 11:42:04 +01:00
|
|
|
if vtx.bVid(uint8 n).isValid:
|
2023-06-02 11:04:29 +01:00
|
|
|
return n
|
|
|
|
-1
|
|
|
|
|
2023-07-13 00:03:14 +01:00
|
|
|
# ------------------
|
|
|
|
|
2024-07-14 12:02:05 +02:00
|
|
|
proc toLeafTiePayload(hike: Hike): (LeafTie,LeafPayload) =
|
2023-07-13 00:03:14 +01:00
|
|
|
## Shortcut for iterators. This function will gloriously crash unless the
|
|
|
|
## `hike` argument is complete.
|
2024-06-22 22:33:37 +02:00
|
|
|
(LeafTie(root: hike.root, path: hike.to(NibblesBuf).pathToTag.value),
|
2023-07-13 00:03:14 +01:00
|
|
|
hike.legs[^1].wp.vtx.lData)
|
|
|
|
|
2023-06-02 11:04:29 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc complete(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
2023-06-02 11:04:29 +01:00
|
|
|
vid: VertexID; # Start ID
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-02 11:04:29 +01:00
|
|
|
hikeLenMax: static[int]; # Beware of loops (if any)
|
|
|
|
doLeast: static[bool]; # Direction: *least* or *most*
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 11:04:29 +01:00
|
|
|
## Extend `hike` using least or last vertex without recursion.
|
2023-06-30 23:22:33 +01:00
|
|
|
if not vid.isValid:
|
|
|
|
return err((VertexID(0),NearbyVidInvalid))
|
2023-06-02 11:04:29 +01:00
|
|
|
var
|
|
|
|
vid = vid
|
2024-07-04 15:46:52 +02:00
|
|
|
vtx = db.getVtx (hike.root, vid)
|
2023-06-02 11:04:29 +01:00
|
|
|
uHike = Hike(root: hike.root, legs: hike.legs)
|
2023-06-12 14:48:47 +01:00
|
|
|
if not vtx.isValid:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,GetVtxNotFound))
|
2023-06-02 11:04:29 +01: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 23:22:33 +01:00
|
|
|
return ok(uHike) # done
|
2023-06-02 11:04:29 +01: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 11:42:04 +01:00
|
|
|
vid = vtx.bVid(uint8 leg.nibble)
|
2024-07-04 15:46:52 +02:00
|
|
|
vtx = db.getVtx (hike.root, vid)
|
2023-06-12 14:48:47 +01:00
|
|
|
if vtx.isValid:
|
2023-06-02 11:04:29 +01:00
|
|
|
uHike.legs.add leg
|
|
|
|
continue
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((leg.wp.vid,NearbyBranchError)) # Oops, no way
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
err((VertexID(0),NearbyNestingTooDeep))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
proc zeroAdjust(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-02 11:04:29 +01:00
|
|
|
doLeast: static[bool]; # Direction: *least* or *most*
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-12 19:16:03 +01:00
|
|
|
## Adjust empty argument path to the first vertex entry to the right. Ths
|
2023-06-02 11:04:29 +01: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 22:33:37 +02:00
|
|
|
proc accept(p: Hike; pfx: NibblesBuf): bool =
|
2023-06-02 11:04:29 +01: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
|
|
|
|
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
proc toHike(pfx: NibblesBuf, root: VertexID, db: AristoTxRef): Hike =
|
2023-06-02 11:04:29 +01:00
|
|
|
when doLeast:
|
2024-09-19 10:39:06 +02:00
|
|
|
discard pfx.pathPfxPad(0).hikeUp(root, db, Opt.none(VertexRef), result)
|
2023-06-02 11:04:29 +01:00
|
|
|
else:
|
2024-09-19 10:39:06 +02:00
|
|
|
discard pfx.pathPfxPad(255).hikeUp(root, db, Opt.none(VertexRef), result)
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
if 0 < hike.legs.len:
|
2023-06-30 23:22:33 +01:00
|
|
|
return ok(hike)
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2024-08-01 10:41:20 +00:00
|
|
|
let rootVtx = db.getVtx (hike.root, hike.root)
|
|
|
|
if rootVtx.isValid:
|
2023-06-02 11:04:29 +01:00
|
|
|
block fail:
|
2024-06-22 22:33:37 +02:00
|
|
|
var pfx: NibblesBuf
|
2024-08-01 10:41:20 +00:00
|
|
|
case rootVtx.vType:
|
2023-06-02 11:04:29 +01:00
|
|
|
of Branch:
|
|
|
|
# Find first non-dangling link and assign it
|
2023-10-27 22:36:51 +01: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 11:04:29 +01:00
|
|
|
if n < 0:
|
|
|
|
# Before or after the database range
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((hike.root,NearbyBeyondRange))
|
2024-09-13 18:55:17 +02: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 11:04:29 +01:00
|
|
|
of Leaf:
|
2024-09-13 18:55:17 +02:00
|
|
|
pfx = rootVtx.pfx
|
2023-10-27 22:36:51 +01:00
|
|
|
if not hike.accept pfx:
|
2023-06-02 11:04:29 +01:00
|
|
|
# Before or after the database range
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((hike.root,NearbyBeyondRange))
|
2023-06-02 11:04:29 +01: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 10:39:06 +02: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 10:39:06 +02:00
|
|
|
vtx: rootVtx))
|
|
|
|
return ok ret
|
2024-08-01 10:41:20 +00:00
|
|
|
|
2023-06-02 11:04:29 +01:00
|
|
|
var newHike = pfx.toHike(hike.root, db)
|
|
|
|
if 0 < newHike.legs.len:
|
2023-06-30 23:22:33 +01:00
|
|
|
return ok(newHike)
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
err((VertexID(0),NearbyEmptyHike))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
proc finalise(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-02 20:21:46 +01:00
|
|
|
moveRight: static[bool]; # Direction of next vertex
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 11:04:29 +01:00
|
|
|
## Handle some pathological cases after main processing failed
|
2024-06-22 22:33:37 +02:00
|
|
|
proc beyond(p: Hike; pfx: NibblesBuf): bool =
|
2023-06-02 11:04:29 +01: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 23:22:33 +01:00
|
|
|
return err((VertexID(0),NearbyEmptyHike))
|
2023-06-02 11:04:29 +01: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 19:16:03 +01:00
|
|
|
# Note that only a `Branch` vertices has a non-zero nibble
|
2023-06-02 11:04:29 +01:00
|
|
|
if 0 <= top.nibble and top.nibble == top.wp.vtx.branchBorderNibble:
|
2023-06-12 19:16:03 +01:00
|
|
|
# Check the following up vertex
|
2023-06-30 23:22:33 +01: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 11:42:04 +01:00
|
|
|
vid = top.wp.vtx.bVid(uint8 top.nibble)
|
2024-07-04 15:46:52 +02:00
|
|
|
vtx = db.getVtx (hike.root, vid)
|
2023-06-12 14:48:47 +01:00
|
|
|
if not vtx.isValid:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,NearbyDanglingLink))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2024-09-13 18:55:17 +02:00
|
|
|
let pfx =
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
vtx.pfx
|
|
|
|
of Branch:
|
|
|
|
vtx.pfx & NibblesBuf.nibble(vtx.branchBorderNibble.byte)
|
2023-06-02 11:04:29 +01:00
|
|
|
if hike.beyond pfx:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,NearbyBeyondRange))
|
2023-06-02 11:04:29 +01: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 23:22:33 +01:00
|
|
|
return err((VertexID(0),NearbyFailed)) # no more vertices
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
err((hike.legs[^1].wp.vid,NearbyUnexpectedVtx)) # error
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
proc nearbyNext(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-02 11:04:29 +01:00
|
|
|
hikeLenMax: static[int]; # Beware of loops (if any)
|
2023-06-02 20:21:46 +01:00
|
|
|
moveRight: static[bool]; # Direction of next vertex
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 11:04:29 +01: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 22:33:37 +02:00
|
|
|
proc accept(p: Hike; pfx: NibblesBuf): bool =
|
2023-06-02 11:04:29 +01: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 21:38:49 +01:00
|
|
|
let hike = ? hike.zeroAdjust(db, doLeast=moveRight)
|
2023-06-02 11:04:29 +01: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 23:22:33 +01:00
|
|
|
return ok(uHike)
|
2023-06-02 11:04:29 +01:00
|
|
|
of Branch:
|
|
|
|
if top.nibble < 0 or uHike.tail.len == 0:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((top.wp.vid,NearbyUnexpectedVtx))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
step = top
|
|
|
|
let
|
|
|
|
uHikeLen = uHike.legs.len # in case of backtracking
|
|
|
|
uHikeTail = uHike.tail # in case of backtracking
|
|
|
|
|
2023-06-12 19:16:03 +01:00
|
|
|
# Look ahead checking next vertex
|
2023-06-02 11:04:29 +01: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 11:42:04 +01:00
|
|
|
let vid = top.wp.vtx.bVid(uint8 top.nibble)
|
2023-06-12 14:48:47 +01:00
|
|
|
if not vid.isValid:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((top.wp.vid,NearbyDanglingLink)) # error
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2024-07-04 15:46:52 +02:00
|
|
|
let vtx = db.getVtx (hike.root, vid)
|
2023-06-12 14:48:47 +01:00
|
|
|
if not vtx.isValid:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,GetVtxNotFound)) # error
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
2024-09-13 18:55:17 +02:00
|
|
|
if uHike.accept vtx.pfx:
|
2023-06-02 11:04:29 +01: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 19:16:03 +01:00
|
|
|
# Step down and complete with a branch link on the child vertex
|
2023-06-02 11:04:29 +01: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 11:42:04 +01:00
|
|
|
step.wp.vtx.bVid(uint8 n), db, hikeLenMax, doLeast=moveRight)
|
2023-06-02 11:04:29 +01: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 19:16:03 +01:00
|
|
|
# Pop current `Branch` vertex on top and append nibble to `tail`
|
2024-06-22 22:33:37 +02:00
|
|
|
uHike.tail = NibblesBuf.nibble(top.nibble.byte) & uHike.tail
|
2023-06-02 11:04:29 +01:00
|
|
|
uHike.legs.setLen(uHike.legs.len - 1)
|
|
|
|
# End while
|
|
|
|
|
|
|
|
# Handle some pathological cases
|
2023-06-30 23:22:33 +01:00
|
|
|
hike.finalise(db, moveRight)
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
proc nearbyNextLeafTie(
|
2023-06-12 14:48:47 +01:00
|
|
|
lty: LeafTie; # Some `Patricia Trie` path
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-02 11:04:29 +01:00
|
|
|
hikeLenMax: static[int]; # Beware of loops (if any)
|
2023-06-09 12:17:37 +01:00
|
|
|
moveRight:static[bool]; # Direction of next vertex
|
2023-10-27 22:36:51 +01:00
|
|
|
): Result[PathID,(VertexID,AristoError)] =
|
2023-09-11 21:38:49 +01:00
|
|
|
## Variant of `nearbyNext()`, convenience wrapper
|
2024-09-19 10:39:06 +02:00
|
|
|
var hike: Hike
|
|
|
|
discard lty.hikeUp(db, Opt.none(VertexRef), hike)
|
|
|
|
hike = ?hike.nearbyNext(db, hikeLenMax, moveRight)
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2023-06-30 23:22:33 +01: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 22:33:37 +02:00
|
|
|
let rc = hike.legsTo(NibblesBuf).pathToTag
|
2023-06-02 11:04:29 +01:00
|
|
|
if rc.isOk:
|
2023-11-08 12:18:32 +00:00
|
|
|
return ok rc.value
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((VertexID(0),rc.error))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
err((VertexID(0),NearbyLeafExpected))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, moving and right boundary proof
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
proc right*(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-12 19:16:03 +01:00
|
|
|
## Extends the maximally extended argument vertices `hike` to the right (i.e.
|
2023-06-02 11:04:29 +01: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 19:16:03 +01:00
|
|
|
## If there is no more leaf vertices to the right of the argument `hike`, the
|
2023-06-02 11:04:29 +01:00
|
|
|
## particular error code `NearbyBeyondRange` is returned.
|
|
|
|
##
|
|
|
|
## This code is intended to be used for verifying a left-bound proof to
|
2023-06-12 19:16:03 +01:00
|
|
|
## verify that there is no leaf vertex *right* of a boundary path value.
|
2023-06-02 11:04:29 +01:00
|
|
|
hike.nearbyNext(db, 64, moveRight=true)
|
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
proc right*(
|
2023-06-12 14:48:47 +01:00
|
|
|
lty: LeafTie; # Some `Patricia Trie` path
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[LeafTie,(VertexID,AristoError)] =
|
2023-10-27 22:36:51 +01:00
|
|
|
## Variant of `nearbyRight()` working with a `LeafTie` argument instead
|
2023-06-02 11:04:29 +01:00
|
|
|
## of a `Hike`.
|
2023-09-11 21:38:49 +01:00
|
|
|
ok LeafTie(
|
|
|
|
root: lty.root,
|
|
|
|
path: ? lty.nearbyNextLeafTie(db, 64, moveRight=true))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
iterator rightPairs*(
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-07-13 00:03:14 +01:00
|
|
|
start = low(LeafTie); # Before or at first value
|
2024-07-14 12:02:05 +02:00
|
|
|
): (LeafTie,LeafPayload) =
|
2023-07-13 00:03:14 +01:00
|
|
|
## Traverse the sub-trie implied by the argument `start` with increasing
|
|
|
|
## order.
|
2024-09-19 10:39:06 +02: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-13 00:03:14 +01:00
|
|
|
hike = rc.value
|
2023-10-27 22:36:51 +01:00
|
|
|
let (key, pyl) = hike.toLeafTiePayload
|
2023-07-13 00:03:14 +01:00
|
|
|
yield (key, pyl)
|
2023-10-27 22:36:51 +01:00
|
|
|
if high(PathID) <= key.path:
|
2023-07-13 00:03:14 +01: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 22:36:51 +01:00
|
|
|
block reuseHike:
|
2024-09-13 18:55:17 +02:00
|
|
|
let tail = hike.legs[^1].wp.vtx.pfx
|
2023-07-13 00:03:14 +01:00
|
|
|
if 0 < tail.len:
|
|
|
|
let topNibble = tail[tail.len - 1]
|
|
|
|
if topNibble < 15:
|
2024-06-22 22:33:37 +02:00
|
|
|
let newNibble = NibblesBuf.nibble(topNibble+1)
|
2023-07-13 00:03:14 +01:00
|
|
|
hike.tail = tail.slice(0, tail.len - 1) & newNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 22:36:51 +01:00
|
|
|
break reuseHike
|
2023-07-13 00:03:14 +01:00
|
|
|
if 1 < tail.len:
|
|
|
|
let nxtNibble = tail[tail.len - 2]
|
|
|
|
if nxtNibble < 15:
|
2024-06-22 22:33:37 +02:00
|
|
|
let dblNibble = NibblesBuf.fromBytes([((nxtNibble+1) shl 4) + 0])
|
2023-07-13 00:03:14 +01:00
|
|
|
hike.tail = tail.slice(0, tail.len - 2) & dblNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 22:36:51 +01:00
|
|
|
break reuseHike
|
2023-07-13 00:03:14 +01:00
|
|
|
# Fall back to default method
|
2024-09-19 10:39:06 +02:00
|
|
|
discard key.next.hikeUp(db, Opt.none(VertexRef), hike)
|
2023-07-13 00:03:14 +01:00
|
|
|
|
|
|
|
rc = hike.right db
|
|
|
|
# End while
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
iterator rightPairsAccount*(
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2024-06-27 09:01:26 +00:00
|
|
|
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*(
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # 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 17:15:48 +07: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 17:15:48 +07:00
|
|
|
yield (lty.path, pyl.stoData)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
2023-07-13 00:03:14 +01:00
|
|
|
# ----------------
|
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
proc left*(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[Hike,(VertexID,AristoError)] =
|
2023-06-02 11:04:29 +01:00
|
|
|
## Similar to `nearbyRight()`.
|
|
|
|
##
|
|
|
|
## This code is intended to be used for verifying a right-bound proof to
|
2023-06-12 19:16:03 +01:00
|
|
|
## verify that there is no leaf vertex *left* to a boundary path value.
|
2023-06-02 11:04:29 +01:00
|
|
|
hike.nearbyNext(db, 64, moveRight=false)
|
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
proc left*(
|
2023-06-12 14:48:47 +01:00
|
|
|
lty: LeafTie; # Some `Patricia Trie` path
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[LeafTie,(VertexID,AristoError)] =
|
2023-10-27 22:36:51 +01:00
|
|
|
## Similar to `nearbyRight()` for `LeafTie` argument instead of a `Hike`.
|
2023-09-11 21:38:49 +01:00
|
|
|
ok LeafTie(
|
|
|
|
root: lty.root,
|
|
|
|
path: ? lty.nearbyNextLeafTie(db, 64, moveRight=false))
|
2023-06-02 11:04:29 +01:00
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
iterator leftPairs*(
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-07-13 00:03:14 +01:00
|
|
|
start = high(LeafTie); # Before or at first value
|
2024-07-14 12:02:05 +02:00
|
|
|
): (LeafTie,LeafPayload) =
|
2023-07-13 00:03:14 +01: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 10:39:06 +02: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-13 00:03:14 +01:00
|
|
|
hike = rc.value
|
2023-10-27 22:36:51 +01:00
|
|
|
let (key, pyl) = hike.toLeafTiePayload
|
2023-07-13 00:03:14 +01:00
|
|
|
yield (key, pyl)
|
2023-10-27 22:36:51 +01:00
|
|
|
if key.path <= low(PathID):
|
2023-07-13 00:03:14 +01: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 22:36:51 +01:00
|
|
|
block reuseHike:
|
2024-09-13 18:55:17 +02:00
|
|
|
let tail = hike.legs[^1].wp.vtx.pfx
|
2023-07-13 00:03:14 +01:00
|
|
|
if 0 < tail.len:
|
|
|
|
let topNibble = tail[tail.len - 1]
|
|
|
|
if 0 < topNibble:
|
2024-06-22 22:33:37 +02:00
|
|
|
let newNibble = NibblesBuf.nibble(topNibble - 1)
|
2023-07-13 00:03:14 +01:00
|
|
|
hike.tail = tail.slice(0, tail.len - 1) & newNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 22:36:51 +01:00
|
|
|
break reuseHike
|
2023-07-13 00:03:14 +01:00
|
|
|
if 1 < tail.len:
|
|
|
|
let nxtNibble = tail[tail.len - 2]
|
|
|
|
if 0 < nxtNibble:
|
2024-06-22 22:33:37 +02:00
|
|
|
let dblNibble = NibblesBuf.fromBytes([((nxtNibble-1) shl 4) + 15])
|
2023-07-13 00:03:14 +01:00
|
|
|
hike.tail = tail.slice(0, tail.len - 2) & dblNibble
|
|
|
|
hike.legs.setLen(hike.legs.len - 1)
|
2023-10-27 22:36:51 +01:00
|
|
|
break reuseHike
|
2023-07-13 00:03:14 +01:00
|
|
|
# Fall back to default method
|
2024-09-19 10:39:06 +02:00
|
|
|
discard key.prev.hikeUp(db, Opt.none(VertexRef), hike)
|
2023-07-13 00:03:14 +01:00
|
|
|
|
|
|
|
rc = hike.left db
|
|
|
|
# End while
|
|
|
|
|
2023-06-02 11:04:29 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public debugging helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
proc rightMissing*(
|
2023-06-02 20:21:46 +01:00
|
|
|
hike: Hike; # Partially expanded chain of vertices
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
db: AristoTxRef; # Database layer
|
2023-06-02 11:04:29 +01:00
|
|
|
): Result[bool,AristoError] =
|
2023-06-12 19:16:03 +01: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 11:04:29 +01:00
|
|
|
## leaf entry to the right of the argument `hike`. This function is an
|
2023-06-12 19:16:03 +01:00
|
|
|
## alternative to
|
2023-06-02 11:04:29 +01: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 19:16:03 +01:00
|
|
|
## # no nore vertices => true
|
2023-06-02 11:04:29 +01: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 11:42:04 +01:00
|
|
|
let vid = top.wp.vtx.bVid(uint8 top.nibble)
|
2023-06-12 14:48:47 +01:00
|
|
|
if not vid.isValid:
|
2023-06-02 11:04:29 +01:00
|
|
|
return err(NearbyDanglingLink) # error
|
|
|
|
|
2024-07-04 15:46:52 +02:00
|
|
|
let vtx = db.getVtx (hike.root, vid)
|
2023-06-12 14:48:47 +01:00
|
|
|
if not vtx.isValid:
|
2023-06-02 11:04:29 +01:00
|
|
|
return err(GetVtxNotFound) # error
|
|
|
|
|
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
2024-09-13 18:55:17 +02:00
|
|
|
return ok(vtx.pfx < hike.tail)
|
2023-06-02 11:04:29 +01:00
|
|
|
of Branch:
|
|
|
|
return ok(vtx.branchNibbleMin(hike.tail[0].int8) < 0)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|