2023-06-30 23:22:33 +01:00
|
|
|
# nimbus-eth1
|
2023-11-08 16:52:25 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2023-06-30 23:22:33 +01: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
|
|
|
|
std/[sequtils, sets, tables],
|
2023-11-08 12:18:32 +00:00
|
|
|
eth/[common, trie/nibbles],
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2023-11-08 12:18:32 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_serialise, aristo_utils]
|
2023-06-30 23:22:33 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-11 21:38:49 +01:00
|
|
|
proc checkTopStrict*(
|
2023-07-04 19:24:03 +01:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
for (vid,vtx) in db.top.sTab.pairs:
|
2023-07-04 19:24:03 +01:00
|
|
|
if vtx.isValid:
|
2023-11-08 16:52:25 +00:00
|
|
|
let node = vtx.toNode(db).valueOr:
|
2023-07-04 19:24:03 +01:00
|
|
|
return err((vid,CheckStkVtxIncomplete))
|
|
|
|
|
|
|
|
let lbl = db.top.kMap.getOrVoid vid
|
|
|
|
if not lbl.isValid:
|
|
|
|
return err((vid,CheckStkVtxKeyMissing))
|
2023-11-08 16:52:25 +00:00
|
|
|
if lbl.key != node.digestTo(HashKey):
|
2023-07-04 19:24:03 +01:00
|
|
|
return err((vid,CheckStkVtxKeyMismatch))
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
let revVids = db.top.pAmk.getOrVoid lbl
|
|
|
|
if not revVids.isValid:
|
2023-07-04 19:24:03 +01:00
|
|
|
return err((vid,CheckStkRevKeyMissing))
|
2023-11-08 12:18:32 +00:00
|
|
|
if vid notin revVids:
|
2023-07-04 19:24:03 +01:00
|
|
|
return err((vid,CheckStkRevKeyMismatch))
|
2023-06-30 23:22:33 +01:00
|
|
|
|
2023-12-04 20:39:26 +00:00
|
|
|
let
|
|
|
|
pAmkVtxCount = db.top.pAmk.values.toSeq.foldl(a + b.len, 0)
|
|
|
|
sTabVtxCount = db.top.sTab.values.toSeq.filterIt(it.isValid).len
|
|
|
|
# Non-zero values mist sum up the same
|
|
|
|
if pAmkVtxCount < sTabVtxCount:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((VertexID(0),CheckStkVtxCountMismatch))
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
2023-09-11 21:38:49 +01:00
|
|
|
proc checkTopRelaxed*(
|
2023-07-04 19:24:03 +01:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 23:22:33 +01: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:
|
2023-11-08 16:52:25 +00:00
|
|
|
let node = vtx.toNode(db).valueOr:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,CheckRlxVtxIncomplete))
|
|
|
|
|
|
|
|
let lbl = db.top.kMap.getOrVoid vid
|
|
|
|
if not lbl.isValid:
|
|
|
|
return err((vid,CheckRlxVtxKeyMissing))
|
2023-11-08 16:52:25 +00:00
|
|
|
if lbl.key != node.digestTo(HashKey):
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,CheckRlxVtxKeyMismatch))
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
let revVids = db.top.pAmk.getOrVoid lbl
|
|
|
|
if not revVids.isValid:
|
2023-06-30 23:22:33 +01:00
|
|
|
return err((vid,CheckRlxRevKeyMissing))
|
2023-11-08 12:18:32 +00:00
|
|
|
if vid notin revVids:
|
2023-06-30 23:22:33 +01:00
|
|
|
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:
|
2023-11-08 16:52:25 +00:00
|
|
|
let node = vtx.toNode(db).valueOr:
|
|
|
|
continue
|
|
|
|
if lbl.key != node.digestTo(HashKey):
|
|
|
|
return err((vid,CheckRlxVtxKeyMismatch))
|
|
|
|
|
|
|
|
let revVids = db.top.pAmk.getOrVoid lbl
|
|
|
|
if not revVids.isValid:
|
|
|
|
return err((vid,CheckRlxRevKeyMissing))
|
|
|
|
if vid notin revVids:
|
|
|
|
return err((vid,CheckRlxRevKeyMismatch))
|
2023-06-30 23:22:33 +01:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
2023-09-11 21:38:49 +01:00
|
|
|
proc checkTopCommon*(
|
2023-07-04 19:24:03 +01:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
# Some `kMap[]` entries may ne void indicating backend deletion
|
2023-07-04 19:24:03 +01:00
|
|
|
let
|
|
|
|
kMapCount = db.top.kMap.values.toSeq.filterIt(it.isValid).len
|
|
|
|
kMapNilCount = db.top.kMap.len - kMapCount
|
|
|
|
|
2023-12-04 20:39:26 +00:00
|
|
|
# Collect leafs and check deleted entries
|
|
|
|
var
|
|
|
|
nNilVtx = 0
|
|
|
|
leafs = db.top.lTab.values.toSeq.filterIt(it.isValid).toHashSet
|
2023-07-04 19:24:03 +01:00
|
|
|
for (vid,vtx) in db.top.sTab.pairs:
|
2023-11-08 12:18:32 +00:00
|
|
|
if vtx.isValid:
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
2023-12-04 20:39:26 +00:00
|
|
|
if vid notin leafs:
|
|
|
|
return err((vid,CheckAnyLeafUnregistered))
|
|
|
|
leafs.excl vid
|
2023-11-08 12:18:32 +00:00
|
|
|
of Branch:
|
|
|
|
block check42Links:
|
|
|
|
var seen = false
|
|
|
|
for n in 0 .. 15:
|
|
|
|
if vtx.bVid[n].isValid:
|
|
|
|
if seen:
|
|
|
|
break check42Links
|
|
|
|
seen = true
|
|
|
|
return err((vid,CheckAnyVtxBranchLinksMissing))
|
|
|
|
of Extension:
|
|
|
|
if vtx.ePfx.len == 0:
|
|
|
|
return err((vid,CheckAnyVtxExtPfxMissing))
|
|
|
|
else:
|
2023-07-04 19:24:03 +01:00
|
|
|
nNilVtx.inc
|
2023-11-08 16:52:25 +00:00
|
|
|
discard db.getVtxBE(vid).valueOr:
|
2023-07-04 19:24:03 +01:00
|
|
|
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))
|
|
|
|
|
2023-12-04 20:39:26 +00:00
|
|
|
# Check for dangling leaf records
|
|
|
|
if 0 < leafs.len:
|
|
|
|
return err((leafs.toSeq[0],CheckAnyLeafVidDangling))
|
|
|
|
|
2023-07-04 19:24:03 +01:00
|
|
|
# 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 23:22:33 +01:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
let pAmkVtxCount = db.top.pAmk.values.toSeq.foldl(a + b.len, 0)
|
|
|
|
if pAmkVtxCount != kMapCount:
|
2023-06-30 23:22:33 +01:00
|
|
|
var knownKeys: HashSet[VertexID]
|
2023-11-08 12:18:32 +00:00
|
|
|
for (key,vids) in db.top.pAmk.pairs:
|
|
|
|
for vid in vids:
|
|
|
|
if not db.top.kMap.hasKey(vid):
|
|
|
|
return err((vid,CheckAnyRevVtxMissing))
|
|
|
|
if vid in knownKeys:
|
|
|
|
return err((vid,CheckAnyRevVtxDup))
|
|
|
|
knownKeys.incl vid
|
2023-06-30 23:22:33 +01:00
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|