2023-11-08 12:18:32 +00:00
|
|
|
# nimbus-eth1
|
2024-05-23 15:37:51 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-08 12:18:32 +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, rlp],
|
2023-11-08 12:18:32 +00:00
|
|
|
results,
|
|
|
|
"."/[aristo_constants, aristo_desc, aristo_get]
|
|
|
|
|
|
|
|
type
|
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
|
|
|
ResolveVidFn = proc(
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[HashKey,AristoError]
|
|
|
|
{.gcsafe, raises: [].}
|
2023-11-08 12:18:32 +00:00
|
|
|
## Resolve storage root vertex ID
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helper
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-12 13:12:25 +00:00
|
|
|
proc serialise(
|
2024-07-14 10:02:05 +00:00
|
|
|
pyl: LeafPayload;
|
2023-11-08 12:18:32 +00:00
|
|
|
getKey: ResolveVidFn;
|
|
|
|
): Result[Blob,(VertexID,AristoError)] =
|
|
|
|
## Encode the data payload of the argument `pyl` as RLP `Blob` if it is of
|
|
|
|
## account type, otherwise pass the data as is.
|
|
|
|
##
|
|
|
|
case pyl.pType:
|
|
|
|
of RawData:
|
|
|
|
ok pyl.rawBlob
|
|
|
|
of AccountData:
|
|
|
|
let
|
2024-06-27 09:01:26 +00:00
|
|
|
vid = pyl.stoID
|
2023-11-08 12:18:32 +00:00
|
|
|
key = block:
|
|
|
|
if vid.isValid:
|
|
|
|
vid.getKey.valueOr:
|
|
|
|
let w = (vid,error)
|
|
|
|
return err(w)
|
|
|
|
else:
|
|
|
|
VOID_HASH_KEY
|
|
|
|
|
|
|
|
ok rlp.encode Account(
|
|
|
|
nonce: pyl.account.nonce,
|
|
|
|
balance: pyl.account.balance,
|
|
|
|
storageRoot: key.to(Hash256),
|
|
|
|
codeHash: pyl.account.codeHash)
|
2024-07-04 23:48:45 +00:00
|
|
|
of StoData:
|
|
|
|
ok rlp.encode pyl.stoData
|
2023-11-08 12:18:32 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public RLP transcoder mixins
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-28 15:03:12 +00:00
|
|
|
func append*(w: var RlpWriter; key: HashKey) =
|
|
|
|
if 1 < key.len and key.len < 32:
|
|
|
|
w.appendRawBytes key.data
|
|
|
|
else:
|
|
|
|
w.append key.data
|
2023-11-08 12:18:32 +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
|
|
|
# ---------------------
|
2023-11-08 12:18:32 +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
|
|
|
proc to*(w: tuple[key: HashKey, node: NodeRef]; T: type seq[(Blob,Blob)]): T =
|
|
|
|
## Convert the argument pait `w` to a single or a double pair of
|
|
|
|
## `(<key>,<rlp-encoded-node>)` tuples. Only in case of a combined extension
|
|
|
|
## and branch vertex argument, there are is a double pair result.
|
|
|
|
var wr = initRlpWriter()
|
|
|
|
case w.node.vType:
|
|
|
|
of Branch:
|
|
|
|
# Do branch node
|
|
|
|
wr.startList(17)
|
|
|
|
for n in 0..15:
|
|
|
|
wr.append w.node.key[n]
|
|
|
|
wr.append EmptyBlob
|
|
|
|
|
|
|
|
if 0 < w.node.ePfx.len:
|
|
|
|
# Do for embedded extension node
|
2024-07-17 13:48:21 +00:00
|
|
|
let brHash = wr.finish().digestTo(HashKey)
|
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.add (@(brHash.data), wr.finish())
|
|
|
|
|
|
|
|
wr = initRlpWriter()
|
|
|
|
wr.startList(2)
|
|
|
|
wr.append w.node.ePfx.toHexPrefix(isleaf = false)
|
|
|
|
wr.append brHash
|
|
|
|
else:
|
|
|
|
# Do for pure branch node
|
|
|
|
result.add (@(w.key.data), wr.finish())
|
2023-11-08 12:18:32 +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
|
|
|
of Leaf:
|
|
|
|
proc getKey0(
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[HashKey,AristoError]
|
|
|
|
{.gcsafe, raises: [].} =
|
|
|
|
ok(w.node.key[0]) # always succeeds
|
2023-11-08 12:18:32 +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
|
|
|
wr.startList(2)
|
|
|
|
wr.append w.node.lPfx.toHexPrefix(isleaf = true)
|
|
|
|
wr.append w.node.lData.serialise(getKey0).value
|
|
|
|
|
|
|
|
result.add (@(w.key.data), wr.finish())
|
|
|
|
|
2024-07-17 13:48:21 +00:00
|
|
|
proc digestTo*(node: NodeRef; T: type HashKey): T =
|
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
|
|
|
## Convert the argument `node` to the corresponding Merkle hash key. Note
|
|
|
|
## that a `Dummy` node is encoded as as a `Leaf`.
|
|
|
|
##
|
|
|
|
var wr = initRlpWriter()
|
|
|
|
case node.vType:
|
|
|
|
of Branch:
|
|
|
|
# Do branch node
|
|
|
|
wr.startList(17)
|
|
|
|
for n in 0..15:
|
|
|
|
wr.append node.key[n]
|
|
|
|
wr.append EmptyBlob
|
|
|
|
|
|
|
|
# Do for embedded extension node
|
|
|
|
if 0 < node.ePfx.len:
|
2024-07-17 13:48:21 +00:00
|
|
|
let brHash = wr.finish().digestTo(HashKey)
|
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
|
|
|
wr= initRlpWriter()
|
|
|
|
wr.startList(2)
|
|
|
|
wr.append node.ePfx.toHexPrefix(isleaf = false)
|
|
|
|
wr.append brHash
|
|
|
|
|
|
|
|
of Leaf:
|
|
|
|
proc getKey0(
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[HashKey,AristoError]
|
|
|
|
{.gcsafe, raises: [].} =
|
|
|
|
ok(node.key[0]) # always succeeds
|
|
|
|
|
|
|
|
wr.startList(2)
|
|
|
|
wr.append node.lPfx.toHexPrefix(isleaf = true)
|
|
|
|
wr.append node.lData.serialise(getKey0).value
|
|
|
|
|
2024-07-17 13:48:21 +00:00
|
|
|
wr.finish().digestTo(HashKey)
|
2023-11-08 12:18:32 +00:00
|
|
|
|
|
|
|
proc serialise*(
|
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
root: VertexID;
|
2024-07-14 10:02:05 +00:00
|
|
|
pyl: LeafPayload;
|
2023-11-08 12:18:32 +00:00
|
|
|
): Result[Blob,(VertexID,AristoError)] =
|
|
|
|
## Encode the data payload of the argument `pyl` as RLP `Blob` if it is of
|
|
|
|
## account type, otherwise pass the data as is.
|
|
|
|
##
|
|
|
|
proc getKey(vid: VertexID): Result[HashKey,AristoError] =
|
2024-07-04 13:46:52 +00:00
|
|
|
db.getKeyRc((root, vid))
|
2023-11-08 12:18:32 +00:00
|
|
|
|
|
|
|
pyl.serialise getKey
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|