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-07-03 15:58:25 +00:00
|
|
|
"."/[aristo_compute, aristo_desc, aristo_get, aristo_layers, 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-07-01 12:07:39 +00:00
|
|
|
for step in stepUp(NibblesBuf.fromBytes(path), root, db):
|
|
|
|
let vtx = step.valueOr:
|
|
|
|
if error in HikeAcceptableStopsNotFound:
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
return err(error)
|
2024-06-19 12:40:00 +00:00
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
if vtx.vType == Leaf:
|
|
|
|
return ok vtx.lData
|
2023-08-07 17:45:23 +00:00
|
|
|
|
2024-07-01 12:07:39 +00:00
|
|
|
return err(FetchPathNotFound)
|
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;
|
2024-06-28 15:03:12 +00:00
|
|
|
updateOk: bool;
|
2024-06-27 09:01:26 +00:00
|
|
|
): Result[Hash256,AristoError] =
|
2024-06-28 15:03:12 +00:00
|
|
|
let key = block:
|
|
|
|
if updateOk:
|
|
|
|
db.computeKey(root).valueOr:
|
|
|
|
if error == GetVtxNotFound:
|
|
|
|
return ok(EMPTY_ROOT_HASH)
|
|
|
|
return err(error)
|
|
|
|
else:
|
|
|
|
db.getKeyRc(root).valueOr:
|
|
|
|
if error == GetKeyNotFound:
|
|
|
|
return ok(EMPTY_ROOT_HASH) # empty sub-tree
|
|
|
|
return err(error)
|
2024-06-27 09:01:26 +00:00
|
|
|
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-07-01 12:07:39 +00:00
|
|
|
let error = db.retrievePayload(root, path).errorOr:
|
|
|
|
return ok(true)
|
|
|
|
|
|
|
|
if error == FetchPathNotFound:
|
|
|
|
return ok(false)
|
|
|
|
err(error)
|
2024-06-19 12:40:00 +00:00
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fetchAccountHike*(
|
|
|
|
db: AristoDbRef; # Database
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256; # Implies a storage ID (if any)
|
2024-06-27 09:01:26 +00:00
|
|
|
): 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
|
2024-06-27 19:21:01 +00:00
|
|
|
var hike = accPath.hikeUp(VertexID(1), db).valueOr:
|
2024-06-27 09:01:26 +00:00
|
|
|
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;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-27 09:01:26 +00:00
|
|
|
): Result[VertexID,AristoError] =
|
|
|
|
## Public helper function fro retrieving a storage (vertex) ID for a
|
|
|
|
## given account.
|
2024-07-03 15:58:25 +00:00
|
|
|
|
|
|
|
if (let stoID = db.layersGetStoID(accPath); stoID.isSome()):
|
|
|
|
if not stoID[].isValid():
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
return ok stoID[]
|
|
|
|
|
|
|
|
let accKey = accPath.to(AccountKey)
|
|
|
|
if (let stoID = db.accSids.lruFetch(accKey); stoID.isSome()):
|
|
|
|
return ok stoID[]
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
let
|
2024-07-03 08:14:26 +00:00
|
|
|
payload = db.retrievePayload(VertexID(1), accPath.data).valueOr:
|
2024-06-27 09:01:26 +00:00
|
|
|
if error == FetchAccInaccessible:
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
return err(error)
|
2024-07-01 12:07:39 +00:00
|
|
|
|
|
|
|
stoID = payload.stoID
|
2024-06-27 09:01:26 +00:00
|
|
|
|
|
|
|
if not stoID.isValid:
|
|
|
|
return err(FetchPathNotFound)
|
|
|
|
|
2024-07-03 15:58:25 +00:00
|
|
|
# If we didn't find a cached storage ID in the layers, we must be using the
|
|
|
|
# database version which we cache here, even across database commits
|
|
|
|
ok db.accSids.lruAppend(accKey, stoID, accLruSize)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
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;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-19 12:40:00 +00:00
|
|
|
): Result[AristoAccount,AristoError] =
|
2024-06-27 19:21:01 +00:00
|
|
|
## Fetch an account record from the database indexed by `accPath`.
|
2024-06-19 12:40:00 +00:00
|
|
|
##
|
2024-07-03 08:14:26 +00:00
|
|
|
let pyl = ? db.retrievePayload(VertexID(1), accPath.data)
|
2024-06-19 12:40:00 +00:00
|
|
|
assert pyl.pType == AccountData # debugging only
|
|
|
|
ok pyl.account
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc fetchAccountState*(
|
|
|
|
db: AristoDbRef;
|
2024-06-28 15:03:12 +00:00
|
|
|
updateOk: bool;
|
2024-06-27 09:01:26 +00:00
|
|
|
): Result[Hash256,AristoError] =
|
|
|
|
## Fetch the Merkle hash of the account root.
|
2024-06-28 15:03:12 +00:00
|
|
|
db.retrieveMerkleHash(VertexID(1), updateOk)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
proc hasPathAccount*(
|
|
|
|
db: AristoDbRef;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-19 12:40:00 +00:00
|
|
|
): Result[bool,AristoError] =
|
2024-06-27 19:21:01 +00:00
|
|
|
## For an account record indexed by `accPath` query whether this record exists
|
2024-06-19 12:40:00 +00:00
|
|
|
## on the database.
|
|
|
|
##
|
2024-07-03 08:14:26 +00:00
|
|
|
db.hasPayload(VertexID(1), accPath.data)
|
2024-06-19 12:40:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2024-06-28 15:03:12 +00:00
|
|
|
updateOk: bool;
|
2024-06-27 09:01:26 +00:00
|
|
|
): Result[Hash256,AristoError] =
|
|
|
|
## Fetch the Merkle hash of the argument `root`.
|
2024-06-28 15:03:12 +00:00
|
|
|
db.retrieveMerkleHash(root, updateOk)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
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;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-27 19:21:01 +00:00
|
|
|
stoPath: openArray[byte];
|
2024-06-19 12:40:00 +00:00
|
|
|
): Result[Blob,AristoError] =
|
|
|
|
## For a storage tree related to account `accPath`, fetch the data record
|
|
|
|
## from the database indexed by `path`.
|
|
|
|
##
|
2024-06-27 19:21:01 +00:00
|
|
|
let pyl = ? db.retrievePayload(? db.fetchStorageID accPath, stoPath)
|
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;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-28 15:03:12 +00:00
|
|
|
updateOk: bool;
|
2024-06-27 09:01:26 +00:00
|
|
|
): 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)
|
2024-06-28 15:03:12 +00:00
|
|
|
db.retrieveMerkleHash(stoID, updateOk)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
2024-06-19 12:40:00 +00:00
|
|
|
proc hasPathStorage*(
|
|
|
|
db: AristoDbRef;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-27 19:21:01 +00:00
|
|
|
stoPath: openArray[byte];
|
2024-06-19 12:40:00 +00:00
|
|
|
): 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 19:21:01 +00:00
|
|
|
db.hasPayload(? db.fetchStorageID accPath, stoPath)
|
2024-06-27 09:01:26 +00:00
|
|
|
|
|
|
|
proc hasStorageData*(
|
|
|
|
db: AristoDbRef;
|
2024-07-03 08:14:26 +00:00
|
|
|
accPath: Hash256;
|
2024-06-27 09:01:26 +00:00
|
|
|
): 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
|
|
|
|
# ------------------------------------------------------------------------------
|