2023-05-14 17:43:01 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
## Read vertex record on the layered Aristo DB delta architecture
|
|
|
|
## ==============================================================
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-06-09 11:17:37 +00:00
|
|
|
std/[sets, tables],
|
2023-05-14 17:43:01 +00:00
|
|
|
stew/results,
|
2023-06-09 11:17:37 +00:00
|
|
|
"."/[aristo_constants, aristo_desc, aristo_error]
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
type
|
|
|
|
VidVtxPair* = object
|
|
|
|
vid*: VertexID ## Table lookup vertex ID (if any)
|
|
|
|
vtx*: VertexRef ## Reference to vertex
|
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
proc getVtxBackend*(
|
|
|
|
db: AristoDb;
|
2023-05-14 17:43:01 +00:00
|
|
|
vid: VertexID;
|
|
|
|
): Result[VertexRef,AristoError] =
|
2023-06-09 11:17:37 +00:00
|
|
|
## Get the vertex from the `backened` layer if available.
|
|
|
|
let be = db.backend
|
2023-05-14 17:43:01 +00:00
|
|
|
if not be.isNil:
|
|
|
|
return be.getVtxFn vid
|
|
|
|
|
|
|
|
err(GetVtxNotFound)
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
proc getKeyBackend*(
|
|
|
|
db: AristoDb;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[NodeKey,AristoError] =
|
|
|
|
## Get the merkle hash/key from the backend
|
|
|
|
# key must not have been locally deleted (but not saved, yet)
|
|
|
|
if vid notin db.top.dKey:
|
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
|
|
|
return be.getKeyFn vid
|
|
|
|
|
|
|
|
err(GetKeyNotFound)
|
|
|
|
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
proc getVtxCascaded*(
|
2023-06-09 11:17:37 +00:00
|
|
|
db: AristoDb;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[VertexRef,AristoError] =
|
|
|
|
## Get the vertex from the top layer or the `backened` layer if available.
|
|
|
|
let vtx = db.top.sTab.getOrDefault(vid, VertexRef(nil))
|
|
|
|
if vtx != VertexRef(nil):
|
|
|
|
return ok vtx
|
|
|
|
|
|
|
|
db.getVtxBackend vid
|
|
|
|
|
|
|
|
proc getKeyCascaded*(
|
|
|
|
db: AristoDb;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[NodeKey,AristoError] =
|
|
|
|
## Get the Merkle hash/key from the top layer or the `backened` layer if
|
|
|
|
## available.
|
|
|
|
let key = db.top.kMap.getOrDefault(vid, EMPTY_ROOT_KEY)
|
|
|
|
if key != EMPTY_ROOT_KEY:
|
|
|
|
return ok key
|
|
|
|
|
|
|
|
db.getKeyBackend vid
|
|
|
|
|
|
|
|
proc getLeaf*(
|
|
|
|
db: AristoDb;
|
|
|
|
lky: LeafKey;
|
2023-05-30 11:47:47 +00:00
|
|
|
): Result[VidVtxPair,AristoError] =
|
2023-06-09 11:17:37 +00:00
|
|
|
## Get the vertex from the top layer by the `Patricia Trie` path. This
|
|
|
|
## function does not search on the `backend` layer.
|
|
|
|
let vid = db.top.lTab.getOrDefault(lky, VertexID(0))
|
|
|
|
if vid != VertexID(0):
|
|
|
|
let vtx = db.top.sTab.getOrDefault(vid, VertexRef(nil))
|
|
|
|
if vtx != VertexRef(nil):
|
|
|
|
return ok VidVtxPair(vid: vid, vtx: vtx)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
|
|
|
err(GetTagNotFound)
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
# ---------
|
|
|
|
|
|
|
|
proc getVtx*(db: AristoDb; vid: VertexID): VertexRef =
|
|
|
|
## Variant of `getVtxCascaded()` returning `nil` on error (while
|
|
|
|
## ignoring the detailed error type information.)
|
|
|
|
db.getVtxCascaded(vid).get(otherwise = VertexRef(nil))
|
|
|
|
|
|
|
|
proc getVtx*(db: AristoDb; lky: LeafKey): VertexRef =
|
|
|
|
## Variant of `getLeaf()` returning `nil` on error (while
|
|
|
|
## ignoring the detailed error type information.)
|
|
|
|
let rc = db.getLeaf lky
|
2023-05-14 17:43:01 +00:00
|
|
|
if rc.isOk:
|
2023-06-09 11:17:37 +00:00
|
|
|
return rc.value.vtx
|
|
|
|
|
|
|
|
proc getKey*(db: AristoDb; vid: VertexID): NodeKey =
|
|
|
|
## Variant of `getKeyCascaded()` returning `EMPTY_ROOT_KEY` on error (while
|
|
|
|
## ignoring the detailed error type information.)
|
|
|
|
db.getKeyCascaded(vid).get(otherwise = EMPTY_ROOT_KEY)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|