Coredb maintenance (#2398)

* CoreDb: remove PHK tries

why:
  There is no general use anymore for an MPT with a pre-hashed key. It
  was used to resemble the `SecureHexaryTrie` logic from the legacy DB.

  The only pace where this is needed is the `Leger` which uses a
  a distinct MPT version anyway (see `distinct_ledgers.nim`.)

* Rename `CoreDx*` -> `CoreDb*`

why:
  The naming `CoreDx*` was used to differentiate the new CoreDb API from
  the legacy API which had descriptors named `CoreDb*`.
This commit is contained in:
Jordan Hrycaj 2024-06-19 14:13:12 +00:00 committed by GitHub
parent e7be0d185c
commit 081cb15493
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 169 additions and 294 deletions

View File

@ -357,7 +357,7 @@ proc consensus*(com: CommonRef, header: BlockHeader): ConsensusType =
proc initializeEmptyDb*(com: CommonRef) =
let kvt = com.db.newKvt()
proc contains(kvt: CoreDxKvtRef; key: openArray[byte]): bool =
proc contains(kvt: CoreDbKvtRef; key: openArray[byte]): bool =
kvt.hasKey(key).expect "valid bool"
if canonicalHeadHashKey().toOpenArray notin kvt:
info "Writing genesis to DB"

View File

@ -36,7 +36,7 @@ type
TxPackerStateRef = ref object
xp: TxPoolRef
tr: CoreDxMptRef
tr: CoreDbMptRef
cleanState: bool
balance: UInt256
blobGasUsed: uint64

View File

@ -127,13 +127,13 @@ proc baseMethods(db: AristoCoreDbRef): CoreDbBaseFns =
kBase = db.kdbBase
when false: # currently disabled
proc tracerSetup(flags: set[CoreDbCaptFlags]): CoreDxCaptRef =
proc tracerSetup(flags: set[CoreDbCaptFlags]): CoreDbCaptRef =
if db.tracer.isNil:
db.tracer = AristoTracerRef(parent: db)
db.tracer.init(kBase, aBase, flags)
else:
db.tracer.push(flags)
CoreDxCaptRef(methods: db.tracer.cptMethods)
CoreDbCaptRef(methods: db.tracer.cptMethods)
proc persistent(bn: Opt[BlockNumber]): CoreDbRc[void] =
const info = "persistentFn()"
@ -164,7 +164,7 @@ proc baseMethods(db: AristoCoreDbRef): CoreDbBaseFns =
errorPrintFn: proc(e: CoreDbErrorRef): string =
e.errorPrint(),
newKvtFn: proc(): CoreDbRc[CoreDxKvtRef] =
newKvtFn: proc(): CoreDbRc[CoreDbKvtRef] =
kBase.newKvtHandler("newKvtFn()"),
newCtxFn: proc(): CoreDbCtxRef =
@ -176,16 +176,16 @@ proc baseMethods(db: AristoCoreDbRef): CoreDbBaseFns =
swapCtxFn: proc(ctx: CoreDbCtxRef): CoreDbCtxRef =
aBase.swapCtx(ctx),
beginFn: proc(): CoreDxTxRef =
beginFn: proc(): CoreDbTxRef =
const info = "beginFn()"
let
aTx = aBase.txBegin info
kTx = kBase.txBegin info
dsc = CoreDxTxRef(methods: db.txMethods(aTx, kTx))
dsc = CoreDbTxRef(methods: db.txMethods(aTx, kTx))
db.bless(dsc),
# # currently disabled
# newCaptureFn: proc(flags:set[CoreDbCaptFlags]): CoreDbRc[CoreDxCaptRef] =
# newCaptureFn: proc(flags:set[CoreDbCaptFlags]): CoreDbRc[CoreDbCaptRef] =
# ok(db.bless flags.tracerSetup()),
persistentFn: proc(bn: Opt[BlockNumber]): CoreDbRc[void] =
@ -230,11 +230,11 @@ func toAristoProfData*(
result.aristo = db.AristoCoreDbRef.adbBase.api.AristoApiProfRef.data
result.kvt = db.AristoCoreDbRef.kdbBase.api.KvtApiProfRef.data
func toAristoApi*(kvt: CoreDxKvtRef): KvtApiRef =
func toAristoApi*(kvt: CoreDbKvtRef): KvtApiRef =
if kvt.parent.isAristo:
return AristoCoreDbRef(kvt.parent).kdbBase.api
func toAristoApi*(mpt: CoreDxMptRef): AristoApiRef =
func toAristoApi*(mpt: CoreDbMptRef): AristoApiRef =
if mpt.parent.isAristo:
return mpt.to(AristoApiRef)
@ -264,7 +264,7 @@ include
# ------------------------
iterator aristoKvtPairsVoid*(dsc: CoreDxKvtRef): (Blob,Blob) {.rlpRaise.} =
iterator aristoKvtPairsVoid*(dsc: CoreDbKvtRef): (Blob,Blob) {.rlpRaise.} =
let
api = dsc.toAristoApi()
p = api.forkTx(dsc.to(KvtDbRef),0).valueOrApiError "aristoKvtPairs()"
@ -272,7 +272,7 @@ iterator aristoKvtPairsVoid*(dsc: CoreDxKvtRef): (Blob,Blob) {.rlpRaise.} =
for (k,v) in use_kvt.VoidBackendRef.walkPairs p:
yield (k,v)
iterator aristoKvtPairsMem*(dsc: CoreDxKvtRef): (Blob,Blob) {.rlpRaise.} =
iterator aristoKvtPairsMem*(dsc: CoreDbKvtRef): (Blob,Blob) {.rlpRaise.} =
let
api = dsc.toAristoApi()
p = api.forkTx(dsc.to(KvtDbRef),0).valueOrApiError "aristoKvtPairs()"
@ -280,19 +280,19 @@ iterator aristoKvtPairsMem*(dsc: CoreDxKvtRef): (Blob,Blob) {.rlpRaise.} =
for (k,v) in use_kvt.MemBackendRef.walkPairs p:
yield (k,v)
iterator aristoMptPairs*(dsc: CoreDxMptRef): (Blob,Blob) {.noRaise.} =
iterator aristoMptPairs*(dsc: CoreDbMptRef): (Blob,Blob) {.noRaise.} =
let
api = dsc.to(AristoApiRef)
mpt = dsc.to(AristoDbRef)
for (k,v) in mpt.rightPairs LeafTie(root: dsc.rootID):
yield (api.pathAsBlob(k.path), api.serialise(mpt, v).valueOr(EmptyBlob))
iterator aristoReplicateMem*(dsc: CoreDxMptRef): (Blob,Blob) {.rlpRaise.} =
iterator aristoReplicateMem*(dsc: CoreDbMptRef): (Blob,Blob) {.rlpRaise.} =
## Instantiation for `MemBackendRef`
for k,v in aristoReplicate[use_ari.MemBackendRef](dsc):
yield (k,v)
iterator aristoReplicateVoid*(dsc: CoreDxMptRef): (Blob,Blob) {.rlpRaise.} =
iterator aristoReplicateVoid*(dsc: CoreDbMptRef): (Blob,Blob) {.rlpRaise.} =
## Instantiation for `VoidBackendRef`
for k,v in aristoReplicate[use_ari.VoidBackendRef](dsc):
yield (k,v)

View File

@ -20,7 +20,7 @@ template valueOrApiError[U,V](rc: Result[U,V]; info: static[string]): U =
rc.valueOr: raise (ref AristoApiRlpError)(msg: info)
iterator aristoReplicate[T](
dsc: CoreDxMptRef;
dsc: CoreDbMptRef;
): (Blob,Blob)
{.gcsafe, raises: [AristoApiRlpError].} =
## Generic iterator used for building dedicated backend iterators.

View File

@ -31,10 +31,10 @@ type
base: AristoBaseRef ## Local base descriptor
mpt*: AristoDbRef ## Aristo MPT database
AristoCoreDxAccRef = ref object of CoreDxAccRef
AristoCoreDbAccRef = ref object of CoreDbAccRef
base: AristoBaseRef ## Local base descriptor
AristoCoreDxMptRef = ref object of CoreDxMptRef
AristoCoreDbMptRef = ref object of CoreDbMptRef
base: AristoBaseRef ## Local base descriptor
mptRoot: VertexID ## State root, may be zero unless account
accPath: PathID ## Needed for storage tree/columns
@ -94,7 +94,7 @@ func resetCol(colType: CoreDbColType): bool =
# -------------------------------
func toCoreDbAccount(
cAcc: AristoCoreDxAccRef;
cAcc: AristoCoreDbAccRef;
acc: AristoAccount;
address: EthAddress;
): CoreDbAccount =
@ -184,7 +184,7 @@ func toVoidRc[T](
# Private `MPT` call back functions
# ------------------------------------------------------------------------------
proc mptMethods(cMpt: AristoCoreDxMptRef): CoreDbMptFns =
proc mptMethods(cMpt: AristoCoreDbMptRef): CoreDbMptFns =
## Generic columns database handlers
let
cMpt = cMpt # So it can savely be captured
@ -319,7 +319,7 @@ proc mptMethods(cMpt: AristoCoreDxMptRef): CoreDbMptFns =
# Private account call back functions
# ------------------------------------------------------------------------------
proc accMethods(cAcc: AristoCoreDxAccRef): CoreDbAccFns =
proc accMethods(cAcc: AristoCoreDbAccRef): CoreDbAccFns =
## Account columns database handlers
let
cAcc = cAcc # So it can savely be captured
@ -333,8 +333,8 @@ proc accMethods(cAcc: AristoCoreDxAccRef): CoreDbAccFns =
base: base,
colType: CtAccounts)
proc accCloneMpt(): CoreDbRc[CoreDxMptRef] =
var xpt = AristoCoreDxMptRef(
proc accCloneMpt(): CoreDbRc[CoreDbMptRef] =
var xpt = AristoCoreDbMptRef(
base: base,
mptRoot: AccountsVID)
xpt.methods = xpt.mptMethods
@ -394,7 +394,7 @@ proc accMethods(cAcc: AristoCoreDxAccRef): CoreDbAccFns =
CoreDbAccFns(
getMptFn: proc(): CoreDbRc[CoreDxMptRef] =
getMptFn: proc(): CoreDbRc[CoreDbMptRef] =
accCloneMpt(),
fetchFn: proc(address: EthAddress): CoreDbRc[CoreDbAccount] =
@ -463,22 +463,22 @@ proc ctxMethods(cCtx: AristoCoreDbCtxRef): CoreDbCtxFns =
err(aristo.GenericError.toError(base, info, RootNotFound))
proc ctxGetMpt(col: CoreDbColRef): CoreDbRc[CoreDxMptRef] =
proc ctxGetMpt(col: CoreDbColRef): CoreDbRc[CoreDbMptRef] =
const
info = "ctx/getMptFn()"
let
col = AristoColRef(col)
var
reset = false
newMpt: AristoCoreDxMptRef
newMpt: AristoCoreDbMptRef
if not col.isValid:
reset = true
newMpt = AristoCoreDxMptRef(
newMpt = AristoCoreDbMptRef(
mptRoot: GenericVID,
accPath: VOID_PATH_ID)
elif col.colType == CtStorage:
newMpt = AristoCoreDxMptRef(
newMpt = AristoCoreDbMptRef(
mptRoot: col.stoRoot,
accPath: col.stoAddr.to(PathID),
address: col.stoAddr)
@ -492,7 +492,7 @@ proc ctxMethods(cCtx: AristoCoreDbCtxRef): CoreDbCtxFns =
return err(rc.error[1].toError(base, info, AccNotFound))
else:
reset = col.colType.resetCol()
newMpt = AristoCoreDxMptRef(
newMpt = AristoCoreDbMptRef(
mptRoot: VertexID(col.colType),
accPath: VOID_PATH_ID)
@ -508,7 +508,7 @@ proc ctxMethods(cCtx: AristoCoreDbCtxRef): CoreDbCtxFns =
newMpt.methods = newMpt.mptMethods()
ok(db.bless newMpt)
proc ctxGetAcc(col: CoreDbColRef): CoreDbRc[CoreDxAccRef] =
proc ctxGetAcc(col: CoreDbColRef): CoreDbRc[CoreDbAccRef] =
const info = "getAccFn()"
let col = AristoColRef(col)
@ -516,7 +516,7 @@ proc ctxMethods(cCtx: AristoCoreDbCtxRef): CoreDbCtxFns =
let error = (AccountsVID, AccRootUnacceptable)
return err(error.toError(base, info, RootUnacceptable))
let acc = AristoCoreDxAccRef(base: base)
let acc = AristoCoreDbAccRef(base: base)
acc.methods = acc.accMethods()
ok(db.bless acc)
@ -534,10 +534,10 @@ proc ctxMethods(cCtx: AristoCoreDbCtxRef): CoreDbCtxFns =
): CoreDbRc[CoreDbColRef] =
ctxNewCol(col, colState, address),
getMptFn: proc(col: CoreDbColRef): CoreDbRc[CoreDxMptRef] =
getMptFn: proc(col: CoreDbColRef): CoreDbRc[CoreDbMptRef] =
ctxGetMpt(col),
getAccFn: proc(col: CoreDbColRef): CoreDbRc[CoreDxAccRef] =
getAccFn: proc(col: CoreDbColRef): CoreDbRc[CoreDbAccRef] =
ctxGetAcc(col),
forgetFn: proc() =
@ -579,14 +579,14 @@ proc getSavedState*(base: AristoBaseRef): Result[SavedState,void] =
# ---------------------
func to*(dsc: CoreDxMptRef, T: type AristoDbRef): T =
AristoCoreDxMptRef(dsc).base.ctx.mpt
func to*(dsc: CoreDbMptRef, T: type AristoDbRef): T =
AristoCoreDbMptRef(dsc).base.ctx.mpt
func to*(dsc: CoreDxMptRef, T: type AristoApiRef): T =
AristoCoreDxMptRef(dsc).base.api
func to*(dsc: CoreDbMptRef, T: type AristoApiRef): T =
AristoCoreDbMptRef(dsc).base.api
func rootID*(dsc: CoreDxMptRef): VertexID =
AristoCoreDxMptRef(dsc).mptRoot
func rootID*(dsc: CoreDbMptRef): VertexID =
AristoCoreDbMptRef(dsc).mptRoot
func txTop*(
base: AristoBaseRef;

View File

@ -24,9 +24,9 @@ type
parent: CoreDbRef ## Opaque top level descriptor
kdb: KvtDbRef ## Shared key-value table
api*: KvtApiRef ## Api functions can be re-directed
cache: KvtCoreDxKvtRef ## Shared transaction table wrapper
cache: KvtCoreDbKvtRef ## Shared transaction table wrapper
KvtCoreDxKvtRef = ref object of CoreDxKvtRef
KvtCoreDbKvtRef = ref object of CoreDbKvtRef
base: KvtBaseRef ## Local base descriptor
kvt: KvtDbRef ## In most cases different from `base.kdb`
@ -68,16 +68,16 @@ func toRc[T](
# Private `kvt` call back functions
# ------------------------------------------------------------------------------
proc kvtMethods(cKvt: KvtCoreDxKvtRef): CoreDbKvtFns =
proc kvtMethods(cKvt: KvtCoreDbKvtRef): CoreDbKvtFns =
## Key-value database table handlers
proc kvtBackend(
cKvt:KvtCoreDxKvtRef;
cKvt:KvtCoreDbKvtRef;
): CoreDbKvtBackendRef =
cKvt.base.parent.bless AristoCoreDbKvtBE(kdb: cKvt.kvt)
proc kvtForget(
cKvt: KvtCoreDxKvtRef;
cKvt: KvtCoreDbKvtRef;
info: static[string];
): CoreDbRc[void] =
let
@ -95,7 +95,7 @@ proc kvtMethods(cKvt: KvtCoreDxKvtRef): CoreDbKvtFns =
ok()
proc kvtGet(
cKvt: KvtCoreDxKvtRef;
cKvt: KvtCoreDbKvtRef;
k: openArray[byte];
info: static[string];
): CoreDbRc[Blob] =
@ -108,7 +108,7 @@ proc kvtMethods(cKvt: KvtCoreDxKvtRef): CoreDbKvtFns =
rc.toRc(cKvt.base, info)
proc kvtPut(
cKvt: KvtCoreDxKvtRef;
cKvt: KvtCoreDbKvtRef;
k: openArray[byte];
v: openArray[byte];
info: static[string];
@ -120,7 +120,7 @@ proc kvtMethods(cKvt: KvtCoreDxKvtRef): CoreDbKvtFns =
err(rc.error.toError(cKvt.base, info))
proc kvtDel(
cKvt: KvtCoreDxKvtRef;
cKvt: KvtCoreDbKvtRef;
k: openArray[byte];
info: static[string];
): CoreDbRc[void] =
@ -131,7 +131,7 @@ proc kvtMethods(cKvt: KvtCoreDxKvtRef): CoreDbKvtFns =
err(rc.error.toError(cKvt.base, info))
proc kvtHasKey(
cKvt: KvtCoreDxKvtRef;
cKvt: KvtCoreDbKvtRef;
k: openArray[byte];
info: static[string];
): CoreDbRc[bool] =
@ -176,8 +176,8 @@ func toVoidRc*[T](
# ---------------------
func to*(dsc: CoreDxKvtRef; T: type KvtDbRef): T =
KvtCoreDxKvtRef(dsc).kvt
func to*(dsc: CoreDbKvtRef; T: type KvtDbRef): T =
KvtCoreDbKvtRef(dsc).kvt
func txTop*(
base: KvtBaseRef;
@ -219,7 +219,7 @@ proc persistent*(
proc newKvtHandler*(
base: KvtBaseRef;
info: static[string];
): CoreDbRc[CoreDxKvtRef] =
): CoreDbRc[CoreDbKvtRef] =
ok(base.cache)
@ -234,7 +234,7 @@ func init*(T: type KvtBaseRef; db: CoreDbRef; kdb: KvtDbRef): T =
kdb: kdb)
# Preallocated shared descriptor
let dsc = KvtCoreDxKvtRef(
let dsc = KvtCoreDbKvtRef(
base: result,
kvt: kdb)
dsc.methods = dsc.kvtMethods()

View File

@ -170,7 +170,7 @@ proc newAristoDualRocksDbCoreDbRef*(path: string, opts: DbOptions): CoreDbRef =
# Public aristo iterators
# ------------------------------------------------------------------------------
iterator aristoReplicateRdb*(dsc: CoreDxMptRef): (Blob, Blob) {.rlpRaise.} =
iterator aristoReplicateRdb*(dsc: CoreDbMptRef): (Blob, Blob) {.rlpRaise.} =
## Instantiation for `VoidBackendRef`
for k, v in aristoReplicate[use_ari.RdbBackendRef](dsc):
yield (k, v)

View File

@ -23,7 +23,7 @@ const
EnableApiTracking = false
## When enabled, functions using this tracking facility need to import
## `chronicles`, as well. Tracking is enabled by setting `true` the flags
## `trackLegaApi` and/or `trackNewApi` in the `CoreDxTxRef` descriptor.
## `trackLegaApi` and/or `trackNewApi` in the `CoreDbTxRef` descriptor.
EnableApiProfiling = true
## Enables functions profiling if `EnableApiTracking` is also set `true`.
@ -49,12 +49,11 @@ export
CoreDbProfListRef,
CoreDbRef,
CoreDbType,
CoreDxAccRef,
CoreDxCaptRef,
CoreDxKvtRef,
CoreDxMptRef,
CoreDxPhkRef,
CoreDxTxRef,
CoreDbAccRef,
CoreDbCaptRef,
CoreDbKvtRef,
CoreDbMptRef,
CoreDbTxRef,
PayloadRef
const
@ -94,7 +93,7 @@ when EnableApiTracking:
proc `$`(h: Hash256): string = h.toStr
template setTrackNewApi(
w: CoreDxApiTrackRef;
w: CoreDbApiTrackRef;
s: static[CoreDbFnInx];
code: untyped;
) =
@ -106,48 +105,17 @@ template setTrackNewApi(
const api {.inject,used.} = s
template setTrackNewApi*(
w: CoreDxApiTrackRef;
w: CoreDbApiTrackRef;
s: static[CoreDbFnInx];
) =
w.setTrackNewApi(s):
discard
template ifTrackNewApi*(w: CoreDxApiTrackRef; code: untyped) =
template ifTrackNewApi*(w: CoreDbApiTrackRef; code: untyped) =
when EnableApiTracking:
w.endNewApiIf:
code
# ---------
func toCoreDxPhkRef(mpt: CoreDxMptRef): CoreDxPhkRef =
## MPT => pre-hashed MPT (aka PHK)
result = CoreDxPhkRef(
toMpt: mpt,
methods: mpt.methods)
result.methods.fetchFn =
proc(k: openArray[byte]): CoreDbRc[Blob] =
mpt.methods.fetchFn(k.keccakHash.data)
result.methods.deleteFn =
proc(k: openArray[byte]): CoreDbRc[void] =
mpt.methods.deleteFn(k.keccakHash.data)
result.methods.mergeFn =
proc(k:openArray[byte]; v: openArray[byte]): CoreDbRc[void] =
mpt.methods.mergeFn(k.keccakHash.data, v)
result.methods.hasPathFn =
proc(k: openArray[byte]): CoreDbRc[bool] =
mpt.methods.hasPathFn(k.keccakHash.data)
when AutoValidateDescriptors:
result.validate
func parent(phk: CoreDxPhkRef): CoreDbRef =
phk.toMpt.parent
# ------------------------------------------------------------------------------
# Public constructor helper
# ------------------------------------------------------------------------------
@ -168,16 +136,16 @@ proc bless*(db: CoreDbRef; col: CoreDbColRef): CoreDbColRef =
col.validate
col
proc bless*(db: CoreDbRef; kvt: CoreDxKvtRef): CoreDxKvtRef =
proc bless*(db: CoreDbRef; kvt: CoreDbKvtRef): CoreDbKvtRef =
## Complete sub-module descriptor, fill in `parent`.
kvt.parent = db
when AutoValidateDescriptors:
kvt.validate
kvt
proc bless*[T: CoreDxKvtRef |
CoreDbCtxRef | CoreDxMptRef | CoreDxPhkRef | CoreDxAccRef |
CoreDxTxRef | CoreDxCaptRef |
proc bless*[T: CoreDbKvtRef |
CoreDbCtxRef | CoreDbMptRef | CoreDbAccRef |
CoreDbTxRef | CoreDbCaptRef |
CoreDbKvtBackendRef | CoreDbMptBackendRef](
db: CoreDbRef;
dsc: T;
@ -226,18 +194,18 @@ proc dbType*(db: CoreDbRef): CoreDbType =
result = db.dbType
db.ifTrackNewApi: debug newApiTxt, api, elapsed, result
proc parent*[T: CoreDxKvtRef |
proc parent*[T: CoreDbKvtRef |
CoreDbColRef |
CoreDbCtxRef | CoreDxMptRef | CoreDxPhkRef | CoreDxAccRef |
CoreDxTxRef |
CoreDxCaptRef |
CoreDbCtxRef | CoreDbMptRef | CoreDbAccRef |
CoreDbTxRef |
CoreDbCaptRef |
CoreDbErrorRef](
child: T): CoreDbRef =
## Getter, common method for all sub-modules
##
result = child.parent
proc backend*(dsc: CoreDxKvtRef | CoreDxMptRef): auto =
proc backend*(dsc: CoreDbKvtRef | CoreDbMptRef): auto =
## Getter, retrieves the *raw* backend object for special/localised support.
##
dsc.setTrackNewApi AnyBackendFn
@ -268,7 +236,7 @@ proc `$$`*(e: CoreDbErrorRef): string =
# Public key-value table methods
# ------------------------------------------------------------------------------
proc newKvt*(db: CoreDbRef): CoreDxKvtRef =
proc newKvt*(db: CoreDbRef): CoreDbKvtRef =
## Constructor, will defect on failure.
##
## This function subscribes to the common base object shared with other
@ -281,13 +249,13 @@ proc newKvt*(db: CoreDbRef): CoreDxKvtRef =
raiseAssert error.prettyText()
db.ifTrackNewApi: debug newApiTxt, api, elapsed
proc get*(kvt: CoreDxKvtRef; key: openArray[byte]): CoreDbRc[Blob] =
proc get*(kvt: CoreDbKvtRef; key: openArray[byte]): CoreDbRc[Blob] =
## This function always returns a non-empty `Blob` or an error code.
kvt.setTrackNewApi KvtGetFn
result = kvt.methods.getFn key
kvt.ifTrackNewApi: debug newApiTxt, api, elapsed, key=key.toStr, result
proc getOrEmpty*(kvt: CoreDxKvtRef; key: openArray[byte]): CoreDbRc[Blob] =
proc getOrEmpty*(kvt: CoreDbKvtRef; key: openArray[byte]): CoreDbRc[Blob] =
## This function sort of mimics the behaviour of the legacy database
## returning an empty `Blob` if the argument `key` is not found on the
## database.
@ -298,13 +266,13 @@ proc getOrEmpty*(kvt: CoreDxKvtRef; key: openArray[byte]): CoreDbRc[Blob] =
result = CoreDbRc[Blob].ok(EmptyBlob)
kvt.ifTrackNewApi: debug newApiTxt, api, elapsed, key=key.toStr, result
proc del*(kvt: CoreDxKvtRef; key: openArray[byte]): CoreDbRc[void] =
proc del*(kvt: CoreDbKvtRef; key: openArray[byte]): CoreDbRc[void] =
kvt.setTrackNewApi KvtDelFn
result = kvt.methods.delFn key
kvt.ifTrackNewApi: debug newApiTxt, api, elapsed, key=key.toStr, result
proc put*(
kvt: CoreDxKvtRef;
kvt: CoreDbKvtRef;
key: openArray[byte];
val: openArray[byte];
): CoreDbRc[void] =
@ -313,7 +281,7 @@ proc put*(
kvt.ifTrackNewApi:
debug newApiTxt, api, elapsed, key=key.toStr, val=val.toLenStr, result
proc hasKey*(kvt: CoreDxKvtRef; key: openArray[byte]): CoreDbRc[bool] =
proc hasKey*(kvt: CoreDbKvtRef; key: openArray[byte]): CoreDbRc[bool] =
## Would be named `contains` if it returned `bool` rather than `Result[]`.
##
kvt.setTrackNewApi KvtHasKeyFn
@ -400,7 +368,7 @@ proc newColumn*(
##
## This function is intended to open a column on the database as in:
## ::
## proc openAccountLedger(db: CoreDbRef, colState: Hash256): CoreDxMptRef =
## proc openAccountLedger(db: CoreDbRef, colState: Hash256): CoreDbMptRef =
## let col = db.ctx.newColumn(CtAccounts, colState).valueOr:
## # some error handling
## return
@ -490,7 +458,7 @@ proc stateEmptyOrVoid*(col: CoreDbColRef): bool =
proc getMpt*(
ctx: CoreDbCtxRef;
col: CoreDbColRef;
): CoreDbRc[CoreDxMptRef] =
): CoreDbRc[CoreDbMptRef] =
## Get an MPT sub-trie view.
##
## If the `col` argument descriptor was created for an `EMPTY_ROOT_HASH`
@ -507,7 +475,7 @@ proc getMpt*(
ctx: CoreDbCtxRef;
colType: CoreDbColType;
address = Opt.none(EthAddress);
): CoreDxMptRef =
): CoreDbMptRef =
## Shortcut for `getMpt(col)` where the `col` argument is
## `db.getColumn(colType,EMPTY_ROOT_HASH).value`. This function will always
## return a non-nil descriptor or throw an exception.
@ -519,7 +487,7 @@ proc getMpt*(
ctx.ifTrackNewApi: debug newApiTxt, api, colType, elapsed
proc getMpt*(acc: CoreDxAccRef): CoreDxMptRef =
proc getMpt*(acc: CoreDbAccRef): CoreDbMptRef =
## Variant of `getMpt()`, will defect on failure.
##
## The needed sub-trie information is taken/implied from the current `acc`
@ -536,7 +504,7 @@ proc getMpt*(acc: CoreDxAccRef): CoreDxMptRef =
proc getAcc*(
ctx: CoreDbCtxRef;
col: CoreDbColRef;
): CoreDbRc[CoreDxAccRef] =
): CoreDbRc[CoreDbAccRef] =
## Accounts trie constructor, will defect on failure.
##
## Example:
@ -549,62 +517,35 @@ proc getAcc*(
## ... # Was not the state root for the accounts column
## return
##
## This function works similar to `getMpt()` for handling accounts. Although
## one can emulate this function by means of `getMpt(..).toPhk()`, it is
## recommended using `CoreDxAccRef` methods for accounts.
## This function works similar to `getMpt()` for handling accounts.
##
ctx.setTrackNewApi CtxGetAccFn
result = ctx.methods.getAccFn col
ctx.ifTrackNewApi: debug newApiTxt, api, elapsed, col, result
proc toMpt*(phk: CoreDxPhkRef): CoreDxMptRef =
## Replaces the pre-hashed argument column `phk` by the non pre-hashed *MPT*.
##
phk.setTrackNewApi PhkToMptFn
result = phk.toMpt
phk.ifTrackNewApi:
let col = result.methods.getColFn()
debug newApiTxt, api, elapsed, col
proc toPhk*(mpt: CoreDxMptRef): CoreDxPhkRef =
## Replaces argument `mpt` by a pre-hashed *MPT*.
##
mpt.setTrackNewApi MptToPhkFn
result = mpt.toCoreDxPhkRef
mpt.ifTrackNewApi:
let col = result.methods.getColFn()
debug newApiTxt, api, elapsed, col
# ------------------------------------------------------------------------------
# Public common methods for all hexary trie databases (`mpt`, `phk`, or `acc`)
# Public common methods for all hexary trie databases (`mpt`, or `acc`)
# ------------------------------------------------------------------------------
proc getColumn*(acc: CoreDxAccRef): CoreDbColRef =
proc getColumn*(acc: CoreDbAccRef): CoreDbColRef =
## Getter, result is not `nil`
##
acc.setTrackNewApi AccGetColFn
result = acc.methods.getColFn()
acc.ifTrackNewApi: debug newApiTxt, api, elapsed, result
proc getColumn*(mpt: CoreDxMptRef): CoreDbColRef =
proc getColumn*(mpt: CoreDbMptRef): CoreDbColRef =
## Variant of `getColumn()`
##
mpt.setTrackNewApi MptGetColFn
result = mpt.methods.getColFn()
mpt.ifTrackNewApi: debug newApiTxt, api, elapsed, result
proc getColumn*(phk: CoreDxPhkRef): CoreDbColRef =
## Variant of `getColumn()`
##
phk.setTrackNewApi PhkGetColFn
result = phk.methods.getColFn()
phk.ifTrackNewApi: debug newApiTxt, api, elapsed, result
# ------------------------------------------------------------------------------
# Public generic hexary trie database methods (`mpt` or `phk`)
# Public generic hexary trie database methods
# ------------------------------------------------------------------------------
proc fetch*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[Blob] =
proc fetch*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[Blob] =
## Fetch data from the argument `mpt`. The function always returns a
## non-empty `Blob` or an error code.
##
@ -614,16 +555,7 @@ proc fetch*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[Blob] =
let col = mpt.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc fetch*(phk: CoreDxPhkRef; key: openArray[byte]): CoreDbRc[Blob] =
## Variant of `fetch()"
phk.setTrackNewApi PhkFetchFn
result = phk.methods.fetchFn key
phk.ifTrackNewApi:
let col = phk.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc fetchOrEmpty*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[Blob] =
proc fetchOrEmpty*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[Blob] =
## This function returns an empty `Blob` if the argument `key` is not found
## on the database.
##
@ -635,34 +567,15 @@ proc fetchOrEmpty*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[Blob] =
let col = mpt.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc fetchOrEmpty*(phk: CoreDxPhkRef; key: openArray[byte]): CoreDbRc[Blob] =
## Variant of `fetchOrEmpty()`
phk.setTrackNewApi PhkFetchOrEmptyFn
result = phk.methods.fetchFn key
if result.isErr and result.error.error == MptNotFound:
result = CoreDbRc[Blob].ok(EmptyBlob)
phk.ifTrackNewApi:
let col = phk.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc delete*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[void] =
proc delete*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[void] =
mpt.setTrackNewApi MptDeleteFn
result = mpt.methods.deleteFn key
mpt.ifTrackNewApi:
let col = mpt.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc delete*(phk: CoreDxPhkRef; key: openArray[byte]): CoreDbRc[void] =
phk.setTrackNewApi PhkDeleteFn
result = phk.methods.deleteFn key
phk.ifTrackNewApi:
let col = phk.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc merge*(
mpt: CoreDxMptRef;
mpt: CoreDbMptRef;
key: openArray[byte];
val: openArray[byte];
): CoreDbRc[void] =
@ -672,19 +585,7 @@ proc merge*(
let col = mpt.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, val=val.toLenStr, result
proc merge*(
phk: CoreDxPhkRef;
key: openArray[byte];
val: openArray[byte];
): CoreDbRc[void] =
phk.setTrackNewApi PhkMergeFn
result = phk.methods.mergeFn(key, val)
phk.ifTrackNewApi:
let col = phk.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, val=val.toLenStr, result
proc hasPath*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[bool] =
proc hasPath*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[bool] =
## This function would be named `contains()` if it returned `bool` rather
## than a `Result[]`.
##
@ -694,19 +595,11 @@ proc hasPath*(mpt: CoreDxMptRef; key: openArray[byte]): CoreDbRc[bool] =
let col = mpt.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
proc hasPath*(phk: CoreDxPhkRef; key: openArray[byte]): CoreDbRc[bool] =
## Variant of `hasPath()`
phk.setTrackNewApi PhkHasPathFn
result = phk.methods.hasPathFn key
phk.ifTrackNewApi:
let col = phk.methods.getColFn()
debug newApiTxt, api, elapsed, col, key=key.toStr, result
# ------------------------------------------------------------------------------
# Public trie database methods for accounts
# ------------------------------------------------------------------------------
proc fetch*(acc: CoreDxAccRef; address: EthAddress): CoreDbRc[CoreDbAccount] =
proc fetch*(acc: CoreDbAccRef; address: EthAddress): CoreDbRc[CoreDbAccount] =
## Fetch data from the argument `acc`.
##
acc.setTrackNewApi AccFetchFn
@ -716,12 +609,12 @@ proc fetch*(acc: CoreDxAccRef; address: EthAddress): CoreDbRc[CoreDbAccount] =
debug newApiTxt, api, elapsed, address, storage, result
proc delete*(acc: CoreDxAccRef; address: EthAddress): CoreDbRc[void] =
proc delete*(acc: CoreDbAccRef; address: EthAddress): CoreDbRc[void] =
acc.setTrackNewApi AccDeleteFn
result = acc.methods.deleteFn address
acc.ifTrackNewApi: debug newApiTxt, api, elapsed, address, result
proc stoDelete*(acc: CoreDxAccRef; address: EthAddress): CoreDbRc[void] =
proc stoDelete*(acc: CoreDbAccRef; address: EthAddress): CoreDbRc[void] =
## Recursively delete all data elements from the storage trie associated to
## the account identified by the argument `address`. After successful run,
## the storage trie will be empty.
@ -738,7 +631,7 @@ proc stoDelete*(acc: CoreDxAccRef; address: EthAddress): CoreDbRc[void] =
proc merge*(
acc: CoreDxAccRef;
acc: CoreDbAccRef;
account: CoreDbAccount;
): CoreDbRc[void] =
acc.setTrackNewApi AccMergeFn
@ -748,7 +641,7 @@ proc merge*(
debug newApiTxt, api, elapsed, address, result
proc hasPath*(acc: CoreDxAccRef; address: EthAddress): CoreDbRc[bool] =
proc hasPath*(acc: CoreDbAccRef; address: EthAddress): CoreDbRc[bool] =
## Would be named `contains` if it returned `bool` rather than `Result[]`.
##
acc.setTrackNewApi AccHasPathFn
@ -835,7 +728,7 @@ proc persistent*(
result = db.methods.persistentFn Opt.some(blockNumber)
db.ifTrackNewApi: debug newApiTxt, api, elapsed, blockNumber, result
proc newTransaction*(db: CoreDbRef): CoreDxTxRef =
proc newTransaction*(db: CoreDbRef): CoreDbTxRef =
## Constructor
##
db.setTrackNewApi BaseNewTxFn
@ -844,26 +737,26 @@ proc newTransaction*(db: CoreDbRef): CoreDxTxRef =
debug newApiTxt, api, elapsed, newLevel=db.methods.levelFn()
proc level*(tx: CoreDxTxRef): int =
proc level*(tx: CoreDbTxRef): int =
## Print positive transaction level for argument `tx`
##
tx.setTrackNewApi TxLevelFn
result = tx.methods.levelFn()
tx.ifTrackNewApi: debug newApiTxt, api, elapsed, result
proc commit*(tx: CoreDxTxRef) =
proc commit*(tx: CoreDbTxRef) =
tx.setTrackNewApi TxCommitFn:
let prvLevel {.used.} = tx.methods.levelFn()
tx.methods.commitFn()
tx.ifTrackNewApi: debug newApiTxt, api, elapsed, prvLevel
proc rollback*(tx: CoreDxTxRef) =
proc rollback*(tx: CoreDbTxRef) =
tx.setTrackNewApi TxRollbackFn:
let prvLevel {.used.} = tx.methods.levelFn()
tx.methods.rollbackFn()
tx.ifTrackNewApi: debug newApiTxt, api, elapsed, prvLevel
proc dispose*(tx: CoreDxTxRef) =
proc dispose*(tx: CoreDbTxRef) =
tx.setTrackNewApi TxDisposeFn:
let prvLevel {.used.} = tx.methods.levelFn()
tx.methods.disposeFn()
@ -877,7 +770,7 @@ when false: # currently disabled
proc newCapture*(
db: CoreDbRef;
flags: set[CoreDbCaptFlags] = {};
): CoreDbRc[CoreDxCaptRef] =
): CoreDbRc[CoreDbCaptRef] =
## Trace constructor providing an overlay on top of the argument database
## `db`. This overlay provides a replacement database handle that can be
## retrieved via `db.recorder()` (which can in turn be ovelayed.) While
@ -893,7 +786,7 @@ when false: # currently disabled
result = db.methods.newCaptureFn flags
db.ifTrackNewApi: debug newApiTxt, api, elapsed, result
proc recorder*(cpt: CoreDxCaptRef): CoreDbRef =
proc recorder*(cpt: CoreDbCaptRef): CoreDbRef =
## Getter, returns a tracer replacement handle to be used as new database.
## It records every action like fetch, store, hasKey, hasPath and delete.
## This descriptor can be superseded by a new overlay tracer (using
@ -908,7 +801,7 @@ when false: # currently disabled
result = cpt.methods.recorderFn()
cpt.ifTrackNewApi: debug newApiTxt, api, elapsed
proc logDb*(cp: CoreDxCaptRef): TableRef[Blob,Blob] =
proc logDb*(cp: CoreDbCaptRef): TableRef[Blob,Blob] =
## Getter, returns the logger table for the overlay tracer database.
##
## Caveat:
@ -920,14 +813,14 @@ when false: # currently disabled
result = cp.methods.logDbFn()
cp.ifTrackNewApi: debug newApiTxt, api, elapsed
proc flags*(cp: CoreDxCaptRef):set[CoreDbCaptFlags] =
proc flags*(cp: CoreDbCaptRef):set[CoreDbCaptFlags] =
## Getter
##
cp.setTrackNewApi CptFlagsFn
result = cp.methods.getFlagsFn()
cp.ifTrackNewApi: debug newApiTxt, api, elapsed, result
proc forget*(cp: CoreDxCaptRef) =
proc forget*(cp: CoreDbCaptRef) =
## Explicitely stop recording the current tracer instance and reset to
## previous level.
##

View File

@ -19,10 +19,10 @@ import
./base_desc
type
CoreDxApiTrackRef* =
CoreDbRef | CoreDxKvtRef | CoreDbColRef |
CoreDbCtxRef | CoreDxMptRef | CoreDxPhkRef | CoreDxAccRef |
CoreDxTxRef | CoreDxCaptRef | CoreDbErrorRef
CoreDbApiTrackRef* =
CoreDbRef | CoreDbKvtRef | CoreDbColRef |
CoreDbCtxRef | CoreDbMptRef | CoreDbAccRef |
CoreDbTxRef | CoreDbCaptRef | CoreDbErrorRef
CoreDbFnInx* = enum
## Profiling table index
@ -84,16 +84,6 @@ type
MptMergeFn = "mpt/merge"
MptPairsIt = "mpt/pairs"
MptReplicateIt = "mpt/replicate"
MptToPhkFn = "mpt/toPhk"
PhkDeleteFn = "phk/delete"
PhkFetchFn = "phk/fetch"
PhkFetchOrEmptyFn = "phk/fetchOrEmpty"
PhkForgetFn = "phk/forget"
PhkGetColFn = "phk/getColumn"
PhkHasPathFn = "phk/hasPath"
PhkMergeFn = "phk/merge"
PhkToMptFn = "phk/toMpt"
TxCommitFn = "commit"
TxDisposeFn = "dispose"
@ -165,12 +155,12 @@ proc toStr[T](rc: CoreDbRc[T]; ifOk: static[string]): string =
proc toStr*(rc: CoreDbRc[CoreDbRef]): string = rc.toStr "db"
proc toStr*(rc: CoreDbRc[CoreDbAccount]): string = rc.toStr "acc"
proc toStr*(rc: CoreDbRc[CoreDxKvtRef]): string = rc.toStr "kvt"
proc toStr*(rc: CoreDbRc[CoreDxTxRef]): string = rc.toStr "tx"
proc toStr*(rc: CoreDbRc[CoreDxCaptRef]): string = rc.toStr "capt"
proc toStr*(rc: CoreDbRc[CoreDbKvtRef]): string = rc.toStr "kvt"
proc toStr*(rc: CoreDbRc[CoreDbTxRef]): string = rc.toStr "tx"
proc toStr*(rc: CoreDbRc[CoreDbCaptRef]): string = rc.toStr "capt"
proc toStr*(rc: CoreDbRc[CoreDbCtxRef]): string = rc.toStr "ctx"
proc toStr*(rc: CoreDbRc[CoreDxMptRef]): string = rc.toStr "mpt"
proc toStr*(rc: CoreDbRc[CoreDxAccRef]): string = rc.toStr "acc"
proc toStr*(rc: CoreDbRc[CoreDbMptRef]): string = rc.toStr "mpt"
proc toStr*(rc: CoreDbRc[CoreDbAccRef]): string = rc.toStr "acc"
func toStr*(ela: Duration): string =
aristo_profile.toStr(ela)
@ -179,12 +169,12 @@ func toStr*(ela: Duration): string =
# Public new API logging framework
# ------------------------------------------------------------------------------
template beginNewApi*(w: CoreDxApiTrackRef; s: static[CoreDbFnInx]) =
template beginNewApi*(w: CoreDbApiTrackRef; s: static[CoreDbFnInx]) =
when CoreDbEnableApiProfiling:
const bnaCtx {.inject.} = s # Local use only
let bnaStart {.inject.} = getTime() # Local use only
template endNewApiIf*(w: CoreDxApiTrackRef; code: untyped) =
template endNewApiIf*(w: CoreDbApiTrackRef; code: untyped) =
block:
when typeof(w) is CoreDbRef:
let db = w

View File

@ -97,15 +97,15 @@ type
CoreDbBaseColPrintFn* = proc(vid: CoreDbColRef): string {.noRaise.}
CoreDbBaseErrorPrintFn* = proc(e: CoreDbErrorRef): string {.noRaise.}
CoreDbBaseLevelFn* = proc(): int {.noRaise.}
CoreDbBaseNewKvtFn* = proc(): CoreDbRc[CoreDxKvtRef] {.noRaise.}
CoreDbBaseNewKvtFn* = proc(): CoreDbRc[CoreDbKvtRef] {.noRaise.}
CoreDbBaseNewCtxFn* = proc(): CoreDbCtxRef {.noRaise.}
CoreDbBaseNewCtxFromTxFn* = proc(
colState: Hash256; kind: CoreDbColType): CoreDbRc[CoreDbCtxRef] {.noRaise.}
CoreDbBaseSwapCtxFn* = proc(ctx: CoreDbCtxRef): CoreDbCtxRef {.noRaise.}
CoreDbBaseTxBeginFn* = proc(): CoreDxTxRef {.noRaise.}
CoreDbBaseTxBeginFn* = proc(): CoreDbTxRef {.noRaise.}
CoreDbBaseNewCaptFn* =
proc(flgs: set[CoreDbCaptFlags]): CoreDbRc[CoreDxCaptRef] {.noRaise.}
CoreDbBaseGetCaptFn* = proc(): CoreDbRc[CoreDxCaptRef] {.noRaise.}
proc(flgs: set[CoreDbCaptFlags]): CoreDbRc[CoreDbCaptRef] {.noRaise.}
CoreDbBaseGetCaptFn* = proc(): CoreDbRc[CoreDbCaptRef] {.noRaise.}
CoreDbBasePersistentFn* =
proc(bn: Opt[BlockNumber]): CoreDbRc[void] {.noRaise.}
@ -164,9 +164,9 @@ type
colType: CoreDbColType; colState: Hash256; address: Opt[EthAddress];
): CoreDbRc[CoreDbColRef] {.noRaise.}
CoreDbCtxGetMptFn* = proc(
root: CoreDbColRef): CoreDbRc[CoreDxMptRef] {.noRaise.}
root: CoreDbColRef): CoreDbRc[CoreDbMptRef] {.noRaise.}
CoreDbCtxGetAccFn* = proc(
root: CoreDbColRef): CoreDbRc[CoreDxAccRef] {.noRaise.}
root: CoreDbColRef): CoreDbRc[CoreDbAccRef] {.noRaise.}
CoreDbCtxForgetFn* = proc() {.noRaise.}
CoreDbCtxFns* = object
@ -207,7 +207,7 @@ type
# ----------------------------------------------------
# Sub-descriptor: Mpt/hexary trie methods for accounts
# ------------------------------------------------------
CoreDbAccGetMptFn* = proc(): CoreDbRc[CoreDxMptRef] {.noRaise.}
CoreDbAccGetMptFn* = proc(): CoreDbRc[CoreDbMptRef] {.noRaise.}
CoreDbAccFetchFn* = proc(k: EthAddress): CoreDbRc[CoreDbAccount] {.noRaise.}
CoreDbAccDeleteFn* = proc(k: EthAddress): CoreDbRc[void] {.noRaise.}
CoreDbAccStoDeleteFn* = proc(k: EthAddress): CoreDbRc[void] {.noRaise.}
@ -283,25 +283,24 @@ type
## Backend wrapper for direct backend access
parent*: CoreDbRef
CoreDxKvtRef* = ref CoreDxKvtObj
CoreDxKvtObj* = object of RootObj
CoreDbKvtRef* = ref object of RootRef
## Statically initialised Key-Value pair table living in `CoreDbRef`
parent*: CoreDbRef
methods*: CoreDbKvtFns
CoreDbCtxRef* = ref object of RootRef
## Context for `CoreDxMptRef` and `CoreDxAccRef`
## Context for `CoreDbMptRef` and `CoreDbAccRef`
parent*: CoreDbRef
methods*: CoreDbCtxFns
CoreDxMptRef* = ref object of RootRef
CoreDbMptRef* = ref object of RootRef
## Hexary/Merkle-Patricia tree derived from `CoreDbRef`, will be
## initialised on-the-fly.
parent*: CoreDbRef
methods*: CoreDbMptFns
CoreDxAccRef* = ref object of RootRef
## Similar to `CoreDxKvtRef`, only dealing with `CoreDbAccount` data
CoreDbAccRef* = ref object of RootRef
## Similar to `CoreDbKvtRef`, only dealing with `CoreDbAccount` data
## rather than `Blob` values.
parent*: CoreDbRef
methods*: CoreDbAccFns
@ -312,19 +311,12 @@ type
parent*: CoreDbRef
ready*: bool ## Must be set `true` to enable
CoreDxPhkRef* = ref object
## Similar to `CoreDbMptRef` but with pre-hashed keys. That is, any
## argument key for `merge()`, `fetch()` etc. will be hashed first
## before being applied.
toMpt*: CoreDxMptRef
methods*: CoreDbMptFns
CoreDxTxRef* = ref object of RootRef
CoreDbTxRef* = ref object of RootRef
## Transaction descriptor derived from `CoreDbRef`
parent*: CoreDbRef
methods*: CoreDbTxFns
CoreDxCaptRef* = ref object
CoreDbCaptRef* = ref object
## Db transaction tracer derived from `CoreDbRef`
parent*: CoreDbRef
methods*: CoreDbCaptFns

View File

@ -16,10 +16,10 @@ type
EphemMethodsDesc = CoreDbKvtBackendRef | CoreDbMptBackendRef | CoreDbColRef
MethodsDesc =
CoreDxKvtRef |
CoreDbCtxRef | CoreDxMptRef | CoreDxPhkRef | CoreDxAccRef |
CoreDxTxRef |
CoreDxCaptRef
CoreDbKvtRef |
CoreDbCtxRef | CoreDbMptRef | CoreDbAccRef |
CoreDbTxRef |
CoreDbCaptRef
ValidateDesc* = MethodsDesc | EphemMethodsDesc | CoreDbErrorRef
@ -88,7 +88,7 @@ proc validateMethodsDesc(eph: EphemMethodsDesc) =
doAssert not eph.isNil
doAssert not eph.parent.isNil
proc validateMethodsDesc(kvt: CoreDxKvtRef) =
proc validateMethodsDesc(kvt: CoreDbKvtRef) =
doAssert not kvt.isNil
doAssert not kvt.parent.isNil
kvt.methods.validateMethodsDesc
@ -98,30 +98,25 @@ proc validateMethodsDesc(ctx: CoreDbCtxRef) =
doAssert not ctx.parent.isNil
ctx.methods.validateMethodsDesc
proc validateMethodsDesc(mpt: CoreDxMptRef) =
proc validateMethodsDesc(mpt: CoreDbMptRef) =
doAssert not mpt.isNil
doAssert not mpt.parent.isNil
mpt.methods.validateMethodsDesc
proc validateMethodsDesc(acc: CoreDxAccRef) =
proc validateMethodsDesc(acc: CoreDbAccRef) =
doAssert not acc.isNil
doAssert not acc.parent.isNil
acc.methods.validateMethodsDesc
proc validateMethodsDesc(phk: CoreDxPhkRef) =
doAssert not phk.isNil
doAssert not phk.toMpt.isNil
phk.methods.validateMethodsDesc
when false: # currently disabled
proc validateMethodsDesc(cpt: CoreDxCaptRef) =
proc validateMethodsDesc(cpt: CoreDbCaptRef) =
doAssert not cpt.isNil
doAssert not cpt.parent.isNil
doAssert not cpt.methods.recorderFn.isNil
doAssert not cpt.methods.getFlagsFn.isNil
doAssert not cpt.methods.forgetFn.isNil
proc validateMethodsDesc(tx: CoreDxTxRef) =
proc validateMethodsDesc(tx: CoreDbTxRef) =
doAssert not tx.isNil
doAssert not tx.parent.isNil
doAssert not tx.methods.levelFn.isNil

View File

@ -31,7 +31,7 @@ when CoreDbEnableApiTracking:
# Public iterators
# ------------------------------------------------------------------------------
iterator pairs*(kvt: CoreDxKvtRef): (Blob, Blob) {.apiRaise.} =
iterator pairs*(kvt: CoreDbKvtRef): (Blob, Blob) {.apiRaise.} =
## Iterator supported on memory DB (otherwise implementation dependent)
##
kvt.setTrackNewApi KvtPairsIt
@ -46,8 +46,8 @@ iterator pairs*(kvt: CoreDxKvtRef): (Blob, Blob) {.apiRaise.} =
raiseAssert: "Unsupported database type: " & $kvt.parent.dbType
kvt.ifTrackNewApi: debug newApiTxt, api, elapsed
iterator pairs*(mpt: CoreDxMptRef): (Blob, Blob) =
## Trie traversal, only supported for `CoreDxMptRef` (not `Phk`)
iterator pairs*(mpt: CoreDbMptRef): (Blob, Blob) =
## Trie traversal, only supported for `CoreDbMptRef`
##
mpt.setTrackNewApi MptPairsIt
case mpt.parent.dbType:
@ -60,8 +60,8 @@ iterator pairs*(mpt: CoreDxMptRef): (Blob, Blob) =
let trie = mpt.methods.getColFn()
debug newApiTxt, api, elapsed, trie
iterator replicate*(mpt: CoreDxMptRef): (Blob, Blob) {.apiRaise.} =
## Low level trie dump, only supported for `CoreDxMptRef` (not `Phk`)
iterator replicate*(mpt: CoreDbMptRef): (Blob, Blob) {.apiRaise.} =
## Low level trie dump, only supported for `CoreDbMptRef`
##
mpt.setTrackNewApi MptReplicateIt
case mpt.parent.dbType:

View File

@ -31,7 +31,7 @@ when CoreDbEnableApiTracking:
# Public iterators
# ------------------------------------------------------------------------------
iterator replicatePersistent*(mpt: CoreDxMptRef): (Blob, Blob) {.rlpRaise.} =
iterator replicatePersistent*(mpt: CoreDbMptRef): (Blob, Blob) {.rlpRaise.} =
## Extended version of `replicate()` for `Aristo` persistent backend.
##
mpt.setTrackNewApi MptReplicateIt

View File

@ -64,7 +64,7 @@ type
AccountsLedgerRef* = ref object
ledger: AccountLedger
kvt: CoreDxKvtRef
kvt: CoreDbKvtRef
savePoint: LedgerSavePoint
witnessCache: Table[EthAddress, WitnessData]
isDirty: bool
@ -412,7 +412,7 @@ proc getNonce*(ac: AccountsLedgerRef, address: EthAddress): AccountNonce =
if acc.isNil: emptyEthAccount.nonce
else: acc.statement.nonce
proc getCode(acc: AccountRef, kvt: CoreDxKvtRef): lent seq[byte] =
proc getCode(acc: AccountRef, kvt: CoreDbKvtRef): lent seq[byte] =
if CodeLoaded notin acc.flags and CodeChanged notin acc.flags:
if acc.statement.codeHash != EMPTY_CODE_HASH:
var rc = kvt.get(contractHashKey(acc.statement.codeHash).toOpenArray)

View File

@ -351,9 +351,9 @@ proc getAccessList*(ldg: LedgerRef): AccessList =
proc rootHash*(ldg: LedgerRef): KeccakHash =
ldg.state()
proc getMpt*(ldg: LedgerRef): CoreDxMptRef =
proc getMpt*(ldg: LedgerRef): CoreDbMptRef =
ldg.beginTrackApi LdgGetMptFn
result = ldg.ac.rawTrie.CoreDxAccRef.getMpt
result = ldg.ac.rawTrie.CoreDbAccRef.getMpt
ldg.ifTrackApi: debug apiTxt, api, elapsed, result
proc getEthAccount*(ldg: LedgerRef, eAddr: EthAddress): Account =

View File

@ -100,7 +100,7 @@ func toStr*(w: Hash256): string =
when declared(CoreDbMptRef):
func toStr*(w: CoreDbMptRef): string =
if w.CoreDxMptRef.isNil: "MptRef(nil)" else: "MptRef"
if w.CoreDbMptRef.isNil: "MptRef(nil)" else: "MptRef"
func toStr*(w: Blob): string =
if 0 < w.len and w.len < 5: "<" & w.oaToStr & ">"

View File

@ -32,8 +32,8 @@ import
".."/[core_db, storage_types]
type
AccountLedger* = distinct CoreDxAccRef
StorageLedger* = distinct CoreDxPhkRef
AccountLedger* = distinct CoreDbAccRef
StorageLedger* = distinct CoreDbMptRef
SomeLedger* = AccountLedger | StorageLedger
const
@ -56,7 +56,7 @@ proc toSvp*(sl: StorageLedger): seq[(UInt256,UInt256)] =
let kvt = db.newKvt
var kvp: Table[UInt256,UInt256]
try:
for (slotHash,val) in sl.distinctBase.toMpt.pairs:
for (slotHash,val) in sl.distinctBase.pairs:
let rc = kvt.get(slotHashToSlotKey(slotHash).toOpenArray)
if rc.isErr:
warn "StorageLedger.dump()", slotHash, error=($$rc.error)
@ -186,22 +186,22 @@ proc init*(
if rc.isErr:
raiseAssert info & $$rc.error
rc.value
mpt.toPhk.T
mpt.T
proc fetch*(sl: StorageLedger, slot: UInt256): Result[Blob,void] =
var rc = sl.distinctBase.fetch(slot.toBytesBE)
var rc = sl.distinctBase.fetch(slot.toBytesBE.keccakHash.data)
if rc.isErr:
return err()
ok move(rc.value)
proc merge*(sl: StorageLedger, slot: UInt256, value: openArray[byte]) =
const info = "StorageLedger/merge(): "
sl.distinctBase.merge(slot.toBytesBE, value).isOkOr:
sl.distinctBase.merge(slot.toBytesBE.keccakHash.data, value).isOkOr:
raiseAssert info & $$error
proc delete*(sl: StorageLedger, slot: UInt256) =
const info = "StorageLedger/delete(): "
sl.distinctBase.delete(slot.toBytesBE).isOkOr:
sl.distinctBase.delete(slot.toBytesBE.keccakHash.data).isOkOr:
if error.error == MptNotFound:
return
raiseAssert info & $$error

View File

@ -8,8 +8,8 @@
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
# This implementation of getBranch on the CoreDbPhkRef type is a temporary solution
# which can be removed once we get an equivient proc defined on the CoreDbPhkRef type
# This implementation of getBranch on the CoreDbMptRef type is a temporary solution
# which can be removed once we get an equivient proc defined on the CoreDbMptRef type
# in the db layer.
{.push raises: [].}
@ -88,7 +88,12 @@ proc getBranchAux(
raise newException(RlpError, "node has an unexpected number of children")
proc getBranch*(
self: CoreDxPhkRef;
# self: CoreDxPhkRef;
# Note that PHK type has been removed. The difference PHK an MPT was that
# the keys of the PHK were pre-hased (as in the legacy `SecureHexaryTrie`
# object.) Can this code have worked at all? Looking at the `keyHash`
# below it would mean the `key` was double hashed? -- j
self: CoreDbMptRef;
key: openArray[byte]): seq[seq[byte]] {.raises: [RlpError].} =
let
keyHash = keccakHash(key).data

View File

@ -104,7 +104,7 @@ proc dumpMemoryDB*(node: JsonNode, kvt: TableRef[common.Blob, common.Blob]) =
n[k.toHex(false)] = %v
node["state"] = n
proc dumpMemoryDB*(node: JsonNode, capture: CoreDxCaptRef) =
proc dumpMemoryDB*(node: JsonNode, capture: CoreDbCaptRef) =
node.dumpMemoryDB capture.logDb
const

View File

@ -40,7 +40,7 @@ else:
template persistToDb(db: CoreDbRef, body: untyped) =
block: body
proc contains(kvt: CoreDxKvtRef; key: openArray[byte]): bool =
proc contains(kvt: CoreDbKvtRef; key: openArray[byte]): bool =
kvt.hasKey(key).expect "valid bool"
proc main() {.used.} =