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
|
|
|
|
results,
|
2024-06-03 20:10:35 +00:00
|
|
|
../aristo_delta/delta_merge,
|
2024-07-18 21:32:32 +00:00
|
|
|
".."/[aristo_desc, aristo_delta, aristo_layers]
|
2024-05-03 17:38:17 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-07-18 21:32:32 +00:00
|
|
|
# Private functions
|
2024-05-03 17:38:17 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
proc txStowOk*(
|
2024-05-03 17:38:17 +00:00
|
|
|
db: AristoDbRef; # Database
|
|
|
|
persistent: bool; # Stage only unless `true`
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
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-18 21:32:32 +00:00
|
|
|
ok()
|
2024-05-03 17:38:17 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc txStow*(
|
|
|
|
db: AristoDbRef; # Database
|
|
|
|
nxtSid: uint64; # Next state ID (aka block number)
|
|
|
|
persistent: bool; # Stage only unless `true`
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Worker for `stow()` and `persist()` variants.
|
|
|
|
##
|
|
|
|
? db.txStowOk persistent
|
2024-05-03 17:38:17 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
if not db.top.isEmpty():
|
|
|
|
# Note that `deltaMerge()` will return the `db.top` argument if the
|
|
|
|
# `db.balancer` is `nil`. Also, the `db.balancer` is read-only. In the
|
|
|
|
# case that there are no forked peers one can ignore that restriction as
|
|
|
|
# no balancer is shared.
|
|
|
|
db.balancer = deltaMerge(
|
|
|
|
db.top, modUpperOk = true, db.balancer, modLowerOk = db.nForked()==0)
|
2024-06-03 20:10:35 +00:00
|
|
|
|
2024-07-17 19:27:33 +00:00
|
|
|
# New empty top layer
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top = LayerRef(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
|
|
|
|
# ------------------------------------------------------------------------------
|