113 lines
3.7 KiB
Nim
Raw Normal View History

# 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.
{.push raises: [].}
import
2024-06-04 15:05:13 +00:00
std/[sets, tables],
eth/common,
2024-06-04 15:05:13 +00:00
results,
stew/interval_set,
../../aristo,
../aristo_walk/persistent,
".."/[aristo_desc, aristo_get, aristo_layers]
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc checkBE*[T: RdbBackendRef|MemBackendRef|VoidBackendRef](
_: type T;
db: AristoDbRef; # Database, top layer
): Result[void,(VertexID,AristoError)] =
## Make sure that each vertex has a Merkle hash and vice versa. Also check
## the vertex ID generator state.
var topVidBe: RootedVertexID = (VertexID(0), VertexID(0))
for (rvid,vtx) in T.walkVtxBe db:
if topVidBe.vid < rvid.vid:
topVidBe = rvid
if not vtx.isValid:
return err((rvid.vid,CheckBeVtxInvalid))
Aristo db update for short nodes key edge cases (#1887) * Aristo: Provide key-value list signature calculator detail: Simple wrappers around `Aristo` core functionality * Update new API for `CoreDb` details: + Renamed new API functions `contains()` => `hasKey()` or `hasPath()` which disables the `in` operator on non-boolean `contains()` functions + The functions `get()` and `fetch()` always return a not-found error if there is no item, available. The new functions `getOrEmpty()` and `mergeOrEmpty()` return an an empty `Blob` if there is no such key found. * Rewrite `core_apps.nim` using new API from `CoreDb` * Use `Aristo` functionality for calculating Merkle signatures details: For debugging, the `VerifyAristoForMerkleRootCalc` can be set so that `Aristo` results will be verified against the legacy versions. * Provide general interface for Merkle signing key-value tables details: Export `Aristo` wrappers * Activate `CoreDb` tests why: Now, API seems to be stable enough for general tests. * Update `toHex()` usage why: Byteutils' `toHex()` is superior to `toSeq.mapIt(it.toHex(2)).join` * Split `aristo_transcode` => `aristo_serialise` + `aristo_blobify` why: + Different modules for different purposes + `aristo_serialise`: RLP encoding/decoding + `aristo_blobify`: Aristo database encoding/decoding * Compacted representation of small nodes' links instead of Keccak hashes why: Ethereum MPTs use Keccak hashes as node links if the size of an RLP encoded node is at least 32 bytes. Otherwise, the RLP encoded node value is used as a pseudo node link (rather than a hash.) Such a node is nor stored on key-value database. Rather the RLP encoded node value is stored instead of a lode link in a parent node instead. Only for the root hash, the top level node is always referred to by the hash. This feature needed an abstraction of the `HashKey` object which is now either a hash or a blob of length at most 31 bytes. This leaves two ways of representing an empty/void `HashKey` type, either as an empty blob of zero length, or the hash of an empty blob. * Update `CoreDb` interface (mainly reducing logger noise) * Fix copyright years (to make `Lint` happy)
2023-11-08 12:18:32 +00:00
case vtx.vType:
of Leaf:
discard
of Branch:
block check42Links:
var seen = false
Pre-allocate vids for branches (#2882) Each branch node may have up to 16 sub-items - currently, these are given VertexID based when they are first needed leading to a mostly-random order of vertexid for each subitem. Here, we pre-allocate all 16 vertex ids such that when a branch subitem is filled, it already has a vertexid waiting for it. This brings several important benefits: * subitems are sorted and "close" in their id sequencing - this means that when rocksdb stores them, they are likely to end up in the same data block thus improving read efficiency * because the ids are consequtive, we can store just the starting id and a bitmap representing which subitems are in use - this reduces disk space usage for branches allowing more of them fit into a single disk read, further improving disk read and caching performance - disk usage at block 18M is down from 84 to 78gb! * the in-memory footprint of VertexRef reduced allowing more instances to fit into caches and less memory to be used overall. Because of the increased locality of reference, it turns out that we no longer need to iterate over the entire database to efficiently generate the hash key database because the normal computation is now faster - this significantly benefits "live" chain processing as well where each dirtied key must be accompanied by a read of all branch subitems next to it - most of the performance benefit in this branch comes from this locality-of-reference improvement. On a sample resync, there's already ~20% improvement with later blocks seeing increasing benefit (because the trie is deeper in later blocks leading to more benefit from branch read perf improvements) ``` blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s Time (total): -36h44m48s, -19.27% ``` Note: clients need to be resynced as the PR changes the on-disk format R.I.P. little bloom filter - your life in the repo was short but valuable
2024-12-04 11:42:04 +01:00
for _, _ in vtx.pairs():
if seen:
break check42Links
seen = true
return err((rvid.vid,CheckBeVtxBranchLinksMissing))
for (rvid,key) in T.walkKeyBe db:
if topVidBe.vid < rvid.vid:
topVidBe = rvid
let _ = db.getVtxBE(rvid).valueOr:
return err((rvid.vid,CheckBeVtxMissing))
2024-06-04 15:05:13 +00:00
# Compare calculated `vTop` against database state
# 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
block:
var topVidCache: RootedVertexID = (VertexID(0), VertexID(0))
# Check structural table
for (rvid,vtx) in db.layersWalkVtx:
if vtx.isValid and topVidCache.vid < rvid.vid:
topVidCache = rvid
let (key, _) = db.layersGetKey(rvid).valueOr: (VOID_HASH_KEY, 0)
2024-06-04 15:05:13 +00:00
if not vtx.isValid:
# Some vertex is to be deleted, the key must be empty
if key.isValid:
return err((rvid.vid,CheckBeCacheKeyNonEmpty))
# Check key table
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))
2024-06-04 15:05:13 +00:00
# Check vTop
# 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))
ok()
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------