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-06-28 18:43:04 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_delta, aristo_layers]
|
2024-06-03 20:10:35 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getBeStateRoot(
|
|
|
|
db: AristoDbRef;
|
|
|
|
chunkedMpt: bool;
|
|
|
|
): Result[HashKey,AristoError] =
|
|
|
|
## Get the Merkle hash key for the current backend state root and check
|
|
|
|
## validity of top layer.
|
|
|
|
let srcRoot = block:
|
|
|
|
let rc = db.getKeyBE VertexID(1)
|
|
|
|
if rc.isOk:
|
|
|
|
rc.value
|
|
|
|
elif rc.error == GetKeyNotFound:
|
|
|
|
VOID_HASH_KEY
|
|
|
|
else:
|
|
|
|
return err(rc.error)
|
|
|
|
|
|
|
|
if db.top.delta.kMap.getOrVoid(VertexID 1).isValid:
|
|
|
|
return ok(srcRoot)
|
|
|
|
|
|
|
|
elif not db.top.delta.kMap.hasKey(VertexID 1) and
|
|
|
|
not db.top.delta.sTab.hasKey(VertexID 1):
|
|
|
|
# This layer is unusable, need both: vertex and key
|
|
|
|
return err(TxPrettyPointlessLayer)
|
|
|
|
|
2024-06-28 15:03:12 +00:00
|
|
|
ok(srcRoot)
|
2024-06-03 20:10:35 +00:00
|
|
|
|
2024-06-28 15:03:12 +00:00
|
|
|
#elif not db.top.delta.sTab.getOrVoid(VertexID 1).isValid:
|
|
|
|
# # Root key and vertex have been deleted
|
|
|
|
# return ok(srcRoot)
|
|
|
|
#
|
|
|
|
#elif chunkedMpt and srcRoot == db.top.delta.kMap.getOrVoid VertexID(1):
|
|
|
|
# # FIXME: this one needs to be double checked with `snap` sunc preload
|
|
|
|
# return ok(srcRoot)
|
|
|
|
#
|
|
|
|
#err(TxStateRootMismatch)
|
2024-06-03 20:10:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc topMerge(db: AristoDbRef; src: HashKey): Result[void,AristoError] =
|
|
|
|
## Merge the `top` layer into the read-only balacer layer.
|
|
|
|
let ubeRoot = block:
|
|
|
|
let rc = db.getKeyUbe VertexID(1)
|
|
|
|
if rc.isOk:
|
|
|
|
rc.value
|
|
|
|
elif rc.error == GetKeyNotFound:
|
|
|
|
VOID_HASH_KEY
|
|
|
|
else:
|
|
|
|
return err(rc.error)
|
|
|
|
|
|
|
|
# This one will return the `db.top.delta` if `db.balancer.isNil`
|
|
|
|
db.balancer = db.deltaMerge(db.top.delta, db.balancer, ubeRoot).valueOr:
|
|
|
|
return err(error[1])
|
|
|
|
|
|
|
|
ok()
|
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`
|
|
|
|
chunkedMpt: bool; # Partial data (e.g. from `snap`)
|
|
|
|
): 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-06-03 20:10:35 +00:00
|
|
|
# Verify database consistency and get `src` field for update
|
|
|
|
let rc = db.getBeStateRoot chunkedMpt
|
|
|
|
if rc.isErr and rc.error != TxPrettyPointlessLayer:
|
|
|
|
return err(rc.error)
|
2024-05-03 17:38:17 +00:00
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
# Move/merge/install `top` layer onto `balancer`
|
|
|
|
if rc.isOk:
|
|
|
|
db.topMerge(rc.value).isOkOr:
|
|
|
|
return err(error)
|
2024-05-03 17:38:17 +00:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
# New empty top layer (probably with `snap` proofs and `vTop` carry over)
|
2024-05-03 17:38:17 +00:00
|
|
|
db.top = LayerRef(
|
2024-07-03 20:21:57 +00:00
|
|
|
delta: LayerDeltaRef())
|
2024-06-03 20:10:35 +00:00
|
|
|
if db.balancer.isValid:
|
2024-06-04 15:05:13 +00:00
|
|
|
db.top.delta.vTop = db.balancer.vTop
|
2024-05-03 17:38:17 +00:00
|
|
|
else:
|
2024-06-04 15:05:13 +00:00
|
|
|
let rc = db.getTuvUbe()
|
2024-05-03 17:38:17 +00:00
|
|
|
if rc.isOk:
|
2024-06-04 15:05:13 +00:00
|
|
|
db.top.delta.vTop = rc.value
|
2024-05-03 17:38:17 +00:00
|
|
|
else:
|
2024-06-04 15:05:13 +00:00
|
|
|
# It is OK if there was no `vTop`. Otherwise something serious happened
|
2024-05-03 17:38:17 +00:00
|
|
|
# and there is no way to recover easily.
|
2024-06-04 15:05:13 +00:00
|
|
|
doAssert rc.error == GetTuvNotFound
|
2024-06-03 20:10:35 +00:00
|
|
|
|
2024-05-30 17:48:38 +00:00
|
|
|
elif db.top.delta.sTab.len != 0 and
|
|
|
|
not db.top.delta.sTab.getOrVoid(VertexID(1)).isValid:
|
|
|
|
# Currently, a `VertexID(1)` root node is required
|
|
|
|
return err(TxAccRootMissing)
|
2024-05-03 17:38:17 +00:00
|
|
|
|
|
|
|
if persistent:
|
2024-06-03 20:10:35 +00:00
|
|
|
# Merge/move `balancer` into persistent tables
|
|
|
|
? db.deltaPersistent nxtSid
|
2024-05-03 17:38:17 +00:00
|
|
|
|
|
|
|
# New empty top layer (probably with `snap` proofs carry over)
|
|
|
|
db.top = LayerRef(
|
2024-06-04 15:05:13 +00:00
|
|
|
delta: LayerDeltaRef(vTop: db.vTop),
|
2024-05-03 17:38:17 +00:00
|
|
|
txUid: db.top.txUid)
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|