2023-06-30 22:22:33 +00:00
|
|
|
# nimbus-eth1
|
2024-02-14 19:11:59 +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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-04 15:05:13 +00:00
|
|
|
std/[sequtils, sets, typetraits],
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-12-19 12:39:23 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_layers, aristo_serialise, aristo_utils]
|
2023-06-30 22:22:33 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
proc checkTopStrict*(
|
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)] =
|
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 13:46:52 +00: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 13:46:52 +00: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 13:46:52 +00: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 13:46:52 +00: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 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckStkVtxKeyMismatch))
|
2023-07-04 18:24:03 +00:00
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
else: # Empty key flags key is for update
|
2024-07-04 13:46:52 +00:00
|
|
|
zeroKeys.incl rvid.vid
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2024-07-04 13:46:52 +00: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 22:22:33 +00: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 22:22:33 +00:00
|
|
|
): Result[void,(VertexID,AristoError)] =
|
2024-07-04 13:46:52 +00:00
|
|
|
for (rvid,key) in db.layersWalkKey:
|
2024-07-03 20:21:57 +00:00
|
|
|
if key.isValid: # Otherwise to be deleted
|
2024-07-04 13:46:52 +00:00
|
|
|
let vtx = db.getVtx rvid
|
2023-06-30 22:22:33 +00:00
|
|
|
if vtx.isValid:
|
2024-07-04 13:46:52 +00:00
|
|
|
let node = vtx.toNode(rvid.root, db).valueOr:
|
2024-07-03 20:21:57 +00:00
|
|
|
continue
|
2024-07-17 13:48:21 +00:00
|
|
|
if key != node.digestTo(HashKey):
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckRlxVtxKeyMismatch))
|
2023-06-30 22:22:33 +00:00
|
|
|
ok()
|
|
|
|
|
2024-02-22 08:24:58 +00:00
|
|
|
|
2023-09-11 20:38:49 +00:00
|
|
|
proc checkTopCommon*(
|
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
|
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 17:48:38 +00:00
|
|
|
var
|
2024-06-04 15:05:13 +00:00
|
|
|
topVid = VertexID(0)
|
2024-05-30 17:48:38 +00:00
|
|
|
stoRoots: HashSet[VertexID]
|
2023-07-04 18:24:03 +00: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 13:46:52 +00:00
|
|
|
for (rvid,vtx) in db.layersWalkVtx:
|
2023-11-08 12:18:32 +00:00
|
|
|
if vtx.isValid:
|
2024-07-04 13:46:52 +00:00
|
|
|
if topVid < rvid.vid:
|
|
|
|
topVid = rvid.vid
|
2023-11-08 12:18:32 +00:00
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
2024-05-30 17:48:38 +00: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 17:48:38 +00:00
|
|
|
if stoVid in stoRoots:
|
|
|
|
return err((stoVid,CheckAnyVidSharedStorageRoot))
|
2024-06-04 15:05:13 +00:00
|
|
|
if vTop < stoVid:
|
2024-05-30 17:48:38 +00:00
|
|
|
return err((stoVid,CheckAnyVidDeadStorageRoot))
|
|
|
|
stoRoots.incl stoVid
|
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
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckAnyVtxBranchLinksMissing))
|
2023-11-08 12:18:32 +00:00
|
|
|
else:
|
2023-07-04 18:24:03 +00:00
|
|
|
nNilVtx.inc
|
2024-07-04 13:46:52 +00:00
|
|
|
let rc = db.layersGetKey rvid
|
2023-12-19 12:39:23 +00:00
|
|
|
if rc.isErr:
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckAnyVtxEmptyKeyMissing))
|
2024-07-18 07:13:56 +00:00
|
|
|
if rc.value[0].isValid:
|
2024-07-04 13:46:52 +00:00
|
|
|
return err((rvid.vid,CheckAnyVtxEmptyKeyExpected))
|
2023-07-04 18:24:03 +00: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 13:46:52 +00: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 18:24:03 +00: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 22:22:33 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|