nimbus-eth1/nimbus/db/aristo/aristo_init/init_common.nim

88 lines
3.0 KiB
Nim
Raw Normal View History

# 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.
{.push raises: [].}
import
../aristo_desc,
../aristo_desc/desc_backend
const
verifyIxId = true # and false
## Enforce session tracking
type
BackendType* = enum
Aristo db api extensions for use as core db backend (#1754) * Update docu * Update Aristo/Kvt constructor prototype why: Previous version used an `enum` value to indicate what backend is to be used. This was replaced by using the backend object type. * Rewrite `hikeUp()` return code into `Result[Hike,(Hike,AristoError)]` why: Better code maintenance. Previously, the `Hike` object was returned. It had an internal error field so partial success was also available on a failure. This error field has been removed. * Use `openArray[byte]` rather than `Blob` in functions prototypes * Provide synchronised multi instance transactions why: The `CoreDB` object was geared towards the legacy DB which used a single transaction for the key-value backend DB. Different state roots are provided by the backend database, so all instances work directly on the same backend. Aristo db instances have different in-memory mappings (aka different state roots) and the transactions are on top of there mappings. So each instance might run different transactions. Multi instance transactions are a compromise to converge towards the legacy behaviour. The synchronised transactions span over all instances available at the time when base transaction was opened. Instances created later are unaffected. * Provide key-value pair database iterator why: Needed in `CoreDB` for `replicate()` emulation also: Some update of internal code * Extend API (i.e. prototype variants) why: Needed for `CoreDB` geared towards the legacy backend which has a more basic API than Aristo.
2023-09-15 15:23:53 +00:00
BackendVoid = 0 ## For providing backend-less constructor
BackendMemory
BackendRocksDB
StorageType* = enum
## Storage types, key prefix
Oops = 0
AdmPfx = 1 ## Admin data, e.g. ID generator
VtxPfx = 2 ## Vertex data
KeyPfx = 3 ## Key/hash data
FilPfx = 4 ## Filter logs (to revert to earlier state)
AdminTabID* = distinct uint64
## Access keys for admin table records. When exposed (e.g. when itereating
## over the tables), this data type is to be used.
TypedBackendRef* = ref object of BackendRef
beKind*: BackendType ## Backend type identifier
when verifyIxId:
txGen: uint ## Transaction ID generator (for debugging)
txId: uint ## Active transaction ID (for debugging)
TypedPutHdlErrRef* = ref object of RootRef
case pfx*: StorageType ## Error sub-table
of VtxPfx, KeyPfx:
vid*: VertexID ## Vertex ID where the error occured
of FilPfx:
qid*: QueueID ## Ditto
of AdmPfx, Oops:
discard
code*: AristoError ## Error code (if any)
TypedPutHdlRef* = ref object of PutHdlRef
error*: TypedPutHdlErrRef ## Track error while collecting transaction
when verifyIxId:
txId: uint ## Transaction ID (for debugging)
const
AdmTabIdIdg* = AdminTabID(0) ## Access key for vertex ID generator state
AdmTabIdFqs* = AdminTabID(1) ## Access key for filter queue states
# ------------------------------------------------------------------------------
# Public helpers
# ------------------------------------------------------------------------------
proc beginSession*(hdl: TypedPutHdlRef; db: TypedBackendRef) =
when verifyIxId:
doAssert db.txId == 0
if db.txGen == 0:
db.txGen = 1
db.txId = db.txGen
hdl.txId = db.txGen
db.txGen.inc
proc verifySession*(hdl: TypedPutHdlRef; db: TypedBackendRef) =
when verifyIxId:
doAssert db.txId == hdl.txId
proc finishSession*(hdl: TypedPutHdlRef; db: TypedBackendRef) =
when verifyIxId:
doAssert db.txId == hdl.txId
db.txId = 0
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------