2023-08-07 17:45:23 +00:00
|
|
|
# nimbus-eth1
|
2023-11-08 16:52:25 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2023-08-07 17:45:23 +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 -- Obects Retrival Via Traversal Path
|
|
|
|
## ===============================================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-09-26 09:21:13 +00:00
|
|
|
eth/trie/nibbles,
|
2023-08-07 17:45:23 +00:00
|
|
|
results,
|
|
|
|
"."/[aristo_desc, aristo_hike]
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
const
|
|
|
|
AcceptableHikeStops = {
|
|
|
|
HikeBranchTailEmpty,
|
|
|
|
HikeBranchBlindEdge,
|
|
|
|
HikeExtTailEmpty,
|
|
|
|
HikeExtTailMismatch}
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fetchPayloadImpl(
|
|
|
|
rc: Result[Hike,(Hike,AristoError)];
|
|
|
|
): Result[PayloadRef,(VertexID,AristoError)] =
|
|
|
|
if rc.isErr:
|
|
|
|
let vid =
|
|
|
|
if rc.error[0].legs.len == 0: VertexID(0)
|
|
|
|
else: rc.error[0].legs[^1].wp.vid
|
2023-10-27 21:36:51 +00:00
|
|
|
if rc.error[1] in AcceptableHikeStops:
|
|
|
|
return err((vid, FetchPathNotFound))
|
2023-09-15 15:23:53 +00:00
|
|
|
return err((vid, rc.error[1]))
|
Optional accounts cache module for creating genesis (#1897)
* Split off `ReadOnlyStateDB` from `AccountStateDB` from `state_db.nim`
why:
Apart from testing, applications use `ReadOnlyStateDB` as an easy
way to access the accounts ledger. This is well supported by the
`Aristo` db, but writable mode is only parially supported.
The writable AccountStateDB` object for modifying accounts is not
used by production code.
So, for lecgacy and testing apps, the full support of the previous
`AccountStateDB` is now enabled by `import db/state_db/read_write`
and the `import db/state_db` provides read-only mode.
* Encapsulate `AccountStateDB` as `GenesisLedgerRef` or genesis creation
why:
`AccountStateDB` has poor support for `Aristo` and is not widely used
in favour of `AccountsLedger` (which will be abstracted as `ledger`.)
Currently, using other than the `AccountStateDB` ledgers within the
`GenesisLedgerRef` wrapper is experimental and test only. Eventually,
the wrapper should disappear so that the `Ledger` object (which
encapsulates `AccountsCache` and `AccountsLedger`) will prevail.
* For the `Ledger`, provide access to raw accounts `MPT`
why:
This gives to the `CoreDbMptRef` descriptor from the `CoreDb` (which is
the legacy version of CoreDxMptRef`.) For the new `ledger` API, the
accounts are based on the `CoreDxMAccRef` descriptor which uses a
particular sub-system for accounts while legacy applications use the
`CoreDbPhkRef` equivalent of the `SecureHexaryTrie`.
The only place where this feature will currently be used is the
`genesis.nim` source file.
* Fix `Aristo` bugs, missing boundary checks, typos, etc.
* Verify root vertex in `MPT` and account constructors
why:
Was missing so far, in particular the accounts constructor must
verify `VertexID(1)
* Fix include file
2023-11-20 11:51:43 +00:00
|
|
|
if rc.value.legs.len == 0:
|
|
|
|
return err((VertexID(0), FetchPathNotFound))
|
2023-09-15 15:23:53 +00:00
|
|
|
ok rc.value.legs[^1].wp.vtx.lData
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
proc fetchPayloadImpl(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
|
|
|
path: openArray[byte];
|
|
|
|
): Result[PayloadRef,(VertexID,AristoError)] =
|
|
|
|
path.initNibbleRange.hikeUp(root, db).fetchPayloadImpl
|
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fetchPayload*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
key: LeafTie;
|
|
|
|
): Result[PayloadRef,(VertexID,AristoError)] =
|
|
|
|
## Cascaded attempt to traverse the `Aristo Trie` and fetch the value of a
|
|
|
|
## leaf vertex. This function is complementary to `merge()`.
|
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
key.hikeUp(db).fetchPayloadImpl
|
|
|
|
|
|
|
|
proc fetchPayload*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
2023-09-18 20:20:28 +00:00
|
|
|
path: openArray[byte];
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[PayloadRef,(VertexID,AristoError)] =
|
|
|
|
## Variant of `fetchPayload()`
|
|
|
|
##
|
2023-09-26 09:21:13 +00:00
|
|
|
if path.len == 0:
|
|
|
|
return err((VertexID(0),LeafKeyInvalid))
|
|
|
|
db.fetchPayloadImpl(root, path)
|
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
proc hasPath*(
|
2023-09-26 09:21:13 +00:00
|
|
|
db: AristoDbRef; # Database
|
|
|
|
root: VertexID;
|
|
|
|
path: openArray[byte]; # Key of database record
|
|
|
|
): Result[bool,(VertexID,AristoError)] =
|
|
|
|
## Variant of `fetchPayload()`
|
|
|
|
##
|
|
|
|
if path.len == 0:
|
|
|
|
return err((VertexID(0),LeafKeyInvalid))
|
|
|
|
let rc = db.fetchPayloadImpl(root, path)
|
|
|
|
if rc.isOk:
|
|
|
|
return ok(true)
|
2023-10-27 21:36:51 +00:00
|
|
|
if rc.error[1] == FetchPathNotFound:
|
|
|
|
return ok(false)
|
|
|
|
err(rc.error)
|
2023-08-07 17:45:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|