2021-09-28 17:58:41 +00:00
|
|
|
# Nimbus
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2021-2023 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
import
|
2022-11-08 17:31:45 +00:00
|
|
|
chronicles,
|
|
|
|
metrics,
|
2021-09-28 17:58:41 +00:00
|
|
|
eth/db/kvstore,
|
|
|
|
eth/db/kvstore_sqlite3,
|
|
|
|
stint,
|
2022-11-08 17:31:45 +00:00
|
|
|
stew/results,
|
|
|
|
./network/state/state_content,
|
|
|
|
"."/network/wire/[portal_protocol, portal_protocol_config]
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
export kvstore_sqlite3
|
|
|
|
|
|
|
|
# This version of content db is the most basic, simple solution where data is
|
|
|
|
# stored no matter what content type or content network in the same kvstore with
|
|
|
|
# the content id as key. The content id is derived from the content key, and the
|
|
|
|
# deriviation is different depending on the content type. As we use content id,
|
|
|
|
# this part is currently out of the scope / API of the ContentDB.
|
|
|
|
# In the future it is likely that that either:
|
|
|
|
# 1. More kvstores are added per network, and thus depending on the network a
|
|
|
|
# different kvstore needs to be selected.
|
|
|
|
# 2. Or more kvstores are added per network and per content type, and thus
|
|
|
|
# content key fields are required to access the data.
|
|
|
|
# 3. Or databases are created per network (and kvstores pre content type) and
|
|
|
|
# thus depending on the network the right db needs to be selected.
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
declareCounter portal_pruning_counter,
|
2023-11-16 14:27:30 +00:00
|
|
|
"Number of pruning events which occured during the node's uptime",
|
2022-11-08 17:31:45 +00:00
|
|
|
labels = ["protocol_id"]
|
|
|
|
|
|
|
|
declareGauge portal_pruning_deleted_elements,
|
2023-11-16 14:27:30 +00:00
|
|
|
"Number of elements deleted in the last pruning",
|
2022-11-08 17:31:45 +00:00
|
|
|
labels = ["protocol_id"]
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
const
|
|
|
|
contentDeletionFraction = 0.05 ## 5% of the content will be deleted when the
|
|
|
|
## storage capacity is hit and radius gets adjusted.
|
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
type
|
2022-04-03 13:14:44 +00:00
|
|
|
RowInfo = tuple
|
|
|
|
contentId: array[32, byte]
|
|
|
|
payloadLength: int64
|
2022-06-03 11:44:42 +00:00
|
|
|
distance: array[32, byte]
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
ContentDB* = ref object
|
|
|
|
kv: KvStoreRef
|
2023-11-10 16:16:15 +00:00
|
|
|
storageCapacity*: uint64
|
2022-03-08 13:49:41 +00:00
|
|
|
sizeStmt: SqliteStmt[NoParams, int64]
|
2022-05-26 06:26:08 +00:00
|
|
|
unusedSizeStmt: SqliteStmt[NoParams, int64]
|
2023-11-07 18:46:26 +00:00
|
|
|
vacuumStmt: SqliteStmt[NoParams, void]
|
|
|
|
contentCountStmt: SqliteStmt[NoParams, int64]
|
2022-06-03 11:44:42 +00:00
|
|
|
contentSizeStmt: SqliteStmt[NoParams, int64]
|
|
|
|
getAllOrderedByDistanceStmt: SqliteStmt[array[32, byte], RowInfo]
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
PutResultType* = enum
|
|
|
|
ContentStored, DbPruned
|
|
|
|
|
|
|
|
PutResult* = object
|
|
|
|
case kind*: PutResultType
|
|
|
|
of ContentStored:
|
|
|
|
discard
|
|
|
|
of DbPruned:
|
2023-11-16 14:27:30 +00:00
|
|
|
distanceOfFurthestElement*: UInt256
|
|
|
|
deletedFraction*: float64
|
|
|
|
deletedElements*: int64
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
func xorDistance(
|
|
|
|
a: openArray[byte],
|
|
|
|
b: openArray[byte]
|
|
|
|
): Result[seq[byte], cstring] {.cdecl.} =
|
|
|
|
var s: seq[byte] = newSeq[byte](32)
|
|
|
|
|
|
|
|
if len(a) != 32 or len(b) != 32:
|
|
|
|
return err("Blobs should have 32 byte length")
|
|
|
|
|
|
|
|
var i = 0
|
|
|
|
while i < 32:
|
|
|
|
s[i] = a[i] xor b[i]
|
|
|
|
inc i
|
|
|
|
|
|
|
|
return ok(s)
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
template expectDb(x: auto): untyped =
|
|
|
|
# There's no meaningful error handling implemented for a corrupt database or
|
|
|
|
# full disk - this requires manual intervention, so we'll panic for now
|
|
|
|
x.expect("working database (disk broken/full?)")
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
proc new*(
|
2023-11-10 16:16:15 +00:00
|
|
|
T: type ContentDB, path: string, storageCapacity: uint64, inMemory = false):
|
2022-08-01 19:00:21 +00:00
|
|
|
ContentDB =
|
2023-11-10 16:16:15 +00:00
|
|
|
doAssert(storageCapacity <= uint64(int64.high))
|
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
let db =
|
|
|
|
if inMemory:
|
|
|
|
SqStoreRef.init("", "fluffy-test", inMemory = true).expect(
|
|
|
|
"working database (out of memory?)")
|
|
|
|
else:
|
|
|
|
SqStoreRef.init(path, "fluffy").expectDb()
|
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
db.registerCustomScalarFunction("xorDistance", xorDistance)
|
|
|
|
.expect("Couldn't register custom xor function")
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
let sizeStmt = db.prepareStmt(
|
2022-03-08 13:49:41 +00:00
|
|
|
"SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();",
|
|
|
|
NoParams, int64).get()
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
let unusedSizeStmt = db.prepareStmt(
|
2022-05-26 06:26:08 +00:00
|
|
|
"SELECT freelist_count * page_size as size FROM pragma_freelist_count(), pragma_page_size();",
|
|
|
|
NoParams, int64).get()
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
let vacuumStmt = db.prepareStmt(
|
2022-03-08 13:49:41 +00:00
|
|
|
"VACUUM;",
|
|
|
|
NoParams, void).get()
|
|
|
|
|
2022-04-03 13:14:44 +00:00
|
|
|
let kvStore = kvStore db.openKvStore().expectDb()
|
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
let contentSizeStmt = db.prepareStmt(
|
|
|
|
"SELECT SUM(length(value)) FROM kvstore",
|
2023-11-07 18:46:26 +00:00
|
|
|
NoParams, int64).get()
|
|
|
|
|
|
|
|
let contentCountStmt = db.prepareStmt(
|
|
|
|
"SELECT COUNT(key) FROM kvstore;",
|
|
|
|
NoParams, int64).get()
|
2022-06-03 11:44:42 +00:00
|
|
|
|
|
|
|
let getAllOrderedByDistanceStmt = db.prepareStmt(
|
|
|
|
"SELECT key, length(value), xorDistance(?, key) as distance FROM kvstore ORDER BY distance DESC",
|
2023-11-07 18:46:26 +00:00
|
|
|
array[32, byte], RowInfo).get()
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
ContentDB(
|
2022-05-26 06:26:08 +00:00
|
|
|
kv: kvStore,
|
2023-11-10 16:16:15 +00:00
|
|
|
storageCapacity: storageCapacity,
|
2023-11-07 18:46:26 +00:00
|
|
|
sizeStmt: sizeStmt,
|
|
|
|
unusedSizeStmt: unusedSizeStmt,
|
|
|
|
vacuumStmt: vacuumStmt,
|
2022-06-03 11:44:42 +00:00
|
|
|
contentSizeStmt: contentSizeStmt,
|
2023-11-07 18:46:26 +00:00
|
|
|
contentCountStmt: contentCountStmt,
|
2022-06-03 11:44:42 +00:00
|
|
|
getAllOrderedByDistanceStmt: getAllOrderedByDistanceStmt
|
2022-05-26 06:26:08 +00:00
|
|
|
)
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
## Private KvStoreRef Calls
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc get(kv: KvStoreRef, key: openArray[byte]): Opt[seq[byte]] =
|
|
|
|
var res: Opt[seq[byte]]
|
|
|
|
proc onData(data: openArray[byte]) = res = Opt.some(@data)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
|
|
|
discard kv.get(key, onData).expectDb()
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc getSszDecoded(kv: KvStoreRef, key: openArray[byte], T: type auto): Opt[T] =
|
2022-08-01 19:00:21 +00:00
|
|
|
let res = kv.get(key)
|
|
|
|
if res.isSome():
|
|
|
|
try:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.some(SSZ.decode(res.get(), T))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError:
|
2022-08-01 19:00:21 +00:00
|
|
|
raiseAssert("Stored data should always be serialized correctly")
|
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.none(T)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
|
|
|
## Private ContentDB calls
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc get(db: ContentDB, key: openArray[byte]): Opt[seq[byte]] =
|
2022-08-01 19:00:21 +00:00
|
|
|
db.kv.get(key)
|
|
|
|
|
|
|
|
proc put(db: ContentDB, key, value: openArray[byte]) =
|
|
|
|
db.kv.put(key, value).expectDb()
|
|
|
|
|
|
|
|
proc contains(db: ContentDB, key: openArray[byte]): bool =
|
|
|
|
db.kv.contains(key).expectDb()
|
|
|
|
|
|
|
|
proc del(db: ContentDB, key: openArray[byte]) =
|
2023-01-27 14:57:48 +00:00
|
|
|
# TODO: Do we want to return the bool here too?
|
|
|
|
discard db.kv.del(key).expectDb()
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
proc getSszDecoded(
|
2022-12-09 16:59:36 +00:00
|
|
|
db: ContentDB, key: openArray[byte], T: type auto): Opt[T] =
|
2022-08-01 19:00:21 +00:00
|
|
|
db.kv.getSszDecoded(key, T)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
## Public ContentId based ContentDB calls
|
|
|
|
|
|
|
|
# TODO: Could also decide to use the ContentKey SSZ bytestring, as this is what
|
|
|
|
# gets send over the network in requests, but that would be a bigger key. Or the
|
|
|
|
# same hashing could be done on it here.
|
|
|
|
# However ContentId itself is already derived through different digests
|
|
|
|
# depending on the content type, and this ContentId typically needs to be
|
|
|
|
# checked with the Radius/distance of the node anyhow. So lets see how we end up
|
|
|
|
# using this mostly in the code.
|
|
|
|
|
|
|
|
proc get*(db: ContentDB, key: ContentId): Opt[seq[byte]] =
|
|
|
|
# TODO: Here it is unfortunate that ContentId is a uint256 instead of Digest256.
|
|
|
|
db.get(key.toBytesBE())
|
|
|
|
|
|
|
|
proc put*(db: ContentDB, key: ContentId, value: openArray[byte]) =
|
|
|
|
db.put(key.toBytesBE(), value)
|
|
|
|
|
|
|
|
proc contains*(db: ContentDB, key: ContentId): bool =
|
|
|
|
db.contains(key.toBytesBE())
|
|
|
|
|
|
|
|
proc del*(db: ContentDB, key: ContentId) =
|
|
|
|
db.del(key.toBytesBE())
|
|
|
|
|
|
|
|
proc getSszDecoded*(db: ContentDB, key: ContentId, T: type auto): Opt[T] =
|
|
|
|
db.getSszDecoded(key.toBytesBE(), T)
|
|
|
|
|
|
|
|
## Public database size, content and pruning related calls
|
|
|
|
|
2022-03-08 13:49:41 +00:00
|
|
|
proc reclaimSpace*(db: ContentDB): void =
|
2022-04-13 05:56:01 +00:00
|
|
|
## Runs sqlite VACUUM commands which rebuilds the db, repacking it into a
|
|
|
|
## minimal amount of disk space.
|
2022-03-08 13:49:41 +00:00
|
|
|
## Ideal mode of operation, is to run it after several deletes.
|
2022-12-09 16:59:36 +00:00
|
|
|
## Another option would be to run 'PRAGMA auto_vacuum = FULL;' statement at
|
2022-04-13 05:56:01 +00:00
|
|
|
## the start of db to leave it up to sqlite to clean up
|
2023-11-07 18:46:26 +00:00
|
|
|
db.vacuumStmt.exec().expectDb()
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
proc size*(db: ContentDB): int64 =
|
2023-11-10 16:16:15 +00:00
|
|
|
## Return current size of DB as product of sqlite page_count and page_size:
|
2022-03-08 13:49:41 +00:00
|
|
|
## https://www.sqlite.org/pragma.html#pragma_page_count
|
|
|
|
## https://www.sqlite.org/pragma.html#pragma_page_size
|
2023-11-10 16:16:15 +00:00
|
|
|
## It returns the total size of db on the disk, i.e both data and metadata
|
|
|
|
## used to store content.
|
|
|
|
## It is worth noting that when deleting content, the size may lag behind due
|
2022-03-08 13:49:41 +00:00
|
|
|
## to the way how deleting works in sqlite.
|
|
|
|
## Good description can be found in: https://www.sqlite.org/lang_vacuum.html
|
|
|
|
var size: int64 = 0
|
|
|
|
discard (db.sizeStmt.exec do(res: int64):
|
|
|
|
size = res).expectDb()
|
|
|
|
return size
|
2021-09-28 17:58:41 +00:00
|
|
|
|
2022-05-26 06:26:08 +00:00
|
|
|
proc unusedSize(db: ContentDB): int64 =
|
|
|
|
## Returns the total size of the pages which are unused by the database,
|
|
|
|
## i.e they can be re-used for new content.
|
|
|
|
var size: int64 = 0
|
|
|
|
discard (db.unusedSizeStmt.exec do(res: int64):
|
|
|
|
size = res).expectDb()
|
|
|
|
return size
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
proc usedSize*(db: ContentDB): int64 =
|
2023-11-10 16:16:15 +00:00
|
|
|
## Returns the total size of the database (data + metadata) minus the unused
|
|
|
|
## pages.
|
2022-05-26 06:26:08 +00:00
|
|
|
db.size() - db.unusedSize()
|
|
|
|
|
2023-03-31 16:52:21 +00:00
|
|
|
proc contentSize*(db: ContentDB): int64 =
|
2023-11-10 16:16:15 +00:00
|
|
|
## Returns total size of the content stored in DB.
|
2022-06-03 11:44:42 +00:00
|
|
|
var size: int64 = 0
|
|
|
|
discard (db.contentSizeStmt.exec do(res: int64):
|
|
|
|
size = res).expectDb()
|
|
|
|
return size
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
proc contentCount*(db: ContentDB): int64 =
|
|
|
|
var count: int64 = 0
|
|
|
|
discard (db.contentCountStmt.exec do(res: int64):
|
|
|
|
count = res).expectDb()
|
|
|
|
return count
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
proc deleteContentFraction*(
|
2022-06-03 11:44:42 +00:00
|
|
|
db: ContentDB,
|
|
|
|
target: UInt256,
|
|
|
|
fraction: float64): (UInt256, int64, int64, int64) =
|
|
|
|
## Deletes at most `fraction` percent of content form database.
|
2023-11-16 14:27:30 +00:00
|
|
|
## Content furthest from provided `target` is deleted first.
|
|
|
|
# TODO: The usage of `db.contentSize()` for the deletion calculation versus
|
|
|
|
# `db.usedSize()` for the pruning threshold leads sometimes to some unexpected
|
|
|
|
# results of how much content gets up deleted.
|
2022-06-03 11:44:42 +00:00
|
|
|
doAssert(
|
2022-08-01 19:00:21 +00:00
|
|
|
fraction > 0 and fraction < 1,
|
2022-06-03 11:44:42 +00:00
|
|
|
"Deleted fraction should be > 0 and < 1"
|
|
|
|
)
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
let totalContentSize = db.contentSize()
|
|
|
|
let bytesToDelete = int64(fraction * float64(totalContentSize))
|
2023-11-16 14:27:30 +00:00
|
|
|
var deletedElements: int64 = 0
|
2022-05-24 11:27:22 +00:00
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
var ri: RowInfo
|
2023-11-16 14:27:30 +00:00
|
|
|
var deletedBytes: int64 = 0
|
2023-09-13 02:32:38 +00:00
|
|
|
let targetBytes = target.toBytesBE()
|
2022-06-03 11:44:42 +00:00
|
|
|
for e in db.getAllOrderedByDistanceStmt.exec(targetBytes, ri):
|
2023-11-16 14:27:30 +00:00
|
|
|
if deletedBytes + ri.payloadLength <= bytesToDelete:
|
2022-06-03 11:44:42 +00:00
|
|
|
db.del(ri.contentId)
|
2023-11-16 14:27:30 +00:00
|
|
|
deletedBytes = deletedBytes + ri.payloadLength
|
|
|
|
inc deletedElements
|
2022-05-09 15:18:57 +00:00
|
|
|
else:
|
2022-06-03 11:44:42 +00:00
|
|
|
return (
|
|
|
|
UInt256.fromBytesBE(ri.distance),
|
2023-11-16 14:27:30 +00:00
|
|
|
deletedBytes,
|
2022-08-01 19:00:21 +00:00
|
|
|
totalContentSize,
|
2023-11-16 14:27:30 +00:00
|
|
|
deletedElements
|
2022-06-03 11:44:42 +00:00
|
|
|
)
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
proc put*(
|
2022-05-24 11:27:22 +00:00
|
|
|
db: ContentDB,
|
|
|
|
key: ContentId,
|
|
|
|
value: openArray[byte],
|
|
|
|
target: UInt256): PutResult =
|
2022-05-09 15:18:57 +00:00
|
|
|
db.put(key, value)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2023-11-10 16:16:15 +00:00
|
|
|
# The used size is used as pruning threshold. This means that the database
|
|
|
|
# size will reach the size specified in db.storageCapacity and will stay
|
|
|
|
# around that size throughout the node's lifetime, as after content deletion
|
|
|
|
# due to pruning, the free pages will be re-used.
|
2022-05-26 06:26:08 +00:00
|
|
|
# TODO:
|
|
|
|
# 1. Devise vacuum strategy - after few pruning cycles database can become
|
|
|
|
# fragmented which may impact performance, so at some point in time `VACUUM`
|
|
|
|
# will need to be run to defragment the db.
|
|
|
|
# 2. Deal with the edge case where a user configures max db size lower than
|
2022-12-09 16:59:36 +00:00
|
|
|
# current db.size(). With such config the database would try to prune itself
|
|
|
|
# with each addition.
|
2023-11-07 18:46:26 +00:00
|
|
|
let dbSize = db.usedSize()
|
2022-05-24 11:27:22 +00:00
|
|
|
|
2023-11-10 16:16:15 +00:00
|
|
|
if dbSize < int64(db.storageCapacity):
|
2022-05-09 15:18:57 +00:00
|
|
|
return PutResult(kind: ContentStored)
|
|
|
|
else:
|
2022-05-17 10:47:24 +00:00
|
|
|
let (
|
2023-11-16 14:27:30 +00:00
|
|
|
distanceOfFurthestElement,
|
2022-05-17 10:47:24 +00:00
|
|
|
deletedBytes,
|
|
|
|
totalContentSize,
|
|
|
|
deletedElements
|
|
|
|
) =
|
2023-11-16 14:27:30 +00:00
|
|
|
db.deleteContentFraction(target, contentDeletionFraction)
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
let deletedFraction = float64(deletedBytes) / float64(totalContentSize)
|
2023-11-16 14:27:30 +00:00
|
|
|
info "Deleted content fraction", deletedBytes, deletedElements, deletedFraction
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
return PutResult(
|
|
|
|
kind: DbPruned,
|
2023-11-16 14:27:30 +00:00
|
|
|
distanceOfFurthestElement: distanceOfFurthestElement,
|
|
|
|
deletedFraction: deletedFraction,
|
|
|
|
deletedElements: deletedElements)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
|
|
|
proc adjustRadius(
|
|
|
|
p: PortalProtocol,
|
2023-11-16 14:27:30 +00:00
|
|
|
deletedFraction: float64,
|
|
|
|
distanceOfFurthestElement: UInt256) =
|
|
|
|
# Invert fraction as the UInt256 implementation does not support
|
2022-11-08 17:31:45 +00:00
|
|
|
# multiplication by float
|
2023-11-16 14:27:30 +00:00
|
|
|
let invertedFractionAsInt = int64(1.0 / deletedFraction)
|
2022-11-08 17:31:45 +00:00
|
|
|
let scaledRadius = p.dataRadius div u256(invertedFractionAsInt)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
# Choose a larger value to avoid the situation where the
|
|
|
|
# `distanceOfFurthestElement is very close to the local id so that the local
|
|
|
|
# radius would end up too small to accept any more data to the database.
|
|
|
|
# If scaledRadius radius will be larger it will still contain all elements.
|
|
|
|
let newRadius = max(scaledRadius, distanceOfFurthestElement)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
info "Database radius adjusted",
|
2022-11-08 17:31:45 +00:00
|
|
|
oldRadius = p.dataRadius,
|
|
|
|
newRadius = newRadius,
|
2023-11-16 14:27:30 +00:00
|
|
|
distanceOfFurthestElement
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
# Both scaledRadius and distanceOfFurthestElement are smaller than current
|
|
|
|
# dataRadius, so the radius will constantly decrease through the node its
|
|
|
|
# lifetime.
|
2022-11-08 17:31:45 +00:00
|
|
|
p.dataRadius = newRadius
|
|
|
|
|
|
|
|
proc createGetHandler*(db: ContentDB): DbGetHandler =
|
|
|
|
return (
|
2022-12-09 16:59:36 +00:00
|
|
|
proc(contentKey: ByteList, contentId: ContentId): Opt[seq[byte]] =
|
|
|
|
let content = db.get(contentId).valueOr:
|
2022-11-08 17:31:45 +00:00
|
|
|
return Opt.none(seq[byte])
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
ok(content)
|
2022-11-08 17:31:45 +00:00
|
|
|
)
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc createStoreHandler*(
|
|
|
|
db: ContentDB, cfg: RadiusConfig, p: PortalProtocol): DbStoreHandler =
|
2022-11-08 17:31:45 +00:00
|
|
|
return (proc(
|
|
|
|
contentKey: ByteList,
|
|
|
|
contentId: ContentId,
|
2023-01-31 12:38:08 +00:00
|
|
|
content: seq[byte]) {.raises: [], gcsafe.} =
|
2022-12-09 16:59:36 +00:00
|
|
|
# always re-check that the key is in the node range to make sure only
|
|
|
|
# content in range is stored.
|
|
|
|
# TODO: current silent assumption is that both ContentDB and PortalProtocol
|
|
|
|
# are using the same xor distance function
|
2022-11-08 17:31:45 +00:00
|
|
|
if p.inRange(contentId):
|
|
|
|
case cfg.kind:
|
|
|
|
of Dynamic:
|
|
|
|
# In case of dynamic radius setting we obey storage limits and adjust
|
|
|
|
# radius to store network fraction corresponding to those storage limits.
|
|
|
|
let res = db.put(contentId, content, p.baseProtocol.localNode.id)
|
|
|
|
if res.kind == DbPruned:
|
|
|
|
portal_pruning_counter.inc(labelValues = [$p.protocolId])
|
|
|
|
portal_pruning_deleted_elements.set(
|
2023-11-16 14:27:30 +00:00
|
|
|
res.deletedElements.int64,
|
2022-11-08 17:31:45 +00:00
|
|
|
labelValues = [$p.protocolId]
|
|
|
|
)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
if res.deletedFraction > 0.0:
|
|
|
|
p.adjustRadius(res.deletedFraction, res.distanceOfFurthestElement)
|
|
|
|
else:
|
|
|
|
# Note:
|
|
|
|
# This can occur when the furthest content is bigger than the fraction
|
|
|
|
# size. This is unlikely to happen as it would require either very
|
|
|
|
# small storage capacity or a very small `contentDeletionFraction`
|
|
|
|
# combined with some big content.
|
|
|
|
info "Database pruning attempt resulted in no content deleted"
|
|
|
|
return
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
of Static:
|
|
|
|
# If the config is set statically, radius is not adjusted, and is kept
|
|
|
|
# constant thorugh node life time, also database max size is disabled
|
|
|
|
# so we will effectivly store fraction of the network
|
|
|
|
db.put(contentId, content)
|
|
|
|
)
|