2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2021-02-26 00:23:22 +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: [].}
|
2021-08-30 19:25:20 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
2022-12-03 00:00:55 +00:00
|
|
|
import pkg/questionable
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/questionable/results
|
2021-08-30 19:25:20 +00:00
|
|
|
|
2023-11-06 08:10:30 +00:00
|
|
|
import ../clock
|
2021-02-26 00:23:22 +00:00
|
|
|
import ../blocktype
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../merkletree
|
|
|
|
import ../utils
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-08-01 23:47:57 +00:00
|
|
|
export blocktype
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
type
|
2022-12-03 00:00:55 +00:00
|
|
|
BlockNotFoundError* = object of CodexError
|
|
|
|
|
|
|
|
BlockType* {.pure.} = enum
|
|
|
|
Manifest, Block, Both
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
BlockStore* = ref object of RootObj
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
method getBlock*(self: BlockStore, cid: Cid): Future[?!Block] {.base.} =
|
|
|
|
## Get a block from the blockstore
|
|
|
|
##
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("getBlock by cid not implemented!")
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
method getBlock*(self: BlockStore, treeCid: Cid, index: Natural): Future[?!Block] {.base.} =
|
|
|
|
## Get a block from the blockstore
|
|
|
|
##
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("getBlock by treecid not implemented!")
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
method getCid*(self: BlockStore, treeCid: Cid, index: Natural): Future[?!Cid] {.base.} =
|
|
|
|
## Get a cid given a tree and index
|
|
|
|
##
|
|
|
|
raiseAssert("getCid by treecid not implemented!")
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
method getBlock*(self: BlockStore, address: BlockAddress): Future[?!Block] {.base.} =
|
2022-07-28 00:39:17 +00:00
|
|
|
## Get a block from the blockstore
|
2021-02-26 00:23:22 +00:00
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("getBlock by addr not implemented!")
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
method getBlockAndProof*(self: BlockStore, treeCid: Cid, index: Natural): Future[?!(Block, CodexProof)] {.base.} =
|
2023-11-14 12:02:17 +00: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 17:52:27 +00:00
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("getBlockAndProof not implemented!")
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
method putBlock*(
|
2024-02-16 21:51:39 +00:00
|
|
|
self: BlockStore,
|
|
|
|
blk: Block,
|
|
|
|
ttl = Duration.none): Future[?!void] {.base.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Put a block to the blockstore
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("putBlock not implemented!")
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
method putCidAndProof*(
|
2023-11-14 12:02:17 +00:00
|
|
|
self: BlockStore,
|
|
|
|
treeCid: Cid,
|
|
|
|
index: Natural,
|
|
|
|
blockCid: Cid,
|
2024-02-16 21:51:39 +00:00
|
|
|
proof: CodexProof): Future[?!void] {.base.} =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Put a block proof to the blockstore
|
|
|
|
##
|
|
|
|
|
|
|
|
raiseAssert("putCidAndProof not implemented!")
|
|
|
|
|
|
|
|
method getCidAndProof*(
|
|
|
|
self: BlockStore,
|
|
|
|
treeCid: Cid,
|
|
|
|
index: Natural): Future[?!(Cid, CodexProof)] {.base.} =
|
|
|
|
## Get a block proof from the blockstore
|
2023-11-14 12:02:17 +00:00
|
|
|
##
|
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
raiseAssert("getCidAndProof not implemented!")
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-06 08:10:30 +00:00
|
|
|
method ensureExpiry*(
|
2024-02-16 21:51:39 +00:00
|
|
|
self: BlockStore,
|
|
|
|
cid: Cid,
|
|
|
|
expiry: SecondsSince1970): Future[?!void] {.base.} =
|
2023-11-06 08:10:30 +00: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 10:09:12 +00:00
|
|
|
method ensureExpiry*(
|
2024-02-16 21:51:39 +00:00
|
|
|
self: BlockStore,
|
|
|
|
treeCid: Cid,
|
|
|
|
index: Natural,
|
|
|
|
expiry: SecondsSince1970): Future[?!void] {.base.} =
|
2023-11-22 10:09:12 +00: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!")
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method delBlock*(self: BlockStore, cid: Cid): Future[?!void] {.base.} =
|
2022-06-28 16:10:05 +00:00
|
|
|
## Delete a block from the blockstore
|
2021-02-26 00:23:22 +00:00
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("delBlock not implemented!")
|
|
|
|
|
|
|
|
method delBlock*(self: BlockStore, treeCid: Cid, index: Natural): Future[?!void] {.base.} =
|
|
|
|
## Delete a block from the blockstore
|
|
|
|
##
|
|
|
|
|
|
|
|
raiseAssert("delBlock not implemented!")
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method hasBlock*(self: BlockStore, cid: Cid): Future[?!bool] {.base.} =
|
2022-01-10 15:32:56 +00:00
|
|
|
## Check if the block exists in the blockstore
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("hasBlock not implemented!")
|
|
|
|
|
|
|
|
method hasBlock*(self: BlockStore, tree: Cid, index: Natural): Future[?!bool] {.base.} =
|
|
|
|
## Check if the block exists in the blockstore
|
|
|
|
##
|
|
|
|
|
|
|
|
raiseAssert("hasBlock not implemented!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
method listBlocks*(
|
|
|
|
self: BlockStore,
|
2023-11-14 12:02:17 +00:00
|
|
|
blockType = BlockType.Manifest): Future[?!AsyncIter[?Cid]] {.base.} =
|
2022-04-13 16:32:35 +00:00
|
|
|
## Get the list of blocks in the BlockStore. This is an intensive operation
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("listBlocks not implemented!")
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
method close*(self: BlockStore): Future[void] {.base.} =
|
2022-07-22 23:38:49 +00:00
|
|
|
## Close the blockstore, cleaning up resources managed by it.
|
|
|
|
## For some implementations this may be a no-op
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
raiseAssert("close not implemented!")
|
2022-07-22 23:38:49 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
proc contains*(self: BlockStore, blk: Cid): Future[bool] {.async.} =
|
|
|
|
## Check if the block exists in the blockstore.
|
|
|
|
## Return false if error encountered
|
|
|
|
##
|
|
|
|
|
|
|
|
return (await self.hasBlock(blk)) |? false
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
proc contains*(self: BlockStore, address: BlockAddress): Future[bool] {.async.} =
|
|
|
|
return if address.leaf:
|
|
|
|
(await self.hasBlock(address.treeCid, address.index)) |? false
|
|
|
|
else:
|
|
|
|
(await self.hasBlock(address.cid)) |? false
|