2023-08-07 17:45:23 +00:00
|
|
|
# nimbus-eth1
|
2023-11-08 16:52:25 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2023-08-07 17:45:23 +00:00
|
|
|
# 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 interface
|
|
|
|
## ==================================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-09-15 15:23:53 +00:00
|
|
|
std/[sequtils, tables],
|
2023-08-07 17:45:23 +00:00
|
|
|
results,
|
2023-08-17 13:42:01 +00:00
|
|
|
"."/[aristo_desc, aristo_filter, aristo_get, aristo_hashify]
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
type
|
|
|
|
DoSpanPrepFn =
|
2023-09-18 20:20:28 +00:00
|
|
|
proc(db: AristoDbRef; flg: bool): Result[void,AristoError] {.gcsafe.}
|
2023-09-15 15:23:53 +00:00
|
|
|
|
|
|
|
DoSpanExecFn =
|
2023-09-18 20:20:28 +00:00
|
|
|
proc(db: AristoDbRef) {.gcsafe.}
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func isTop*(tx: AristoTxRef): bool
|
2023-09-15 15:23:53 +00:00
|
|
|
func level*(db: AristoDbRef): int
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
func fromVae(err: (VertexID,AristoError)): AristoError =
|
|
|
|
## Map error pair to error reason component
|
|
|
|
err[1]
|
2023-09-11 20:38:49 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func getDbDescFromTopTx(tx: AristoTxRef): Result[AristoDbRef,AristoError] =
|
|
|
|
if not tx.isTop():
|
|
|
|
return err(TxNotTopTx)
|
|
|
|
let db = tx.db
|
|
|
|
if tx.level != db.stack.len:
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxStackGarbled)
|
2023-08-11 17:23:57 +00:00
|
|
|
ok db
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
proc getTxUid(db: AristoDbRef): uint =
|
2023-08-11 17:23:57 +00:00
|
|
|
if db.txUidGen == high(uint):
|
|
|
|
db.txUidGen = 0
|
2023-08-07 17:45:23 +00:00
|
|
|
db.txUidGen.inc
|
|
|
|
db.txUidGen
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: Single descriptor transaction frame
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc txBeginPrepImpl(db: AristoDbRef): Result[void,AristoError] =
|
|
|
|
## Starts a new transaction.
|
|
|
|
##
|
|
|
|
if db.level != db.stack.len:
|
|
|
|
return err(TxStackGarbled)
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc txBeginExecImpl(db: AristoDbRef) =
|
|
|
|
## Starts a new transaction.
|
|
|
|
##
|
|
|
|
db.stack.add db.top.dup # push (save and use top later)
|
|
|
|
db.top.txUid = db.getTxUid()
|
|
|
|
|
|
|
|
db.txRef = AristoTxRef(
|
|
|
|
db: db,
|
|
|
|
txUid: db.top.txUid,
|
|
|
|
parent: db.txRef,
|
|
|
|
level: db.stack.len)
|
|
|
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
proc rollbackImpl(db: AristoDbRef) =
|
|
|
|
## Roll back to previous layer.
|
|
|
|
##
|
|
|
|
db.top = db.stack[^1]
|
|
|
|
db.stack.setLen(db.stack.len-1)
|
|
|
|
db.txRef = db.txRef.parent # `db.txRef` needs to be checked by caller
|
|
|
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
proc commitPrepImpl(
|
|
|
|
db: AristoDbRef; # Top transaction on database
|
|
|
|
dontHashify: bool; # Process/fix MPT hashes
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Commit transaction layer.
|
|
|
|
##
|
|
|
|
if db.top.dirty and not dontHashify:
|
|
|
|
discard ? db.hashify().mapErr fromVae
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc commitExecImpl(db: AristoDbRef) =
|
|
|
|
## Commit transaction layer.
|
|
|
|
##
|
|
|
|
# Keep top and discard layer below
|
|
|
|
db.top.txUid = db.stack[^1].txUid
|
|
|
|
db.stack.setLen(db.stack.len-1)
|
|
|
|
db.txRef = db.txRef.parent # `db.txRef` needs to be checked by caller
|
|
|
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
proc collapseCommitPrepImpl(
|
|
|
|
db: AristoDbRef;
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
# For commit, hashify the current layer, otherwise the stack bottom layer.
|
|
|
|
# install the stack bottom.
|
|
|
|
if db.top.dirty and not dontHashify:
|
|
|
|
discard ? db.hashify().mapErr fromVae
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc collapseRollbackPrepImpl(
|
|
|
|
db: AristoDbRef;
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
# Rollback hashify the current layer, otherwise the stack bottom layer.
|
|
|
|
# install the stack bottom.
|
|
|
|
if db.top.dirty and not dontHashify:
|
|
|
|
db.stack[0].swap db.top
|
|
|
|
defer: db.stack[0].swap db.top
|
|
|
|
discard ? db.hashify().mapErr fromVae
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc collapseCommitExecImpl(db: AristoDbRef) =
|
|
|
|
# If commit, then leave the current layer and clear the stack, oterwise
|
|
|
|
# install the stack bottom.
|
|
|
|
db.top.txUid = 0
|
|
|
|
db.stack.setLen(0)
|
|
|
|
db.txRef = AristoTxRef(nil)
|
|
|
|
|
|
|
|
proc collapseRollbackExecImpl(db: AristoDbRef) =
|
|
|
|
db.stack[0].swap db.top
|
|
|
|
db.top.txUid = 0
|
|
|
|
db.stack.setLen(0)
|
|
|
|
db.txRef = AristoTxRef(nil)
|
|
|
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
proc doSpan(
|
|
|
|
db: AristoDbRef; # Top transaction on database
|
|
|
|
prepFn = DoSpanPrepFn(nil); # Optional preparation layer
|
|
|
|
prepFlag = false; # `prepFn` argument
|
|
|
|
execFn: DoSpanExecFn; # Mandatory execution layer
|
2023-09-18 20:20:28 +00:00
|
|
|
): Result[void,AristoError]
|
|
|
|
{.gcsafe.} =
|
2023-09-15 15:23:53 +00:00
|
|
|
## Common execution framework for `rollbackImpl()` or `commitImpl()` over
|
|
|
|
## all descriptors in the transaction span.
|
|
|
|
##
|
|
|
|
if not prepFn.isNil:
|
|
|
|
var
|
|
|
|
revert: Table[AristoDbRef,LayerRef]
|
|
|
|
defer:
|
|
|
|
# Restore previous layer
|
|
|
|
for (dude,top) in revert.pairs:
|
|
|
|
dude.top = top
|
|
|
|
|
|
|
|
for dude in db.txSpan:
|
|
|
|
if dude.stack.len == 0 or
|
|
|
|
dude.stack.len != dude.txRef.level or
|
|
|
|
dude.top.txUid != dude.txRef.txUid:
|
|
|
|
return err(TxStackGarbled)
|
|
|
|
let keep = db.top
|
|
|
|
? dude.prepFn prepFlag # Preparation function
|
|
|
|
revert[dude] = keep
|
|
|
|
revert.clear # Done, no restoring
|
|
|
|
|
|
|
|
for dude in db.txSpan:
|
|
|
|
dude.execFn() # Commit function
|
|
|
|
|
|
|
|
if db.level == 0:
|
|
|
|
db.txSpanClear()
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc doThisPrep(
|
|
|
|
db: AristoDbRef; # Top transaction on database
|
|
|
|
prepFn = DoSpanPrepFn(nil); # Mandatory preparation layer function
|
|
|
|
prepFlag = false; # `prepFn` argument
|
2023-09-18 20:20:28 +00:00
|
|
|
): Result[void,AristoError]
|
|
|
|
{.gcsafe.} =
|
2023-09-15 15:23:53 +00:00
|
|
|
## ..
|
|
|
|
let
|
|
|
|
keep = db.top
|
|
|
|
rc = db.prepFn prepFlag
|
|
|
|
if rc.isErr:
|
|
|
|
db.top = keep
|
|
|
|
return err(rc.error)
|
|
|
|
ok()
|
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func txTop*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Getter, returns top level transaction if there is any.
|
|
|
|
if db.txRef.isNil:
|
|
|
|
err(TxNoPendingTx)
|
|
|
|
else:
|
|
|
|
ok(db.txRef)
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func isTop*(tx: AristoTxRef): bool =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Getter, returns `true` if the argument `tx` referes to the current top
|
|
|
|
## level transaction.
|
|
|
|
tx.db.txRef == tx and tx.db.top.txUid == tx.txUid
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func level*(tx: AristoTxRef): int =
|
|
|
|
## Getter, positive nesting level of transaction argument `tx`
|
|
|
|
tx.level
|
|
|
|
|
|
|
|
func level*(db: AristoDbRef): int =
|
|
|
|
## Getter, non-negative nesting level (i.e. number of pending transactions)
|
|
|
|
if not db.txRef.isNil:
|
|
|
|
result = db.txRef.level
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
func to*(tx: AristoTxRef; T: type[AristoDbRef]): T =
|
|
|
|
## Getter, retrieves the parent database descriptor from argument `tx`
|
2023-08-07 17:45:23 +00:00
|
|
|
tx.db
|
|
|
|
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
proc forkTx*(
|
|
|
|
tx: AristoTxRef; # Transaction descriptor
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[AristoDbRef,AristoError] =
|
2023-09-11 20:38:49 +00:00
|
|
|
## Clone a transaction into a new DB descriptor accessing the same backend
|
|
|
|
## (if any) database as the argument `db`. The new descriptor is linked to
|
|
|
|
## the transaction parent and is fully functional as a forked instance (see
|
|
|
|
## comments on `aristo_desc.reCentre()` for details.)
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
2023-09-11 20:38:49 +00:00
|
|
|
## The new DB descriptor will contain a copy of the argument transaction
|
|
|
|
## `tx` as top layer of level 1 (i.e. this is he only transaction.) Rolling
|
|
|
|
## back will end up at the backend layer (incl. backend filter.)
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
2023-09-18 20:20:28 +00:00
|
|
|
## If the arguent flag `dontHashify` is passed `true`, the clone descriptor
|
|
|
|
## will *NOT* be hashified right after construction.
|
|
|
|
##
|
2023-09-11 20:38:49 +00:00
|
|
|
## Use `aristo_desc.forget()` to clean up this descriptor.
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
|
|
|
let db = tx.db
|
|
|
|
|
|
|
|
# Provide new top layer
|
2023-08-18 19:46:55 +00:00
|
|
|
var topLayer: LayerRef
|
2023-08-17 13:42:01 +00:00
|
|
|
if db.txRef == tx:
|
|
|
|
topLayer = db.top.dup
|
|
|
|
elif tx.level < db.stack.len:
|
|
|
|
topLayer = db.stack[tx.level].dup
|
|
|
|
else:
|
|
|
|
return err(TxArgStaleTx)
|
|
|
|
if topLayer.txUid != tx.txUid:
|
|
|
|
return err(TxArgStaleTx)
|
|
|
|
topLayer.txUid = 1
|
|
|
|
|
|
|
|
# Empty stack
|
|
|
|
let stackLayer = block:
|
|
|
|
let rc = db.getIdgBE()
|
|
|
|
if rc.isOk:
|
2023-08-18 19:46:55 +00:00
|
|
|
LayerRef(vGen: rc.value)
|
2023-08-17 13:42:01 +00:00
|
|
|
elif rc.error == GetIdgNotFound:
|
2023-08-18 19:46:55 +00:00
|
|
|
LayerRef()
|
2023-08-17 13:42:01 +00:00
|
|
|
else:
|
|
|
|
return err(rc.error)
|
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
let txClone = ? db.fork(rawToplayer = true)
|
|
|
|
|
2023-08-17 13:42:01 +00:00
|
|
|
# Set up clone associated to `db`
|
2023-09-11 20:38:49 +00:00
|
|
|
txClone.top = topLayer # is a deep copy
|
|
|
|
txClone.stack = @[stackLayer]
|
|
|
|
txClone.roFilter = db.roFilter # no need to copy contents (done when updated)
|
|
|
|
txClone.backend = db.backend
|
|
|
|
txClone.txUidGen = 1
|
2023-08-17 13:42:01 +00:00
|
|
|
|
|
|
|
# Install transaction similar to `tx` on clone
|
|
|
|
txClone.txRef = AristoTxRef(
|
|
|
|
db: txClone,
|
|
|
|
txUid: 1,
|
|
|
|
level: 1)
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
if db.top.dirty and not dontHashify:
|
|
|
|
let rc = txClone.hashify()
|
|
|
|
if rc.isErr:
|
|
|
|
discard txClone.forget()
|
|
|
|
return err(rc.error.fromVae)
|
|
|
|
|
2023-08-17 13:42:01 +00:00
|
|
|
ok(txClone)
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
|
|
|
|
proc forkTop*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
): Result[AristoDbRef,AristoError] =
|
2023-09-11 20:38:49 +00:00
|
|
|
## Variant of `forkTx()` for the top transaction if there is any. Otherwise
|
|
|
|
## the top layer is cloned, only.
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
2023-09-11 20:38:49 +00:00
|
|
|
## Use `aristo_desc.forget()` to clean up this descriptor.
|
2023-08-17 13:42:01 +00:00
|
|
|
##
|
|
|
|
if db.txRef.isNil:
|
2023-09-11 20:38:49 +00:00
|
|
|
let dbClone = ? db.fork(rawToplayer = true)
|
|
|
|
|
|
|
|
dbClone.top = db.top.dup # is a deep copy
|
|
|
|
dbClone.roFilter = db.roFilter # no need to copy contents when updated
|
|
|
|
dbClone.backend = db.backend
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
if db.top.dirty and not dontHashify:
|
|
|
|
let rc = dbClone.hashify()
|
|
|
|
if rc.isErr:
|
|
|
|
discard dbClone.forget()
|
|
|
|
return err(rc.error.fromVae)
|
2023-08-17 13:42:01 +00:00
|
|
|
return ok(dbClone)
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
db.txRef.forkTx dontHashify
|
2023-08-17 13:42:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc exec*(
|
|
|
|
tx: AristoTxRef;
|
|
|
|
action: AristoDbAction;
|
2023-09-18 20:20:28 +00:00
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
2023-09-26 09:21:13 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-09-18 20:20:28 +00:00
|
|
|
## Execute function argument `action()` on a temporary `tx.forkTx()`
|
|
|
|
## transaction clone database. After return, the temporary database gets
|
2023-08-17 13:42:01 +00:00
|
|
|
## destroyed.
|
|
|
|
##
|
2023-09-18 20:20:28 +00:00
|
|
|
## If the arguent flag `dontHashify` is passed `true`, the clone database
|
|
|
|
## will *NOT* be hashified right after construction.
|
|
|
|
##
|
|
|
|
let db = ? tx.forkTx dontHashify
|
2023-08-17 13:42:01 +00:00
|
|
|
db.action()
|
2023-09-11 20:38:49 +00:00
|
|
|
? db.forget()
|
2023-08-07 17:45:23 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: Transaction frame
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
proc txBegin*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Starts a new transaction.
|
|
|
|
##
|
|
|
|
## Example:
|
|
|
|
## ::
|
|
|
|
## proc doSomething(db: AristoDbRef) =
|
|
|
|
## let tx = db.begin
|
|
|
|
## defer: tx.rollback()
|
|
|
|
## ... continue using db ...
|
|
|
|
## tx.commit()
|
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
if not db.inTxSpan:
|
|
|
|
? db.txBeginPrepImpl()
|
|
|
|
db.txBeginExecImpl()
|
2023-08-11 17:23:57 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
elif not db.isCentre:
|
|
|
|
return err(TxSpanOffCentre)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
else:
|
|
|
|
for dude in db.txSpan:
|
|
|
|
? dude.txBeginPrepImpl() # Only check, no need to restore
|
|
|
|
for dude in db.txSpan:
|
|
|
|
dude.txBeginExecImpl()
|
2023-08-11 17:23:57 +00:00
|
|
|
|
|
|
|
ok db.txRef
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
proc txBeginSpan*(db: AristoDbRef): Result[AristoTxRef,AristoError] =
|
|
|
|
## Start a new transaction simultaneously on all descriptors accessing the
|
|
|
|
## same backend.
|
|
|
|
##
|
|
|
|
## This function must be run on the centre argument descriptor `db` (see
|
|
|
|
## comments on `aristo_desc.reCentre()` for details.) This function is
|
|
|
|
## effective only when there is no transaction opened, yet. Sub-transactions
|
|
|
|
## are handled by `txBegin()` accordingly.
|
|
|
|
##
|
|
|
|
## When starting sub-transactions outside a transaction span, these
|
|
|
|
## transactions are handled independently.
|
|
|
|
##
|
|
|
|
## Example:
|
|
|
|
## ::
|
|
|
|
## let
|
|
|
|
## tx = db.txBeginSpan # includes all forked descriptors
|
|
|
|
## ty = db.txBegin # includes all forked descriptors
|
|
|
|
##
|
|
|
|
## tmpDb = tx.forkTx # outside transaction span
|
|
|
|
## tz = tmpDb.txBegin # outside transaction span
|
|
|
|
##
|
|
|
|
if not db.isCentre:
|
|
|
|
return err(TxSpanOffCentre)
|
|
|
|
|
|
|
|
if 0 < db.nForked:
|
|
|
|
if db.level == 0:
|
|
|
|
if 0 < db.nTxSpan:
|
|
|
|
return err(TxGarbledSpan)
|
|
|
|
db.forked.toSeq.txSpanSet
|
|
|
|
|
|
|
|
db.txBegin
|
|
|
|
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc rollback*(
|
|
|
|
tx: AristoTxRef; # Top transaction on database
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Given a *top level* handle, this function discards all database operations
|
|
|
|
## performed for this transactio. The previous transaction is returned if
|
|
|
|
## there was any.
|
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
let db = ? tx.getDbDescFromTopTx()
|
|
|
|
if not db.inTxSpan:
|
|
|
|
db.rollbackImpl()
|
|
|
|
return ok()
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
if not db.isCentre:
|
|
|
|
return err(TxSpanOffCentre)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
db.doSpan(execFn = rollbackImpl)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
|
|
|
|
proc commit*(
|
|
|
|
tx: AristoTxRef; # Top transaction on database
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Given a *top level* handle, this function accepts all database operations
|
|
|
|
## performed through this handle and merges it to the previous layer. The
|
|
|
|
## previous transaction is returned if there was any.
|
|
|
|
##
|
2023-08-11 17:23:57 +00:00
|
|
|
## Unless the argument `dontHashify` is set `true`, the function will process
|
|
|
|
## Merkle Patricia Treee hashes unless there was no change to this layer.
|
|
|
|
## This may produce additional errors (see `hashify()`.)
|
2023-09-15 15:23:53 +00:00
|
|
|
##
|
|
|
|
let db = ? tx.getDbDescFromTopTx()
|
|
|
|
if not db.inTxSpan:
|
|
|
|
? db.doThisPrep(commitPrepImpl, dontHashify)
|
|
|
|
db.commitExecImpl()
|
|
|
|
return ok()
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
if not db.isCentre:
|
|
|
|
return err(TxSpanOffCentre)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
db.doSpan(
|
|
|
|
prepFn = commitPrepImpl,
|
|
|
|
prepFlag = dontHashify,
|
|
|
|
execFn = commitExecImpl)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc collapse*(
|
2023-08-11 17:23:57 +00:00
|
|
|
tx: AristoTxRef; # Top transaction on database
|
|
|
|
commit: bool; # Commit if `true`, otherwise roll back
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-07 17:45:23 +00:00
|
|
|
## Iterated application of `commit()` or `rollback()` performing the
|
|
|
|
## something similar to
|
|
|
|
## ::
|
2023-08-11 17:23:57 +00:00
|
|
|
## while true:
|
|
|
|
## discard tx.commit() # ditto for rollback()
|
|
|
|
## if db.topTx.isErr: break
|
|
|
|
## tx = db.topTx.value
|
2023-08-07 17:45:23 +00:00
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
## The `dontHashify` flag is treated as described for `commit()`
|
|
|
|
##
|
|
|
|
let db = ? tx.getDbDescFromTopTx()
|
|
|
|
if not db.inTxSpan:
|
|
|
|
if commit:
|
|
|
|
? db.doThisPrep(collapseCommitPrepImpl, dontHashify)
|
|
|
|
db.collapseCommitExecImpl()
|
|
|
|
else:
|
|
|
|
? db.doThisPrep(collapseRollbackPrepImpl, dontHashify)
|
|
|
|
db.collapseRollbackExecImpl()
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
if not db.isCentre:
|
|
|
|
return err(TxSpanOffCentre)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
db.doSpan(
|
|
|
|
prepFn = collapseCommitPrepImpl,
|
|
|
|
prepFlag = dontHashify,
|
|
|
|
execFn = collapseCommitExecImpl)
|
|
|
|
else:
|
|
|
|
db.doSpan(
|
|
|
|
prepFn = collapseRollbackPrepImpl,
|
|
|
|
prepFlag = dontHashify,
|
|
|
|
execFn = collapseRollbackExecImpl)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: save database
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
proc stow*(
|
2023-08-11 17:23:57 +00:00
|
|
|
db: AristoDbRef; # Database
|
|
|
|
persistent = false; # Stage only unless `true`
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
chunkedMpt = false; # Partial data (e.g. from `snap`)
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## If there is no backend while the `persistent` argument is set `true`,
|
2023-09-12 18:45:12 +00:00
|
|
|
## the function returns immediately with an error. The same happens if there
|
2023-08-17 13:42:01 +00:00
|
|
|
## is a pending transaction.
|
2023-08-11 17:23:57 +00:00
|
|
|
##
|
|
|
|
## The `dontHashify` is treated as described for `commit()`.
|
2023-08-10 20:01:28 +00:00
|
|
|
##
|
2023-08-11 17:23:57 +00:00
|
|
|
## The function then merges the data from the top layer cache into the
|
2023-08-10 20:01:28 +00:00
|
|
|
## backend stage area. After that, the top layer cache is cleared.
|
|
|
|
##
|
2023-08-11 17:23:57 +00:00
|
|
|
## Staging the top layer cache might fail withh a partial MPT when it is
|
|
|
|
## set up from partial MPT chunks as it happens with `snap` sync processing.
|
|
|
|
## In this case, the `chunkedMpt` argument must be set `true` (see alse
|
|
|
|
## `fwdFilter`.)
|
|
|
|
##
|
|
|
|
## If the argument `persistent` is set `true`, all the staged data are merged
|
|
|
|
## into the physical backend database and the staged data area is cleared.
|
2023-08-10 20:01:28 +00:00
|
|
|
##
|
2023-08-17 13:42:01 +00:00
|
|
|
if not db.txRef.isNil:
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxPendingTx)
|
2023-08-17 13:42:01 +00:00
|
|
|
if 0 < db.stack.len:
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxStackGarbled)
|
2023-09-11 20:38:49 +00:00
|
|
|
if persistent and not db.canResolveBackendFilter():
|
2023-09-15 15:23:53 +00:00
|
|
|
return err(TxBackendNotWritable)
|
2023-08-17 13:42:01 +00:00
|
|
|
|
|
|
|
if db.top.dirty and not dontHashify:
|
2023-09-15 15:23:53 +00:00
|
|
|
discard ? db.hashify().mapErr fromVae
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
let fwd = ? db.fwdFilter(db.top, chunkedMpt).mapErr fromVae
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
if fwd.isValid:
|
|
|
|
# Merge `top` layer into `roFilter`
|
2023-09-15 15:23:53 +00:00
|
|
|
? db.merge(fwd).mapErr fromVae
|
2023-08-18 19:46:55 +00:00
|
|
|
db.top = LayerRef(vGen: db.roFilter.vGen)
|
2023-08-17 13:42:01 +00:00
|
|
|
|
|
|
|
if persistent:
|
2023-09-15 15:23:53 +00:00
|
|
|
? db.resolveBackendFilter()
|
2023-08-18 19:46:55 +00:00
|
|
|
db.roFilter = FilterRef(nil)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
# Delete or clear stack and clear top
|
|
|
|
db.stack.setLen(0)
|
2023-08-18 19:46:55 +00:00
|
|
|
db.top = LayerRef(vGen: db.top.vGen, txUid: db.top.txUid)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
proc stow*(
|
2023-08-11 17:23:57 +00:00
|
|
|
db: AristoDbRef; # Database
|
|
|
|
stageLimit: int; # Policy based persistent storage
|
|
|
|
dontHashify = false; # Process/fix MPT hashes
|
|
|
|
chunkedMpt = false; # Partial data (e.g. from `snap`)
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[void,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Variant of `stow()` with the `persistent` argument replaced by
|
|
|
|
## `stageLimit < max(db.roFilter.bulk, db.top.bulk)`.
|
|
|
|
db.stow(
|
|
|
|
persistent = (stageLimit < max(db.roFilter.bulk, db.top.bulk)),
|
|
|
|
dontHashify = dontHashify,
|
|
|
|
chunkedMpt = chunkedMpt)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|