mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-03-01 12:20:49 +00:00
* 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>
211 lines
7.0 KiB
Nim
211 lines
7.0 KiB
Nim
# nimbus-eth1
|
|
# Copyright (c) 2023-2025 Status Research & Development GmbH
|
|
# Licensed under either of
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
# http://opensource.org/licenses/MIT)
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
# except according to those terms.
|
|
|
|
{.push raises: [].}
|
|
|
|
import
|
|
std/[sets, tables],
|
|
eth/common/hashes,
|
|
results,
|
|
./aristo_desc,
|
|
../../utils/mergeutils
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions: getter variants
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func layersGetVtx*(db: AristoTxRef; rvid: RootedVertexID): Opt[(VertexRef, int)] =
|
|
## Find a vertex on the cache layers. An `ok()` result might contain a
|
|
## `nil` vertex if it is stored on the cache that way.
|
|
##
|
|
for w, level in db.rstack:
|
|
w.sTab.withValue(rvid, item):
|
|
return Opt.some((item[], level))
|
|
|
|
Opt.none((VertexRef, int))
|
|
|
|
func layersGetKey*(db: AristoTxRef; rvid: RootedVertexID): Opt[(HashKey, int)] =
|
|
## Find a hash key on the cache layers. An `ok()` result might contain a void
|
|
## hash key if it is stored on the cache that way.
|
|
##
|
|
|
|
for w, level in db.rstack:
|
|
w.kMap.withValue(rvid, item):
|
|
return ok((item[], level))
|
|
if rvid in w.sTab:
|
|
return Opt.some((VOID_HASH_KEY, level))
|
|
|
|
Opt.none((HashKey, int))
|
|
|
|
func layersGetKeyOrVoid*(db: AristoTxRef; rvid: RootedVertexID): HashKey =
|
|
## Simplified version of `layersGetKey()`
|
|
(db.layersGetKey(rvid).valueOr (VOID_HASH_KEY, 0))[0]
|
|
|
|
func layersGetAccLeaf*(db: AristoTxRef; accPath: Hash32): Opt[VertexRef] =
|
|
for w, _ in db.rstack:
|
|
w.accLeaves.withValue(accPath, item):
|
|
return Opt.some(item[])
|
|
|
|
Opt.none(VertexRef)
|
|
|
|
func layersGetStoLeaf*(db: AristoTxRef; mixPath: Hash32): Opt[VertexRef] =
|
|
for w, _ in db.rstack:
|
|
w.stoLeaves.withValue(mixPath, item):
|
|
return Opt.some(item[])
|
|
|
|
Opt.none(VertexRef)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions: setter variants
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func layersPutVtx*(
|
|
db: AristoTxRef;
|
|
rvid: RootedVertexID;
|
|
vtx: VertexRef;
|
|
) =
|
|
## Store a (potentally empty) vertex on the top layer
|
|
db.layer.sTab[rvid] = vtx
|
|
db.layer.kMap.del(rvid)
|
|
|
|
func layersResVtx*(
|
|
db: AristoTxRef;
|
|
rvid: RootedVertexID;
|
|
) =
|
|
## Shortcut for `db.layersPutVtx(vid, VertexRef(nil))`. It is sort of the
|
|
## equivalent of a delete function.
|
|
db.layersPutVtx(rvid, VertexRef(nil))
|
|
|
|
func layersPutKey*(
|
|
db: AristoTxRef;
|
|
rvid: RootedVertexID;
|
|
vtx: VertexRef,
|
|
key: HashKey;
|
|
) =
|
|
## Store a (potentally void) hash key on the top layer
|
|
db.layer.sTab[rvid] = vtx
|
|
db.layer.kMap[rvid] = key
|
|
|
|
func layersResKey*(db: AristoTxRef; rvid: RootedVertexID, vtx: VertexRef) =
|
|
## Shortcut for `db.layersPutKey(vid, VOID_HASH_KEY)`. It is sort of the
|
|
## equivalent of a delete function.
|
|
db.layersPutVtx(rvid, vtx)
|
|
|
|
func layersResKeys*(db: AristoTxRef; hike: Hike) =
|
|
## Reset all cached keys along the given hike
|
|
for i in 1..hike.legs.len:
|
|
db.layersResKey((hike.root, hike.legs[^i].wp.vid), hike.legs[^i].wp.vtx)
|
|
|
|
func layersPutAccLeaf*(db: AristoTxRef; accPath: Hash32; leafVtx: VertexRef) =
|
|
db.layer.accLeaves[accPath] = leafVtx
|
|
|
|
func layersPutStoLeaf*(db: AristoTxRef; mixPath: Hash32; leafVtx: VertexRef) =
|
|
db.layer.stoLeaves[mixPath] = leafVtx
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func isEmpty*(ly: LayerRef): bool =
|
|
## Returns `true` if the layer does not contain any changes, i.e. all the
|
|
## tables are empty.
|
|
ly.sTab.len == 0 and
|
|
ly.kMap.len == 0 and
|
|
ly.accLeaves.len == 0 and
|
|
ly.stoLeaves.len == 0
|
|
|
|
proc mergeAndReset*(trg, src: var Layer) =
|
|
## Merges the argument `src` into the argument `trg` and clears `src`.
|
|
trg.vTop = src.vTop
|
|
|
|
if trg.kMap.len > 0:
|
|
# Invalidate cached keys in the lower layer
|
|
for vid in src.sTab.keys:
|
|
trg.kMap.del vid
|
|
|
|
mergeAndReset(trg.sTab, src.sTab)
|
|
mergeAndReset(trg.kMap, src.kMap)
|
|
mergeAndReset(trg.accLeaves, src.accLeaves)
|
|
mergeAndReset(trg.stoLeaves, src.stoLeaves)
|
|
|
|
# func layersCc*(db: AristoDbRef; level = high(int)): LayerRef =
|
|
# ## Provide a collapsed copy of layers up to a particular transaction level.
|
|
# ## If the `level` argument is too large, the maximum transaction level is
|
|
# ## returned.
|
|
# ##
|
|
# let layers = if db.stack.len <= level: db.stack & @[db.top]
|
|
# else: db.stack[0 .. level]
|
|
|
|
# # Set up initial layer (bottom layer)
|
|
# result = LayerRef(
|
|
# sTab: layers[0].sTab.dup, # explicit dup for ref values
|
|
# kMap: layers[0].kMap,
|
|
# vTop: layers[^1].vTop,
|
|
# accLeaves: layers[0].accLeaves,
|
|
# stoLeaves: layers[0].stoLeaves)
|
|
|
|
# # Consecutively merge other layers on top
|
|
# for n in 1 ..< layers.len:
|
|
# for (vid,vtx) in layers[n].sTab.pairs:
|
|
# result.sTab[vid] = vtx
|
|
# result.kMap.del vid
|
|
# for (vid,key) in layers[n].kMap.pairs:
|
|
# result.kMap[vid] = key
|
|
# for (accPath,vtx) in layers[n].accLeaves.pairs:
|
|
# result.accLeaves[accPath] = vtx
|
|
# for (mixPath,vtx) in layers[n].stoLeaves.pairs:
|
|
# result.stoLeaves[mixPath] = vtx
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public iterators
|
|
# ------------------------------------------------------------------------------
|
|
|
|
iterator layersWalkVtx*(
|
|
db: AristoTxRef;
|
|
seen: var HashSet[VertexID];
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
|
## Walk over all `(VertexID,VertexRef)` pairs on the cache layers. Note that
|
|
## entries are unsorted.
|
|
##
|
|
## The argument `seen` collects a set of all visited vertex IDs including
|
|
## the one with a zero vertex which are othewise skipped by the iterator.
|
|
## The `seen` argument must not be modified while the iterator is active.
|
|
##
|
|
for w, _ in db.rstack:
|
|
for (rvid,vtx) in w.sTab.pairs:
|
|
if rvid.vid notin seen:
|
|
yield (rvid,vtx)
|
|
seen.incl rvid.vid
|
|
|
|
iterator layersWalkVtx*(
|
|
db: AristoTxRef;
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
|
## Variant of `layersWalkVtx()`.
|
|
var seen: HashSet[VertexID]
|
|
for (rvid,vtx) in db.layersWalkVtx seen:
|
|
yield (rvid,vtx)
|
|
|
|
|
|
iterator layersWalkKey*(
|
|
db: AristoTxRef;
|
|
): tuple[rvid: RootedVertexID, key: HashKey] =
|
|
## Walk over all `(VertexID,HashKey)` pairs on the cache layers. Note that
|
|
## entries are unsorted.
|
|
var seen: HashSet[VertexID]
|
|
for w, _ in db.rstack:
|
|
for (rvid,key) in w.kMap.pairs:
|
|
if rvid.vid notin seen:
|
|
yield (rvid,key)
|
|
seen.incl rvid.vid
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|