2018-06-24 15:39:03 +00:00
|
|
|
import
|
2018-07-26 20:08:43 +00:00
|
|
|
unittest, macros, os,
|
2018-07-05 12:41:01 +00:00
|
|
|
nimcrypto/[keccak, hash], ranges, eth_common/eth_types,
|
2018-06-24 15:39:03 +00:00
|
|
|
../nimbus/db/[storage_types],
|
|
|
|
../nimbus/db/backends/[sqlite_backend, rocksdb_backend]
|
|
|
|
|
|
|
|
template dummyInstance(T: type SqliteChainDB): auto =
|
2018-07-26 20:08:43 +00:00
|
|
|
sqlite_backend.newChainDB ":memory:"
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
template dummyInstance(T: type RocksChainDB): auto =
|
2018-07-26 20:08:43 +00:00
|
|
|
let tmp = getTempDir() / "nimbus-test-db"
|
|
|
|
removeDir(tmp)
|
|
|
|
rocksdb_backend.newChainDB(tmp)
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
template backendTests(DB) =
|
|
|
|
suite("storage tests: " & astToStr(DB)):
|
|
|
|
setup:
|
|
|
|
var db = dummyInstance(DB)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
close(db)
|
|
|
|
|
|
|
|
test "basic insertions and deletions":
|
2018-07-26 20:08:43 +00:00
|
|
|
var keyA = @(genericHashKey(keccak256.digest("A")).toOpenArray)
|
|
|
|
var keyB = @(blockNumberToHashKey(100.toBlockNumber).toOpenArray)
|
|
|
|
var value1 = @[1.byte, 2, 3, 4, 5]
|
|
|
|
var value2 = @[7.byte, 8, 9, 10]
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
db.put(keyA, value1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
keyA in db
|
|
|
|
keyB notin db
|
|
|
|
|
|
|
|
db.put(keyB, value2)
|
|
|
|
|
|
|
|
check:
|
|
|
|
keyA in db
|
|
|
|
keyB in db
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.get(keyA) == value1
|
|
|
|
db.get(keyB) == value2
|
|
|
|
|
|
|
|
db.del(keyA)
|
|
|
|
db.put(keyB, value1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
keyA notin db
|
|
|
|
keyB in db
|
|
|
|
|
|
|
|
check db.get(keyB) == value1
|
|
|
|
db.del(keyA)
|
|
|
|
|
|
|
|
backendTests(RocksChainDB)
|
|
|
|
backendTests(SqliteChainDB)
|
|
|
|
|