2023-06-30 22:22:33 +00:00
|
|
|
# nimbus-eth1
|
2024-02-22 08:24:58 +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.
|
|
|
|
|
|
|
|
## Aristo DB -- Consistency checks
|
|
|
|
## ===============================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[algorithm, sequtils, sets, tables],
|
|
|
|
eth/common,
|
2023-09-15 15:23:53 +00:00
|
|
|
stew/interval_set,
|
|
|
|
results,
|
2023-08-10 20:01:28 +00:00
|
|
|
./aristo_walk/persistent,
|
2024-02-22 08:24:58 +00:00
|
|
|
"."/[aristo_desc, aristo_get, aristo_init, aristo_utils],
|
2024-04-26 13:43:52 +00:00
|
|
|
./aristo_check/[check_be, check_journal, check_top]
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
proc checkTop*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-12-12 17:47:41 +00:00
|
|
|
proofMode = false; # Has proof nodes
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
|
|
|
## Verify that the cache structure is correct as it would be after `merge()`
|
2023-12-12 17:47:41 +00:00
|
|
|
## and `hashify()` operations. Unless `proofMode` is set `true` it would not
|
2023-06-30 22:22:33 +00:00
|
|
|
## fully check against the backend, which is typically not applicable after
|
|
|
|
## `delete()` operations.
|
|
|
|
##
|
|
|
|
## The following is verified:
|
|
|
|
##
|
|
|
|
## * Each `sTab[]` entry has a valid vertex which can be compiled as a node.
|
2023-12-12 17:47:41 +00:00
|
|
|
## If `proofMode` is set `false`, the Merkle hashes are recompiled and must
|
2023-06-30 22:22:33 +00:00
|
|
|
## match.
|
|
|
|
##
|
2023-12-12 17:47:41 +00:00
|
|
|
if proofMode:
|
|
|
|
? db.checkTopProofMode()
|
2023-06-30 22:22:33 +00:00
|
|
|
else:
|
2023-09-11 20:38:49 +00:00
|
|
|
? db.checkTopStrict()
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
db.checkTopCommon()
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc checkBE*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 22:22:33 +00:00
|
|
|
relax = true; # Not re-compiling hashes if `true`
|
2023-09-11 20:38:49 +00:00
|
|
|
cache = true; # Also verify against top layer cache
|
|
|
|
fifos = false; # Also verify cascaded filter fifos
|
2023-06-30 22:22:33 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2024-04-26 13:43:52 +00:00
|
|
|
## Verify database backend structure. If the argument `relax` is set `false`,
|
2023-06-30 22:22:33 +00:00
|
|
|
## all necessary Merkle hashes are compiled and verified. If the argument
|
2023-09-11 20:38:49 +00:00
|
|
|
## `cache` is set `true`, the cache is also checked so that a safe operation
|
|
|
|
## (like `resolveBackendFilter()`) will leave the backend consistent.
|
2023-06-30 22:22:33 +00:00
|
|
|
##
|
|
|
|
## The following is verified:
|
|
|
|
##
|
|
|
|
## * Each vertex ID on the structural table can be represented as a Merkle
|
|
|
|
## patricia Tree node. If `relax` is set `false`, the Merkle hashes are
|
|
|
|
## all recompiled and must match.
|
|
|
|
##
|
|
|
|
## * The set of free vertex IDa as potentally suppliedby the ID generator
|
|
|
|
## state is disjunct to the set of already used vertex IDs on the database.
|
|
|
|
## Moreover, the union of both sets is equivalent to the set of positive
|
|
|
|
## `uint64` numbers.
|
|
|
|
##
|
2023-09-05 13:57:20 +00:00
|
|
|
case db.backend.kind:
|
|
|
|
of BackendMemory:
|
|
|
|
return MemBackendRef.checkBE(db, cache=cache, relax=relax)
|
|
|
|
of BackendRocksDB:
|
|
|
|
return RdbBackendRef.checkBE(db, cache=cache, relax=relax)
|
|
|
|
of BackendVoid:
|
|
|
|
return VoidBackendRef.checkBE(db, cache=cache, relax=relax)
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2024-04-26 13:43:52 +00:00
|
|
|
proc checkJournal*(
|
|
|
|
db: AristoDbRef; # Database, top layer
|
|
|
|
): Result[void,(QueueID,AristoError)] =
|
|
|
|
## Verify database backend journal.
|
|
|
|
case db.backend.kind:
|
|
|
|
of BackendMemory:
|
|
|
|
return MemBackendRef.checkJournal(db)
|
|
|
|
of BackendRocksDB:
|
|
|
|
return RdbBackendRef.checkJournal(db)
|
|
|
|
of BackendVoid:
|
|
|
|
return ok() # no journal
|
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
|
|
|
|
proc check*(
|
|
|
|
db: AristoDbRef; # Database, top layer
|
|
|
|
relax = false; # Check existing hashes only
|
|
|
|
cache = true; # Also verify against top layer cache
|
|
|
|
fifos = true; # Also verify cascaded filter fifos
|
2023-12-12 17:47:41 +00:00
|
|
|
proofMode = false; # Has proof nodes
|
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2023-09-11 20:38:49 +00:00
|
|
|
## Shortcut for running `checkTop()` followed by `checkBE()`
|
2023-12-12 17:47:41 +00:00
|
|
|
? db.checkTop(proofMode = proofMode)
|
2024-04-26 13:43:52 +00:00
|
|
|
? db.checkBE(relax = relax, cache = cache, fifos = fifos)
|
|
|
|
if fifos:
|
|
|
|
let rc = db.checkJournal()
|
|
|
|
if rc.isErr:
|
|
|
|
return err((VertexID(0),rc.error[1]))
|
2023-09-11 20:38:49 +00:00
|
|
|
ok()
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|