2024-05-07 19:59:27 +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.
|
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
## Kvt DB -- Transaction save helper
|
2024-05-07 19:59:27 +00:00
|
|
|
## ======================================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/tables,
|
|
|
|
results,
|
2024-07-18 21:32:32 +00:00
|
|
|
../kvt_delta/delta_merge,
|
2024-06-13 18:15:11 +00:00
|
|
|
".."/[kvt_desc, kvt_delta]
|
2024-05-07 19:59:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
proc txPersistOk*(
|
2024-06-13 18:15:11 +00:00
|
|
|
db: KvtDbRef; # Database
|
|
|
|
): Result[void,KvtError] =
|
2024-12-18 16:03:51 +00:00
|
|
|
## Verify that `txPersist()` can go ahead
|
2024-06-13 18:15:11 +00:00
|
|
|
if not db.txRef.isNil:
|
|
|
|
return err(TxPendingTx)
|
|
|
|
if 0 < db.stack.len:
|
|
|
|
return err(TxStackGarbled)
|
2024-12-18 16:03:51 +00:00
|
|
|
if not db.deltaPersistentOk():
|
2024-06-13 18:15:11 +00:00
|
|
|
return err(TxBackendNotWritable)
|
|
|
|
ok()
|
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
proc txPersist*(
|
2024-05-07 19:59:27 +00:00
|
|
|
db: KvtDbRef; # Database
|
|
|
|
): Result[void,KvtError] =
|
|
|
|
## The function saves the data from the top layer cache into the
|
|
|
|
## backend database.
|
|
|
|
##
|
|
|
|
## If there is no backend the function returns immediately with an error.
|
|
|
|
## The same happens if there is a pending transaction.
|
|
|
|
##
|
2024-12-18 16:03:51 +00:00
|
|
|
? db.txPersistOk()
|
2024-05-07 19:59:27 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
if 0 < db.top.sTab.len:
|
|
|
|
# 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.
|
2024-12-18 10:56:46 +00:00
|
|
|
db.balancer = deltaMerge(db.top, db.balancer)
|
2024-07-18 21:32:32 +00:00
|
|
|
|
|
|
|
# New empty top layer
|
|
|
|
db.top = LayerRef()
|
2024-05-07 19:59:27 +00:00
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
# Move `balancer` data into persistent tables
|
|
|
|
? db.deltaPersistent()
|
2024-05-07 19:59:27 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|