2023-08-07 17:45:23 +00:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 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
|
2024-06-03 20:10:35 +00:00
|
|
|
eth/trie/nibbles,
|
2023-08-07 17:45:23 +00:00
|
|
|
results,
|
2024-05-31 17:32:22 +00:00
|
|
|
"."/[aristo_desc, aristo_get, aristo_hike]
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fetchPayloadImpl(
|
2024-02-08 16:32:16 +00:00
|
|
|
rc: Result[Hike,(VertexID,AristoError,Hike)];
|
2023-09-15 15:23:53 +00:00
|
|
|
): Result[PayloadRef,(VertexID,AristoError)] =
|
|
|
|
if rc.isErr:
|
2024-02-08 16:32:16 +00:00
|
|
|
if rc.error[1] in HikeAcceptableStopsNotFound:
|
|
|
|
return err((rc.error[0], FetchPathNotFound))
|
|
|
|
return err((rc.error[0], rc.error[1]))
|
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
|
2024-02-29 21:10:24 +00:00
|
|
|
## leaf vertex. This function is complementary to `mergePayload()`.
|
2023-08-07 17:45:23 +00:00
|
|
|
##
|
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
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
proc fetchLastSavedState*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[SavedState,AristoError] =
|
|
|
|
## Wrapper around `getLstUbe()`. The function returns the state of the last
|
|
|
|
## saved state. This is a Merkle hash tag for vertex with ID 1 and a bespoke
|
|
|
|
## `uint64` identifier (may be interpreted as block number.)
|
|
|
|
db.getLstUbe()
|
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|