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-19 12:40:00 +00:00
|
|
|
std/typetraits,
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
2023-08-07 17:45:23 +00:00
|
|
|
results,
|
2024-06-27 09:01:26 +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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
func mustBeGeneric(
|
|
|
|
root: VertexID;
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Verify that `root` is neither from an accounts tree nor a strorage tree.
|
|
|
|
if not root.isValid:
|
|
|
|
return err(FetchRootVidMissing)
|
|
|
|
elif root == VertexID(1):
|
|
|
|
return err(FetchAccRootNotAccepted)
|
|
|
|
elif LEAST_FREE_VID <= root.distinctBase:
|
|
|
|
return err(FetchStoRootNotAccepted)
|
|
|
|
ok()
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
proc retrievePayload(
|
2023-09-26 09:21:13 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
|
|
|
path: openArray[byte];
|
2024-06-19 12:40:00 +00:00
|
|
|
): Result[PayloadRef,AristoError] =
|
|
|
|
if path.len == 0:
|
|
|
|
return err(FetchPathInvalid)
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
let hike = NibblesBuf.fromBytes(path).hikeUp(root, db).valueOr:
|
2024-06-19 12:40:00 +00:00
|
|
|
if error[1] in HikeAcceptableStopsNotFound:
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
return err(error[1])
|
|
|
|
|
|
|
|
ok hike.legs[^1].wp.vtx.lData
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc retrieveMerkleHash(
|
2023-08-07 17:45:23 +00:00
|
|
|
db: AristoDbRef;
|
2024-06-27 09:01:26 +00:00
|
|
|
root: VertexID;
|
|
|
|
): Result[Hash256,AristoError] =
|
|
|
|
let key = db.getKeyRc(root).valueOr:
|
|
|
|
if error == GetKeyNotFound:
|
|
|
|
return ok(EMPTY_ROOT_HASH) # empty sub-tree
|
|
|
|
return err(error)
|
|
|
|
ok key.to(Hash256)
|
2024-06-19 12:40:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc hasPayload(
|
2023-09-15 15:23:53 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
2023-09-18 20:20:28 +00:00
|
|
|
path: openArray[byte];
|
2024-06-19 12:40:00 +00:00
|
|
|
): Result[bool,AristoError] =
|
2023-09-26 09:21:13 +00:00
|
|
|
if path.len == 0:
|
2024-06-19 12:40:00 +00:00
|
|
|
return err(FetchPathInvalid)
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2024-06-22 20:33:37 +00:00
|
|
|
let hike = NibblesBuf.fromBytes(path).hikeUp(VertexID(1), db).valueOr:
|
2024-06-19 12:40:00 +00:00
|
|
|
if error[1] in HikeAcceptableStopsNotFound:
|
|
|
|
return ok(false)
|
|
|
|
return err(error[1])
|
|
|
|
ok(true)
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fetchAccountHike*(
|
|
|
|
db: AristoDbRef; # Database
|
|
|
|
accPath: PathID; # Implies a storage ID (if any)
|
|
|
|
): Result[Hike,AristoError] =
|
|
|
|
## Verify that the `accPath` argument properly referres to a storage root
|
|
|
|
## vertex ID. The function will reset the keys along the `accPath` for
|
|
|
|
## being modified.
|
|
|
|
##
|
|
|
|
## On success, the function will return an account leaf pair with the leaf
|
|
|
|
## vertex and the vertex ID.
|
|
|
|
##
|
|
|
|
# Expand vertex path to account leaf
|
|
|
|
var hike = accPath.to(NibblesBuf).hikeUp(VertexID(1), db).valueOr:
|
|
|
|
return err(FetchAccInaccessible)
|
|
|
|
|
|
|
|
# Extract the account payload from the leaf
|
|
|
|
let wp = hike.legs[^1].wp
|
|
|
|
if wp.vtx.vType != Leaf:
|
|
|
|
return err(FetchAccPathWithoutLeaf)
|
|
|
|
assert wp.vtx.lData.pType == AccountData # debugging only
|
|
|
|
|
|
|
|
ok(move(hike))
|
|
|
|
|
|
|
|
|
|
|
|
proc fetchStorageID*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
accPath: PathID;
|
|
|
|
): Result[VertexID,AristoError] =
|
|
|
|
## Public helper function fro retrieving a storage (vertex) ID for a
|
|
|
|
## given account.
|
|
|
|
let
|
|
|
|
accHike = db.fetchAccountHike(accPath).valueOr:
|
|
|
|
if error == FetchAccInaccessible:
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
return err(error)
|
|
|
|
stoID = accHike.legs[^1].wp.vtx.lData.stoID
|
|
|
|
|
|
|
|
if not stoID.isValid:
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
|
|
|
|
ok stoID
|
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
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()
|
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc fetchAccountRecord*(
|
2024-06-19 12:40:00 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
path: openArray[byte];
|
|
|
|
): Result[AristoAccount,AristoError] =
|
|
|
|
## Fetch an account record from the database indexed by `path`.
|
|
|
|
##
|
|
|
|
let pyl = ? db.retrievePayload(VertexID(1), path)
|
|
|
|
assert pyl.pType == AccountData # debugging only
|
|
|
|
ok pyl.account
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc fetchAccountState*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[Hash256,AristoError] =
|
|
|
|
## Fetch the Merkle hash of the account root.
|
|
|
|
db.retrieveMerkleHash VertexID(1)
|
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
proc hasPathAccount*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
path: openArray[byte];
|
|
|
|
): Result[bool,AristoError] =
|
|
|
|
## For an account record indexed by `path` query whether this record exists
|
|
|
|
## on the database.
|
|
|
|
##
|
|
|
|
db.hasPayload(VertexID(1), path)
|
|
|
|
|
|
|
|
|
|
|
|
proc fetchGenericData*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
|
|
|
path: openArray[byte];
|
|
|
|
): Result[Blob,AristoError] =
|
|
|
|
## For a generic sub-tree starting at `root`, fetch the data record
|
|
|
|
## indexed by `path`.
|
|
|
|
##
|
|
|
|
? root.mustBeGeneric()
|
|
|
|
let pyl = ? db.retrievePayload(root, path)
|
|
|
|
assert pyl.pType == RawData # debugging only
|
|
|
|
ok pyl.rawBlob
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc fetchGenericState*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
|
|
|
): Result[Hash256,AristoError] =
|
|
|
|
## Fetch the Merkle hash of the argument `root`.
|
|
|
|
db.retrieveMerkleHash root
|
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
proc hasPathGeneric*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
root: VertexID;
|
|
|
|
path: openArray[byte];
|
|
|
|
): Result[bool,AristoError] =
|
|
|
|
## For a generic sub-tree starting at `root` and indexed by `path`, query
|
|
|
|
## whether this record exists on the database.
|
|
|
|
##
|
|
|
|
? root.mustBeGeneric()
|
|
|
|
db.hasPayload(root, path)
|
|
|
|
|
|
|
|
|
|
|
|
proc fetchStorageData*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
path: openArray[byte];
|
|
|
|
accPath: PathID;
|
|
|
|
): Result[Blob,AristoError] =
|
|
|
|
## For a storage tree related to account `accPath`, fetch the data record
|
|
|
|
## from the database indexed by `path`.
|
|
|
|
##
|
2024-06-27 09:01:26 +00:00
|
|
|
let pyl = ? db.retrievePayload(? db.fetchStorageID accPath, path)
|
2024-06-19 12:40:00 +00:00
|
|
|
assert pyl.pType == RawData # debugging only
|
|
|
|
ok pyl.rawBlob
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc fetchStorageState*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
accPath: PathID;
|
|
|
|
): Result[Hash256,AristoError] =
|
|
|
|
## Fetch the Merkle hash of the storage root related to `accPath`.
|
|
|
|
let stoID = db.fetchStorageID(accPath).valueOr:
|
|
|
|
if error == FetchPathNotFound:
|
|
|
|
return ok(EMPTY_ROOT_HASH) # no sub-tree
|
|
|
|
return err(error)
|
|
|
|
db.retrieveMerkleHash stoID
|
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
proc hasPathStorage*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
path: openArray[byte];
|
|
|
|
accPath: PathID;
|
|
|
|
): Result[bool,AristoError] =
|
|
|
|
## For a storage tree related to account `accPath`, query whether the data
|
|
|
|
## record indexed by `path` exists on the database.
|
|
|
|
##
|
2024-06-27 09:01:26 +00:00
|
|
|
db.hasPayload(? db.fetchStorageID accPath, path)
|
|
|
|
|
|
|
|
proc hasStorageData*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
accPath: PathID;
|
|
|
|
): Result[bool,AristoError] =
|
|
|
|
## For a storage tree related to account `accPath`, query whether there
|
|
|
|
## is a non-empty data storage area at all.
|
|
|
|
##
|
|
|
|
let stoID = db.fetchStorageID(accPath).valueOr:
|
|
|
|
if error == FetchPathNotFound:
|
|
|
|
return ok(false) # no sub-tree
|
|
|
|
return err(error)
|
|
|
|
ok stoID.isValid
|
2024-06-19 12:40:00 +00:00
|
|
|
|
2023-08-07 17:45:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|