mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-15 23:04:34 +00:00
df4a21c910
When lazily verifying state roots, we may end up with an entire state without roots that gets computed for the whole database - in the current design, that would result in hashes for the entire trie being held in memory. Since the hash depends only on the data in the vertex, we can store it directly at the top-most level derived from the verticies it depends on - be that memory or database - this makes the memory usage broadly linear with respect to the already-existing in-memory change set stored in the layers. It also ensures that if we have multiple forks in memory, hashes get cached in the correct layer maximising reuse between forks. The same layer numbering scheme as elsewhere is reused, where -2 is the backend, -1 is the balancer, then 0+ is the top of the stack and stack. A downside of this approach is that we create many small batches - a future improvement could be to collect all such writes in a single batch, though the memory profile of this approach should be examined first (where is the batch kept, exactly?).
116 lines
3.7 KiB
Nim
116 lines
3.7 KiB
Nim
# 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.
|
|
|
|
## Aristo DB -- Handy Helpers
|
|
## ==========================
|
|
##
|
|
{.push raises: [].}
|
|
|
|
import
|
|
eth/common,
|
|
results,
|
|
"."/[aristo_constants, aristo_desc, aristo_get, aristo_hike, aristo_layers]
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions, converters
|
|
# ------------------------------------------------------------------------------
|
|
|
|
proc toNode*(
|
|
vtx: VertexRef; # Vertex to convert
|
|
root: VertexID; # Sub-tree root the `vtx` belongs to
|
|
db: AristoDbRef; # Database
|
|
stopEarly = true; # Full list of missing links if `false`
|
|
beKeyOk = true; # Allow fetching DB backend keys
|
|
): Result[NodeRef,seq[VertexID]] =
|
|
## Convert argument the vertex `vtx` to a node type. Missing Merkle hash
|
|
## keys are searched for on the argument database `db`.
|
|
##
|
|
## On error, at least the vertex ID of the first missing Merkle hash key is
|
|
## returned. If the argument `stopEarly` is set `false`, all missing Merkle
|
|
## hash keys are returned.
|
|
##
|
|
## In the argument `beKeyOk` is set `false`, keys for node links are accepted
|
|
## only from the cache layer. This does not affect a link key for a payload
|
|
## storage root.
|
|
##
|
|
proc getKey(db: AristoDbRef; rvid: RootedVertexID; beOk: bool): HashKey =
|
|
block body:
|
|
let key = db.layersGetKey(rvid).valueOr:
|
|
break body
|
|
if key[0].isValid:
|
|
return key[0]
|
|
else:
|
|
return VOID_HASH_KEY
|
|
if beOk:
|
|
let rc = db.getKeyBE rvid
|
|
if rc.isOk:
|
|
return rc.value[0]
|
|
VOID_HASH_KEY
|
|
|
|
case vtx.vType:
|
|
of Leaf:
|
|
let node = NodeRef(vType: Leaf, lPfx: vtx.lPfx, lData: vtx.lData)
|
|
# Need to resolve storage root for account leaf
|
|
if vtx.lData.pType == AccountData:
|
|
let vid = vtx.lData.stoID
|
|
if vid.isValid:
|
|
let key = db.getKey (root, vid)
|
|
if not key.isValid:
|
|
return err(@[vid])
|
|
node.key[0] = key
|
|
return ok node
|
|
|
|
of Branch:
|
|
let node = NodeRef(vType: Branch, bVid: vtx.bVid, ePfx: vtx.ePfx)
|
|
var missing: seq[VertexID]
|
|
for n in 0 .. 15:
|
|
let vid = vtx.bVid[n]
|
|
if vid.isValid:
|
|
let key = db.getKey((root, vid), beOk=beKeyOk)
|
|
if key.isValid:
|
|
node.key[n] = key
|
|
elif stopEarly:
|
|
return err(@[vid])
|
|
else:
|
|
missing.add vid
|
|
if 0 < missing.len:
|
|
return err(missing)
|
|
return ok node
|
|
|
|
|
|
iterator subVids*(vtx: VertexRef): VertexID =
|
|
## Returns the list of all sub-vertex IDs for the argument `vtx`.
|
|
case vtx.vType:
|
|
of Leaf:
|
|
if vtx.lData.pType == AccountData:
|
|
let vid = vtx.lData.stoID
|
|
if vid.isValid:
|
|
yield vid
|
|
of Branch:
|
|
for vid in vtx.bVid:
|
|
if vid.isValid:
|
|
yield vid
|
|
|
|
# ---------------------
|
|
|
|
proc updateAccountForHasher*(
|
|
db: AristoDbRef; # Database
|
|
hike: Hike; # Return value from `retrieveStorageID()`
|
|
) =
|
|
## The argument `hike` is used to mark/reset the keys along the implied
|
|
## vertex path for being re-calculated.
|
|
##
|
|
for w in hike.legs:
|
|
db.layersResKey((hike.root, w.wp.vid))
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|