2021-09-28 17:58:41 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
unittest2, stint,
|
2022-04-03 13:14:44 +00:00
|
|
|
eth/keys,
|
2021-09-28 17:58:41 +00:00
|
|
|
../network/state/state_content,
|
2022-05-12 16:04:37 +00:00
|
|
|
../content_db,
|
|
|
|
./test_helpers
|
2022-03-08 13:49:41 +00:00
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
proc generateNRandomU256(rng: var HmacDrbgContext, n: int): seq[UInt256] =
|
2022-04-03 13:14:44 +00:00
|
|
|
var i = 0
|
2022-09-10 19:00:27 +00:00
|
|
|
var res = newSeq[UInt256]()
|
2022-04-03 13:14:44 +00:00
|
|
|
while i < n:
|
2022-07-04 07:38:02 +00:00
|
|
|
let bytes = rng.generateBytes(32)
|
2022-09-10 19:00:27 +00:00
|
|
|
let num = UInt256.fromBytesBE(bytes)
|
2022-04-03 13:14:44 +00:00
|
|
|
res.add(num)
|
|
|
|
inc i
|
|
|
|
return res
|
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
suite "Content Database":
|
2022-04-03 13:14:44 +00:00
|
|
|
let rng = newRng()
|
2022-05-09 15:18:57 +00:00
|
|
|
let testId = u256(0)
|
2021-09-28 17:58:41 +00:00
|
|
|
# Note: We are currently not really testing something new here just basic
|
|
|
|
# underlying kvstore.
|
|
|
|
test "ContentDB basic API":
|
|
|
|
let
|
2022-05-09 15:18:57 +00:00
|
|
|
db = ContentDB.new("", uint32.high, inMemory = true)
|
2021-09-28 17:58:41 +00:00
|
|
|
key = ContentId(UInt256.high()) # Some key
|
|
|
|
|
|
|
|
block:
|
|
|
|
let val = db.get(key)
|
|
|
|
|
|
|
|
check:
|
|
|
|
val.isNone()
|
|
|
|
db.contains(key) == false
|
|
|
|
|
|
|
|
block:
|
2022-05-09 15:18:57 +00:00
|
|
|
discard db.put(key, [byte 0, 1, 2, 3], testId)
|
2021-09-28 17:58:41 +00:00
|
|
|
let val = db.get(key)
|
|
|
|
|
|
|
|
check:
|
|
|
|
val.isSome()
|
|
|
|
val.get() == [byte 0, 1, 2, 3]
|
|
|
|
db.contains(key) == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
db.del(key)
|
|
|
|
let val = db.get(key)
|
|
|
|
|
|
|
|
check:
|
|
|
|
val.isNone()
|
|
|
|
db.contains(key) == false
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
test "ContentDB size":
|
|
|
|
let
|
2022-05-09 15:18:57 +00:00
|
|
|
db = ContentDB.new("", uint32.high, inMemory = true)
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
let numBytes = 10000
|
|
|
|
let size1 = db.size()
|
2022-05-09 15:18:57 +00:00
|
|
|
discard db.put(u256(1), genByteSeq(numBytes), testId)
|
2022-03-08 13:49:41 +00:00
|
|
|
let size2 = db.size()
|
2022-05-09 15:18:57 +00:00
|
|
|
discard db.put(u256(2), genByteSeq(numBytes), testId)
|
2022-03-08 13:49:41 +00:00
|
|
|
let size3 = db.size()
|
2022-05-09 15:18:57 +00:00
|
|
|
discard db.put(u256(2), genByteSeq(numBytes), testId)
|
2022-03-08 13:49:41 +00:00
|
|
|
let size4 = db.size()
|
2022-05-26 06:26:08 +00:00
|
|
|
let realSize = db.realSize()
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
size2 > size1
|
|
|
|
size3 > size2
|
|
|
|
size3 == size4
|
2022-05-26 06:26:08 +00:00
|
|
|
realSize == size4
|
2022-03-08 13:49:41 +00:00
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
db.del(u256(2))
|
|
|
|
db.del(u256(1))
|
2022-09-09 11:12:09 +00:00
|
|
|
|
2022-05-26 06:26:08 +00:00
|
|
|
let realSize1 = db.realSize()
|
2022-03-08 13:49:41 +00:00
|
|
|
let size5 = db.size()
|
2022-09-09 11:12:09 +00:00
|
|
|
|
2022-03-08 13:49:41 +00:00
|
|
|
check:
|
|
|
|
size4 == size5
|
2022-05-26 06:26:08 +00:00
|
|
|
# real size will be smaller as after del, there are free pages in sqlite
|
|
|
|
# which can be re-used for further additions
|
|
|
|
realSize1 < size5
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
db.reclaimSpace()
|
|
|
|
|
|
|
|
let size6 = db.size()
|
2022-05-26 06:26:08 +00:00
|
|
|
let realSize2 = db.realSize()
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# After space reclamation size of db should be equal to initial size
|
|
|
|
size6 == size1
|
2022-05-26 06:26:08 +00:00
|
|
|
realSize2 == size6
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
test "ContentDB pruning":
|
|
|
|
let
|
|
|
|
maxDbSize = uint32(100000)
|
|
|
|
db = ContentDB.new("", maxDbSize, inMemory = true)
|
|
|
|
|
|
|
|
let furthestElement = u256(40)
|
|
|
|
let secondFurthest = u256(30)
|
|
|
|
let thirdFurthest = u256(20)
|
|
|
|
|
|
|
|
|
|
|
|
let numBytes = 10000
|
|
|
|
let pr1 = db.put(u256(1), genByteSeq(numBytes), u256(0))
|
|
|
|
let pr2 = db.put(thirdFurthest, genByteSeq(numBytes), u256(0))
|
|
|
|
let pr3 = db.put(u256(3), genByteSeq(numBytes), u256(0))
|
|
|
|
let pr4 = db.put(u256(10), genByteSeq(numBytes), u256(0))
|
|
|
|
let pr5 = db.put(u256(5), genByteSeq(numBytes), u256(0))
|
|
|
|
let pr6 = db.put(u256(10), genByteSeq(numBytes), u256(0))
|
|
|
|
let pr7 = db.put(furthestElement, genByteSeq(numBytes), u256(0))
|
|
|
|
let pr8 = db.put(secondFurthest, genByteSeq(numBytes), u256(0))
|
|
|
|
let pr9 = db.put(u256(2), genByteSeq(numBytes), u256(0))
|
|
|
|
let pr10 = db.put(u256(4), genByteSeq(numBytes), u256(0))
|
|
|
|
|
|
|
|
check:
|
|
|
|
pr1.kind == ContentStored
|
|
|
|
pr2.kind == ContentStored
|
|
|
|
pr3.kind == ContentStored
|
|
|
|
pr4.kind == ContentStored
|
|
|
|
pr5.kind == ContentStored
|
|
|
|
pr6.kind == ContentStored
|
|
|
|
pr7.kind == ContentStored
|
|
|
|
pr8.kind == ContentStored
|
|
|
|
pr9.kind == ContentStored
|
|
|
|
pr10.kind == DbPruned
|
|
|
|
|
|
|
|
check:
|
2022-05-17 10:47:24 +00:00
|
|
|
pr10.numOfDeletedElements == 2
|
2022-05-26 06:26:08 +00:00
|
|
|
uint32(db.realSize()) < maxDbSize
|
2022-05-09 15:18:57 +00:00
|
|
|
# With current settings 2 furthers elements will be delted i.e 30 and 40
|
|
|
|
# so the furthest non deleted one will be 20
|
|
|
|
pr10.furthestStoredElementDistance == thirdFurthest
|
|
|
|
db.get(furthestElement).isNone()
|
|
|
|
db.get(secondFurthest).isNone()
|
|
|
|
db.get(thirdFurthest).isSome()
|