mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-03-03 13:20:48 +00:00
* Simplify txFrame protocol, improve persist performance To prepare forked-layers for further surgery to avoid the nesting tax, the commit/rollback style of interacting must first be adjusted, since it does not provide a point in time where the frame is "done" and goes from being actively written to, to simply waiting to be persisted or discarded. A collateral benefit of this change is that the scheme removes some complexity from the process by moving the "last saved block number" into txframe along with the actual state changes thus reducing the risk that they go "out of sync" and removing the "commit" consolidation responsibility from ForkedChain. * commit/rollback become checkpoint/dispose - since these are pure in-memory constructs, there's less error handling and there's no real "rollback" involved - dispose better implies that the instance cannot be used and we can more aggressively clear the memory it uses * simplified block number handling that moves to become part of txFrame just like the data that the block number references * avoid reparenting step by replacing the base instead of keeping a singleton instance * persist builds the set of changes from the bottom which helps avoid moving changes in the top layers through each ancestor level of the frame stack * when using an in-memory database in tests, allow the instance to be passed around to enable testing persist and reload logic
118 lines
3.4 KiB
Nim
118 lines
3.4 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.
|
|
|
|
## Aristo DB -- Transaction frames helper
|
|
## ======================================
|
|
##
|
|
{.push raises: [].}
|
|
|
|
import
|
|
results,
|
|
./[aristo_desc, aristo_layers]
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
proc txFrameBegin*(db: AristoDbRef, parent: AristoTxRef): AristoTxRef =
|
|
let parent = if parent == nil:
|
|
db.txRef
|
|
else:
|
|
parent
|
|
|
|
let
|
|
vTop = parent.layer.vTop
|
|
layer = LayerRef(vTop: vTop)
|
|
|
|
AristoTxRef(
|
|
db: db,
|
|
parent: parent,
|
|
layer: layer)
|
|
|
|
proc baseTxFrame*(db: AristoDbRef): AristoTxRef=
|
|
db.txRef
|
|
|
|
proc dispose*(
|
|
tx: AristoTxRef;
|
|
) =
|
|
tx[].reset()
|
|
|
|
proc checkpoint*(
|
|
tx: AristoTxRef;
|
|
blockNumber: uint64;
|
|
) =
|
|
tx.blockNumber = Opt.some(blockNumber)
|
|
|
|
proc txFramePersist*(
|
|
db: AristoDbRef; # Database
|
|
batch: PutHdlRef;
|
|
txFrame: AristoTxRef;
|
|
) =
|
|
|
|
if txFrame == db.txRef and txFrame.layer.sTab.len == 0:
|
|
# No changes in frame - no `checkpoint` requirement - nothing to do here
|
|
return
|
|
|
|
let be = db.backend
|
|
doAssert not be.isNil, "Persisting to backend requires ... a backend!"
|
|
|
|
let lSst = SavedState(
|
|
key: emptyRoot, # placeholder for more
|
|
serial: txFrame.blockNumber.expect("`checkpoint` before persisting frame"))
|
|
|
|
# Squash all changes up to the base
|
|
if txFrame != db.txRef:
|
|
# Consolidate the changes from the old to the new base going from the
|
|
# bottom of the stack to avoid having to cascade each change through
|
|
# the full stack
|
|
assert txFrame.parent != nil
|
|
for frame in txFrame.stack():
|
|
if frame == db.txRef:
|
|
continue
|
|
mergeAndReset(db.txRef.layer[], frame.layer[])
|
|
db.txRef.blockNumber = frame.blockNumber
|
|
|
|
frame.dispose() # This will also dispose `txFrame` itself!
|
|
|
|
# Put the now-merged contents in txFrame and make it the new base
|
|
swap(db.txRef[], txFrame[])
|
|
db.txRef = txFrame
|
|
|
|
# Store structural single trie entries
|
|
for rvid, vtx in txFrame.layer.sTab:
|
|
txFrame.layer.kMap.withValue(rvid, key) do:
|
|
be.putVtxFn(batch, rvid, vtx, key[])
|
|
do:
|
|
be.putVtxFn(batch, rvid, vtx, default(HashKey))
|
|
|
|
be.putTuvFn(batch, txFrame.layer.vTop)
|
|
be.putLstFn(batch, lSst)
|
|
|
|
# TODO above, we only prepare the changes to the database but don't actually
|
|
# write them to disk - the code below that updates the frame should
|
|
# really run after things have been written (to maintain sync betweeen
|
|
# in-memory and on-disk state)
|
|
|
|
# Copy back updated payloads
|
|
for accPath, vtx in txFrame.layer.accLeaves:
|
|
db.accLeaves.put(accPath, vtx)
|
|
|
|
for mixPath, vtx in txFrame.layer.stoLeaves:
|
|
db.stoLeaves.put(mixPath, vtx)
|
|
|
|
txFrame.layer.sTab.clear()
|
|
txFrame.layer.kMap.clear()
|
|
txFrame.layer.accLeaves.clear()
|
|
txFrame.layer.stoLeaves.clear()
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|