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
Re-type FilterRef.src as HashKey
why:
  So it is directly comparable to `kMap[$1]`
  • Loading branch information
mjfh committed Jun 3, 2024
commit 9eacfebb04de60957f760d42d5b41ead8a9935d4
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_debug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ proc ppFilter(
if fl.isNil:
result &= " n/a"
return
result &= pfx & "src=" & fl.src.to(HashKey).ppKey(db)
result &= pfx & "src=" & fl.src.ppKey(db)
result &= pfx & "vGen" & pfx1 & "[" &
fl.vGen.mapIt(it.ppVid).join(",") & "]"
result &= pfx & "sTab" & pfx1 & "{"
Expand Down
10 changes: 5 additions & 5 deletions nimbus/db/aristo/aristo_delta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
##

import
std/[options, sequtils, sets, tables],
std/[sequtils, sets, tables],
eth/common,
results,
"."/[aristo_desc, aristo_get, aristo_vid],
Expand Down Expand Up @@ -73,14 +73,14 @@ proc deltaMerge*(
let ubeRoot = block:
let rc = db.getKeyUbe VertexID(1)
if rc.isOk:
rc.value.to(Hash256)
rc.value
elif rc.error == GetKeyNotFound:
EMPTY_ROOT_HASH
VOID_HASH_KEY
else:
return err((VertexID(1),rc.error))

db.roFilter = ? db.merge(filter, db.roFilter, ubeRoot)
if db.roFilter.src == db.roFilter.kMap.getOrVoid(VertexID 1).to(Hash256):
if db.roFilter.src == db.roFilter.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
Expand Down Expand Up @@ -142,7 +142,7 @@ proc deltaPersistent*(

let lSst = SavedState(
src: db.roFilter.src,
trg: db.roFilter.kMap.getOrVoid(VertexID 1).to(Hash256),
trg: db.roFilter.kMap.getOrVoid(VertexID 1),
serial: nxtFid)

# Store structural single trie entries
Expand Down
8 changes: 4 additions & 4 deletions nimbus/db/aristo/aristo_delta/delta_merge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ proc merge*(
db: AristoDbRef;
upper: FilterRef; # Src filter, `nil` is ok
lower: FilterRef; # Trg filter, `nil` is ok
beStateRoot: Hash256; # Merkle hash key
beStateRoot: HashKey; # Merkle hash key
): Result[FilterRef,(VertexID,AristoError)] =
## Merge argument `upper` into the `lower` filter instance.
##
Expand Down Expand Up @@ -58,7 +58,7 @@ proc merge*(
return ok(lower)

# Verify stackability
let lowerTrg = lower.kMap.getOrVoid(VertexID(1)).to(Hash256)
let lowerTrg = lower.kMap.getOrVoid VertexID(1)
if upper.src != lowerTrg:
return err((VertexID(0), FilTrgSrcMismatch))
if lower.src != beStateRoot:
Expand Down Expand Up @@ -96,7 +96,7 @@ proc merge*(
return err((vid,rc.error))

# Check consistency
if (newFilter.src == newFilter.kMap.getOrVoid(VertexID 1).to(Hash256)) !=
if (newFilter.src == newFilter.kMap.getOrVoid(VertexID 1)) !=
(newFilter.sTab.len == 0 and newFilter.kMap.len == 0):
return err((VertexID(0),FilSrcTrgInconsistent))

Expand All @@ -123,7 +123,7 @@ proc merge*(
return err((VertexID(0),FilNilFilterRejected))

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

Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_delta/delta_reverse.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ proc revFilter*(
## backend (excluding optionally installed read-only filter.)
##
# Register MPT state roots for reverting back
let rev = FilterRef(src: filter.kMap.getOrVoid(VertexID 1).to(Hash256))
let rev = FilterRef(src: filter.kMap.getOrVoid(VertexID 1))

# Get vid generator state on backend
block:
Expand Down
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 @@ -95,7 +95,7 @@ proc update*(ctx: UpdateSiblingsRef): Result[UpdateSiblingsRef,AristoError] =
# Update distributed filters. Note that the physical backend database
# must not have been updated, yet. So the new root key for the backend
# will be `db.roFilter.kMap[$1]`.
let trg = db.roFilter.kMap.getOrVoid(VertexID 1).to(Hash256)
let trg = db.roFilter.kMap.getOrVoid(VertexID 1)
for w in db.forked:
let rc = db.merge(w.roFilter, ctx.rev, trg)
if rc.isErr:
Expand Down
15 changes: 6 additions & 9 deletions nimbus/db/aristo/aristo_delta/delta_state_root.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import
type
LayerStateRoot* = tuple
## Helper structure for analysing state roots.
be: Hash256 ## Backend state root
fg: Hash256 ## Layer or filter implied state root
be: HashKey ## Backend state root
fg: HashKey ## Layer or filter implied state root

# ------------------------------------------------------------------------------
# Public functions
Expand All @@ -42,21 +42,18 @@ proc getLayerStateRoots*(
VOID_HASH_KEY
else:
return err(rc.error)
spr.be = sprBeKey.to(Hash256)

spr.fg = block:
let key = delta.kMap.getOrVoid VertexID(1)
if key.isValid:
key.to(Hash256)
else:
EMPTY_ROOT_HASH
spr.be = sprBeKey
spr.fg = delta.kMap.getOrVoid VertexID(1)

if spr.fg.isValid:
return ok(spr)

if not delta.kMap.hasKey(VertexID(1)) and
not delta.sTab.hasKey(VertexID(1)):
# This layer is unusable, need both: vertex and key
return err(FilPrettyPointlessLayer)

elif not delta.sTab.getOrVoid(VertexID(1)).isValid:
# Root key and vertex has been deleted
return ok(spr)
Expand Down
14 changes: 7 additions & 7 deletions nimbus/db/aristo/aristo_desc/desc_identifiers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ func `==`*(a, b: PathID): bool =
func cmp*(a, b: PathID): int =
if a < b: -1 elif b < a: 1 else: 0

# ------------------------------------------------------------------------------
# Public helpers: `HashKey` ordered scalar data model
# ------------------------------------------------------------------------------

func len*(lid: HashKey): int =
lid.len.int # if lid.isHash: 32 else: lid.blob.len

template data*(lid: HashKey): openArray[byte] =
lid.buf.toOpenArray(0, lid.len - 1)

Expand All @@ -174,13 +181,6 @@ func to*(lid: HashKey; T: type PathID): T =
else:
PathID()

# ------------------------------------------------------------------------------
# Public helpers: `HashKey` ordered scalar data model
# ------------------------------------------------------------------------------

func len*(lid: HashKey): int =
lid.len.int # if lid.isHash: 32 else: lid.blob.len

func fromBytes*(T: type HashKey; data: openArray[byte]): Result[T,void] =
## Write argument `data` of length 0 or between 2 and 32 bytes as a `HashKey`.
##
Expand Down
6 changes: 3 additions & 3 deletions nimbus/db/aristo/aristo_desc/desc_structural.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ type

SavedState* = object
## Last saved state
src*: Hash256 ## Previous state hash
trg*: Hash256 ## Last state hash
src*: HashKey ## Previous state hash
trg*: HashKey ## Last state hash
serial*: uint64 ## Generic identifier froom application

FilterRef* = ref object
## Delta layer
src*: Hash256 ## Applicable to this state root
src*: HashKey ## Applicable to this state root
sTab*: Table[VertexID,VertexRef] ## Filter structural vertex table
kMap*: Table[VertexID,HashKey] ## Filter Merkle hash key mapping
vGen*: seq[VertexID] ## Filter unique vertex ID generator
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/core_db/backend/aristo_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ proc toAristoSavedStateBlockNumber*(
if not mBe.isNil and mBe.parent.isAristo:
let rc = mBe.parent.AristoCoreDbRef.adbBase.getSavedState()
if rc.isOk:
return (rc.value.src, rc.value.serial.toBlockNumber)
return (rc.value.src.to(Hash256), rc.value.serial.toBlockNumber)
(EMPTY_ROOT_HASH, 0.toBlockNumber)

# ------------------------------------------------------------------------------
Expand Down