mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-03-01 12:20:49 +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
88 lines
2.5 KiB
Nim
88 lines
2.5 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.
|
|
|
|
## Kvt DB -- Transaction frames helper
|
|
## ===================================
|
|
##
|
|
{.push raises: [].}
|
|
|
|
import
|
|
./[kvt_desc, kvt_layers]
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
proc txFrameBegin*(db: KvtDbRef, parent: KvtTxRef): KvtTxRef =
|
|
## Starts a new transaction.
|
|
##
|
|
## Example:
|
|
## ::
|
|
## proc doSomething(db: KvtDbRef) =
|
|
## let tx = db.begin
|
|
## defer: tx.rollback()
|
|
## ... continue using db ...
|
|
## tx.commit()
|
|
##
|
|
|
|
let parent = if parent == nil: db.txRef else: parent
|
|
KvtTxRef(
|
|
db: db,
|
|
layer: LayerRef(),
|
|
parent: parent,
|
|
)
|
|
|
|
proc baseTxFrame*(db: KvtDbRef): KvtTxRef =
|
|
db.txRef
|
|
|
|
proc dispose*(
|
|
tx: KvtTxRef;
|
|
) =
|
|
|
|
tx[].reset()
|
|
|
|
proc txFramePersist*(
|
|
db: KvtDbRef;
|
|
batch: PutHdlRef;
|
|
txFrame: KvtTxRef;
|
|
) =
|
|
let be = db.backend
|
|
doAssert not be.isNil, "Persisting to backend requires ... a backend!"
|
|
|
|
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[])
|
|
frame.dispose()
|
|
|
|
# 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 k,v in txFrame.layer.sTab:
|
|
be.putKvpFn(batch, k, v)
|
|
# 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)
|
|
|
|
txFrame.layer.sTab.clear()
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|