Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aristo cull journal related stuff #2288

Merged
merged 9 commits into from
Jun 3, 2024
Prev Previous commit
Next Next commit
Refactor way how to store data persistently
why:
  Avoid useless copy when staging `top` layer for persistently saving to
  backend.
  • Loading branch information
mjfh committed Jun 3, 2024
commit e6ccbf5b3f8aff614d3839f6da74d5f57d59bb3f
80 changes: 4 additions & 76 deletions nimbus/db/aristo/aristo_delta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,17 @@
##

import
std/[sequtils, sets, tables],
std/[sequtils, tables],
eth/common,
results,
"."/[aristo_desc, aristo_get, aristo_vid],
./aristo_desc,
./aristo_desc/desc_backend,
./aristo_delta/[delta_state_root, delta_merge, delta_siblings]
./aristo_delta/delta_siblings

# ------------------------------------------------------------------------------
# Public functions, construct filters
# Public functions, save to backend
# ------------------------------------------------------------------------------

proc deltaFwd*(
db: AristoDbRef; # Database
layer: LayerRef; # Layer to derive filter from
chunkedMpt = false; # Relax for snap/proof scenario
): Result[LayerDeltaRef,(VertexID,AristoError)] =
## Assemble forward delta, i.e. changes to the backend equivalent to applying
## the current top layer.
##
## Typically, the `layer` layer would reflect a change of the MPT but there
## is the case of partial MPTs sent over the network when synchronising (see
## `snap` protocol.) In this case, the state root might not see a change on
## the `layer` layer which would result in an error unless the argument
## `extendOK` is set `true`
##
## This delta is taken against the current backend including optional
## read-only filter.
##
# Register the Merkle hash keys of the MPT where this reverse filter will be
# applicable: `be => fg`
let (srcRoot, trgRoot) = block:
let rc = db.getLayerStateRoots(layer.delta, chunkedMpt)
if rc.isOK:
(rc.value.be, rc.value.fg)
elif rc.error == FilPrettyPointlessLayer:
return ok LayerDeltaRef(nil)
else:
return err((VertexID(1), rc.error))

ok LayerDeltaRef(
src: srcRoot,
sTab: layer.delta.sTab,
kMap: layer.delta.kMap,
vGen: layer.delta.vGen.vidReorg) # Compact recycled IDs

# ------------------------------------------------------------------------------
# Public functions, apply/install filters
# ------------------------------------------------------------------------------

proc deltaMerge*(
db: AristoDbRef; # Database
filter: LayerDeltaRef; # Filter to apply to database
): Result[void,(VertexID,AristoError)] =
## Merge the argument `filter` into the read-only filter layer. Note that
## this function has no control of the filter source. Having merged the
## argument `filter`, all the `top` and `stack` layers should be cleared.
##
let ubeRoot = block:
let rc = db.getKeyUbe VertexID(1)
if rc.isOk:
rc.value
elif rc.error == GetKeyNotFound:
VOID_HASH_KEY
else:
return err((VertexID(1),rc.error))

db.balancer = ? db.merge(filter, db.balancer, ubeRoot)
if db.balancer.src == db.balancer.kMap.getOrVoid(VertexID 1):
# Under normal conditions, the root keys cannot be the same unless the
# database is empty. This changes if there is a fixed root vertex as
# used with the `snap` sync protocol boundaty proof. In that case, there
# can be no history chain and the filter is just another cache.
if VertexID(1) notin db.top.final.pPrf:
db.balancer = LayerDeltaRef(nil)

ok()


