fix level reporting (#3085)

Oops, level 0 was always used which needlessly increases mem usage -
comes with an assortment of simplifications
This commit is contained in:
Jacek Sieka 2025-02-18 02:01:44 +01:00 committed by GitHub
parent 816ce73a2a
commit 3732b3f95e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 12 deletions

View File

@ -73,8 +73,7 @@ type
accLeaves*: Table[Hash32, VertexRef] ## Account path -> VertexRef
stoLeaves*: Table[Hash32, VertexRef] ## Storage path -> VertexRef
cTop*: VertexID ## Last committed vertex ID
blockNumber*: Opt[uint64] ## Block number set when freezing the frame
blockNumber*: Opt[uint64] ## Block number set when checkpointing the frame
AristoDbRef* = ref object
## Three tier database object supporting distributed instances.
@ -202,6 +201,7 @@ iterator rstack*(tx: AristoTxRef): (AristoTxRef, int) =
let level = if tx.parent == nil: -1 else: i
yield (tx, level)
tx = tx.parent
i += 1
proc deltaAtLevel*(db: AristoTxRef, level: int): AristoTxRef =
if level == -2:

View File

@ -124,6 +124,7 @@ func isEmpty*(ly: AristoTxRef): bool =
proc mergeAndReset*(trg, src: AristoTxRef) =
## Merges the argument `src` into the argument `trg` and clears `src`.
trg.vTop = src.vTop
trg.blockNumber = src.blockNumber
if trg.kMap.len > 0:
# Invalidate cached keys in the lower layer

View File

@ -73,7 +73,6 @@ proc txFramePersist*(
if frame == db.txRef:
continue
mergeAndReset(db.txRef, frame)
db.txRef.blockNumber = frame.blockNumber
frame.dispose() # This will also dispose `txFrame` itself!
@ -82,7 +81,7 @@ proc txFramePersist*(
db.txRef = txFrame
# Store structural single trie entries
for rvid, vtx in db.txRef.sTab:
for rvid, vtx in txFrame.sTab:
txFrame.kMap.withValue(rvid, key) do:
be.putVtxFn(batch, rvid, vtx, key[])
do:
@ -107,6 +106,7 @@ proc txFramePersist*(
txFrame.kMap.clear()
txFrame.accLeaves.clear()
txFrame.stoLeaves.clear()
txFrame.blockNumber.reset()
# ------------------------------------------------------------------------------
# End

View File

@ -221,13 +221,6 @@ proc getScore*(
warn info, data = data.toHex(), error=exc.msg
Opt.none(UInt256)
proc setScore*(db: CoreDbTxRef; blockHash: Hash32, score: UInt256) =
## for testing purpose
let scoreKey = blockHashToScoreKey blockHash
db.put(scoreKey.toOpenArray, rlp.encode(score)).isOkOr:
warn "setScore()", scoreKey, error=($$error)
return
proc headTotalDifficulty*(
db: CoreDbTxRef;
): UInt256 =

View File

@ -19,11 +19,13 @@ import
aristo_delete,
aristo_desc,
aristo_fetch,
aristo_tx_frame,
aristo_hike,
aristo_init,
aristo_init/memory_db,
aristo_layers,
aristo_merge,
aristo_persist,
aristo_tx_frame,
]
proc makeAccount(i: uint64): (Hash32, AristoAccount) =
@ -34,6 +36,7 @@ proc makeAccount(i: uint64): (Hash32, AristoAccount) =
const
acc1 = makeAccount(1)
acc2 = makeAccount(2)
acc3 = makeAccount(3)
suite "Aristo TxFrame":
setup:
@ -47,12 +50,14 @@ suite "Aristo TxFrame":
tx1 = db.txFrameBegin(tx0)
tx2 = db.txFrameBegin(tx1)
tx2b = db.txFrameBegin(tx1)
tx2c = db.txFrameBegin(tx1)
check:
tx0.mergeAccountRecord(acc1[0], acc1[1]).isOk()
tx1.mergeAccountRecord(acc2[0], acc2[1]).isOk()
tx2.deleteAccountRecord(acc2[0]).isOk()
tx2b.deleteAccountRecord(acc1[0]).isOk()
tx2c.mergeAccountRecord(acc2[0], acc3[1]).isOk()
check:
tx0.fetchAccountRecord(acc1[0]).isOk()
@ -68,6 +73,15 @@ suite "Aristo TxFrame":
tx0.fetchStateRoot() != tx1.fetchStateRoot()
tx0.fetchStateRoot() == tx2.fetchStateRoot()
var acc1Hike: Hike
check:
tx2c.fetchAccountHike(acc1[0], acc1Hike).isOk()
# The vid for acc1 gets created in tx1 because it has to move to a new
# mpt node from the root - tx2c updates only data, so the level at which
# we find the vtx should be one below tx2c!
tx2c.layersGetVtx((VertexID(1), acc1Hike.legs[^1].wp.vid)).value()[1] == 1
tx2.checkpoint(1)
let batch = db.backend.putBegFn().expect("working batch")
db.persist(batch, tx2)