2024-06-07 10:56:31 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-07-11 11:26:46 +00:00
|
|
|
import eth/common, results, ".."/[aristo_desc, aristo_get, aristo_layers, aristo_vid]
|
2024-06-07 10:56:31 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private getters & setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
proc xPfx(vtx: VertexRef): NibblesBuf =
|
2024-07-11 11:26:46 +00:00
|
|
|
case vtx.vType
|
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
|
|
|
of Leaf: vtx.lPfx
|
|
|
|
of Branch: vtx.ePfx
|
2024-06-07 10:56:31 +00:00
|
|
|
|
|
|
|
# -----------
|
|
|
|
|
2024-07-11 11:26:46 +00:00
|
|
|
proc layersPutLeaf(
|
2024-07-14 10:02:05 +00:00
|
|
|
db: AristoDbRef, rvid: RootedVertexID, path: NibblesBuf, payload: LeafPayload
|
|
|
|
): VertexRef =
|
2024-07-11 11:26:46 +00:00
|
|
|
let vtx = VertexRef(vType: Leaf, lPfx: path, lData: payload)
|
|
|
|
db.layersPutVtx(rvid, vtx)
|
2024-07-14 10:02:05 +00:00
|
|
|
vtx
|
2024-07-11 11:26:46 +00:00
|
|
|
|
2024-06-18 11:14:02 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc mergePayloadImpl*(
|
2024-07-11 11:26:46 +00:00
|
|
|
db: AristoDbRef, # Database, top layer
|
|
|
|
root: VertexID, # MPT state root
|
|
|
|
path: openArray[byte], # Leaf item to add to the database
|
2024-07-14 10:02:05 +00:00
|
|
|
payload: LeafPayload, # Payload value
|
|
|
|
): Result[VertexRef, AristoError] =
|
2024-06-18 11:14:02 +00:00
|
|
|
## Merge the argument `(root,path)` key-value-pair into the top level vertex
|
|
|
|
## table of the database `db`. The `path` argument is used to address the
|
|
|
|
## leaf vertex with the payload. It is stored or updated on the database
|
|
|
|
## accordingly.
|
|
|
|
##
|
2024-07-11 11:26:46 +00:00
|
|
|
var
|
|
|
|
path = NibblesBuf.fromBytes(path)
|
|
|
|
cur = root
|
|
|
|
touched: array[NibblesBuf.high + 1, VertexID]
|
|
|
|
pos = 0
|
|
|
|
vtx = db.getVtxRc((root, cur)).valueOr:
|
|
|
|
if error != GetVtxNotFound:
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
# We're at the root vertex and there is no data - this must be a fresh
|
|
|
|
# VertexID!
|
2024-07-14 10:02:05 +00:00
|
|
|
return ok db.layersPutLeaf((root, cur), path, payload)
|
2024-07-11 11:26:46 +00:00
|
|
|
|
|
|
|
template resetKeys() =
|
|
|
|
# Reset cached hashes of touched verticies
|
|
|
|
for i in 0 ..< pos:
|
|
|
|
db.layersResKey((root, touched[pos - i - 1]))
|
|
|
|
|
|
|
|
while path.len > 0:
|
|
|
|
# Clear existing merkle keys along the traversal path
|
|
|
|
touched[pos] = cur
|
|
|
|
pos += 1
|
|
|
|
|
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
|
|
|
let n = path.sharedPrefixLen(vtx.xPfx)
|
2024-07-11 11:26:46 +00:00
|
|
|
case vtx.vType
|
2024-06-18 11:14:02 +00:00
|
|
|
of Leaf:
|
2024-07-14 10:02:05 +00:00
|
|
|
let leafVtx =
|
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 n == vtx.lPfx.len:
|
|
|
|
# Same path - replace the current vertex with a new payload
|
2024-06-18 11:14:02 +00:00
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
if vtx.lData == payload:
|
|
|
|
# TODO is this still needed? Higher levels should already be doing
|
|
|
|
# these checks
|
|
|
|
return err(MergeLeafPathCachedAlready)
|
2024-06-18 11:14:02 +00:00
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
if root == VertexID(1):
|
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
|
|
|
var payload = payload.dup()
|
2024-07-14 10:02:05 +00:00
|
|
|
# TODO can we avoid this hack? it feels like the caller should already
|
|
|
|
# have set an appropriate stoID - this "fixup" feels risky,
|
|
|
|
# specially from a caching point of view
|
|
|
|
payload.stoID = vtx.lData.stoID
|
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
|
|
|
db.layersPutLeaf((root, cur), path, payload)
|
|
|
|
else:
|
|
|
|
db.layersPutLeaf((root, cur), path, payload)
|
|
|
|
else:
|
|
|
|
# Turn leaf into a branch (or extension) then insert the two leaves
|
|
|
|
# into the branch
|
|
|
|
let branch = VertexRef(vType: Branch, ePfx: path.slice(0, n))
|
|
|
|
block: # Copy of existing leaf node, now one level deeper
|
|
|
|
let local = db.vidFetch()
|
|
|
|
branch.bVid[vtx.lPfx[n]] = local
|
|
|
|
discard db.layersPutLeaf((root, local), vtx.lPfx.slice(n + 1), vtx.lData)
|
2024-06-18 11:14:02 +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
|
|
|
let leafVtx = block: # Newly inserted leaf node
|
|
|
|
let local = db.vidFetch()
|
|
|
|
branch.bVid[path[n]] = local
|
|
|
|
db.layersPutLeaf((root, local), path.slice(n + 1), payload)
|
2024-07-11 11:26:46 +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
|
|
|
# Put the branch at the vid where the leaf was
|
|
|
|
db.layersPutVtx((root, cur), branch)
|
|
|
|
|
|
|
|
leafVtx
|
2024-07-11 11:26:46 +00:00
|
|
|
|
|
|
|
resetKeys()
|
2024-07-14 10:02:05 +00:00
|
|
|
return ok(leafVtx)
|
2024-07-11 11:26:46 +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
|
|
|
if vtx.ePfx.len == n:
|
|
|
|
# The existing branch is a prefix of the new entry
|
|
|
|
let
|
|
|
|
nibble = path[vtx.ePfx.len]
|
|
|
|
next = vtx.bVid[nibble]
|
|
|
|
|
|
|
|
if next.isValid:
|
|
|
|
cur = next
|
|
|
|
path = path.slice(n + 1)
|
|
|
|
vtx = ?db.getVtxRc((root, next))
|
|
|
|
else:
|
|
|
|
# There's no vertex at the branch point - insert the payload as a new
|
|
|
|
# leaf and update the existing branch
|
|
|
|
let
|
|
|
|
local = db.vidFetch()
|
|
|
|
leafVtx = db.layersPutLeaf((root, local), path.slice(n + 1), payload)
|
|
|
|
brDup = vtx.dup()
|
|
|
|
|
|
|
|
brDup.bVid[nibble] = local
|
|
|
|
db.layersPutVtx((root, cur), brDup)
|
|
|
|
|
|
|
|
resetKeys()
|
|
|
|
return ok(leafVtx)
|
2024-07-11 11:26:46 +00:00
|
|
|
else:
|
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
|
|
|
# Partial path match - we need to split the existing branch at
|
|
|
|
# the point of divergence, inserting a new branch
|
|
|
|
let branch = VertexRef(vType: Branch, ePfx: path.slice(0, n))
|
|
|
|
block: # Copy the existing vertex and add it to the new branch
|
|
|
|
let local = db.vidFetch()
|
|
|
|
branch.bVid[vtx.ePfx[n]] = local
|
|
|
|
|
|
|
|
db.layersPutVtx(
|
|
|
|
(root, local),
|
|
|
|
VertexRef(vType: Branch, ePfx: vtx.ePfx.slice(n + 1), bVid: vtx.bVid),
|
|
|
|
)
|
|
|
|
|
|
|
|
let leafVtx = block: # add the new entry
|
|
|
|
let local = db.vidFetch()
|
|
|
|
branch.bVid[path[n]] = local
|
|
|
|
db.layersPutLeaf((root, local), path.slice(n + 1), payload)
|
|
|
|
|
|
|
|
db.layersPutVtx((root, cur), branch)
|
|
|
|
|
2024-07-11 11:26:46 +00:00
|
|
|
resetKeys()
|
2024-07-14 10:02:05 +00:00
|
|
|
return ok(leafVtx)
|
2024-07-11 11:26:46 +00:00
|
|
|
|
|
|
|
err(MergeHikeFailed)
|
2024-06-18 11:14:02 +00:00
|
|
|
|
2024-06-07 10:56:31 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|