2023-09-26 09:21:13 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 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
|
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
../../../errors
|
|
|
|
|
|
|
|
# Annotation helpers
|
|
|
|
{.pragma: noRaise, gcsafe, raises: [].}
|
|
|
|
{.pragma: apiRaise, gcsafe, raises: [CoreDbApiError].}
|
|
|
|
|
|
|
|
type
|
|
|
|
CoreDbType* = enum
|
|
|
|
Ooops
|
|
|
|
LegacyDbMemory
|
|
|
|
LegacyDbPersistent
|
|
|
|
|
|
|
|
const
|
2023-10-25 14:03:09 +00:00
|
|
|
CoreDbPersistentTypes* = {LegacyDbPersistent}
|
2023-09-26 09:21:13 +00:00
|
|
|
|
|
|
|
type
|
2023-10-02 18:05:17 +00:00
|
|
|
CoreDbRc*[T] = Result[T,CoreDbErrorRef]
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbAccount* = object
|
|
|
|
## Generic account representation referencing an *MPT* sub-trie
|
|
|
|
nonce*: AccountNonce ## Some `uint64` type
|
|
|
|
balance*: UInt256
|
|
|
|
storageVid*: CoreDbVidRef ## Implies storage root sub-MPT
|
|
|
|
codeHash*: Hash256
|
|
|
|
|
|
|
|
CoreDbErrorCode* = enum
|
2023-11-08 12:18:32 +00:00
|
|
|
Unset = 0
|
|
|
|
Unspecified
|
2023-10-11 19:09:11 +00:00
|
|
|
RlpException
|
|
|
|
KvtNotFound
|
|
|
|
MptNotFound
|
2023-11-08 12:18:32 +00:00
|
|
|
AccNotFound
|
2023-10-11 19:09:11 +00:00
|
|
|
RootNotFound
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
CoreDbCaptFlags* {.pure.} = enum
|
|
|
|
PersistPut
|
|
|
|
PersistDel
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
2023-10-11 19:09:11 +00:00
|
|
|
# Sub-descriptor: Misc methods for main descriptor
|
2023-09-26 09:21:13 +00:00
|
|
|
# --------------------------------------------------
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbBaseBackendFn* = proc(): CoreDbBackendRef {.noRaise.}
|
|
|
|
CoreDbBaseDestroyFn* = proc(flush = true) {.noRaise.}
|
|
|
|
CoreDbBaseVidHashFn* =
|
|
|
|
proc(vid: CoreDbVidRef): Result[Hash256,void] {.noRaise.}
|
|
|
|
CoreDbBaseErrorPrintFn* = proc(e: CoreDbErrorRef): string {.noRaise.}
|
|
|
|
CoreDbBaseInitLegaSetupFn* = proc() {.noRaise.}
|
|
|
|
CoreDbBaseRootFn* =
|
|
|
|
proc(root: Hash256; createOk: bool): CoreDbRc[CoreDbVidRef] {.noRaise.}
|
|
|
|
CoreDbBaseKvtFn* = proc(): CoreDxKvtRef {.noRaise.}
|
|
|
|
CoreDbBaseMptFn* =
|
|
|
|
proc(root: CoreDbVidRef; prune: bool): CoreDbRc[CoreDxMptRef] {.noRaise.}
|
|
|
|
CoreDbBaseAccFn* =
|
|
|
|
proc(root: CoreDbVidRef; prune: bool): CoreDbRc[CoreDxAccRef] {.noRaise.}
|
|
|
|
CoreDbBaseTxGetIdFn* = proc(): CoreDbRc[CoreDxTxID] {.noRaise.}
|
|
|
|
CoreDbBaseTxBeginFn* = proc(): CoreDbRc[CoreDxTxRef] {.noRaise.}
|
|
|
|
CoreDbBaseCaptFn* =
|
2023-09-26 09:21:13 +00:00
|
|
|
proc(flgs: set[CoreDbCaptFlags]): CoreDbRc[CoreDxCaptRef] {.noRaise.}
|
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbBaseFns* = object
|
|
|
|
backendFn*: CoreDbBaseBackendFn
|
|
|
|
destroyFn*: CoreDbBaseDestroyFn
|
|
|
|
vidHashFn*: CoreDbBaseVidHashFn
|
|
|
|
errorPrintFn*: CoreDbBaseErrorPrintFn
|
|
|
|
legacySetupFn*: CoreDbBaseInitLegaSetupFn
|
|
|
|
getRootFn*: CoreDbBaseRootFn
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
# Kvt constructor
|
|
|
|
newKvtFn*: CoreDbBaseKvtFn
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
# Hexary trie constructors
|
|
|
|
newMptFn*: CoreDbBaseMptFn
|
|
|
|
newAccFn*: CoreDbBaseAccFn
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
# Transactions constructors
|
|
|
|
getIdFn*: CoreDbBaseTxGetIdFn
|
|
|
|
beginFn*: CoreDbBaseTxBeginFn
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
# capture/tracer constructors
|
|
|
|
captureFn*: CoreDbBaseCaptFn
|
2023-09-26 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
|
|
|
# Sub-descriptor: KVT methods
|
|
|
|
# --------------------------------------------------
|
|
|
|
CoreDbKvtBackendFn* = proc(): CoreDbKvtBackendRef {.noRaise.}
|
|
|
|
CoreDbKvtGetFn* = proc(k: openArray[byte]): CoreDbRc[Blob] {.noRaise.}
|
|
|
|
CoreDbKvtDelFn* = proc(k: openArray[byte]): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbKvtPutFn* =
|
|
|
|
proc(k: openArray[byte]; v: openArray[byte]): CoreDbRc[void] {.noRaise.}
|
2023-11-08 12:18:32 +00:00
|
|
|
CoreDbKvtHasKeyFn* = proc(k: openArray[byte]): CoreDbRc[bool] {.noRaise.}
|
2023-09-26 09:21:13 +00:00
|
|
|
CoreDbKvtPairsIt* = iterator(): (Blob,Blob) {.apiRaise.}
|
|
|
|
|
|
|
|
CoreDbKvtFns* = object
|
|
|
|
## Methods for key-value table
|
|
|
|
backendFn*: CoreDbKvtBackendFn
|
|
|
|
getFn*: CoreDbKvtGetFn
|
|
|
|
delFn*: CoreDbKvtDelFn
|
|
|
|
putFn*: CoreDbKvtPutFn
|
2023-11-08 12:18:32 +00:00
|
|
|
hasKeyFn*: CoreDbKvtHasKeyFn
|
2023-09-26 09:21:13 +00:00
|
|
|
pairsIt*: CoreDbKvtPairsIt
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
2023-10-11 19:09:11 +00:00
|
|
|
# Sub-descriptor: generic Mpt/hexary trie methods
|
2023-09-26 09:21:13 +00:00
|
|
|
# --------------------------------------------------
|
|
|
|
CoreDbMptBackendFn* = proc(): CoreDbMptBackendRef {.noRaise.}
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbMptFetchFn* =
|
2023-09-26 09:21:13 +00:00
|
|
|
proc(k: openArray[byte]): CoreDbRc[Blob] {.noRaise.}
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbMptFetchAccountFn* =
|
|
|
|
proc(k: openArray[byte]): CoreDbRc[CoreDbAccount] {.noRaise.}
|
|
|
|
CoreDbMptDeleteFn* =
|
2023-09-26 09:21:13 +00:00
|
|
|
proc(k: openArray[byte]): CoreDbRc[void] {.noRaise.}
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbMptMergeFn* =
|
|
|
|
proc(k: openArray[byte]; v: openArray[byte]): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbMptMergeAccountFn* =
|
|
|
|
proc(k: openArray[byte]; v: CoreDbAccount): CoreDbRc[void] {.noRaise.}
|
2023-11-08 12:18:32 +00:00
|
|
|
CoreDbMptHasPathFn* = proc(k: openArray[byte]): CoreDbRc[bool] {.noRaise.}
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbMptRootVidFn* = proc(): CoreDbVidRef {.noRaise.}
|
2023-09-26 09:21:13 +00:00
|
|
|
CoreDbMptIsPruningFn* = proc(): bool {.noRaise.}
|
|
|
|
CoreDbMptPairsIt* = iterator(): (Blob,Blob) {.apiRaise.}
|
|
|
|
CoreDbMptReplicateIt* = iterator(): (Blob,Blob) {.apiRaise.}
|
|
|
|
|
|
|
|
CoreDbMptFns* = object
|
|
|
|
## Methods for trie objects
|
2023-10-11 19:09:11 +00:00
|
|
|
backendFn*: CoreDbMptBackendFn
|
|
|
|
fetchFn*: CoreDbMptFetchFn
|
|
|
|
deleteFn*: CoreDbMptDeleteFn
|
|
|
|
mergeFn*: CoreDbMptMergeFn
|
2023-11-08 12:18:32 +00:00
|
|
|
hasPathFn*: CoreDbMptHasPathFn
|
2023-10-11 19:09:11 +00:00
|
|
|
rootVidFn*: CoreDbMptRootVidFn
|
|
|
|
pairsIt*: CoreDbMptPairsIt
|
|
|
|
replicateIt*: CoreDbMptReplicateIt
|
|
|
|
isPruningFn*: CoreDbMptIsPruningFn
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Sub-descriptor: Mpt/hexary trie methods for accounts
|
|
|
|
# ------------------------------------------------------
|
|
|
|
CoreDbAccBackendFn* = proc(): CoreDbAccBackendRef {.noRaise.}
|
|
|
|
CoreDbAccFetchFn* = proc(k: EthAddress): CoreDbRc[CoreDbAccount] {.noRaise.}
|
|
|
|
CoreDbAccDeleteFn* = proc(k: EthAddress): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbAccMergeFn* =
|
|
|
|
proc(k: EthAddress; v: CoreDbAccount): CoreDbRc[void] {.noRaise.}
|
2023-11-08 12:18:32 +00:00
|
|
|
CoreDbAccHasPathFn* = proc(k: EthAddress): CoreDbRc[bool] {.noRaise.}
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbAccRootVidFn* = proc(): CoreDbVidRef {.noRaise.}
|
|
|
|
CoreDbAccIsPruningFn* = proc(): bool {.noRaise.}
|
|
|
|
|
|
|
|
CoreDbAccFns* = object
|
|
|
|
## Methods for trie objects
|
|
|
|
backendFn*: CoreDbAccBackendFn
|
|
|
|
fetchFn*: CoreDbAccFetchFn
|
|
|
|
deleteFn*: CoreDbAccDeleteFn
|
|
|
|
mergeFn*: CoreDbAccMergeFn
|
2023-11-08 12:18:32 +00:00
|
|
|
hasPathFn*: CoreDbAccHasPathFn
|
2023-10-11 19:09:11 +00:00
|
|
|
rootVidFn*: CoreDbAccRootVidFn
|
|
|
|
isPruningFn*: CoreDbAccIsPruningFn
|
2023-09-26 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
|
|
|
# Sub-descriptor: Transaction frame management
|
|
|
|
# --------------------------------------------------
|
|
|
|
CoreDbTxCommitFn* = proc(applyDeletes: bool): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbTxRollbackFn* = proc(): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbTxDisposeFn* = proc(): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbTxSafeDisposeFn* = proc(): CoreDbRc[void] {.noRaise.}
|
|
|
|
|
|
|
|
CoreDbTxFns* = object
|
|
|
|
commitFn*: CoreDbTxCommitFn
|
|
|
|
rollbackFn*: CoreDbTxRollbackFn
|
|
|
|
disposeFn*: CoreDbTxDisposeFn
|
|
|
|
safeDisposeFn*: CoreDbTxSafeDisposeFn
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
|
|
|
# Sub-descriptor: Transaction ID management
|
|
|
|
# --------------------------------------------------
|
|
|
|
CoreDbTxIdSetIdFn* = proc(): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbTxIdActionFn* = proc() {.noRaise.}
|
|
|
|
CoreDbTxIdRoWrapperFn* =
|
|
|
|
proc(action: CoreDbTxIdActionFn): CoreDbRc[void] {.noRaise.}
|
|
|
|
CoreDbTxIdFns* = object
|
|
|
|
roWrapperFn*: CoreDbTxIdRoWrapperFn
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
|
|
|
# Sub-descriptor: capture recorder methods
|
|
|
|
# --------------------------------------------------
|
|
|
|
CoreDbCaptRecorderFn* = proc(): CoreDbRc[CoreDbRef] {.noRaise.}
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbCaptLogDbFn* = proc(): CoreDbRc[CoreDbRef] {.noRaise.}
|
2023-09-26 09:21:13 +00:00
|
|
|
CoreDbCaptFlagsFn* = proc(): set[CoreDbCaptFlags] {.noRaise.}
|
|
|
|
|
|
|
|
CoreDbCaptFns* = object
|
|
|
|
recorderFn*: CoreDbCaptRecorderFn
|
2023-10-11 19:09:11 +00:00
|
|
|
logDbFn*: CoreDbCaptLogDbFn
|
2023-09-26 09:21:13 +00:00
|
|
|
getFlagsFn*: CoreDbCaptFlagsFn
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
|
|
|
# Production descriptors
|
|
|
|
# --------------------------------------------------
|
|
|
|
CoreDbRef* = ref object of RootRef
|
|
|
|
## Database descriptor
|
2023-10-25 14:03:09 +00:00
|
|
|
dbType*: CoreDbType ## Type of database backend
|
|
|
|
trackLegaApi*: bool ## Debugging support
|
|
|
|
trackNewApi*: bool ## Debugging support
|
|
|
|
trackLedgerApi*: bool ## Debugging suggestion for subsequent ledger
|
|
|
|
localDbOnly*: bool ## Debugging, suggestion to ignore async fetch
|
2023-10-11 19:09:11 +00:00
|
|
|
methods*: CoreDbBaseFns
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2023-10-02 18:05:17 +00:00
|
|
|
CoreDbErrorRef* = ref object of RootRef
|
2023-09-26 09:21:13 +00:00
|
|
|
## Generic error object
|
2023-10-11 19:09:11 +00:00
|
|
|
error*: CoreDbErrorCode
|
2023-09-26 09:21:13 +00:00
|
|
|
parent*: CoreDbRef
|
|
|
|
|
|
|
|
CoreDbBackendRef* = ref object of RootRef
|
|
|
|
## Backend wrapper for direct backend access
|
|
|
|
parent*: CoreDbRef
|
|
|
|
|
|
|
|
CoreDbKvtBackendRef* = ref object of RootRef
|
|
|
|
## Backend wrapper for direct backend access
|
|
|
|
parent*: CoreDbRef
|
|
|
|
|
|
|
|
CoreDbMptBackendRef* = ref object of RootRef
|
|
|
|
## Backend wrapper for direct backend access
|
|
|
|
parent*: CoreDbRef
|
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDbAccBackendRef* = ref object of RootRef
|
|
|
|
## Backend wrapper for direct backend access
|
|
|
|
parent*: CoreDbRef
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
CoreDxKvtRef* = ref object
|
|
|
|
## Statically initialised Key-Value pair table living in `CoreDbRef`
|
|
|
|
parent*: CoreDbRef
|
|
|
|
methods*: CoreDbKvtFns
|
|
|
|
|
|
|
|
CoreDxMptRef* = ref object
|
|
|
|
## Hexary/Merkle-Patricia tree derived from `CoreDbRef`, will be
|
|
|
|
## initialised on-the-fly.
|
|
|
|
parent*: CoreDbRef
|
|
|
|
methods*: CoreDbMptFns
|
|
|
|
|
2023-10-11 19:09:11 +00:00
|
|
|
CoreDxAccRef* = ref object
|
|
|
|
## Similar to `CoreDxKvtRef`, only dealing with `CoreDbAccount` data
|
|
|
|
## rather than `Blob` values.
|
|
|
|
parent*: CoreDbRef
|
|
|
|
methods*: CoreDbAccFns
|
|
|
|
|
|
|
|
CoreDbVidRef* = ref object of RootRef
|
|
|
|
## Generic state root: `Hash256` for legacy, `VertexID` for Aristo. This
|
|
|
|
## object makes only sense in the context od an *MPT*.
|
|
|
|
parent*: CoreDbRef
|
|
|
|
ready*: bool ## Must be set `true` to enable
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
CoreDxPhkRef* = ref object
|
|
|
|
## Similar to `CoreDbMptRef` but with pre-hashed keys. That is, any
|
2023-10-11 19:09:11 +00:00
|
|
|
## argument key for `merge()`, `fetch()` etc. will be hashed first
|
|
|
|
## before being applied.
|
2023-09-26 09:21:13 +00:00
|
|
|
fromMpt*: CoreDxMptRef
|
|
|
|
methods*: CoreDbMptFns
|
|
|
|
|
|
|
|
CoreDxTxRef* = ref object
|
|
|
|
## Transaction descriptor derived from `CoreDbRef`
|
|
|
|
parent*: CoreDbRef
|
|
|
|
methods*: CoreDbTxFns
|
|
|
|
|
|
|
|
CoreDxTxID* = ref object
|
|
|
|
## Transaction ID descriptor derived from `CoreDbRef`
|
|
|
|
parent*: CoreDbRef
|
|
|
|
methods*: CoreDbTxIdFns
|
|
|
|
|
|
|
|
CoreDxCaptRef* = ref object
|
|
|
|
## Db transaction tracer derived from `CoreDbRef`
|
|
|
|
parent*: CoreDbRef
|
|
|
|
methods*: CoreDbCaptFns
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|