2022-05-19 14:56:03 -05:00
## Nim-Codex
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.
2022-03-18 16:17:51 -06:00
import pkg / upraises
push : {. upraises : [ ] . }
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
2021-02-25 18:23:22 -06:00
import .. / blocktype
2023-10-12 15:56:10 +02: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
2021-02-25 18:23:22 -06:00
BlockStore * = ref object of RootObj
2023-11-01 11:18:33 +01:00
method getBlock * ( self : BlockStore , cid : Cid ) : Future [ ? ! Block ] {. base . } =
## Get a block from the blockstore
##
raiseAssert ( " getBlock by cid Not implemented! " )
method getBlock * ( self : BlockStore , treeCid : Cid , index : Natural ) : Future [ ? ! Block ] {. base . } =
## Get a block from the blockstore
##
raiseAssert ( " getBlock by treecid Not implemented! " )
2023-10-26 13:23:23 +02:00
method getBlock * ( self : BlockStore , address : BlockAddress ) : Future [ ? ! Block ] {. base . } =
2022-07-28 03:39:17 +03:00
## Get a block from the blockstore
2021-02-25 18:23:22 -06:00
##
2023-11-01 11:18:33 +01:00
raiseAssert ( " getBlock by addr Not implemented! " )
2021-02-25 18:23:22 -06:00
2023-10-12 15:56:10 +02:00
method getBlockAndProof * ( self : BlockStore , treeCid : Cid , index : Natural ) : Future [ ? ! ( Block , MerkleProof ) ] {. base . } =
## Get a block and associated inclusion proof by Cid of a merkle tree and an index of a leaf in a tree
##
2023-11-01 11:18:33 +01:00
raiseAssert ( " getBlockAndProof Not implemented! " )
2023-10-12 15:56:10 +02:00
2022-12-02 18:00:55 -06:00
method putBlock * (
2023-06-22 08:11:18 -07:00
self : BlockStore ,
blk : Block ,
ttl = Duration . none
) : Future [ ? ! void ] {. base . } =
2021-02-25 18:23:22 -06:00
## Put a block to the blockstore
##
2023-11-01 11:18:33 +01:00
raiseAssert ( " putBlock Not implemented! " )
2021-02-25 18:23:22 -06:00
2023-10-26 13:23:23 +02:00
# I cant use `BlockAddress` to wrap (treeCid, index) here. because as far as I know there's no way in Nim to force `assert(leaf == true)` in a compile time
method putBlockCidAndProof * (
self : BlockStore ,
treeCid : Cid ,
2023-11-01 11:18:33 +01:00
index : Natural ,
2023-10-26 13:23:23 +02:00
blockCid : Cid ,
proof : MerkleProof
) : Future [ ? ! void ] {. base . } =
## Put a block to the blockstore
##
2023-11-01 11:18:33 +01:00
raiseAssert ( " putBlockCidAndProof Not implemented! " )
2023-10-26 13:23:23 +02:00
2022-07-28 03:39:17 +03:00
method delBlock * ( self : BlockStore , cid : Cid ) : Future [ ? ! void ] {. base . } =
2022-06-28 19:10:05 +03:00
## Delete a block from the blockstore
2021-02-25 18:23:22 -06:00
##
2022-03-18 16:17:51 -06:00
raiseAssert ( " Not implemented! " )
2021-02-25 18:23:22 -06:00
2023-10-12 15:56:10 +02:00
method delBlock * ( self : BlockStore , treeCid : Cid , index : Natural ) : Future [ ? ! void ] {. base . } =
## Delete a block from the blockstore
##
raiseAssert ( " Not implemented! " )
2022-07-28 03:39:17 +03:00
method hasBlock * ( self : BlockStore , cid : Cid ) : Future [ ? ! bool ] {. base . } =
2022-01-10 09:32:56 -06:00
## Check if the block exists in the blockstore
##
2022-07-28 03:39:17 +03:00
raiseAssert ( " Not implemented! " )
2022-01-10 09:32:56 -06:00
2023-10-12 15:56:10 +02:00
method hasBlock * ( self : BlockStore , tree : Cid , index : Natural ) : Future [ ? ! bool ] {. base . } =
## Check if the block exists in the blockstore
##
raiseAssert ( " Not implemented! " )
2022-12-02 18:00:55 -06:00
method listBlocks * (
self : BlockStore ,
2023-10-12 15:56:10 +02:00
blockType = BlockType . Manifest ) : Future [ ? ! AsyncIter [ ? Cid ] ] {. base . } =
2022-04-13 18:32:35 +02:00
## Get the list of blocks in the BlockStore. This is an intensive operation
##
raiseAssert ( " Not implemented! " )
2023-03-10 08:02:54 +01:00
method close * ( self : BlockStore ) : Future [ void ] {. base . } =
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
##
raiseAssert ( " Not implemented! " )
2022-07-28 03:39:17 +03: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-10-12 15:56:10 +02: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