2023-06-12 14:48:47 +01:00
|
|
|
# nimbus-eth1
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-12 14:48:47 +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.
|
|
|
|
|
|
|
|
## Aristo DB -- Patricia Trie structural data types
|
|
|
|
## ================================================
|
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-07-03 22:21:57 +02:00
|
|
|
std/[hashes, tables],
|
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
|
|
|
stint,
|
2024-06-22 22:33:37 +02:00
|
|
|
eth/common,
|
2024-07-22 18:10:04 +00:00
|
|
|
./desc_identifiers
|
2023-06-12 14:48:47 +01:00
|
|
|
|
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
|
|
|
export stint
|
|
|
|
|
2023-06-12 14:48:47 +01:00
|
|
|
type
|
2024-06-07 10:56:31 +00:00
|
|
|
LeafTiePayload* = object
|
|
|
|
## Generalised key-value pair for a sub-trie. The main trie is the
|
|
|
|
## sub-trie with `root=VertexID(1)`.
|
|
|
|
leafTie*: LeafTie ## Full `Patricia Trie` path root-to-leaf
|
2024-07-14 12:02:05 +02:00
|
|
|
payload*: LeafPayload ## Leaf data payload (see below)
|
2024-06-07 10:56:31 +00:00
|
|
|
|
2023-06-12 14:48:47 +01:00
|
|
|
VertexType* = enum
|
|
|
|
## Type of `Aristo Trie` vertex
|
|
|
|
Leaf
|
|
|
|
Branch
|
|
|
|
|
2023-07-05 21:27:48 +01:00
|
|
|
AristoAccount* = object
|
2024-06-27 09:01:26 +00:00
|
|
|
## Application relevant part of an Ethereum account. Note that the storage
|
2024-07-14 12:02:05 +02:00
|
|
|
## data/tree reference is not part of the account (see `LeafPayload` below.)
|
2023-07-05 21:27:48 +01:00
|
|
|
nonce*: AccountNonce ## Some `uint64` type
|
|
|
|
balance*: UInt256
|
2024-10-01 21:03:10 +00:00
|
|
|
codeHash*: Hash32
|
2023-07-05 21:27:48 +01:00
|
|
|
|
2023-06-12 14:48:47 +01:00
|
|
|
PayloadType* = enum
|
2023-12-19 12:39:23 +00:00
|
|
|
## Type of leaf data.
|
2023-07-05 21:27:48 +01:00
|
|
|
AccountData ## `Aristo account` with vertex IDs links
|
2024-07-12 13:12:25 +00:00
|
|
|
StoData ## Slot storage data
|
2023-06-12 14:48:47 +01:00
|
|
|
|
2024-08-07 13:28:01 +00:00
|
|
|
StorageID* = tuple
|
|
|
|
## Once a storage tree is allocated, its root vertex ID is registered in
|
|
|
|
## the leaf payload of an acoount. After subsequent storage tree deletion
|
|
|
|
## the root vertex ID will be kept in the leaf payload for re-use but set
|
|
|
|
## disabled (`.isValid` = `false`).
|
|
|
|
isValid: bool ## See also `isValid()` for `VertexID`
|
|
|
|
vid: VertexID ## Storage root vertex ID
|
|
|
|
|
2024-07-14 12:02:05 +02:00
|
|
|
LeafPayload* = object
|
2024-07-13 20:42:49 +02:00
|
|
|
## The payload type depends on the sub-tree used. The `VertexID(1)` rooted
|
|
|
|
## sub-tree only has `AccountData` type payload, stoID-based have StoData
|
2023-06-12 14:48:47 +01:00
|
|
|
case pType*: PayloadType
|
|
|
|
of AccountData:
|
2023-07-05 21:27:48 +01:00
|
|
|
account*: AristoAccount
|
2024-08-07 13:28:01 +00:00
|
|
|
stoID*: StorageID ## Storage vertex ID (if any)
|
2024-07-05 01:48:45 +02:00
|
|
|
of StoData:
|
|
|
|
stoData*: UInt256
|
2023-06-12 14:48:47 +01:00
|
|
|
|
2024-09-13 18:55:17 +02:00
|
|
|
VertexRef* = ref object
|
2023-06-12 14:48:47 +01:00
|
|
|
## Vertex for building a hexary Patricia or Merkle Patricia Trie
|
2024-09-13 18:55:17 +02:00
|
|
|
pfx*: NibblesBuf
|
|
|
|
## Portion of path segment - extension nodes are branch nodes with
|
|
|
|
## non-empty prefix
|
2023-06-12 14:48:47 +01:00
|
|
|
case vType*: VertexType
|
|
|
|
of Leaf:
|
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
|
|
|
lData*: LeafPayload ## Reference to data payload
|
2023-06-12 14:48:47 +01:00
|
|
|
of Branch:
|
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
|
|
|
startVid*: VertexID
|
|
|
|
used*: uint16
|
2023-06-12 14:48:47 +01:00
|
|
|
|
2024-09-13 18:55:17 +02:00
|
|
|
NodeRef* = ref object of RootRef
|
2023-06-12 14:48:47 +01:00
|
|
|
## Combined record for a *traditional* ``Merkle Patricia Tree` node merged
|
|
|
|
## with a structural `VertexRef` type object.
|
2024-09-13 18:55:17 +02:00
|
|
|
vtx*: VertexRef
|
2023-07-05 21:27:48 +01:00
|
|
|
key*: array[16,HashKey] ## Merkle hash/es for vertices
|
2023-06-12 14:48:47 +01:00
|
|
|
|
2023-08-10 21:01:28 +01:00
|
|
|
# ----------------------
|
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
VidVtxPair* = object
|
|
|
|
## Handy helper structure
|
2024-10-01 21:03:10 +00:00
|
|
|
vid*: VertexID ## Table lookup vertex ID (if any)
|
|
|
|
vtx*: VertexRef ## Reference to vertex
|
2024-03-14 22:17:43 +00:00
|
|
|
|
2024-05-31 18:32:22 +01:00
|
|
|
SavedState* = object
|
|
|
|
## Last saved state
|
2024-10-01 21:03:10 +00:00
|
|
|
key*: Hash32 ## Some state hash (if any)
|
2024-06-28 18:43:04 +00:00
|
|
|
serial*: uint64 ## Generic identifier from application
|
2024-05-31 18:32:22 +01:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
LayerRef* = ref LayerObj
|
|
|
|
LayerObj* = object
|
2023-12-19 12:39:23 +00:00
|
|
|
## Delta layers are stacked implying a tables hierarchy. Table entries on
|
|
|
|
## a higher level take precedence over lower layer table entries. So an
|
|
|
|
## existing key-value table entry of a layer on top supersedes same key
|
|
|
|
## entries on all lower layers. A missing entry on a higher layer indicates
|
|
|
|
## that the key-value pair might be fond on some lower layer.
|
|
|
|
##
|
|
|
|
## A zero value (`nil`, empty hash etc.) is considered am missing key-value
|
|
|
|
## pair. Tables on the `LayerDelta` may have stray zero key-value pairs for
|
|
|
|
## missing entries due to repeated transactions while adding and deleting
|
|
|
|
## entries. There is no need to purge redundant zero entries.
|
|
|
|
##
|
|
|
|
## As for `kMap[]` entries, there might be a zero value entriy relating
|
|
|
|
## (i.e. indexed by the same vertex ID) to an `sMap[]` non-zero value entry
|
|
|
|
## (of the same layer or a lower layer whatever comes first.) This entry
|
|
|
|
## is kept as a reminder that the hash value of the `kMap[]` entry needs
|
|
|
|
## to be re-compiled.
|
|
|
|
##
|
|
|
|
## The reasoning behind the above scenario is that every vertex held on the
|
|
|
|
## `sTab[]` tables must correspond to a hash entry held on the `kMap[]`
|
|
|
|
## tables. So a corresponding zero value or missing entry produces an
|
|
|
|
## inconsistent state that must be resolved.
|
|
|
|
##
|
2024-07-04 15:46:52 +02:00
|
|
|
sTab*: Table[RootedVertexID,VertexRef] ## Structural vertex table
|
|
|
|
kMap*: Table[RootedVertexID,HashKey] ## Merkle hash key mapping
|
|
|
|
vTop*: VertexID ## Last used vertex ID
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
accLeaves*: Table[Hash32, VertexRef] ## Account path -> VertexRef
|
|
|
|
stoLeaves*: Table[Hash32, VertexRef] ## Storage path -> VertexRef
|
2024-07-03 17:58:25 +02:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
txUid*: uint ## Transaction identifier if positive
|
2023-08-10 21:01:28 +01:00
|
|
|
|
2024-09-20 07:43:53 +02:00
|
|
|
GetVtxFlag* = enum
|
|
|
|
PeekCache
|
|
|
|
## Peek into, but don't update cache - useful on work loads that are
|
|
|
|
## unfriendly to caches
|
|
|
|
|
2023-06-12 14:48:47 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
2024-05-07 20:59:27 +01:00
|
|
|
# Public helpers (misc)
|
2023-06-12 14:48:47 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
func bVid*(vtx: VertexRef, nibble: uint8): VertexID =
|
|
|
|
if (vtx.used and (1'u16 shl nibble)) > 0:
|
|
|
|
VertexID(uint64(vtx.startVid) + nibble)
|
|
|
|
else:
|
|
|
|
default(VertexID)
|
|
|
|
|
|
|
|
func setUsed*(vtx: VertexRef, nibble: uint8, used: static bool): VertexID =
|
|
|
|
vtx.used =
|
|
|
|
when used:
|
|
|
|
vtx.used or (1'u16 shl nibble)
|
|
|
|
else:
|
|
|
|
vtx.used and (not (1'u16 shl nibble))
|
|
|
|
vtx.bVid(nibble)
|
|
|
|
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
func init*(T: type LayerRef): T =
|
|
|
|
## Constructor, returns empty layer
|
2024-07-18 21:32:32 +00:00
|
|
|
T()
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func hash*(node: NodeRef): Hash =
|
|
|
|
## Table/KeyedQueue/HashSet mixin
|
|
|
|
cast[pointer](node).hash
|
|
|
|
|
2024-05-07 20:59:27 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
2024-07-14 12:02:05 +02:00
|
|
|
# Public helpers: `NodeRef` and `LeafPayload`
|
2024-05-07 20:59:27 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-11-08 12:18:32 +00:00
|
|
|
|
2024-07-14 12:02:05 +02:00
|
|
|
proc `==`*(a, b: LeafPayload): bool =
|
2023-06-12 14:48:47 +01:00
|
|
|
## Beware, potential deep comparison
|
|
|
|
if unsafeAddr(a) != unsafeAddr(b):
|
|
|
|
if a.pType != b.pType:
|
|
|
|
return false
|
|
|
|
case a.pType:
|
|
|
|
of AccountData:
|
2024-06-27 09:01:26 +00:00
|
|
|
if a.account != b.account or
|
|
|
|
a.stoID != b.stoID:
|
2023-06-12 14:48:47 +01:00
|
|
|
return false
|
2024-07-05 01:48:45 +02:00
|
|
|
of StoData:
|
|
|
|
if a.stoData != b.stoData:
|
|
|
|
return false
|
2023-06-12 14:48:47 +01:00
|
|
|
true
|
|
|
|
|
|
|
|
proc `==`*(a, b: VertexRef): bool =
|
|
|
|
## Beware, potential deep comparison
|
|
|
|
if a.isNil:
|
|
|
|
return b.isNil
|
|
|
|
if b.isNil:
|
|
|
|
return false
|
|
|
|
if unsafeAddr(a[]) != unsafeAddr(b[]):
|
|
|
|
if a.vType != b.vType:
|
|
|
|
return false
|
|
|
|
case a.vType:
|
|
|
|
of Leaf:
|
2024-09-13 18:55:17 +02:00
|
|
|
if a.pfx != b.pfx or a.lData != b.lData:
|
2023-06-12 14:48:47 +01:00
|
|
|
return false
|
|
|
|
of Branch:
|
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
|
|
|
if a.pfx != b.pfx or a.startVid != b.startVid or a.used != b.used:
|
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
|
|
|
return false
|
2023-06-12 14:48:47 +01:00
|
|
|
true
|
|
|
|
|
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
|
|
|
iterator pairs*(vtx: VertexRef): tuple[nibble: uint8, vid: VertexID] =
|
|
|
|
## Iterates over the sub-vids of a branch (does nothing for leaves)
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
discard
|
|
|
|
of Branch:
|
|
|
|
for n in 0'u8 .. 15'u8:
|
|
|
|
if (vtx.used and (1'u16 shl n)) > 0:
|
|
|
|
yield (n, VertexID(uint64(vtx.startVid) + n))
|
|
|
|
|
|
|
|
iterator allPairs*(vtx: VertexRef): tuple[nibble: uint8, vid: VertexID] =
|
|
|
|
## Iterates over the sub-vids of a branch (does nothing for leaves) including
|
|
|
|
## currently unset nodes
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
discard
|
|
|
|
of Branch:
|
|
|
|
for n in 0'u8 .. 15'u8:
|
|
|
|
if (vtx.used and (1'u16 shl n)) > 0:
|
|
|
|
yield (n, VertexID(uint64(vtx.startVid) + n))
|
|
|
|
else:
|
|
|
|
yield (n, default(VertexID))
|
|
|
|
|
2023-06-12 14:48:47 +01:00
|
|
|
proc `==`*(a, b: NodeRef): bool =
|
|
|
|
## Beware, potential deep comparison
|
2024-09-13 18:55:17 +02:00
|
|
|
if a.vtx != b.vtx:
|
2023-06-12 14:48:47 +01:00
|
|
|
return false
|
2024-09-13 18:55:17 +02:00
|
|
|
case a.vtx.vType:
|
2023-06-12 14:48:47 +01:00
|
|
|
of Branch:
|
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 n in 0'u8..15'u8:
|
|
|
|
if a.vtx.bVid(n) != 0.VertexID or b.vtx.bVid(n) != 0.VertexID:
|
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
|
|
|
if a.key[n] != b.key[n]:
|
|
|
|
return false
|
2023-06-12 14:48:47 +01:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
true
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers, miscellaneous functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-14 12:02:05 +02:00
|
|
|
func dup*(pld: LeafPayload): LeafPayload =
|
2023-06-22 20:21:33 +01:00
|
|
|
## Duplicate payload.
|
|
|
|
case pld.pType:
|
|
|
|
of AccountData:
|
2024-07-14 12:02:05 +02:00
|
|
|
LeafPayload(
|
Core db and aristo updates for destructor and tx logic (#1894)
* Disable `TransactionID` related functions from `state_db.nim`
why:
Functions `getCommittedStorage()` and `updateOriginalRoot()` from
the `state_db` module are nowhere used. The emulation of a legacy
`TransactionID` type functionality is administratively expensive to
provide by `Aristo` (the legacy DB version is only partially
implemented, anyway).
As there is no other place where `TransactionID`s are used, they will
not be provided by the `Aristo` variant of the `CoreDb`. For the
legacy DB API, nothing will change.
* Fix copyright headers in source code
* Get rid of compiler warning
* Update Aristo code, remove unused `merge()` variant, export `hashify()`
why:
Adapt to upcoming `CoreDb` wrapper
* Remove synced tx feature from `Aristo`
why:
+ This feature allowed to synchronise transaction methods like begin,
commit, and rollback for a group of descriptors.
+ The feature is over engineered and not needed for `CoreDb`, neither
is it complete (some convergence features missing.)
* Add debugging helpers to `Kvt`
also:
Update database iterator, add count variable yield argument similar
to `Aristo`.
* Provide optional destructors for `CoreDb` API
why;
For the upcoming Aristo wrapper, this allows to control when certain
smart destruction and update can take place. The auto destructor works
fine in general when the storage/cache strategy is known and acceptable
when creating descriptors.
* Add update option for `CoreDb` API function `hash()`
why;
The hash function is typically used to get the state root of the MPT.
Due to lazy hashing, this might be not available on the `Aristo` DB.
So the `update` function asks for re-hashing the gurrent state changes
if needed.
* Update API tracking log mode: `info` => `debug
* Use shared `Kvt` descriptor in new Ledger API
why:
No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
|
|
|
pType: AccountData,
|
2024-06-27 09:01:26 +00:00
|
|
|
account: pld.account,
|
|
|
|
stoID: pld.stoID)
|
2024-07-05 01:48:45 +02:00
|
|
|
of StoData:
|
2024-07-14 12:02:05 +02:00
|
|
|
LeafPayload(
|
2024-07-05 01:48:45 +02:00
|
|
|
pType: StoData,
|
|
|
|
stoData: pld.stoData
|
|
|
|
)
|
2023-06-22 20:21:33 +01:00
|
|
|
|
Core db and aristo updates for destructor and tx logic (#1894)
* Disable `TransactionID` related functions from `state_db.nim`
why:
Functions `getCommittedStorage()` and `updateOriginalRoot()` from
the `state_db` module are nowhere used. The emulation of a legacy
`TransactionID` type functionality is administratively expensive to
provide by `Aristo` (the legacy DB version is only partially
implemented, anyway).
As there is no other place where `TransactionID`s are used, they will
not be provided by the `Aristo` variant of the `CoreDb`. For the
legacy DB API, nothing will change.
* Fix copyright headers in source code
* Get rid of compiler warning
* Update Aristo code, remove unused `merge()` variant, export `hashify()`
why:
Adapt to upcoming `CoreDb` wrapper
* Remove synced tx feature from `Aristo`
why:
+ This feature allowed to synchronise transaction methods like begin,
commit, and rollback for a group of descriptors.
+ The feature is over engineered and not needed for `CoreDb`, neither
is it complete (some convergence features missing.)
* Add debugging helpers to `Kvt`
also:
Update database iterator, add count variable yield argument similar
to `Aristo`.
* Provide optional destructors for `CoreDb` API
why;
For the upcoming Aristo wrapper, this allows to control when certain
smart destruction and update can take place. The auto destructor works
fine in general when the storage/cache strategy is known and acceptable
when creating descriptors.
* Add update option for `CoreDb` API function `hash()`
why;
The hash function is typically used to get the state root of the MPT.
Due to lazy hashing, this might be not available on the `Aristo` DB.
So the `update` function asks for re-hashing the gurrent state changes
if needed.
* Update API tracking log mode: `info` => `debug
* Use shared `Kvt` descriptor in new Ledger API
why:
No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
|
|
|
func dup*(vtx: VertexRef): VertexRef =
|
2023-06-22 20:21:33 +01:00
|
|
|
## Duplicate vertex.
|
|
|
|
# Not using `deepCopy()` here (some `gc` needs `--deepcopy:on`.)
|
2023-06-30 23:22:33 +01:00
|
|
|
if vtx.isNil:
|
|
|
|
VertexRef(nil)
|
|
|
|
else:
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
VertexRef(
|
|
|
|
vType: Leaf,
|
2024-09-13 18:55:17 +02:00
|
|
|
pfx: vtx.pfx,
|
2024-02-20 10:07:38 +07:00
|
|
|
lData: vtx.lData.dup)
|
2023-06-30 23:22:33 +01:00
|
|
|
of Branch:
|
|
|
|
VertexRef(
|
|
|
|
vType: Branch,
|
2024-09-13 18:55:17 +02:00
|
|
|
pfx: vtx.pfx,
|
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
|
|
|
startVid: vtx.startVid,
|
|
|
|
used: vtx.used)
|
2023-06-22 20:21:33 +01:00
|
|
|
|
Core db and aristo updates for destructor and tx logic (#1894)
* Disable `TransactionID` related functions from `state_db.nim`
why:
Functions `getCommittedStorage()` and `updateOriginalRoot()` from
the `state_db` module are nowhere used. The emulation of a legacy
`TransactionID` type functionality is administratively expensive to
provide by `Aristo` (the legacy DB version is only partially
implemented, anyway).
As there is no other place where `TransactionID`s are used, they will
not be provided by the `Aristo` variant of the `CoreDb`. For the
legacy DB API, nothing will change.
* Fix copyright headers in source code
* Get rid of compiler warning
* Update Aristo code, remove unused `merge()` variant, export `hashify()`
why:
Adapt to upcoming `CoreDb` wrapper
* Remove synced tx feature from `Aristo`
why:
+ This feature allowed to synchronise transaction methods like begin,
commit, and rollback for a group of descriptors.
+ The feature is over engineered and not needed for `CoreDb`, neither
is it complete (some convergence features missing.)
* Add debugging helpers to `Kvt`
also:
Update database iterator, add count variable yield argument similar
to `Aristo`.
* Provide optional destructors for `CoreDb` API
why;
For the upcoming Aristo wrapper, this allows to control when certain
smart destruction and update can take place. The auto destructor works
fine in general when the storage/cache strategy is known and acceptable
when creating descriptors.
* Add update option for `CoreDb` API function `hash()`
why;
The hash function is typically used to get the state root of the MPT.
Due to lazy hashing, this might be not available on the `Aristo` DB.
So the `update` function asks for re-hashing the gurrent state changes
if needed.
* Update API tracking log mode: `info` => `debug
* Use shared `Kvt` descriptor in new Ledger API
why:
No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
|
|
|
func dup*(node: NodeRef): NodeRef =
|
2023-07-13 00:03:14 +01:00
|
|
|
## Duplicate node.
|
|
|
|
# Not using `deepCopy()` here (some `gc` needs `--deepcopy:on`.)
|
|
|
|
if node.isNil:
|
|
|
|
NodeRef(nil)
|
|
|
|
else:
|
2024-09-13 18:55:17 +02:00
|
|
|
NodeRef(
|
|
|
|
vtx: node.vtx.dup(),
|
|
|
|
key: node.key)
|
2023-07-13 00:03:14 +01:00
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
func dup*(wp: VidVtxPair): VidVtxPair =
|
|
|
|
## Safe copy of `wp` argument
|
|
|
|
VidVtxPair(
|
|
|
|
vid: wp.vid,
|
|
|
|
vtx: wp.vtx.dup)
|
|
|
|
|
2023-09-05 14:57:20 +01:00
|
|
|
# ---------------
|
2023-08-17 14:42:01 +01:00
|
|
|
|
2023-09-05 14:57:20 +01:00
|
|
|
func to*(node: NodeRef; T: type VertexRef): T =
|
2023-06-22 20:21:33 +01:00
|
|
|
## Extract a copy of the `VertexRef` part from a `NodeRef`.
|
|
|
|
node.VertexRef.dup
|
2023-06-12 14:48:47 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|