2023-06-30 22:22:33 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[sequtils, sets, tables],
|
|
|
|
eth/common,
|
|
|
|
stew/results,
|
2023-07-12 23:03:14 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_transcode, aristo_utils]
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc checkCacheStrict*(
|
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)] =
|
|
|
|
for (vid,vtx) in db.top.sTab.pairs:
|
2023-07-04 18:24:03 +00:00
|
|
|
if vtx.isValid:
|
|
|
|
let rc = vtx.toNode db
|
|
|
|
if rc.isErr:
|
|
|
|
return err((vid,CheckStkVtxIncomplete))
|
|
|
|
|
|
|
|
let lbl = db.top.kMap.getOrVoid vid
|
|
|
|
if not lbl.isValid:
|
|
|
|
return err((vid,CheckStkVtxKeyMissing))
|
2023-07-12 23:03:14 +00:00
|
|
|
if lbl.key != rc.value.to(HashKey):
|
2023-07-04 18:24:03 +00:00
|
|
|
return err((vid,CheckStkVtxKeyMismatch))
|
|
|
|
|
|
|
|
let revVid = db.top.pAmk.getOrVoid lbl
|
|
|
|
if not revVid.isValid:
|
|
|
|
return err((vid,CheckStkRevKeyMissing))
|
|
|
|
if revVid != vid:
|
|
|
|
return err((vid,CheckStkRevKeyMismatch))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
if 0 < db.top.pAmk.len and db.top.pAmk.len < db.top.sTab.len:
|
|
|
|
# Cannot have less changes than cached entries
|
|
|
|
return err((VertexID(0),CheckStkVtxCountMismatch))
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc checkCacheRelaxed*(
|
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)] =
|
|
|
|
if 0 < db.top.pPrf.len:
|
|
|
|
for vid in db.top.pPrf:
|
|
|
|
let vtx = db.top.sTab.getOrVoid vid
|
|
|
|
if vtx.isValid:
|
|
|
|
let rc = vtx.toNode db
|
|
|
|
if rc.isErr:
|
|
|
|
return err((vid,CheckRlxVtxIncomplete))
|
|
|
|
|
|
|
|
let lbl = db.top.kMap.getOrVoid vid
|
|
|
|
if not lbl.isValid:
|
|
|
|
return err((vid,CheckRlxVtxKeyMissing))
|
2023-07-12 23:03:14 +00:00
|
|
|
if lbl.key != rc.value.to(HashKey):
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,CheckRlxVtxKeyMismatch))
|
|
|
|
|
|
|
|
let revVid = db.top.pAmk.getOrVoid lbl
|
|
|
|
if not revVid.isValid:
|
|
|
|
return err((vid,CheckRlxRevKeyMissing))
|
|
|
|
if revVid != vid:
|
|
|
|
return err((vid,CheckRlxRevKeyMismatch))
|
|
|
|
else:
|
|
|
|
for (vid,lbl) in db.top.kMap.pairs:
|
|
|
|
if lbl.isValid: # Otherwise to be deleted
|
|
|
|
let vtx = db.getVtx vid
|
|
|
|
if vtx.isValid:
|
|
|
|
let rc = vtx.toNode db
|
|
|
|
if rc.isOk:
|
2023-07-12 23:03:14 +00:00
|
|
|
if lbl.key != rc.value.to(HashKey):
|
2023-06-30 22:22:33 +00:00
|
|
|
return err((vid,CheckRlxVtxKeyMismatch))
|
|
|
|
|
|
|
|
let revVid = db.top.pAmk.getOrVoid lbl
|
|
|
|
if not revVid.isValid:
|
|
|
|
return err((vid,CheckRlxRevKeyMissing))
|
|
|
|
if revVid != vid:
|
|
|
|
return err((vid,CheckRlxRevKeyMissing))
|
|
|
|
if revVid != vid:
|
|
|
|
return err((vid,CheckRlxRevKeyMismatch))
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc checkCacheCommon*(
|
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)] =
|
|
|
|
# Some `kMap[]` entries may ne void indicating backend deletion
|
2023-07-04 18:24:03 +00:00
|
|
|
let
|
|
|
|
kMapCount = db.top.kMap.values.toSeq.filterIt(it.isValid).len
|
|
|
|
kMapNilCount = db.top.kMap.len - kMapCount
|
|
|
|
|
|
|
|
# Check deleted entries
|
|
|
|
var nNilVtx = 0
|
|
|
|
for (vid,vtx) in db.top.sTab.pairs:
|
|
|
|
if not vtx.isValid:
|
|
|
|
nNilVtx.inc
|
2023-08-11 17:23:57 +00:00
|
|
|
let rc = db.getVtxBE vid
|
2023-07-04 18:24:03 +00:00
|
|
|
if rc.isErr:
|
|
|
|
return err((vid,CheckAnyVidVtxMissing))
|
|
|
|
if not db.top.kMap.hasKey vid:
|
|
|
|
return err((vid,CheckAnyVtxEmptyKeyMissing))
|
|
|
|
if db.top.kMap.getOrVoid(vid).isValid:
|
|
|
|
return err((vid,CheckAnyVtxEmptyKeyExpected))
|
|
|
|
|
|
|
|
# If present, there are at least as many deleted hashes as there are deleted
|
|
|
|
# vertices.
|
|
|
|
if kMapNilCount != 0 and kMapNilCount < nNilVtx:
|
|
|
|
return err((VertexID(0),CheckAnyVtxEmptyKeyMismatch))
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
if db.top.pAmk.len != kMapCount:
|
|
|
|
var knownKeys: HashSet[VertexID]
|
|
|
|
for (key,vid) in db.top.pAmk.pairs:
|
|
|
|
if not db.top.kMap.hasKey(vid):
|
|
|
|
return err((vid,CheckAnyRevVtxMissing))
|
|
|
|
if vid in knownKeys:
|
|
|
|
return err((vid,CheckAnyRevVtxDup))
|
|
|
|
knownKeys.incl vid
|
|
|
|
return err((VertexID(0),CheckAnyRevCountMismatch)) # should not apply(!)
|
|
|
|
|
|
|
|
for vid in db.top.pPrf:
|
|
|
|
if not db.top.kMap.hasKey(vid):
|
|
|
|
return err((vid,CheckAnyVtxLockWithoutKey))
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|