proc deltaPersistentOk*(db: AristoDbRef): bool =
## Check whether the read-only filter can be merged into the backend
not db.backend.isNil and db.isCentre
Expand All @@ -113,11 +46,6 @@ proc deltaPersistent*(
## Also, other non-centre descriptors are updated so there is no visible
## database change for these descriptors.
##
## Caveat: This function will delete entries from the cascaded fifos if the
## current backend filter is the reverse compiled from the top item
## chain from the cascaded fifos as implied by the function
## `forkBackLog()`, for example.
##
let be = db.backend
if be.isNil:
return err(FilBackendMissing)
Expand Down
42 changes: 1 addition & 41 deletions nimbus/db/aristo/aristo_delta/delta_merge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import
# Public functions
# ------------------------------------------------------------------------------

proc merge*(
proc deltaMerge*(
db: AristoDbRef;
upper: LayerDeltaRef; # Src filter, `nil` is ok
lower: LayerDeltaRef; # Trg filter, `nil` is ok
Expand Down Expand Up @@ -102,46 +102,6 @@ proc merge*(

ok newFilter


proc merge*(
upper: LayerDeltaRef; # filter, not `nil`
lower: LayerDeltaRef; # filter, not `nil`
): Result[LayerDeltaRef,(VertexID,AristoError)] =
## Variant of `merge()` without optimising filters relative to the backend.
## Also, filter arguments `upper` and `lower` are expected not`nil`.
## Otherwise an error is returned.
##
## Comparing before and after merge
## ::
## arguments | merged result
## --------------------------------+--------------------------------
## (src2==trg1) --> upper --> trg2 |
## | (src1==trg0) --> newFilter --> trg2
## (src1==trg0) --> lower --> trg1 |
## |
if upper.isNil or lower.isNil:
return err((VertexID(0),FilNilFilterRejected))

# Verify stackability
let lowerTrg = lower.kMap.getOrVoid VertexID(1)
if upper.src != lowerTrg:
return err((VertexID(0), FilTrgSrcMismatch))

# There is no need to deep copy table vertices as they will not be modified.
let newFilter = LayerDeltaRef(
src: lower.src,
sTab: lower.sTab,
kMap: lower.kMap,
vGen: upper.vGen)

for (vid,vtx) in upper.sTab.pairs:
newFilter.sTab[vid] = vtx

for (vid,key) in upper.kMap.pairs:
newFilter.kMap[vid] = key

ok newFilter

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_delta/delta_siblings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ proc update*(ctx: UpdateSiblingsRef): Result[UpdateSiblingsRef,AristoError] =
# will be `db.balancer.kMap[$1]`.
let trg = db.balancer.kMap.getOrVoid(VertexID 1)
for w in db.forked:
let rc = db.merge(w.balancer, ctx.rev, trg)
let rc = db.deltaMerge(w.balancer, ctx.rev, trg)
if rc.isErr:
ctx.rollback()
return err(rc.error[1])
Expand Down
74 changes: 0 additions & 74 deletions nimbus/db/aristo/aristo_delta/delta_state_root.nim

This file was deleted.

21 changes: 3 additions & 18 deletions nimbus/db/aristo/aristo_desc/desc_error.nim
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,13 @@ type
DelVidStaleVtx

# Functions from `aristo_filter.nim`
FilBackStepsExpected
FilBackendMissing
FilBackendRoMode
FilDudeFilterUpdateError
FilExecDublicateSave
FilExecHoldExpected
FilExecOops
FilExecSaveMissing
FilExecStackUnderflow
FilFilterInvalid
FilFilterNotFound
FilInxByQidFailed
FilNegativeEpisode
FilNilFilterRejected
FilNoMatchOnFifo
FilPrettyPointlessLayer
FilQidByLeFidFailed
FilQuBespokeFidTooSmall
FilQuSchedDisabled
FilSiblingsCommitUnfinshed
FilSrcTrgInconsistent
FilStateRootMismatch
FilStateRootMissing
FilTrgSrcMismatch
FilTrgTopSrcMismatch

# Get functions from `aristo_get.nim`
GetLeafMissing
Expand Down Expand Up @@ -281,6 +263,9 @@ type
TxStackGarbled
TxStackUnderflow

TxPrettyPointlessLayer
TxStateRootMismatch

# Functions from `aristo_desc.nim`
MustBeOnCentre
NotAllowedOnCentre
Expand Down
Loading
Loading