2023-05-30 11:47:47 +00:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-30 11:47: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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-06-12 18:16:03 +00:00
|
|
|
"."/[aristo_desc, aristo_get]
|
2023-05-30 11:47:47 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Leg* = object
|
|
|
|
## For constructing a `VertexPath`
|
|
|
|
wp*: VidVtxPair ## Vertex ID and data ref
|
|
|
|
nibble*: int8 ## Next vertex selector for `Branch` (if any)
|
|
|
|
|
|
|
|
Hike* = object
|
|
|
|
## Trie traversal path
|
|
|
|
root*: VertexID ## Handy for some fringe cases
|
|
|
|
legs*: seq[Leg] ## Chain of vertices and IDs
|
2024-06-22 20:33:37 +00:00
|
|
|
tail*: NibblesBuf ## Portion of non completed path
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
const
|
|
|
|
HikeAcceptableStopsNotFound* = {
|
|
|
|
HikeBranchTailEmpty,
|
|
|
|
HikeBranchMissingEdge,
|
|
|
|
HikeLeafUnexpected,
|
|
|
|
HikeNoLegs}
|
|
|
|
## When trying to find a leaf vertex the Patricia tree, there are several
|
|
|
|
## conditions where the search stops which do not constitute a problem
|
|
|
|
## with the trie (aka sysetm error.)
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
func getNibblesImpl(hike: Hike; start = 0; maxLen = high(int)): NibblesBuf =
|
2023-05-30 11:47:47 +00:00
|
|
|
## May be needed for partial rebuild, as well
|
|
|
|
for n in start ..< min(hike.legs.len, maxLen):
|
|
|
|
let leg = hike.legs[n]
|
|
|
|
case leg.wp.vtx.vType:
|
|
|
|
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
|
|
|
result = result & leg.wp.vtx.ePfx & NibblesBuf.nibble(leg.nibble.byte)
|
2023-05-30 11:47:47 +00:00
|
|
|
of Leaf:
|
|
|
|
result = result & leg.wp.vtx.lPfx
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
func to*(rc: Result[Hike,(VertexID,AristoError,Hike)]; T: type Hike): T =
|
2023-09-15 15:23:53 +00:00
|
|
|
## Extract `Hike` from either ok ot error part of argument `rc`.
|
2024-02-08 16:32:16 +00:00
|
|
|
if rc.isOk: rc.value else: rc.error[2]
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
func to*(hike: Hike; T: type NibblesBuf): T =
|
2023-05-30 11:47:47 +00:00
|
|
|
## Convert back
|
|
|
|
hike.getNibblesImpl() & hike.tail
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
func legsTo*(hike: Hike; T: type NibblesBuf): T =
|
2023-05-30 11:47:47 +00:00
|
|
|
## Convert back
|
|
|
|
hike.getNibblesImpl()
|
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
func legsTo*(hike: Hike; numLegs: int; T: type NibblesBuf): T =
|
2023-12-04 20:39:26 +00:00
|
|
|
## variant of `legsTo()`
|
|
|
|
hike.getNibblesImpl(0, numLegs)
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# --------
|
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
proc step*(
|
2024-07-04 13:46:52 +00:00
|
|
|
path: NibblesBuf, rvid: RootedVertexID, db: AristoDbRef
|
2024-07-01 12:07:39 +00:00
|
|
|
): Result[(VertexRef, NibblesBuf, VertexID), AristoError] =
|
|
|
|
# Fetch next vertex
|
2024-07-04 13:46:52 +00:00
|
|
|
let vtx = db.getVtxRc(rvid).valueOr:
|
2024-07-01 12:07:39 +00:00
|
|
|
if error != GetVtxNotFound:
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
# The vertex ID `vid` was a follow up from a parent vertex, but there is
|
|
|
|
# no child vertex on the database. So `vid` is a dangling link which is
|
|
|
|
# allowed only if there is a partial trie (e.g. with `snap` sync.)
|
|
|
|
return err(HikeDanglingEdge)
|
|
|
|
|
|
|
|
case vtx.vType:
|
|
|
|
of Leaf:
|
|
|
|
# This must be the last vertex, so there cannot be any `tail` left.
|
|
|
|
if path.len != path.sharedPrefixLen(vtx.lPfx):
|
|
|
|
return err(HikeLeafUnexpected)
|
|
|
|
|
|
|
|
ok (vtx, NibblesBuf(), VertexID(0))
|
|
|
|
|
|
|
|
of Branch:
|
|
|
|
# There must be some more data (aka `tail`) after a `Branch` vertex.
|
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 path.len <= vtx.ePfx.len:
|
2024-07-01 12:07:39 +00:00
|
|
|
return err(HikeBranchTailEmpty)
|
|
|
|
|
|
|
|
let
|
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
|
|
|
nibble = path[vtx.ePfx.len].int8
|
2024-07-01 12:07:39 +00:00
|
|
|
nextVid = vtx.bVid[nibble]
|
|
|
|
|
|
|
|
if not nextVid.isValid:
|
|
|
|
return err(HikeBranchMissingEdge)
|
|
|
|
|
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
|
|
|
ok (vtx, path.slice(vtx.ePfx.len + 1), nextVid)
|
2024-07-01 12:07:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
iterator stepUp*(
|
|
|
|
path: NibblesBuf; # Partial path
|
|
|
|
root: VertexID; # Start vertex
|
|
|
|
db: AristoDbRef; # Database
|
|
|
|
): Result[VertexRef, AristoError] =
|
|
|
|
## For the argument `path`, iterate over the logest possible path in the
|
|
|
|
## argument database `db`.
|
|
|
|
var
|
|
|
|
path = path
|
|
|
|
next = root
|
|
|
|
vtx: VertexRef
|
|
|
|
block iter:
|
|
|
|
while true:
|
2024-07-04 13:46:52 +00:00
|
|
|
(vtx, path, next) = step(path, (root, next), db).valueOr:
|
2024-07-01 12:07:39 +00:00
|
|
|
yield Result[VertexRef, AristoError].err(error)
|
|
|
|
break iter
|
|
|
|
|
|
|
|
yield Result[VertexRef, AristoError].ok(vtx)
|
|
|
|
|
|
|
|
if path.len == 0:
|
|
|
|
break
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
proc hikeUp*(
|
2024-06-22 20:33:37 +00:00
|
|
|
path: NibblesBuf; # Partial path
|
2023-05-30 11:47:47 +00:00
|
|
|
root: VertexID; # Start vertex
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database
|
2024-02-08 16:32:16 +00:00
|
|
|
): Result[Hike,(VertexID,AristoError,Hike)] =
|
2023-05-30 11:47:47 +00:00
|
|
|
## For the argument `path`, find and return the logest possible path in the
|
|
|
|
## argument database `db`.
|
2023-09-15 15:23:53 +00:00
|
|
|
var hike = Hike(
|
2023-05-30 11:47:47 +00:00
|
|
|
root: root,
|
|
|
|
tail: path)
|
|
|
|
|
2023-06-12 13:48:47 +00:00
|
|
|
if not root.isValid:
|
2024-02-08 16:32:16 +00:00
|
|
|
return err((VertexID(0),HikeRootMissing,hike))
|
2023-10-27 21:36:51 +00:00
|
|
|
if path.len == 0:
|
2024-02-08 16:32:16 +00:00
|
|
|
return err((VertexID(0),HikeEmptyPath,hike))
|
2023-09-15 15:23:53 +00:00
|
|
|
|
|
|
|
var vid = root
|
2024-05-03 17:38:17 +00:00
|
|
|
while true:
|
2024-07-04 13:46:52 +00:00
|
|
|
let (vtx, path, next) = step(hike.tail, (root, vid), db).valueOr:
|
2024-07-01 12:07:39 +00:00
|
|
|
return err((vid,error,hike))
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
let wp = VidVtxPair(vid:vid, vtx:vtx)
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
|
|
|
hike.legs.add Leg(wp: wp, nibble: -1)
|
|
|
|
hike.tail = path
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
break
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-07-01 12:07:39 +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
|
|
|
hike.legs.add Leg(wp: wp, nibble: int8 hike.tail[vtx.ePfx.len])
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
hike.tail = path
|
|
|
|
vid = next
|
2023-09-15 15:23:53 +00:00
|
|
|
|
|
|
|
ok hike
|
|
|
|
|
2024-02-08 16:32:16 +00:00
|
|
|
proc hikeUp*(
|
|
|
|
lty: LeafTie;
|
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[Hike,(VertexID,AristoError,Hike)] =
|
2023-05-30 11:47:47 +00:00
|
|
|
## Variant of `hike()`
|
2024-06-22 20:33:37 +00:00
|
|
|
lty.path.to(NibblesBuf).hikeUp(lty.root, db)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2024-06-27 19:21:01 +00:00
|
|
|
proc hikeUp*(
|
|
|
|
path: openArray[byte];
|
|
|
|
root: VertexID;
|
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[Hike,(VertexID,AristoError,Hike)] =
|
|
|
|
## Variant of `hike()`
|
|
|
|
NibblesBuf.fromBytes(path).hikeUp(root, db)
|
|
|
|
|
2024-07-03 08:14:26 +00:00
|
|
|
proc hikeUp*(
|
|
|
|
path: Hash256;
|
|
|
|
root: VertexID;
|
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[Hike,(VertexID,AristoError,Hike)] =
|
|
|
|
## Variant of `hike()`
|
|
|
|
NibblesBuf.fromBytes(path.data).hikeUp(root, db)
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|