2023-06-30 22:22:33 +00:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-30 22:22:33 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-04 15:05:13 +00:00
|
|
|
std/[sets, tables],
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
2024-06-04 15:05:13 +00:00
|
|
|
results,
|
2023-06-30 22:22:33 +00:00
|
|
|
stew/interval_set,
|
2023-08-10 20:01:28 +00:00
|
|
|
../../aristo,
|
|
|
|
../aristo_walk/persistent,
|
2024-07-01 10:59:18 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_layers]
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc checkBE*[T: RdbBackendRef|MemBackendRef|VoidBackendRef](
|
2023-08-10 20:01:28 +00:00
|
|
|
_: type T;
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Make sure that each vertex has a Merkle hash and vice versa. Also check
|
|
|
|
## the vertex ID generator state.
|
2024-07-04 13:46:52 +00:00
|
|
|
var topVidBe: RootedVertexID = (VertexID(0), VertexID(0))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
for (rvid,vtx) in T.walkVtxBe db:
|
|
|
|
if topVidBe.vid < rvid.vid:
|
|
|
|
topVidBe = rvid
|
2023-06-30 22:22:33 +00:00
|
|
|
if not vtx.isValid:
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckBeVtxInvalid))
|
2023-11-08 12:18:32 +00:00
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
discard
|
|
|
|
of Branch:
|
|
|
|
block check42Links:
|
|
|
|
var seen = false
|
|
|
|
for n in 0 .. 15:
|
|
|
|
if vtx.bVid[n].isValid:
|
|
|
|
if seen:
|
|
|
|
break check42Links
|
|
|
|
seen = true
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckBeVtxBranchLinksMissing))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
for (rvid,key) in T.walkKeyBe db:
|
|
|
|
if topVidBe.vid < rvid.vid:
|
|
|
|
topVidBe = rvid
|
|
|
|
let vtx = db.getVtxBE(rvid).valueOr:
|
|
|
|
return err((rvid.vid,CheckBeVtxMissing))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
# Compare calculated `vTop` against database state
|
2024-07-04 13:46:52 +00:00
|
|
|
# TODO
|
|
|
|
# if topVidBe.isValid:
|
|
|
|
# let vidTuvBe = block:
|
|
|
|
# let rc = db.getTuvBE()
|
|
|
|
# if rc.isOk:
|
|
|
|
# rc.value
|
|
|
|
# elif rc.error == GetTuvNotFound:
|
|
|
|
# VertexID(0)
|
|
|
|
# else:
|
|
|
|
# return err((VertexID(0),rc.error))
|
|
|
|
# if vidTuvBe != topVidBe:
|
|
|
|
# # All vertices and keys between `topVidBe` and `vidTuvBe` must have
|
|
|
|
# # been deleted.
|
|
|
|
# for vid in max(topVidBe + 1, VertexID(LEAST_FREE_VID)) .. vidTuvBe:
|
|
|
|
# if db.getVtxBE(vid).isOk or db.getKeyBE(vid).isOk:
|
|
|
|
# return err((vid,CheckBeGarbledVTop))
|
2024-06-04 15:05:13 +00:00
|
|
|
|
|
|
|
# Check layer cache against backend
|
2024-07-01 10:59:18 +00:00
|
|
|
block:
|
2024-07-04 13:46:52 +00:00
|
|
|
var topVidCache: RootedVertexID = (VertexID(0), VertexID(0))
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
# Check structural table
|
2024-07-04 13:46:52 +00:00
|
|
|
for (rvid,vtx) in db.layersWalkVtx:
|
|
|
|
if vtx.isValid and topVidCache.vid < rvid.vid:
|
|
|
|
topVidCache = rvid
|
2024-07-18 07:13:56 +00:00
|
|
|
let (key, _) = db.layersGetKey(rvid).valueOr: (VOID_HASH_KEY, 0)
|
2024-06-04 15:05:13 +00:00
|
|
|
if not vtx.isValid:
|
2023-06-30 22:22:33 +00:00
|
|
|
# Some vertex is to be deleted, the key must be empty
|
2024-07-01 10:59:18 +00:00
|
|
|
if key.isValid:
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckBeCacheKeyNonEmpty))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# Check key table
|
2024-07-04 13:46:52 +00:00
|
|
|
var list: seq[RootedVertexID]
|
|
|
|
for (rvid,key) in db.layersWalkKey:
|
|
|
|
if key.isValid and topVidCache.vid < rvid.vid:
|
|
|
|
topVidCache = rvid
|
|
|
|
list.add rvid
|
|
|
|
let vtx = db.getVtx rvid
|
|
|
|
if db.layersGetVtx(rvid).isErr and not vtx.isValid:
|
|
|
|
return err((rvid.vid,CheckBeCacheKeyDangling))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
# Check vTop
|
2024-07-04 13:46:52 +00:00
|
|
|
# TODO
|
|
|
|
# if topVidCache.isValid and topVidCache != db.vTop:
|
|
|
|
# # All vertices and keys between `topVidCache` and `db.vTop` must have
|
|
|
|
# # been deleted.
|
|
|
|
# for vid in max(db.vTop + 1, VertexID(LEAST_FREE_VID)) .. topVidCache:
|
|
|
|
# if db.layersGetVtxOrVoid(vid).isValid or
|
|
|
|
# db.layersGetKeyOrVoid(vid).isValid:
|
|
|
|
# return err((db.vTop,CheckBeCacheGarbledVTop))
|
2023-06-30 22:22:33 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|