2023-06-30 23:22:33 +01:00
|
|
|
# nimbus-eth1
|
2024-02-14 19:11:59 +00:00
|
|
|
# Copyright (c) 2023-2024 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
|
2024-06-04 15:05:13 +00:00
|
|
|
std/[sequtils, sets, typetraits],
|
2024-06-22 22:33:37 +02:00
|
|
|
eth/common,
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2023-12-19 12:39:23 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_layers, 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)] =
|
2023-12-12 17:47:41 +00:00
|
|
|
# No need to specify zero keys if implied by a leaf path with valid target
|
|
|
|
# vertex ID (i.e. not deleted).
|
|
|
|
var zeroKeys: HashSet[VertexID]
|
2024-07-04 15:46:52 +02:00
|
|
|
for (rvid,vtx) in db.layersWalkVtx:
|
|
|
|
let key = db.layersGetKeyOrVoid rvid
|
2023-12-12 17:47:41 +00:00
|
|
|
|
|
|
|
if not vtx.isValid:
|
2024-02-14 19:11:59 +00:00
|
|
|
if key.isValid:
|
2024-07-04 15:46:52 +02:00
|
|
|
return err((rvid.vid,CheckStkVtxKeyMismatch))
|
2023-12-12 17:47:41 +00:00
|
|
|
else: # Empty key flags key is for update
|
2024-07-04 15:46:52 +02:00
|
|
|
zeroKeys.incl rvid.vid
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2024-02-14 19:11:59 +00:00
|
|
|
elif key.isValid:
|
|
|
|
# So `vtx` and `key` exist
|
2024-07-04 15:46:52 +02:00
|
|
|
let node = vtx.toNode(rvid.root, db).valueOr:
|
No ext update (#2494)
* Imported/rebase from `no-ext`, PR #2485
Store extension nodes together with the branch
Extension nodes must be followed by a branch - as such, it makes sense
to store the two together both in the database and in memory:
* fewer reads, writes and updates to traverse the tree
* simpler logic for maintaining the node structure
* less space used, both memory and storage, because there are fewer
nodes overall
There is also a downside: hashes can no longer be cached for an
extension - instead, only the extension+branch hash can be cached - this
seems like a fine tradeoff since computing it should be fast.
TODO: fix commented code
* Fix merge functions and `toNode()`
* Update `merkleSignCommit()` prototype
why:
Result is always a 32bit hash
* Update short Merkle hash key generation
details:
Ethereum reference 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.) This is
specified in the yellow paper, appendix D.
Different to the `Aristo` implementation, the reference MPT would not
store such a node on the key-value database. Rather the RLP encoded node value is stored instead of a node link in a parent node
is stored as a node link on the parent database.
Only for the root hash, the top level node is always referred to by the
hash.
* Fix/update `Extension` sections
why:
Were commented out after removal of a dedicated `Extension` type which
left the system disfunctional.
* Clean up unused error codes
* Update unit tests
* Update docu
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
2024-07-16 19:47:59 +00:00
|
|
|
# not all sub-keys might be ready du to lazy hashing
|
|
|
|
continue
|
2024-07-17 13:48:21 +00:00
|
|
|
if key != node.digestTo(HashKey):
|
2024-07-04 15:46:52 +02:00
|
|
|
return err((rvid.vid,CheckStkVtxKeyMismatch))
|
2023-07-04 19:24:03 +01:00
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
else: # Empty key flags key is for update
|
2024-07-04 15:46:52 +02:00
|
|
|
zeroKeys.incl rvid.vid
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2024-07-04 15:46:52 +02:00
|
|
|
for (rvid,key) in db.layersWalkKey:
|
|
|
|
if not key.isValid and rvid.vid notin zeroKeys:
|
|
|
|
if not db.getVtx(rvid).isValid:
|
|
|
|
return err((rvid.vid,CheckStkKeyStrayZeroEntry))
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2023-06-30 23:22:33 +01:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
proc checkTopProofMode*(
|
2024-02-14 19:11:59 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2023-06-30 23:22:33 +01:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2024-07-04 15:46:52 +02:00
|
|
|
for (rvid,key) in db.layersWalkKey:
|
2024-07-03 22:21:57 +02:00
|
|
|
if key.isValid: # Otherwise to be deleted
|
2024-07-04 15:46:52 +02:00
|
|
|
let vtx = db.getVtx rvid
|
2023-06-30 23:22:33 +01:00
|
|
|
if vtx.isValid:
|
2024-07-04 15:46:52 +02:00
|
|
|
let node = vtx.toNode(rvid.root, db).valueOr:
|
2024-07-03 22:21:57 +02:00
|
|
|
continue
|
2024-07-17 13:48:21 +00:00
|
|
|
if key != node.digestTo(HashKey):
|
2024-07-04 15:46:52 +02:00
|
|
|
return err((rvid.vid,CheckRlxVtxKeyMismatch))
|
2023-06-30 23:22:33 +01:00
|
|
|
ok()
|
|
|
|
|
2024-02-22 08:24:58 +00:00
|
|
|
|
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
|
2024-02-14 19:11:59 +00:00
|
|
|
kMapCount = db.layersWalkKey.toSeq.mapIt(it[1]).filterIt(it.isValid).len
|
|
|
|
kMapNilCount = db.layersWalkKey.toSeq.len - kMapCount
|
2024-06-04 15:05:13 +00:00
|
|
|
vTop = db.vTop
|
2024-05-30 18:48:38 +01:00
|
|
|
var
|
2024-06-04 15:05:13 +00:00
|
|
|
topVid = VertexID(0)
|
2024-05-30 18:48:38 +01:00
|
|
|
stoRoots: HashSet[VertexID]
|
2023-07-04 19:24:03 +01:00
|
|
|
|
2023-12-04 20:39:26 +00:00
|
|
|
# Collect leafs and check deleted entries
|
2023-12-12 17:47:41 +00:00
|
|
|
var nNilVtx = 0
|
2024-07-04 15:46:52 +02:00
|
|
|
for (rvid,vtx) in db.layersWalkVtx:
|
2023-11-08 12:18:32 +00:00
|
|
|
if vtx.isValid:
|
2024-07-04 15:46:52 +02:00
|
|
|
if topVid < rvid.vid:
|
|
|
|
topVid = rvid.vid
|
2023-11-08 12:18:32 +00:00
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
2024-05-30 18:48:38 +01:00
|
|
|
if vtx.lData.pType == AccountData:
|
2024-08-07 13:28:01 +00:00
|
|
|
let stoID = vtx.lData.stoID
|
|
|
|
if stoID.isValid:
|
|
|
|
let stoVid = stoID.vid
|
2024-05-30 18:48:38 +01:00
|
|
|
if stoVid in stoRoots:
|
|
|
|
return err((stoVid,CheckAnyVidSharedStorageRoot))
|
2024-06-04 15:05:13 +00:00
|
|
|
if vTop < stoVid:
|
2024-05-30 18:48:38 +01:00
|
|
|
return err((stoVid,CheckAnyVidDeadStorageRoot))
|
|
|
|
stoRoots.incl stoVid
|
2023-11-08 12:18:32 +00:00
|
|
|
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
|
2024-07-04 15:46:52 +02:00
|
|
|
return err((rvid.vid,CheckAnyVtxBranchLinksMissing))
|
2023-11-08 12:18:32 +00:00
|
|
|
else:
|
2023-07-04 19:24:03 +01:00
|
|
|
nNilVtx.inc
|
2024-07-04 15:46:52 +02:00
|
|
|
let rc = db.layersGetKey rvid
|
2023-12-19 12:39:23 +00:00
|
|
|
if rc.isErr:
|
2024-07-04 15:46:52 +02:00
|
|
|
return err((rvid.vid,CheckAnyVtxEmptyKeyMissing))
|
2024-07-18 09:13:56 +02:00
|
|
|
if rc.value[0].isValid:
|
2024-07-04 15:46:52 +02:00
|
|
|
return err((rvid.vid,CheckAnyVtxEmptyKeyExpected))
|
2023-07-04 19:24:03 +01:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
if vTop.distinctBase < LEAST_FREE_VID:
|
|
|
|
# Verify that all vids are below `LEAST_FREE_VID`
|
|
|
|
if topVid.distinctBase < LEAST_FREE_VID:
|
2024-07-04 15:46:52 +02:00
|
|
|
for (rvid,key) in db.layersWalkKey:
|
|
|
|
if key.isValid and LEAST_FREE_VID <= rvid.vid.distinctBase:
|
2024-06-04 15:05:13 +00:00
|
|
|
return err((topVid,CheckAnyVTopUnset))
|
|
|
|
|
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
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|