2023-07-31 13:43:38 +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
|
|
|
|
std/options,
|
|
|
|
eth/[common, rlp, trie/db, trie/hexary],
|
|
|
|
./base
|
|
|
|
|
|
|
|
type
|
2023-08-04 11:10:09 +00:00
|
|
|
LegacyDbRef* = ref object of CoreDbRef
|
2023-08-02 20:46:41 +00:00
|
|
|
tdb: TrieDatabaseRef # copy of descriptor reference captured with closures
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
HexaryTrieRef = ref object
|
|
|
|
trie: HexaryTrie # nedded for decriptor capturing with closures
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
RecorderRef = ref object of RootRef
|
|
|
|
flags: set[CoreDbCaptFlags]
|
|
|
|
parent: TrieDatabaseRef
|
2023-07-31 13:43:38 +00:00
|
|
|
recorder: TrieDatabaseRef
|
2023-08-02 20:46:41 +00:00
|
|
|
appDb: CoreDbRef
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
# Annotation helpers
|
|
|
|
{.pragma: noRaise, gcsafe, raises: [].}
|
|
|
|
{.pragma: rlpRaise, gcsafe, raises: [RlpError].}
|
|
|
|
{.pragma: catchRaise, gcsafe, raises: [CatchableError].}
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc init*(
|
|
|
|
db: LegacyDbRef;
|
2023-08-02 20:46:41 +00:00
|
|
|
dbType: CoreDbType;
|
|
|
|
tdb: TrieDatabaseRef;
|
2023-09-18 20:20:28 +00:00
|
|
|
): CoreDbRef
|
|
|
|
{.noRaise.}
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-02 20:46:41 +00:00
|
|
|
# Private helpers
|
2023-07-31 13:43:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
template ifLegacyOk(db: CoreDbRef; body: untyped) =
|
|
|
|
case db.dbType:
|
|
|
|
of LegacyDbMemory, LegacyDbPersistent:
|
|
|
|
body
|
|
|
|
else:
|
|
|
|
discard
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-02 20:46:41 +00:00
|
|
|
# Private mixin methods for `trieDB` (backport from capturedb/tracer sources)
|
2023-07-31 13:43:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc get(db: RecorderRef, key: openArray[byte]): Blob =
|
2023-07-31 13:43:38 +00:00
|
|
|
## Mixin for `trieDB()`
|
|
|
|
result = db.recorder.get(key)
|
2023-08-02 20:46:41 +00:00
|
|
|
if result.len == 0:
|
|
|
|
result = db.parent.get(key)
|
|
|
|
if result.len != 0:
|
|
|
|
db.recorder.put(key, result)
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc put(db: RecorderRef, key, value: openArray[byte]) =
|
2023-07-31 13:43:38 +00:00
|
|
|
## Mixin for `trieDB()`
|
|
|
|
db.recorder.put(key, value)
|
|
|
|
if PersistPut in db.flags:
|
2023-08-02 20:46:41 +00:00
|
|
|
db.parent.put(key, value)
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc contains(db: RecorderRef, key: openArray[byte]): bool =
|
2023-07-31 13:43:38 +00:00
|
|
|
## Mixin for `trieDB()`
|
2023-08-02 20:46:41 +00:00
|
|
|
result = db.parent.contains(key)
|
2023-07-31 13:43:38 +00:00
|
|
|
doAssert(db.recorder.contains(key) == result)
|
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc del(db: RecorderRef, key: openArray[byte]) =
|
2023-07-31 13:43:38 +00:00
|
|
|
## Mixin for `trieDB()`
|
|
|
|
db.recorder.del(key)
|
|
|
|
if PersistDel in db.flags:
|
2023-08-02 20:46:41 +00:00
|
|
|
db.parent.del(key)
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc newRecorderRef(
|
|
|
|
tdb: TrieDatabaseRef;
|
2023-07-31 13:43:38 +00:00
|
|
|
flags: set[CoreDbCaptFlags] = {};
|
2023-08-02 20:46:41 +00:00
|
|
|
): RecorderRef =
|
|
|
|
## Capture constuctor, uses `mixin` values from above
|
|
|
|
result = RecorderRef(
|
|
|
|
flags: flags,
|
|
|
|
parent: tdb,
|
|
|
|
recorder: newMemoryDB())
|
2023-08-04 11:10:09 +00:00
|
|
|
result.appDb = LegacyDbRef().init(LegacyDbPersistent, trieDB result)
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-02 20:46:41 +00:00
|
|
|
# Private database method function tables
|
2023-07-31 13:43:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc miscMethods(tdb: TrieDatabaseRef): CoreDbMiscFns =
|
|
|
|
CoreDbMiscFns(
|
|
|
|
legacySetupFn: proc() =
|
|
|
|
tdb.put(EMPTY_ROOT_HASH.data, @[0x80u8]))
|
|
|
|
|
|
|
|
proc kvtMethods(tdb: TrieDatabaseRef): CoreDbKvtFns =
|
|
|
|
## Key-value database table handlers
|
|
|
|
CoreDbKvtFns(
|
2023-09-18 20:20:28 +00:00
|
|
|
getFn: proc(k: openArray[byte]): Blob = tdb.get(k),
|
|
|
|
maybeGetFn: proc(k: openArray[byte]): Option[Blob] = tdb.maybeGet(k),
|
|
|
|
delFn: proc(k: openArray[byte]) = tdb.del(k),
|
|
|
|
putFn: proc(k: openArray[byte]; v: openArray[byte]) = tdb.put(k,v),
|
|
|
|
containsFn: proc(k: openArray[byte]): bool = tdb.contains(k),
|
|
|
|
pairsIt: iterator(): (Blob, Blob) {.noRaise.} =
|
|
|
|
for k,v in tdb.pairsInMemoryDB:
|
|
|
|
yield (k,v))
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
proc mptMethods(mpt: HexaryTrieRef): CoreDbMptFns =
|
|
|
|
## Hexary trie database handlers
|
|
|
|
CoreDbMptFns(
|
2023-09-18 20:20:28 +00:00
|
|
|
getFn: proc(k: openArray[byte]): Blob {.rlpRaise.} =
|
|
|
|
mpt.trie.get(k),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
maybeGetFn: proc(k: openArray[byte]): Option[Blob] {.rlpRaise.} =
|
|
|
|
mpt.trie.maybeGet(k),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
delFn: proc(k: openArray[byte]) {.rlpRaise.} =
|
2023-08-02 20:46:41 +00:00
|
|
|
mpt.trie.del(k),
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
putFn: proc(k: openArray[byte]; v: openArray[byte]) {.rlpRaise.} =
|
2023-08-02 20:46:41 +00:00
|
|
|
mpt.trie.put(k,v),
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
containsFn: proc(k: openArray[byte]): bool {.rlpRaise.} =
|
|
|
|
mpt.trie.contains(k),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
rootHashFn: proc(): Hash256 =
|
2023-09-18 20:20:28 +00:00
|
|
|
mpt.trie.rootHash,
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
isPruningFn: proc(): bool =
|
2023-09-18 20:20:28 +00:00
|
|
|
mpt.trie.isPruning,
|
2023-08-02 20:46:41 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
pairsIt: iterator(): (Blob, Blob) {.rlpRaise.} =
|
2023-08-02 20:46:41 +00:00
|
|
|
for k,v in mpt.trie.pairs():
|
|
|
|
yield (k,v),
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
replicateIt: iterator(): (Blob, Blob) {.rlpRaise.} =
|
2023-08-02 20:46:41 +00:00
|
|
|
for k,v in mpt.trie.replicate():
|
|
|
|
yield (k,v))
|
|
|
|
|
|
|
|
proc txMethods(tx: DbTransaction): CoreDbTxFns =
|
|
|
|
CoreDbTxFns(
|
|
|
|
commitFn: proc(applyDeletes: bool) = tx.commit(applyDeletes),
|
|
|
|
rollbackFn: proc() = tx.rollback(),
|
|
|
|
disposeFn: proc() = tx.dispose(),
|
|
|
|
safeDisposeFn: proc() = tx.safeDispose())
|
|
|
|
|
|
|
|
proc tidMethods(tid: TransactionID; tdb: TrieDatabaseRef): CoreDbTxIdFns =
|
|
|
|
CoreDbTxIdFns(
|
|
|
|
setIdFn: proc() =
|
|
|
|
tdb.setTransactionID(tid),
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
roWrapperFn: proc(action: CoreDbTxIdActionFn) {.catchRaise.} =
|
2023-08-02 20:46:41 +00:00
|
|
|
tdb.shortTimeReadOnly(tid, action()))
|
|
|
|
|
|
|
|
proc cptMethods(cpt: RecorderRef): CoreDbCaptFns =
|
|
|
|
CoreDbCaptFns(
|
|
|
|
recorderFn: proc(): CoreDbRef =
|
2023-09-18 20:20:28 +00:00
|
|
|
cpt.appDb,
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
getFlagsFn: proc(): set[CoreDbCaptFlags] =
|
2023-09-18 20:20:28 +00:00
|
|
|
cpt.flags)
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-02 20:46:41 +00:00
|
|
|
# Private constructor functions table
|
2023-07-31 13:43:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc constructors(tdb: TrieDatabaseRef, parent: CoreDbRef): CoreDbConstructors =
|
|
|
|
CoreDbConstructors(
|
|
|
|
mptFn: proc(root: Hash256): CoreDbMptRef =
|
|
|
|
let mpt = HexaryTrieRef(trie: initHexaryTrie(tdb, root, false))
|
2023-09-18 20:20:28 +00:00
|
|
|
newCoreDbMptRef(parent, mpt.mptMethods),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
legacyMptFn: proc(root: Hash256; prune: bool): CoreDbMptRef =
|
|
|
|
let mpt = HexaryTrieRef(trie: initHexaryTrie(tdb, root, prune))
|
2023-09-18 20:20:28 +00:00
|
|
|
newCoreDbMptRef(parent, mpt.mptMethods),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
getIdFn: proc(): CoreDbTxID =
|
2023-09-18 20:20:28 +00:00
|
|
|
newCoreDbTxID(parent, tdb.getTransactionID.tidMethods tdb),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
beginFn: proc(): CoreDbTxRef =
|
2023-09-18 20:20:28 +00:00
|
|
|
newCoreDbTxRef(parent, tdb.beginTransaction.txMethods),
|
2023-08-02 20:46:41 +00:00
|
|
|
|
|
|
|
captureFn: proc(flags: set[CoreDbCaptFlags] = {}): CoreDbCaptRef =
|
2023-09-18 20:20:28 +00:00
|
|
|
newCoreDbCaptRef(parent, newRecorderRef(tdb, flags).cptMethods))
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-04 11:10:09 +00:00
|
|
|
# Public constructor helpers
|
2023-07-31 13:43:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc init*(
|
|
|
|
db: LegacyDbRef;
|
2023-08-02 20:46:41 +00:00
|
|
|
dbType: CoreDbType;
|
|
|
|
tdb: TrieDatabaseRef;
|
|
|
|
): CoreDbRef =
|
2023-08-04 11:10:09 +00:00
|
|
|
db.tdb = tdb
|
|
|
|
db.init(
|
2023-08-02 20:46:41 +00:00
|
|
|
dbType = dbType,
|
|
|
|
dbMethods = tdb.miscMethods,
|
|
|
|
kvtMethods = tdb.kvtMethods,
|
2023-08-04 11:10:09 +00:00
|
|
|
new = tdb.constructors db)
|
2023-09-18 20:20:28 +00:00
|
|
|
db
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-02 20:46:41 +00:00
|
|
|
# Public constructor and low level data retrieval, storage & transation frame
|
2023-07-31 13:43:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc newLegacyPersistentCoreDbRef*(db: TrieDatabaseRef): CoreDbRef =
|
2023-08-04 11:10:09 +00:00
|
|
|
LegacyDbRef().init(LegacyDbPersistent, db)
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc newLegacyMemoryCoreDbRef*(): CoreDbRef =
|
2023-08-04 11:10:09 +00:00
|
|
|
LegacyDbRef().init(LegacyDbMemory, newMemoryDB())
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public legacy helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-07-31 13:43:38 +00:00
|
|
|
|
2023-08-02 20:46:41 +00:00
|
|
|
proc toLegacyTrieRef*(db: CoreDbRef): TrieDatabaseRef =
|
|
|
|
db.ifLegacyOk:
|
|
|
|
return db.LegacyDbRef.tdb
|
2023-07-31 13:43:38 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|