2025-12-18 18:23:09 +01:00
|
|
|
## Logos Storage
|
2021-02-25 18:23:22 -06: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.
|
|
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
{.push raises: [].}
|
2021-08-30 13:25:20 -06:00
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
|
import pkg/libp2p
|
2022-12-02 18:00:55 -06:00
|
|
|
import pkg/questionable
|
2022-01-10 09:32:56 -06:00
|
|
|
import pkg/questionable/results
|
2021-08-30 13:25:20 -06:00
|
|
|
|
2023-11-06 09:10:30 +01:00
|
|
|
import ../clock
|
2021-02-25 18:23:22 -06:00
|
|
|
import ../blocktype
|
2023-11-14 13:02:17 +01:00
|
|
|
import ../merkletree
|
|
|
|
|
import ../utils
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-08-01 16:47:57 -07:00
|
|
|
export blocktype
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
type
|
2022-12-02 18:00:55 -06:00
|
|
|
BlockNotFoundError* = object of CodexError
|
|
|
|
|
|
|
|
|
|
BlockType* {.pure.} = enum
|
|
|
|
|
Manifest
|
|
|
|
|
Block
|
|
|
|
|
Both
|
|
|
|
|
|
2025-12-11 10:03:43 +01:00
|
|
|
CidCallback* = proc(cid: Cid): Future[void] {.async: (raises: []).}
|
2023-11-14 13:02:17 +01:00
|
|
|
BlockStore* = ref object of RootObj
|
2024-08-26 15:18:59 +02:00
|
|
|
onBlockStored*: ?CidCallback
|
2022-12-02 18:00:55 -06:00
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
method getBlock*(
|
|
|
|
|
self: BlockStore, cid: Cid
|
|
|
|
|
): Future[?!Block] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-14 13:02:17 +01:00
|
|
|
## Get a block from the blockstore
|
|
|
|
|
##
|
2022-12-02 18:00:55 -06:00
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("getBlock by cid not implemented!")
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-01-10 15:12:37 +01:00
|
|
|
method getBlock*(
|
|
|
|
|
self: BlockStore, treeCid: Cid, index: Natural
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!Block] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-14 13:02:17 +01:00
|
|
|
## Get a block from the blockstore
|
|
|
|
|
##
|
2022-12-02 18:00:55 -06:00
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("getBlock by treecid not implemented!")
|
|
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
method getCid*(
|
|
|
|
|
self: BlockStore, treeCid: Cid, index: Natural
|
|
|
|
|
): Future[?!Cid] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2024-01-08 16:52:46 -06:00
|
|
|
## Get a cid given a tree and index
|
|
|
|
|
##
|
2025-05-21 23:17:04 +02:00
|
|
|
|
2024-01-08 16:52:46 -06:00
|
|
|
raiseAssert("getCid by treecid not implemented!")
|
|
|
|
|
|
2025-01-10 15:12:37 +01:00
|
|
|
method getBlock*(
|
|
|
|
|
self: BlockStore, address: BlockAddress
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!Block] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2022-07-28 03:39:17 +03:00
|
|
|
## Get a block from the blockstore
|
2021-02-25 18:23:22 -06:00
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("getBlock by addr not implemented!")
|
|
|
|
|
|
2025-06-24 18:05:25 +03:00
|
|
|
method completeBlock*(
|
|
|
|
|
self: BlockStore, address: BlockAddress, blk: Block
|
|
|
|
|
) {.base, gcsafe.} =
|
|
|
|
|
discard
|
|
|
|
|
|
2025-11-13 07:47:02 +02:00
|
|
|
method getBlocks*(
|
|
|
|
|
self: BlockStore, addresses: seq[BlockAddress]
|
|
|
|
|
): Future[SafeAsyncIter[Block]] {.async: (raises: [CancelledError]).} =
|
|
|
|
|
## Gets a set of blocks from the blockstore. Blocks might
|
|
|
|
|
## be returned in any order.
|
|
|
|
|
|
|
|
|
|
raiseAssert("getBlocks not implemented!")
|
|
|
|
|
|
2025-01-10 15:12:37 +01:00
|
|
|
method getBlockAndProof*(
|
|
|
|
|
self: BlockStore, treeCid: Cid, index: Natural
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!(Block, CodexProof)] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-14 13:02:17 +01:00
|
|
|
## Get a block and associated inclusion proof by Cid of a merkle tree and an index of a leaf in a tree
|
2023-11-14 11:52:27 -06:00
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("getBlockAndProof not implemented!")
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2022-12-02 18:00:55 -06:00
|
|
|
method putBlock*(
|
2025-01-10 15:12:37 +01:00
|
|
|
self: BlockStore, blk: Block, ttl = Duration.none
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!void] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2021-02-25 18:23:22 -06:00
|
|
|
## Put a block to the blockstore
|
|
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("putBlock not implemented!")
|
|
|
|
|
|
2024-01-08 16:52:46 -06:00
|
|
|
method putCidAndProof*(
|
2023-11-14 13:02:17 +01:00
|
|
|
self: BlockStore, treeCid: Cid, index: Natural, blockCid: Cid, proof: CodexProof
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!void] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2024-01-08 16:52:46 -06:00
|
|
|
## Put a block proof to the blockstore
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
raiseAssert("putCidAndProof not implemented!")
|
|
|
|
|
|
|
|
|
|
method getCidAndProof*(
|
|
|
|
|
self: BlockStore, treeCid: Cid, index: Natural
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!(Cid, CodexProof)] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2024-01-08 16:52:46 -06:00
|
|
|
## Get a block proof from the blockstore
|
2023-11-14 13:02:17 +01:00
|
|
|
##
|
|
|
|
|
|
2024-01-17 13:24:34 -06:00
|
|
|
raiseAssert("getCidAndProof not implemented!")
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-11-06 09:10:30 +01:00
|
|
|
method ensureExpiry*(
|
2025-01-10 15:12:37 +01:00
|
|
|
self: BlockStore, cid: Cid, expiry: SecondsSince1970
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!void] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-06 09:10:30 +01:00
|
|
|
## Ensure that block's assosicated expiry is at least given timestamp
|
|
|
|
|
## If the current expiry is lower then it is updated to the given one, otherwise it is left intact
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
raiseAssert("Not implemented!")
|
|
|
|
|
|
2023-11-22 11:09:12 +01:00
|
|
|
method ensureExpiry*(
|
2025-01-10 15:12:37 +01:00
|
|
|
self: BlockStore, treeCid: Cid, index: Natural, expiry: SecondsSince1970
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!void] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-22 11:09:12 +01:00
|
|
|
## Ensure that block's associated expiry is at least given timestamp
|
|
|
|
|
## If the current expiry is lower then it is updated to the given one, otherwise it is left intact
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
raiseAssert("Not implemented!")
|
|
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
method delBlock*(
|
|
|
|
|
self: BlockStore, cid: Cid
|
|
|
|
|
): Future[?!void] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2022-06-28 19:10:05 +03:00
|
|
|
## Delete a block from the blockstore
|
2021-02-25 18:23:22 -06:00
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("delBlock not implemented!")
|
|
|
|
|
|
2025-01-10 15:12:37 +01:00
|
|
|
method delBlock*(
|
|
|
|
|
self: BlockStore, treeCid: Cid, index: Natural
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!void] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-14 13:02:17 +01:00
|
|
|
## Delete a block from the blockstore
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
raiseAssert("delBlock not implemented!")
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
method hasBlock*(
|
|
|
|
|
self: BlockStore, cid: Cid
|
|
|
|
|
): Future[?!bool] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2022-01-10 09:32:56 -06:00
|
|
|
## Check if the block exists in the blockstore
|
|
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("hasBlock not implemented!")
|
|
|
|
|
|
2025-01-10 15:12:37 +01:00
|
|
|
method hasBlock*(
|
|
|
|
|
self: BlockStore, tree: Cid, index: Natural
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!bool] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-14 13:02:17 +01:00
|
|
|
## Check if the block exists in the blockstore
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
raiseAssert("hasBlock not implemented!")
|
2022-01-10 09:32:56 -06:00
|
|
|
|
2022-12-02 18:00:55 -06:00
|
|
|
method listBlocks*(
|
2025-01-10 15:12:37 +01:00
|
|
|
self: BlockStore, blockType = BlockType.Manifest
|
2025-05-21 23:17:04 +02:00
|
|
|
): Future[?!SafeAsyncIter[Cid]] {.base, async: (raises: [CancelledError]), gcsafe.} =
|
2022-04-13 18:32:35 +02:00
|
|
|
## Get the list of blocks in the BlockStore. This is an intensive operation
|
|
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("listBlocks not implemented!")
|
2022-04-13 18:32:35 +02:00
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
method close*(self: BlockStore): Future[void] {.base, async: (raises: []), gcsafe.} =
|
2022-07-22 18:38:49 -05:00
|
|
|
## Close the blockstore, cleaning up resources managed by it.
|
|
|
|
|
## For some implementations this may be a no-op
|
|
|
|
|
##
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
raiseAssert("close not implemented!")
|
2022-07-22 18:38:49 -05:00
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
proc contains*(
|
|
|
|
|
self: BlockStore, blk: Cid
|
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]), gcsafe.} =
|
2022-07-28 03:39:17 +03:00
|
|
|
## Check if the block exists in the blockstore.
|
|
|
|
|
## Return false if error encountered
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
return (await self.hasBlock(blk)) |? false
|
2023-11-14 13:02:17 +01:00
|
|
|
|
2025-05-21 23:17:04 +02:00
|
|
|
proc contains*(
|
|
|
|
|
self: BlockStore, address: BlockAddress
|
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]), gcsafe.} =
|
2023-11-14 13:02:17 +01:00
|
|
|
return
|
|
|
|
|
if address.leaf:
|
|
|
|
|
(await self.hasBlock(address.treeCid, address.index)) |? false
|
|
|
|
|
else:
|
|
|
|
|
(await self.hasBlock(address.cid)) |? false
|