2024-05-03 17:38:17 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2023-2024 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 stow/save helper
|
|
|
|
## =========================================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-04 15:05:13 +00:00
|
|
|
std/tables,
|
2024-05-03 17:38:17 +00:00
|
|
|
results,
|
2024-06-03 20:10:35 +00:00
|
|
|
../aristo_delta/delta_merge,
|
2024-07-17 19:27:33 +00:00
|
|
|
".."/[aristo_desc, aristo_delta]
|
2024-05-03 17:38:17 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc txStow*(
|
|
|
|
db: AristoDbRef; # Database
|
2024-06-03 20:10:35 +00:00
|
|
|
nxtSid: uint64; # Next state ID (aka block number)
|
2024-05-03 17:38:17 +00:00
|
|
|
persistent: bool; # Stage only unless `true`
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Worker for `stow()` and `persist()` variants.
|
|
|
|
##
|
|
|
|
if not db.txRef.isNil:
|
|
|
|
return err(TxPendingTx)
|
|
|
|
if 0 < db.stack.len:
|
|
|
|
return err(TxStackGarbled)
|
2024-06-03 20:10:35 +00:00
|
|
|
if persistent and not db.deltaPersistentOk():
|
2024-05-03 17:38:17 +00:00
|
|
|
return err(TxBackendNotWritable)
|
|
|
|
|
2024-07-17 19:27:33 +00:00
|
|
|
if db.top.delta.sTab.len != 0 or
|
|
|
|
db.top.delta.kMap.len != 0 or
|
|
|
|
db.top.delta.accLeaves.len != 0 or
|
|
|
|
db.top.delta.stoLeaves.len != 0:
|
2024-05-03 17:38:17 +00:00
|
|
|
|
2024-07-17 19:27:33 +00:00
|
|
|
# Note that `deltaMerge()` will return the 1st argument if the 2nd is `nil`
|
|
|
|
db.balancer = db.deltaMerge(db.top.delta, db.balancer).valueOr:
|
|
|
|
return err(error[1])
|
2024-06-03 20:10:35 +00:00
|
|
|
|
2024-07-17 19:27:33 +00:00
|
|
|
# New empty top layer
|
|
|
|
db.top = LayerRef(delta: LayerDeltaRef(vTop: db.balancer.vTop))
|
2024-05-03 17:38:17 +00:00
|
|
|
|
|
|
|
if persistent:
|
2024-07-17 19:27:33 +00:00
|
|
|
# Merge/move `balancer` into persistent tables (unless missing)
|
2024-06-03 20:10:35 +00:00
|
|
|
? db.deltaPersistent nxtSid
|
2024-05-03 17:38:17 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|