2023-06-12 13:48:47 +00: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 13:48:47 +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.
|
|
|
|
|
|
|
|
## Aristo DB -- Patricia Trie structural data types
|
|
|
|
## ================================================
|
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-07-03 20:21:57 +00: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 20:33:37 +00:00
|
|
|
eth/common,
|
2023-08-25 22:53:59 +00:00
|
|
|
"."/[desc_error, desc_identifiers]
|
2023-06-12 13:48:47 +00: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 13:48:47 +00: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 10:02:05 +00:00
|
|
|
payload*: LeafPayload ## Leaf data payload (see below)
|
2024-06-07 10:56:31 +00:00
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
VertexType* = enum
|
|
|
|
## Type of `Aristo Trie` vertex
|
|
|
|
Leaf
|
|
|
|
Branch
|
|
|
|
|
2023-07-05 20:27:48 +00: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 10:02:05 +00:00
|
|
|
## data/tree reference is not part of the account (see `LeafPayload` below.)
|
2023-07-05 20:27:48 +00:00
|
|
|
nonce*: AccountNonce ## Some `uint64` type
|
|
|
|
balance*: UInt256
|
|
|
|
codeHash*: Hash256
|
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
PayloadType* = enum
|
2023-12-19 12:39:23 +00:00
|
|
|
## Type of leaf data.
|
2023-07-05 20:27:48 +00:00
|
|
|
RawData ## Generic data
|
|
|
|
AccountData ## `Aristo account` with vertex IDs links
|
2024-07-12 13:12:25 +00:00
|
|
|
StoData ## Slot storage data
|
2023-06-12 13:48:47 +00:00
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
LeafPayload* = object
|
2024-07-13 18:42:49 +00: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
|
|
|
|
## while generic have RawData
|
2023-06-12 13:48:47 +00:00
|
|
|
case pType*: PayloadType
|
2023-07-05 20:27:48 +00:00
|
|
|
of RawData:
|
|
|
|
rawBlob*: Blob ## Opaque data, default value
|
2023-06-12 13:48:47 +00:00
|
|
|
of AccountData:
|
2023-07-05 20:27:48 +00:00
|
|
|
account*: AristoAccount
|
2024-06-27 09:01:26 +00:00
|
|
|
stoID*: VertexID ## Storage vertex ID (if any)
|
2024-07-04 23:48:45 +00:00
|
|
|
of StoData:
|
|
|
|
stoData*: UInt256
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
VertexRef* = ref object of RootRef
|
|
|
|
## Vertex for building a hexary Patricia or Merkle Patricia Trie
|
|
|
|
case vType*: VertexType
|
|
|
|
of Leaf:
|
2024-06-22 20:33:37 +00:00
|
|
|
lPfx*: NibblesBuf ## Portion of path segment
|
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 13:48:47 +00:00
|
|
|
of Branch:
|
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
|
|
|
ePfx*: NibblesBuf ## Portion of path segment - if non-empty,
|
|
|
|
## it's an extension node!
|
2023-06-12 13:48:47 +00:00
|
|
|
bVid*: array[16,VertexID] ## Edge list with vertex IDs
|
|
|
|
|
|
|
|
NodeRef* = ref object of VertexRef
|
|
|
|
## Combined record for a *traditional* ``Merkle Patricia Tree` node merged
|
|
|
|
## with a structural `VertexRef` type object.
|
2023-11-08 12:18:32 +00:00
|
|
|
error*: AristoError ## Used for error signalling in RLP decoder
|
2023-07-05 20:27:48 +00:00
|
|
|
key*: array[16,HashKey] ## Merkle hash/es for vertices
|
2023-06-12 13:48:47 +00:00
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
# ----------------------
|
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
VidVtxPair* = object
|
|
|
|
## Handy helper structure
|
|
|
|
vid*: VertexID ## Table lookup vertex ID (if any)
|
|
|
|
vtx*: VertexRef ## Reference to vertex
|
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
SavedState* = object
|
|
|
|
## Last saved state
|
2024-06-28 18:43:04 +00:00
|
|
|
key*: Hash256 ## Some state hash (if any)
|
|
|
|
serial*: uint64 ## Generic identifier from application
|
2024-05-31 17:32:22 +00:00
|
|
|
|
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
|
|
|
LayerDeltaRef* = ref 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 13:46:52 +00: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-07-14 17:12:10 +00:00
|
|
|
accLeaves*: Table[Hash256, VertexRef] ## Account path -> VertexRef
|
|
|
|
stoLeaves*: Table[Hash256, VertexRef] ## Storage path -> VertexRef
|
2024-07-03 15:58:25 +00:00
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
LayerRef* = ref LayerObj
|
|
|
|
LayerObj* = object
|
2023-08-10 20:01:28 +00:00
|
|
|
## Hexary trie database layer structures. Any layer holds the full
|
|
|
|
## change relative to the backend.
|
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
|
|
|
delta*: LayerDeltaRef ## Most structural tables held as deltas
|
2023-12-19 12:39:23 +00:00
|
|
|
txUid*: uint ## Transaction identifier if positive
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2024-05-07 19:59:27 +00:00
|
|
|
# Public helpers (misc)
|
2023-06-12 13:48:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
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-03 20:21:57 +00:00
|
|
|
T(delta: LayerDeltaRef())
|
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 19:59:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2024-07-14 10:02:05 +00:00
|
|
|
# Public helpers: `NodeRef` and `LeafPayload`
|
2024-05-07 19:59:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-11-08 12:18:32 +00:00
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
proc `==`*(a, b: LeafPayload): bool =
|
2023-06-12 13:48:47 +00:00
|
|
|
## Beware, potential deep comparison
|
|
|
|
if unsafeAddr(a) != unsafeAddr(b):
|
|
|
|
if a.pType != b.pType:
|
|
|
|
return false
|
|
|
|
case a.pType:
|
2023-07-05 20:27:48 +00:00
|
|
|
of RawData:
|
|
|
|
if a.rawBlob != b.rawBlob:
|
|
|
|
return false
|
2023-06-12 13:48:47 +00:00
|
|
|
of AccountData:
|
2024-06-27 09:01:26 +00:00
|
|
|
if a.account != b.account or
|
|
|
|
a.stoID != b.stoID:
|
2023-06-12 13:48:47 +00:00
|
|
|
return false
|
2024-07-04 23:48:45 +00:00
|
|
|
of StoData:
|
|
|
|
if a.stoData != b.stoData:
|
|
|
|
return false
|
2023-06-12 13:48:47 +00: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:
|
|
|
|
if a.lPfx != b.lPfx or a.lData != b.lData:
|
|
|
|
return false
|
|
|
|
of Branch:
|
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.ePfx != b.ePfx or a.bVid != b.bVid:
|
|
|
|
return false
|
2023-06-12 13:48:47 +00:00
|
|
|
true
|
|
|
|
|
|
|
|
proc `==`*(a, b: NodeRef): bool =
|
|
|
|
## Beware, potential deep comparison
|
|
|
|
if a.VertexRef != b.VertexRef:
|
|
|
|
return false
|
|
|
|
case a.vType:
|
|
|
|
of Branch:
|
|
|
|
for n in 0..15:
|
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.bVid[n] != 0.VertexID or b.bVid[n] != 0.VertexID:
|
|
|
|
if a.key[n] != b.key[n]:
|
|
|
|
return false
|
2023-06-12 13:48:47 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
true
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers, miscellaneous functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
func dup*(pld: LeafPayload): LeafPayload =
|
2023-06-22 19:21:33 +00:00
|
|
|
## Duplicate payload.
|
|
|
|
case pld.pType:
|
2023-07-05 20:27:48 +00:00
|
|
|
of RawData:
|
2024-07-14 10:02:05 +00:00
|
|
|
LeafPayload(
|
2023-07-05 20:27:48 +00:00
|
|
|
pType: RawData,
|
|
|
|
rawBlob: pld.rawBlob)
|
2023-06-22 19:21:33 +00:00
|
|
|
of AccountData:
|
2024-07-14 10:02:05 +00: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-04 23:48:45 +00:00
|
|
|
of StoData:
|
2024-07-14 10:02:05 +00:00
|
|
|
LeafPayload(
|
2024-07-04 23:48:45 +00:00
|
|
|
pType: StoData,
|
|
|
|
stoData: pld.stoData
|
|
|
|
)
|
2023-06-22 19:21:33 +00: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 19:21:33 +00:00
|
|
|
## Duplicate vertex.
|
|
|
|
# Not using `deepCopy()` here (some `gc` needs `--deepcopy:on`.)
|
2023-06-30 22:22:33 +00:00
|
|
|
if vtx.isNil:
|
|
|
|
VertexRef(nil)
|
|
|
|
else:
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
VertexRef(
|
|
|
|
vType: Leaf,
|
|
|
|
lPfx: vtx.lPfx,
|
2024-02-20 03:07:38 +00:00
|
|
|
lData: vtx.lData.dup)
|
2023-06-30 22:22:33 +00:00
|
|
|
of Branch:
|
|
|
|
VertexRef(
|
|
|
|
vType: Branch,
|
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
|
|
|
ePfx: vtx.ePfx,
|
2023-06-30 22:22:33 +00:00
|
|
|
bVid: vtx.bVid)
|
2023-06-22 19:21:33 +00: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-12 23:03:14 +00:00
|
|
|
## Duplicate node.
|
|
|
|
# Not using `deepCopy()` here (some `gc` needs `--deepcopy:on`.)
|
|
|
|
if node.isNil:
|
|
|
|
NodeRef(nil)
|
|
|
|
else:
|
|
|
|
case node.vType:
|
|
|
|
of Leaf:
|
|
|
|
NodeRef(
|
|
|
|
vType: Leaf,
|
|
|
|
lPfx: node.lPfx,
|
2024-02-20 03:07:38 +00:00
|
|
|
lData: node.lData.dup,
|
2023-07-12 23:03:14 +00:00
|
|
|
key: node.key)
|
|
|
|
of Branch:
|
|
|
|
NodeRef(
|
|
|
|
vType: Branch,
|
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
|
|
|
ePfx: node.ePfx,
|
2023-07-12 23:03:14 +00:00
|
|
|
bVid: node.bVid,
|
|
|
|
key: node.key)
|
|
|
|
|
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 13:57:20 +00:00
|
|
|
# ---------------
|
2023-08-17 13:42:01 +00:00
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
func to*(node: NodeRef; T: type VertexRef): T =
|
2023-06-22 19:21:33 +00:00
|
|
|
## Extract a copy of the `VertexRef` part from a `NodeRef`.
|
|
|
|
node.VertexRef.dup
|
2023-06-12 13:48:47 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|