nimbus-eth1/nimbus/db/backends/caching_backend.nim
Zahary Karadjov 343cc4fa43 Populate the persistent databases with the empty RLP key.
Also implements transactional block persistence. Two issues
in the transaction processing code have been discovered that
might affect other usages such as the CALL instruction.

The main fix gets us past block 49000.

You may need to clean up your database.
2018-10-05 03:36:48 +03:00

47 lines
1.1 KiB
Nim

import
ranges, tables, sets,
eth_trie/db,
../storage_types
type
CachingDB* = ref object of RootObj
backing: TrieDatabaseRef
changed: Table[seq[byte], seq[byte]]
deleted: HashSet[seq[byte]]
proc newCachingDB*(backing: TrieDatabaseRef): CachingDB =
result.new()
result.backing = backing
result.changed = initTable[seq[byte], seq[byte]]()
result.deleted = initSet[seq[byte]]()
proc get*(db: CachingDB, key: openarray[byte]): seq[byte] =
let key = @key
result = db.changed.getOrDefault(key)
if result.len == 0 and key notin db.deleted:
result = db.backing.get(key)
proc put*(db: CachingDB, key, value: openarray[byte]) =
let key = @key
db.deleted.excl(key)
db.changed[key] = @value
proc contains*(db: CachingDB, key: openarray[byte]): bool =
let key = @key
result = key in db.changed
if not result and key notin db.deleted:
result = db.backing.contains(key)
proc del*(db: CachingDB, key: openarray[byte]) =
let key = @key
db.changed.del(key)
db.deleted.incl(key)
proc commit*(db: CachingDB) =
for k in db.deleted:
db.backing.del(k)
for k, v in db.changed:
db.backing.put(k, v)