2024-06-03 20:10:35 +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 -- Delta filter management
|
|
|
|
## ====================================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2024-06-26 07:25:09 +00:00
|
|
|
std/tables,
|
2024-06-03 20:10:35 +00:00
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
./aristo_desc,
|
|
|
|
./aristo_desc/desc_backend,
|
|
|
|
./aristo_delta/delta_siblings
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, save to backend
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc deltaPersistentOk*(db: AristoDbRef): bool =
|
|
|
|
## Check whether the read-only filter can be merged into the backend
|
|
|
|
not db.backend.isNil and db.isCentre
|
|
|
|
|
|
|
|
|
|
|
|
proc deltaPersistent*(
|
|
|
|
db: AristoDbRef; # Database
|
|
|
|
nxtFid = 0u64; # Next filter ID (if any)
|
|
|
|
reCentreOk = false;
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Resolve (i.e. move) the backend filter into the physical backend database.
|
|
|
|
##
|
|
|
|
## This needs write permission on the backend DB for the argument `db`
|
|
|
|
## descriptor (see the function `aristo_desc.isCentre()`.) With the argument
|
|
|
|
## flag `reCentreOk` passed `true`, write permission will be temporarily
|
|
|
|
## acquired when needed.
|
|
|
|
##
|
|
|
|
## When merging the current backend filter, its reverse will be is stored as
|
|
|
|
## back log on the filter fifos (so the current state can be retrieved.)
|
|
|
|
## Also, other non-centre descriptors are updated so there is no visible
|
|
|
|
## database change for these descriptors.
|
|
|
|
##
|
|
|
|
let be = db.backend
|
|
|
|
if be.isNil:
|
|
|
|
return err(FilBackendMissing)
|
|
|
|
|
|
|
|
# Blind or missing filter
|
|
|
|
if db.balancer.isNil:
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
# Make sure that the argument `db` is at the centre so the backend is in
|
|
|
|
# read-write mode for this peer.
|
|
|
|
let parent = db.getCentre
|
|
|
|
if db != parent:
|
|
|
|
if not reCentreOk:
|
|
|
|
return err(FilBackendRoMode)
|
2024-06-13 18:15:11 +00:00
|
|
|
? db.reCentre()
|
2024-06-03 20:10:35 +00:00
|
|
|
# Always re-centre to `parent` (in case `reCentreOk` was set)
|
2024-06-13 18:15:11 +00:00
|
|
|
defer: discard parent.reCentre()
|
2024-06-03 20:10:35 +00:00
|
|
|
|
|
|
|
# Initialise peer filter balancer.
|
|
|
|
let updateSiblings = ? UpdateSiblingsRef.init db
|
|
|
|
defer: updateSiblings.rollback()
|
|
|
|
|
|
|
|
let lSst = SavedState(
|
2024-06-28 18:43:04 +00:00
|
|
|
key: EMPTY_ROOT_HASH, # placeholder for more
|
2024-06-03 20:10:35 +00:00
|
|
|
serial: nxtFid)
|
|
|
|
|
|
|
|
# Store structural single trie entries
|
2024-06-13 18:15:11 +00:00
|
|
|
let writeBatch = ? be.putBegFn()
|
2024-07-04 13:46:52 +00:00
|
|
|
for rvid, vtx in db.balancer.sTab:
|
|
|
|
be.putVtxFn(writeBatch, rvid, vtx)
|
|
|
|
for rvid, key in db.balancer.kMap:
|
|
|
|
be.putKeyFn(writeBatch, rvid, key)
|
2024-06-04 15:05:13 +00:00
|
|
|
be.putTuvFn(writeBatch, db.balancer.vTop)
|
2024-06-03 20:10:35 +00:00
|
|
|
be.putLstFn(writeBatch, lSst)
|
|
|
|
? be.putEndFn writeBatch # Finalise write batch
|
|
|
|
|
2024-07-12 13:08:26 +00:00
|
|
|
# Copy back updated payloads
|
2024-07-14 10:02:05 +00:00
|
|
|
for accPath, pyl in db.balancer.accLeaves:
|
2024-07-03 15:58:25 +00:00
|
|
|
let accKey = accPath.to(AccountKey)
|
2024-07-14 10:02:05 +00:00
|
|
|
if not db.accLeaves.lruUpdate(accKey, pyl):
|
|
|
|
discard db.accLeaves.lruAppend(accKey, pyl, accLruSize)
|
2024-07-03 15:58:25 +00:00
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
# Update dudes and this descriptor
|
|
|
|
? updateSiblings.update().commit()
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|