2023-11-23 10:44:57 +00:00
|
|
|
# Fluffy
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-09-28 17:58:41 +00:00
|
|
|
# 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
|
2024-02-28 17:31:45 +00:00
|
|
|
unittest2,
|
|
|
|
stint,
|
2021-09-28 17:58:41 +00:00
|
|
|
../network/state/state_content,
|
2023-12-01 16:20:52 +00:00
|
|
|
../database/content_db,
|
2022-05-12 16:04:37 +00:00
|
|
|
./test_helpers
|
2022-03-08 13:49:41 +00:00
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
suite "Content Database":
|
2024-09-07 13:03:13 +00:00
|
|
|
const 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
|
2024-09-05 16:31:55 +00:00
|
|
|
db = ContentDB.new(
|
2024-09-07 13:03:13 +00:00
|
|
|
"", uint32.high, RadiusConfig(kind: Dynamic), testId, inMemory = true
|
2024-09-05 16:31:55 +00:00
|
|
|
)
|
2021-09-28 17:58:41 +00:00
|
|
|
key = ContentId(UInt256.high()) # Some key
|
|
|
|
|
|
|
|
block:
|
2024-11-04 14:02:51 +00:00
|
|
|
var val = Opt.none(seq[byte])
|
|
|
|
proc onData(data: openArray[byte]) =
|
|
|
|
val = Opt.some(@data)
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
check:
|
2024-11-04 14:02:51 +00:00
|
|
|
db.get(key, onData) == false
|
2021-09-28 17:58:41 +00:00
|
|
|
val.isNone()
|
|
|
|
db.contains(key) == false
|
|
|
|
|
|
|
|
block:
|
2024-09-07 13:03:13 +00:00
|
|
|
discard db.putAndPrune(key, [byte 0, 1, 2, 3])
|
2024-11-04 14:02:51 +00:00
|
|
|
|
|
|
|
var val = Opt.none(seq[byte])
|
|
|
|
proc onData(data: openArray[byte]) =
|
|
|
|
val = Opt.some(@data)
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
check:
|
2024-11-04 14:02:51 +00:00
|
|
|
db.get(key, onData) == true
|
2021-09-28 17:58:41 +00:00
|
|
|
val.isSome()
|
|
|
|
val.get() == [byte 0, 1, 2, 3]
|
|
|
|
db.contains(key) == true
|
|
|
|
|
|
|
|
block:
|
|
|
|
db.del(key)
|
2024-11-04 14:02:51 +00:00
|
|
|
|
|
|
|
var val = Opt.none(seq[byte])
|
|
|
|
proc onData(data: openArray[byte]) =
|
|
|
|
val = Opt.some(@data)
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
check:
|
2024-11-04 14:02:51 +00:00
|
|
|
db.get(key, onData) == false
|
2021-09-28 17:58:41 +00:00
|
|
|
val.isNone()
|
|
|
|
db.contains(key) == false
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
test "ContentDB size":
|
2024-09-05 16:31:55 +00:00
|
|
|
let db = ContentDB.new(
|
2024-09-07 13:03:13 +00:00
|
|
|
"", uint32.high, RadiusConfig(kind: Dynamic), testId, inMemory = true
|
2024-09-05 16:31:55 +00:00
|
|
|
)
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
let numBytes = 10000
|
|
|
|
let size1 = db.size()
|
2024-09-07 13:03:13 +00:00
|
|
|
discard db.putAndPrune(u256(1), genByteSeq(numBytes))
|
2022-03-08 13:49:41 +00:00
|
|
|
let size2 = db.size()
|
2024-09-07 13:03:13 +00:00
|
|
|
discard db.putAndPrune(u256(2), genByteSeq(numBytes))
|
2022-03-08 13:49:41 +00:00
|
|
|
let size3 = db.size()
|
2024-09-07 13:03:13 +00:00
|
|
|
discard db.putAndPrune(u256(2), genByteSeq(numBytes))
|
2022-03-08 13:49:41 +00:00
|
|
|
let size4 = db.size()
|
2023-11-07 18:46:26 +00:00
|
|
|
let usedSize = db.usedSize()
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
size2 > size1
|
|
|
|
size3 > size2
|
|
|
|
size3 == size4
|
2023-11-07 18:46:26 +00:00
|
|
|
usedSize == 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
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
let usedSize1 = db.usedSize()
|
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
|
2023-11-07 18:46:26 +00:00
|
|
|
# The real size will be smaller as after a deletion there are free pages
|
|
|
|
# in the db which can be re-used for further additions.
|
|
|
|
usedSize1 < size5
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
db.reclaimSpace()
|
|
|
|
|
|
|
|
let size6 = db.size()
|
2023-11-07 18:46:26 +00:00
|
|
|
let usedSize2 = db.usedSize()
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
check:
|
2023-11-07 18:46:26 +00:00
|
|
|
# After space reclamation the size of the db back to the initial size.
|
2022-03-08 13:49:41 +00:00
|
|
|
size6 == size1
|
2023-11-07 18:46:26 +00:00
|
|
|
usedSize2 == size6
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
test "ContentDB pruning":
|
2023-11-16 14:27:30 +00:00
|
|
|
# TODO: This test is extremely breakable when changing
|
|
|
|
# `contentDeletionFraction` and/or the used test values.
|
|
|
|
# Need to rework either this test, or the pruning mechanism, or probably
|
|
|
|
# both.
|
2022-05-09 15:18:57 +00:00
|
|
|
let
|
2023-11-23 10:44:57 +00:00
|
|
|
storageCapacity = 100_000'u64
|
2024-09-05 16:31:55 +00:00
|
|
|
db = ContentDB.new(
|
2024-09-07 13:03:13 +00:00
|
|
|
"", storageCapacity, RadiusConfig(kind: Dynamic), testId, inMemory = true
|
2024-09-05 16:31:55 +00:00
|
|
|
)
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
furthestElement = u256(40)
|
|
|
|
secondFurthest = u256(30)
|
|
|
|
thirdFurthest = u256(20)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
numBytes = 10_000
|
2024-09-07 13:03:13 +00:00
|
|
|
pr1 = db.putAndPrune(u256(1), genByteSeq(numBytes))
|
|
|
|
pr2 = db.putAndPrune(thirdFurthest, genByteSeq(numBytes))
|
|
|
|
pr3 = db.putAndPrune(u256(3), genByteSeq(numBytes))
|
|
|
|
pr4 = db.putAndPrune(u256(10), genByteSeq(numBytes))
|
|
|
|
pr5 = db.putAndPrune(u256(5), genByteSeq(numBytes))
|
|
|
|
pr6 = db.putAndPrune(u256(11), genByteSeq(numBytes))
|
|
|
|
pr7 = db.putAndPrune(furthestElement, genByteSeq(2000))
|
|
|
|
pr8 = db.putAndPrune(secondFurthest, genByteSeq(2000))
|
|
|
|
pr9 = db.putAndPrune(u256(2), genByteSeq(numBytes))
|
|
|
|
pr10 = db.putAndPrune(u256(4), genByteSeq(12000))
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
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:
|
2023-11-16 14:27:30 +00:00
|
|
|
pr10.deletedElements == 2
|
2023-11-23 10:44:57 +00:00
|
|
|
uint64(db.usedSize()) < storageCapacity
|
2023-11-07 18:46:26 +00:00
|
|
|
# With the current settings the 2 furthest elements will be deleted,
|
|
|
|
# i.e key 30 and 40. The furthest non deleted one will have key 20.
|
2023-11-16 14:27:30 +00:00
|
|
|
pr10.distanceOfFurthestElement == thirdFurthest
|
2024-11-04 14:02:51 +00:00
|
|
|
not db.contains(furthestElement)
|
|
|
|
not db.contains(secondFurthest)
|
|
|
|
db.contains(thirdFurthest)
|
2023-11-23 10:44:57 +00:00
|
|
|
|
|
|
|
test "ContentDB force pruning":
|
|
|
|
const
|
|
|
|
# This start capacity doesn't really matter here as we are directly
|
|
|
|
# putting data in the db without additional size checks.
|
|
|
|
startCapacity = 14_159_872'u64
|
|
|
|
endCapacity = 500_000'u64
|
|
|
|
amountOfItems = 10_000
|
|
|
|
|
|
|
|
let
|
2024-09-05 16:31:55 +00:00
|
|
|
db = ContentDB.new(
|
2024-09-07 13:03:13 +00:00
|
|
|
"", startCapacity, RadiusConfig(kind: Dynamic), testId, inMemory = true
|
2024-09-05 16:31:55 +00:00
|
|
|
)
|
2023-11-23 10:44:57 +00:00
|
|
|
localId = UInt256.fromHex(
|
2024-02-28 17:31:45 +00:00
|
|
|
"30994892f3e4889d99deb5340050510d1842778acc7a7948adffa475fed51d6e"
|
|
|
|
)
|
2023-11-23 10:44:57 +00:00
|
|
|
content = genByteSeq(1000)
|
|
|
|
|
|
|
|
# Note: We could randomly generate the above localId and the content keys
|
|
|
|
# that are added to the database below. However we opt for a more
|
|
|
|
# deterministic test case as the randomness makes it difficult to chose a
|
|
|
|
# reasonable value to check if pruning was succesful.
|
|
|
|
let
|
|
|
|
increment = UInt256.high div amountOfItems
|
|
|
|
remainder = UInt256.high mod amountOfItems
|
|
|
|
var id = u256(0)
|
|
|
|
while id < UInt256.high - remainder:
|
|
|
|
db.put(id, content)
|
|
|
|
id = id + increment
|
|
|
|
|
|
|
|
db.storageCapacity = endCapacity
|
|
|
|
|
2024-09-05 16:31:55 +00:00
|
|
|
let newRadius = db.estimateNewRadius(RadiusConfig(kind: Dynamic))
|
2023-11-23 10:44:57 +00:00
|
|
|
|
|
|
|
db.forcePrune(localId, newRadius)
|
|
|
|
|
|
|
|
let diff = abs(db.size() - int64(db.storageCapacity))
|
|
|
|
# Quite a big marging (20%) is added as it is all an approximation.
|
2024-02-28 17:31:45 +00:00
|
|
|
check diff < int64(float(db.storageCapacity) * 0.20)
|
2024-09-05 16:31:55 +00:00
|
|
|
|
2024-09-07 13:03:13 +00:00
|
|
|
test "ContentDB radius - start with full radius":
|
|
|
|
let
|
|
|
|
storageCapacity = 100_000'u64
|
|
|
|
db = ContentDB.new(
|
|
|
|
"", storageCapacity, RadiusConfig(kind: Dynamic), testId, inMemory = true
|
|
|
|
)
|
|
|
|
radiusHandler = createRadiusHandler(db)
|
2024-09-05 16:31:55 +00:00
|
|
|
|
2024-09-07 13:03:13 +00:00
|
|
|
check radiusHandler() == UInt256.high()
|
2024-09-05 16:31:55 +00:00
|
|
|
|
2024-09-07 13:03:13 +00:00
|
|
|
test "ContentDB radius - 0 capacity":
|
|
|
|
let
|
|
|
|
db = ContentDB.new("", 0, RadiusConfig(kind: Dynamic), testId, inMemory = true)
|
|
|
|
radiusHandler = createRadiusHandler(db)
|
2024-09-05 16:31:55 +00:00
|
|
|
|
2024-09-07 13:03:13 +00:00
|
|
|
check radiusHandler() == UInt256.low()
|