Jacek Sieka 81e75622cf
storage: store root id together with vid, for better locality of refe… (#2449)
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.
2024-07-04 15:46:52 +02:00

67 lines
2.2 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.
import
std/tables,
eth/common,
results,
".."/[aristo_desc, aristo_get]
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc revFilter*(
db: AristoDbRef; # Database
filter: LayerDeltaRef; # Filter to revert
): Result[LayerDeltaRef,(VertexID,AristoError)] =
## Assemble reverse filter for the `filter` argument, i.e. changes to the
## backend that reverse the effect of applying the this read-only filter.
##
## This read-only filter is calculated against the current unfiltered
## backend (excluding optionally installed read-only filter.)
##
# Register MPT state roots for reverting back
let rev = LayerDeltaRef()
# Get vid generator state on backend
block:
let rc = db.getTuvUbe()
if rc.isOk:
rev.vTop = rc.value
elif rc.error != GetTuvNotFound:
return err((VertexID(0), rc.error))
# Calculate reverse changes for the `sTab[]` structural table
for rvid in filter.sTab.keys:
let rc = db.getVtxUbe rvid
if rc.isOk:
rev.sTab[rvid] = rc.value
elif rc.error == GetVtxNotFound:
rev.sTab[rvid] = VertexRef(nil)
else:
return err((rvid.vid,rc.error))
# Calculate reverse changes for the `kMap` sequence.
for rvid in filter.kMap.keys:
let rc = db.getKeyUbe rvid
if rc.isOk:
rev.kMap[rvid] = rc.value
elif rc.error == GetKeyNotFound:
rev.kMap[rvid] = VOID_HASH_KEY
else:
return err((rvid.vid,rc.error))
ok(rev)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------