2021-05-11 15:32:47 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2019-02-05 12:01:10 +00:00
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
std/[tables, hashes, sets],
|
2020-04-20 18:14:39 +00:00
|
|
|
nimcrypto/[hash, keccak],
|
2021-04-06 11:33:24 +00:00
|
|
|
"."/[trie_defs, db_tracing]
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
MemDBRec = object
|
|
|
|
refCount: int
|
2020-04-20 18:14:39 +00:00
|
|
|
value: seq[byte]
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
MemoryLayer* = ref object of RootObj
|
2020-04-20 18:14:39 +00:00
|
|
|
records: Table[seq[byte], MemDBRec]
|
|
|
|
deleted: HashSet[seq[byte]]
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
# XXX: poor's man vtref types
|
2021-12-20 12:14:50 +00:00
|
|
|
PutProc = proc (db: RootRef, key, val: openArray[byte]) {.
|
2021-05-11 15:32:47 +00:00
|
|
|
gcsafe, raises: [Defect].}
|
2020-02-17 22:47:13 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
GetProc = proc (db: RootRef, key: openArray[byte]): seq[byte] {.
|
2021-05-11 15:32:47 +00:00
|
|
|
gcsafe, raises: [Defect].}
|
2020-02-17 22:47:13 +00:00
|
|
|
## The result will be empty seq if not found
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
DelProc = proc (db: RootRef, key: openArray[byte]) {.
|
2021-05-11 15:32:47 +00:00
|
|
|
gcsafe, raises: [Defect].}
|
2020-02-17 22:47:13 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
ContainsProc = proc (db: RootRef, key: openArray[byte]): bool {.
|
2021-05-11 15:32:47 +00:00
|
|
|
gcsafe, raises: [Defect].}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
TrieDatabaseRef* = ref object
|
|
|
|
obj: RootRef
|
|
|
|
putProc: PutProc
|
|
|
|
getProc: GetProc
|
|
|
|
delProc: DelProc
|
|
|
|
containsProc: ContainsProc
|
|
|
|
mostInnerTransaction: DbTransaction
|
|
|
|
|
2019-02-18 09:12:01 +00:00
|
|
|
TransactionState = enum
|
|
|
|
Pending
|
2019-02-18 04:02:26 +00:00
|
|
|
Committed
|
|
|
|
RolledBack
|
|
|
|
|
2019-02-05 12:01:10 +00:00
|
|
|
DbTransaction* = ref object
|
|
|
|
db: TrieDatabaseRef
|
|
|
|
parentTransaction: DbTransaction
|
|
|
|
modifications: MemoryLayer
|
2019-02-18 09:12:01 +00:00
|
|
|
state: TransactionState
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-11-28 10:34:38 +00:00
|
|
|
TransactionID* = distinct DbTransaction
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc put*(db: TrieDatabaseRef, key, val: openArray[byte]) {.gcsafe.}
|
|
|
|
proc get*(db: TrieDatabaseRef, key: openArray[byte]): seq[byte] {.gcsafe.}
|
|
|
|
proc del*(db: TrieDatabaseRef, key: openArray[byte]) {.gcsafe.}
|
2019-02-05 12:01:10 +00:00
|
|
|
proc beginTransaction*(db: TrieDatabaseRef): DbTransaction {.gcsafe.}
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc keccak*(r: openArray[byte]): KeccakHash =
|
|
|
|
keccak256.digest r
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc get*(db: MemoryLayer, key: openArray[byte]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
result = db.records.getOrDefault(@key).value
|
|
|
|
traceGet key, result
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc del*(db: MemoryLayer, key: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
traceDel key
|
|
|
|
|
|
|
|
# The database should ensure that the empty key is always active:
|
|
|
|
if key != emptyRlpHash.data:
|
|
|
|
# TODO: This is quite inefficient and it won't be necessary once
|
|
|
|
# https://github.com/nim-lang/Nim/issues/7457 is developed.
|
|
|
|
let key = @key
|
|
|
|
|
|
|
|
db.records.withValue(key, v):
|
|
|
|
dec v.refCount
|
|
|
|
if v.refCount <= 0:
|
|
|
|
db.records.del(key)
|
|
|
|
db.deleted.incl(key)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc contains*(db: MemoryLayer, key: openArray[byte]): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
db.records.hasKey(@key)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc put*(db: MemoryLayer, key, val: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
tracePut key, val
|
|
|
|
|
|
|
|
# TODO: This is quite inefficient and it won't be necessary once
|
|
|
|
# https://github.com/nim-lang/Nim/issues/7457 is developed.
|
|
|
|
let key = @key
|
|
|
|
|
|
|
|
db.deleted.excl(key)
|
|
|
|
|
|
|
|
if key.len != 32:
|
|
|
|
# This is not a Trie key, but a regular system mutable key
|
|
|
|
# (e.g. the cannonical head hash). We don't need to ref count such keys.
|
|
|
|
db.records[key] = MemDBRec(refCount: 1, value: @val)
|
|
|
|
else:
|
|
|
|
db.records.withValue(key, v) do:
|
|
|
|
inc v.refCount
|
|
|
|
if v.value != val: v.value = @val
|
|
|
|
do:
|
|
|
|
db.records[key] = MemDBRec(refCount: 1, value: @val)
|
|
|
|
|
|
|
|
proc newMemoryLayer: MemoryLayer =
|
|
|
|
result.new
|
2020-04-20 18:14:39 +00:00
|
|
|
result.records = initTable[seq[byte], MemDBRec]()
|
|
|
|
result.deleted = initHashSet[seq[byte]]()
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-11-28 10:34:38 +00:00
|
|
|
proc commit(memDb: MemoryLayer, db: TrieDatabaseRef, applyDeletes: bool = true) =
|
|
|
|
if applyDeletes:
|
|
|
|
for k in memDb.deleted:
|
|
|
|
db.del(k)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
for k, v in memDb.records:
|
|
|
|
db.put(k, v.value)
|
|
|
|
|
|
|
|
proc init(db: var MemoryLayer) =
|
|
|
|
db = newMemoryLayer()
|
|
|
|
|
|
|
|
proc newMemoryDB*: TrieDatabaseRef =
|
|
|
|
new result
|
|
|
|
discard result.beginTransaction
|
|
|
|
put(result, emptyRlpHash.data, emptyRlp)
|
|
|
|
|
|
|
|
template isMemoryDB(db: TrieDatabaseRef): bool =
|
|
|
|
# Make sure this is really a MemoryDB
|
|
|
|
db.obj == nil and
|
|
|
|
db.mostInnerTransaction != nil and
|
|
|
|
db.mostInnerTransaction.parentTransaction == nil
|
|
|
|
|
|
|
|
proc totalRecordsInMemoryDB*(db: TrieDatabaseRef): int =
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert isMemoryDB(db)
|
2019-02-05 12:01:10 +00:00
|
|
|
return db.mostInnerTransaction.modifications.records.len
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
iterator pairsInMemoryDB*(db: TrieDatabaseRef): (seq[byte], seq[byte]) =
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert isMemoryDB(db)
|
2019-02-05 12:01:10 +00:00
|
|
|
for k, v in db.mostInnerTransaction.modifications.records:
|
|
|
|
yield (k, v.value)
|
|
|
|
|
|
|
|
proc beginTransaction*(db: TrieDatabaseRef): DbTransaction =
|
|
|
|
new result
|
|
|
|
result.db = db
|
|
|
|
init result.modifications
|
2019-02-18 09:12:01 +00:00
|
|
|
result.state = Pending
|
2019-02-05 12:01:10 +00:00
|
|
|
result.parentTransaction = db.mostInnerTransaction
|
|
|
|
db.mostInnerTransaction = result
|
|
|
|
|
|
|
|
proc rollback*(t: DbTransaction) =
|
|
|
|
# Transactions should be handled in a strictly nested fashion.
|
|
|
|
# Any child transaction must be committed or rolled-back before
|
|
|
|
# its parent transactions:
|
2019-02-18 09:12:01 +00:00
|
|
|
doAssert t.db.mostInnerTransaction == t and t.state == Pending
|
2019-02-05 12:01:10 +00:00
|
|
|
t.db.mostInnerTransaction = t.parentTransaction
|
2019-02-18 09:12:01 +00:00
|
|
|
t.state = RolledBack
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-11-28 10:34:38 +00:00
|
|
|
proc commit*(t: DbTransaction, applyDeletes: bool = true) =
|
2019-02-05 12:01:10 +00:00
|
|
|
# Transactions should be handled in a strictly nested fashion.
|
|
|
|
# Any child transaction must be committed or rolled-back before
|
|
|
|
# its parent transactions:
|
2019-02-18 09:12:01 +00:00
|
|
|
doAssert t.db.mostInnerTransaction == t and t.state == Pending
|
2019-02-05 12:01:10 +00:00
|
|
|
t.db.mostInnerTransaction = t.parentTransaction
|
2019-11-28 10:34:38 +00:00
|
|
|
t.modifications.commit(t.db, applyDeletes)
|
2019-02-18 09:12:01 +00:00
|
|
|
t.state = Committed
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc dispose*(t: DbTransaction) {.inline.} =
|
2019-02-18 09:12:01 +00:00
|
|
|
if t.state == Pending:
|
2019-02-05 12:01:10 +00:00
|
|
|
t.rollback()
|
|
|
|
|
|
|
|
proc safeDispose*(t: DbTransaction) {.inline.} =
|
2019-02-18 09:48:26 +00:00
|
|
|
if (not isNil(t)) and (t.state == Pending):
|
2019-02-05 12:01:10 +00:00
|
|
|
t.rollback()
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc putImpl[T](db: RootRef, key, val: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
mixin put
|
|
|
|
put(T(db), key, val)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc getImpl[T](db: RootRef, key: openArray[byte]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
mixin get
|
|
|
|
return get(T(db), key)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc delImpl[T](db: RootRef, key: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
mixin del
|
|
|
|
del(T(db), key)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc containsImpl[T](db: RootRef, key: openArray[byte]): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
mixin contains
|
|
|
|
return contains(T(db), key)
|
|
|
|
|
|
|
|
proc trieDB*[T: RootRef](x: T): TrieDatabaseRef =
|
|
|
|
mixin del, get, put
|
|
|
|
|
|
|
|
new result
|
|
|
|
result.obj = x
|
|
|
|
result.putProc = putImpl[T]
|
|
|
|
result.getProc = getImpl[T]
|
|
|
|
result.delProc = delImpl[T]
|
|
|
|
result.containsProc = containsImpl[T]
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc put*(db: TrieDatabaseRef, key, val: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
var t = db.mostInnerTransaction
|
|
|
|
if t != nil:
|
|
|
|
t.modifications.put(key, val)
|
|
|
|
else:
|
|
|
|
db.putProc(db.obj, key, val)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc get*(db: TrieDatabaseRef, key: openArray[byte]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
# TODO: This is quite inefficient and it won't be necessary once
|
|
|
|
# https://github.com/nim-lang/Nim/issues/7457 is developed.
|
|
|
|
let key = @key
|
|
|
|
|
|
|
|
var t = db.mostInnerTransaction
|
|
|
|
while t != nil:
|
|
|
|
result = t.modifications.records.getOrDefault(key).value
|
|
|
|
if result.len > 0 or key in t.modifications.deleted:
|
|
|
|
return
|
|
|
|
t = t.parentTransaction
|
|
|
|
|
|
|
|
if db.getProc != nil:
|
|
|
|
result = db.getProc(db.obj, key)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc del*(db: TrieDatabaseRef, key: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
var t = db.mostInnerTransaction
|
|
|
|
if t != nil:
|
|
|
|
t.modifications.del(key)
|
|
|
|
else:
|
|
|
|
db.delProc(db.obj, key)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc contains*(db: TrieDatabaseRef, key: openArray[byte]): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
# TODO: This is quite inefficient and it won't be necessary once
|
|
|
|
# https://github.com/nim-lang/Nim/issues/7457 is developed.
|
|
|
|
let key = @key
|
|
|
|
|
|
|
|
var t = db.mostInnerTransaction
|
|
|
|
while t != nil:
|
|
|
|
result = key in t.modifications.records
|
|
|
|
if result or key in t.modifications.deleted:
|
|
|
|
return
|
|
|
|
t = t.parentTransaction
|
|
|
|
|
|
|
|
if db.containsProc != nil:
|
|
|
|
result = db.containsProc(db.obj, key)
|
|
|
|
|
2019-11-28 10:34:38 +00:00
|
|
|
# TransactionID imitate subset of JournalDB behaviour
|
|
|
|
# but there is no need to rollback or dispose
|
|
|
|
# TransactionID, because it will be handled elsewhere
|
|
|
|
# this is useful when we need to jump back to specific point
|
|
|
|
# in history where the database still in 'original' state
|
|
|
|
# and retrieve data from that point
|
|
|
|
proc getTransactionID*(db: TrieDatabaseRef): TransactionID =
|
|
|
|
TransactionID(db.mostInnerTransaction)
|
|
|
|
|
|
|
|
proc setTransactionID*(db: TrieDatabaseRef, id: TransactionID) =
|
|
|
|
db.mostInnerTransaction = DbTransaction(id)
|
|
|
|
|
|
|
|
template shortTimeReadOnly*(db: TrieDatabaseRef, id: TransactionID, body: untyped) =
|
|
|
|
# hmm, how can we prevent unwanted database modification
|
|
|
|
# inside this block?
|
|
|
|
block:
|
|
|
|
let tmpID = db.getTransactionID()
|
|
|
|
db.setTransactionID(id)
|
|
|
|
body
|
|
|
|
db.setTransactionID(tmpID)
|