mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-29 05:25:34 +00:00
81e75622cf
The state and account MPT:s currenty share key space in the database based on that vertex id:s are assigned essentially randomly, which means that when two adjacent slot values from the same contract are accessed, they might reside at large distance from each other. Here, we prefix each vertex id by its root causing them to be sorted together thus bringing all data belonging to a particular contract closer together - the same effect also happens for the main state MPT whose nodes now end up clustered together more tightly. In the future, the prefix given to the storage keys can also be used to perform range operations such as reading all the storage at once and/or deleting an account with a batch operation. Notably, parts of the API already supported this rooting concept while parts didn't - this PR makes the API consistent by always working with a root+vid.
109 lines
3.4 KiB
Nim
109 lines
3.4 KiB
Nim
# Nimbus - Types, data structures and shared utilities used in network sync
|
|
#
|
|
# 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.
|
|
|
|
import
|
|
std/[algorithm, sequtils, sets, tables],
|
|
results,
|
|
".."/[aristo_desc, aristo_get, aristo_init, aristo_layers, aristo_utils]
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public generic iterators
|
|
# ------------------------------------------------------------------------------
|
|
|
|
iterator walkVtxBeImpl*[T](
|
|
db: AristoDbRef; # Database with optional backend filter
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
|
## Generic iterator
|
|
when T is VoidBackendRef:
|
|
let filter = if db.balancer.isNil: LayerDeltaRef() else: db.balancer
|
|
|
|
else:
|
|
mixin walkVtx
|
|
|
|
let filter = LayerDeltaRef()
|
|
if not db.balancer.isNil:
|
|
filter.sTab = db.balancer.sTab # copy table
|
|
|
|
for (rvid,vtx) in db.backend.T.walkVtx:
|
|
if filter.sTab.hasKey rvid:
|
|
let fVtx = filter.sTab.getOrVoid rvid
|
|
if fVtx.isValid:
|
|
yield (rvid,fVtx)
|
|
filter.sTab.del rvid
|
|
else:
|
|
yield (rvid,vtx)
|
|
|
|
for rvid in filter.sTab.keys.toSeq.sorted:
|
|
let vtx = filter.sTab.getOrVoid rvid
|
|
if vtx.isValid:
|
|
yield (rvid,vtx)
|
|
|
|
|
|
iterator walkKeyBeImpl*[T](
|
|
db: AristoDbRef; # Database with optional backend filter
|
|
): tuple[rvid: RootedVertexID, key: HashKey] =
|
|
## Generic iterator
|
|
when T is VoidBackendRef:
|
|
let filter = if db.balancer.isNil: LayerDeltaRef() else: db.balancer
|
|
|
|
else:
|
|
mixin walkKey
|
|
|
|
let filter = LayerDeltaRef()
|
|
if not db.balancer.isNil:
|
|
filter.kMap = db.balancer.kMap # copy table
|
|
|
|
for (rvid,key) in db.backend.T.walkKey:
|
|
if filter.kMap.hasKey rvid:
|
|
let fKey = filter.kMap.getOrVoid rvid
|
|
if fKey.isValid:
|
|
yield (rvid,fKey)
|
|
filter.kMap.del rvid
|
|
else:
|
|
yield (rvid,key)
|
|
|
|
for rvid in filter.kMap.keys.toSeq.sorted:
|
|
let key = filter.kMap.getOrVoid rvid
|
|
if key.isValid:
|
|
yield (rvid,key)
|
|
|
|
|
|
iterator walkPairsImpl*[T](
|
|
db: AristoDbRef; # Database with top layer & backend filter
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
|
## Walk over all `(VertexID,VertexRef)` in the database. Note that entries
|
|
## are unsorted.
|
|
var seen: HashSet[VertexID]
|
|
for (rvid,vtx) in db.layersWalkVtx seen:
|
|
if vtx.isValid:
|
|
yield (rvid,vtx)
|
|
|
|
for (rvid,vtx) in walkVtxBeImpl[T](db):
|
|
if rvid.vid notin seen:
|
|
yield (rvid,vtx)
|
|
|
|
iterator replicateImpl*[T](
|
|
db: AristoDbRef; # Database with top layer & backend filter
|
|
): tuple[rvid: RootedVertexID, key: HashKey, vtx: VertexRef, node: NodeRef] =
|
|
## Variant of `walkPairsImpl()` for legacy applications.
|
|
for (rvid,vtx) in walkPairsImpl[T](db):
|
|
let node = block:
|
|
let rc = vtx.toNode(rvid.root, db)
|
|
if rc.isOk:
|
|
rc.value
|
|
else:
|
|
NodeRef(nil)
|
|
yield (rvid, db.getKey rvid, vtx, node)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|