2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-03-02 16:30:42 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
import std/options
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/lrucache
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
import ./blockstore
|
2023-07-06 23:23:27 +00:00
|
|
|
import ../units
|
2022-03-02 16:30:42 +00:00
|
|
|
import ../chunker
|
|
|
|
import ../errors
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 07:35:03 +00:00
|
|
|
import ../logutils
|
2022-12-03 00:00:55 +00:00
|
|
|
import ../manifest
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../merkletree
|
|
|
|
import ../utils
|
2023-11-06 08:10:30 +00:00
|
|
|
import ../clock
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
export blockstore
|
|
|
|
|
|
|
|
logScope:
|
2022-05-19 19:56:03 +00:00
|
|
|
topics = "codex cachestore"
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
CacheStore* = ref object of BlockStore
|
2023-07-06 23:23:27 +00:00
|
|
|
currentSize*: NBytes
|
|
|
|
size*: NBytes
|
2022-03-02 16:30:42 +00:00
|
|
|
cache: LruCache[Cid, Block]
|
2023-12-21 06:41:43 +00:00
|
|
|
cidAndProofCache: LruCache[(Cid, Natural), (Cid, CodexProof)]
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
InvalidBlockSize* = object of CodexError
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
const
|
2023-07-06 23:23:27 +00:00
|
|
|
DefaultCacheSize*: NBytes = 5.MiBs
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
method getBlock*(self: CacheStore, cid: Cid): Future[?!Block] {.async.} =
|
2022-03-02 16:30:42 +00:00
|
|
|
## Get a block from the stores
|
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Getting block from cache", cid
|
2022-08-19 00:56:36 +00:00
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2023-11-14 12:02:17 +00:00
|
|
|
return cid.emptyBlock
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
if cid notin self.cache:
|
2023-11-14 12:02:17 +00:00
|
|
|
return failure (ref BlockNotFoundError)(msg: "Block not in cache " & $cid)
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
try:
|
2022-08-19 00:56:36 +00:00
|
|
|
return success self.cache[cid]
|
2024-05-23 15:29:30 +00:00
|
|
|
except CancelledError as error:
|
|
|
|
raise error
|
2022-07-28 00:39:17 +00:00
|
|
|
except CatchableError as exc:
|
2022-08-19 00:56:36 +00:00
|
|
|
trace "Error requesting block from cache", cid, error = exc.msg
|
|
|
|
return failure exc
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
method getCidAndProof*(
|
|
|
|
self: CacheStore,
|
|
|
|
treeCid: Cid,
|
|
|
|
index: Natural): Future[?!(Cid, CodexProof)] {.async.} =
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
if cidAndProof =? self.cidAndProofCache.getOption((treeCid, index)):
|
|
|
|
success(cidAndProof)
|
|
|
|
else:
|
|
|
|
failure(newException(BlockNotFoundError, "Block not in cache: " & $BlockAddress.init(treeCid, index)))
|
|
|
|
|
|
|
|
method getBlock*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!Block] {.async.} =
|
2024-01-08 22:52:46 +00:00
|
|
|
without cidAndProof =? (await self.getCidAndProof(treeCid, index)), err:
|
2023-11-14 12:02:17 +00:00
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
await self.getBlock(cidAndProof[0])
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
method getBlockAndProof*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!(Block, CodexProof)] {.async.} =
|
2024-01-08 22:52:46 +00:00
|
|
|
without cidAndProof =? (await self.getCidAndProof(treeCid, index)), err:
|
2023-11-14 12:02:17 +00:00
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
let (cid, proof) = cidAndProof
|
|
|
|
|
|
|
|
without blk =? await self.getBlock(cid), err:
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
success((blk, proof))
|
|
|
|
|
|
|
|
method getBlock*(self: CacheStore, address: BlockAddress): Future[?!Block] =
|
|
|
|
if address.leaf:
|
|
|
|
self.getBlock(address.treeCid, address.index)
|
|
|
|
else:
|
|
|
|
self.getBlock(address.cid)
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method hasBlock*(self: CacheStore, cid: Cid): Future[?!bool] {.async.} =
|
|
|
|
## Check if the block exists in the blockstore
|
2022-03-02 16:30:42 +00:00
|
|
|
##
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Checking CacheStore for block presence", cid
|
2022-04-05 14:24:48 +00:00
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-07-28 00:39:17 +00:00
|
|
|
return true.success
|
|
|
|
|
|
|
|
return (cid in self.cache).success
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
method hasBlock*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!bool] {.async.} =
|
2024-01-08 22:52:46 +00:00
|
|
|
without cidAndProof =? (await self.getCidAndProof(treeCid, index)), err:
|
2023-11-14 12:02:17 +00:00
|
|
|
if err of BlockNotFoundError:
|
|
|
|
return success(false)
|
|
|
|
else:
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
await self.hasBlock(cidAndProof[0])
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
func cids(self: CacheStore): (iterator: Cid {.gcsafe.}) =
|
|
|
|
return iterator(): Cid =
|
|
|
|
for cid in self.cache.keys:
|
|
|
|
yield cid
|
|
|
|
|
|
|
|
method listBlocks*(
|
2023-06-22 15:11:18 +00:00
|
|
|
self: CacheStore,
|
|
|
|
blockType = BlockType.Manifest
|
2023-11-14 12:02:17 +00:00
|
|
|
): Future[?!AsyncIter[?Cid]] {.async.} =
|
2022-07-28 00:39:17 +00:00
|
|
|
## Get the list of blocks in the BlockStore. This is an intensive operation
|
|
|
|
##
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
let
|
|
|
|
cids = self.cids()
|
|
|
|
|
2024-06-10 22:47:29 +00:00
|
|
|
proc isFinished(): bool =
|
|
|
|
return finished(cids)
|
|
|
|
|
|
|
|
proc genNext(): Future[Cid] {.async.} =
|
|
|
|
cids()
|
|
|
|
|
|
|
|
let iter = await (AsyncIter[Cid].new(genNext, isFinished)
|
|
|
|
.filter(
|
|
|
|
proc (cid: Cid): Future[bool] {.async.} =
|
|
|
|
without isManifest =? cid.isManifest, err:
|
|
|
|
trace "Error checking if cid is a manifest", err = err.msg
|
|
|
|
return false
|
|
|
|
|
|
|
|
case blockType:
|
|
|
|
of BlockType.Both:
|
|
|
|
return true
|
|
|
|
of BlockType.Manifest:
|
|
|
|
return isManifest
|
|
|
|
of BlockType.Block:
|
|
|
|
return not isManifest
|
|
|
|
))
|
|
|
|
|
|
|
|
return success(map[Cid, ?Cid](iter,
|
|
|
|
proc (cid: Cid): Future[?Cid] {.async.} =
|
|
|
|
some(cid)
|
|
|
|
))
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
func putBlockSync(self: CacheStore, blk: Block): bool =
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
let blkSize = blk.data.len.NBytes # in bytes
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
if blkSize > self.size:
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Block size is larger than cache size", blk = blkSize, cache = self.size
|
2022-03-02 16:30:42 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
while self.currentSize + blkSize > self.size:
|
|
|
|
try:
|
|
|
|
let removed = self.cache.removeLru()
|
2023-07-06 23:23:27 +00:00
|
|
|
self.currentSize -= removed.data.len.NBytes
|
2022-04-05 14:24:48 +00:00
|
|
|
except EmptyLruCacheError as exc:
|
2022-03-02 16:30:42 +00:00
|
|
|
# if the cache is empty, can't remove anything, so break and add item
|
|
|
|
# to the cache
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Exception puting block to cache", exc = exc.msg
|
2022-03-02 16:30:42 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
self.cache[blk.cid] = blk
|
|
|
|
self.currentSize += blkSize
|
|
|
|
return true
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
method putBlock*(
|
|
|
|
self: CacheStore,
|
|
|
|
blk: Block,
|
|
|
|
ttl = Duration.none): Future[?!void] {.async.} =
|
2022-03-02 16:30:42 +00:00
|
|
|
## Put a block to the blockstore
|
|
|
|
##
|
2022-04-05 14:24:48 +00:00
|
|
|
|
|
|
|
trace "Storing block in cache", cid = blk.cid
|
|
|
|
if blk.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-07-28 00:39:17 +00:00
|
|
|
return success()
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
discard self.putBlockSync(blk)
|
2024-08-26 13:18:59 +00:00
|
|
|
if onBlock =? self.onBlockStored:
|
|
|
|
await onBlock(blk.cid)
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
return success()
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
method putCidAndProof*(
|
2023-11-14 12:02:17 +00:00
|
|
|
self: CacheStore,
|
|
|
|
treeCid: Cid,
|
|
|
|
index: Natural,
|
|
|
|
blockCid: Cid,
|
2023-12-21 06:41:43 +00:00
|
|
|
proof: CodexProof
|
2023-11-14 12:02:17 +00:00
|
|
|
): Future[?!void] {.async.} =
|
|
|
|
self.cidAndProofCache[(treeCid, index)] = (blockCid, proof)
|
|
|
|
success()
|
|
|
|
|
2023-11-06 08:10:30 +00:00
|
|
|
method ensureExpiry*(
|
|
|
|
self: CacheStore,
|
|
|
|
cid: Cid,
|
|
|
|
expiry: SecondsSince1970
|
|
|
|
): Future[?!void] {.async.} =
|
|
|
|
## Updates block's assosicated TTL in store - not applicable for CacheStore
|
|
|
|
##
|
|
|
|
|
|
|
|
discard # CacheStore does not have notion of TTL
|
|
|
|
|
2023-11-22 10:09:12 +00:00
|
|
|
method ensureExpiry*(
|
|
|
|
self: CacheStore,
|
|
|
|
treeCid: Cid,
|
|
|
|
index: Natural,
|
|
|
|
expiry: SecondsSince1970
|
|
|
|
): Future[?!void] {.async.} =
|
|
|
|
## Updates block's associated TTL in store - not applicable for CacheStore
|
|
|
|
##
|
|
|
|
|
|
|
|
discard # CacheStore does not have notion of TTL
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method delBlock*(self: CacheStore, cid: Cid): Future[?!void] {.async.} =
|
2022-06-28 16:10:05 +00:00
|
|
|
## Delete a block from the blockstore
|
2022-03-02 16:30:42 +00:00
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Deleting block from cache", cid
|
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-06-28 16:10:05 +00:00
|
|
|
return success()
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-06-28 16:10:05 +00:00
|
|
|
let removed = self.cache.del(cid)
|
|
|
|
if removed.isSome:
|
2023-07-06 23:23:27 +00:00
|
|
|
self.currentSize -= removed.get.data.len.NBytes
|
2022-06-28 16:10:05 +00:00
|
|
|
|
|
|
|
return success()
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
method delBlock*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!void] {.async.} =
|
|
|
|
let maybeRemoved = self.cidAndProofCache.del((treeCid, index))
|
|
|
|
|
|
|
|
if removed =? maybeRemoved:
|
|
|
|
return await self.delBlock(removed[0])
|
2023-11-14 17:52:27 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
return success()
|
|
|
|
|
2022-07-22 23:38:49 +00:00
|
|
|
method close*(self: CacheStore): Future[void] {.async.} =
|
|
|
|
## Close the blockstore, a no-op for this implementation
|
|
|
|
##
|
|
|
|
|
|
|
|
discard
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
proc new*(
|
2022-03-02 16:30:42 +00:00
|
|
|
_: type CacheStore,
|
|
|
|
blocks: openArray[Block] = [],
|
2023-07-06 23:23:27 +00:00
|
|
|
cacheSize: NBytes = DefaultCacheSize,
|
|
|
|
chunkSize: NBytes = DefaultChunkSize
|
2023-06-22 15:11:18 +00:00
|
|
|
): CacheStore {.raises: [Defect, ValueError].} =
|
|
|
|
## Create a new CacheStore instance
|
2023-09-25 14:31:10 +00:00
|
|
|
##
|
2023-06-22 15:11:18 +00:00
|
|
|
## `cacheSize` and `chunkSize` are both in bytes
|
2023-09-25 14:31:10 +00:00
|
|
|
##
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
if cacheSize < chunkSize:
|
|
|
|
raise newException(ValueError, "cacheSize cannot be less than chunkSize")
|
|
|
|
|
|
|
|
let
|
2023-07-06 23:23:27 +00:00
|
|
|
currentSize = 0'nb
|
|
|
|
size = int(cacheSize div chunkSize)
|
2022-03-02 16:30:42 +00:00
|
|
|
cache = newLruCache[Cid, Block](size)
|
2023-12-21 06:41:43 +00:00
|
|
|
cidAndProofCache = newLruCache[(Cid, Natural), (Cid, CodexProof)](size)
|
2022-03-02 16:30:42 +00:00
|
|
|
store = CacheStore(
|
|
|
|
cache: cache,
|
2023-11-14 12:02:17 +00:00
|
|
|
cidAndProofCache: cidAndProofCache,
|
2022-03-02 16:30:42 +00:00
|
|
|
currentSize: currentSize,
|
2024-08-26 13:18:59 +00:00
|
|
|
size: cacheSize,
|
|
|
|
onBlockStored: CidCallback.none)
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
for blk in blocks:
|
|
|
|
discard store.putBlockSync(blk)
|
|
|
|
|
|
|
|
return store
|
2023-07-06 23:23:27 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
_: type CacheStore,
|
|
|
|
blocks: openArray[Block] = [],
|
|
|
|
cacheSize: int,
|
|
|
|
chunkSize: int
|
|
|
|
): CacheStore {.raises: [Defect, ValueError].} =
|
|
|
|
CacheStore.new(blocks, NBytes cacheSize, NBytes chunkSize)
|