2022-12-03 00:00:55 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/strutils
|
|
|
|
import std/options
|
|
|
|
|
|
|
|
import pkg/chronos
|
2024-01-10 21:25:57 +00:00
|
|
|
import pkg/asynctest/chronos/unittest
|
2023-08-01 23:47:57 +00:00
|
|
|
import pkg/libp2p/multicodec
|
2022-12-03 00:00:55 +00:00
|
|
|
import pkg/stew/byteutils
|
2023-08-01 23:47:57 +00:00
|
|
|
import pkg/questionable
|
2022-12-03 00:00:55 +00:00
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/codex/stores/cachestore
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/manifest
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/codex/merkletree
|
|
|
|
import pkg/codex/utils
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
import ../helpers
|
|
|
|
|
|
|
|
type
|
|
|
|
StoreProvider* = proc(): BlockStore {.gcsafe.}
|
|
|
|
Before* = proc(): Future[void] {.gcsafe.}
|
|
|
|
After* = proc(): Future[void] {.gcsafe.}
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc commonBlockStoreTests*(name: string,
|
|
|
|
provider: StoreProvider,
|
|
|
|
before: Before = nil,
|
|
|
|
after: After = nil) =
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite name & " Store Common":
|
2022-12-03 00:00:55 +00:00
|
|
|
var
|
|
|
|
newBlock, newBlock1, newBlock2, newBlock3: Block
|
2023-11-14 12:02:17 +00:00
|
|
|
manifest: Manifest
|
2023-12-21 06:41:43 +00:00
|
|
|
tree: CodexTree
|
2022-12-03 00:00:55 +00:00
|
|
|
store: BlockStore
|
|
|
|
|
|
|
|
setup:
|
|
|
|
newBlock = Block.new("New Kids on the Block".toBytes()).tryGet()
|
|
|
|
newBlock1 = Block.new("1".repeat(100).toBytes()).tryGet()
|
|
|
|
newBlock2 = Block.new("2".repeat(100).toBytes()).tryGet()
|
|
|
|
newBlock3 = Block.new("3".repeat(100).toBytes()).tryGet()
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
(manifest, tree) = makeManifestAndTree(@[newBlock, newBlock1, newBlock2, newBlock3]).tryGet()
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
if not isNil(before):
|
|
|
|
await before()
|
|
|
|
|
|
|
|
store = provider()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await store.close()
|
|
|
|
|
|
|
|
if not isNil(after):
|
|
|
|
await after()
|
|
|
|
|
|
|
|
test "putBlock":
|
|
|
|
(await store.putBlock(newBlock1)).tryGet()
|
|
|
|
check (await store.hasBlock(newBlock1.cid)).tryGet()
|
|
|
|
|
|
|
|
test "getBlock":
|
|
|
|
(await store.putBlock(newBlock)).tryGet()
|
|
|
|
let blk = await store.getBlock(newBlock.cid)
|
|
|
|
check blk.tryGet() == newBlock
|
|
|
|
|
|
|
|
test "fail getBlock":
|
|
|
|
expect BlockNotFoundError:
|
|
|
|
discard (await store.getBlock(newBlock.cid)).tryGet()
|
|
|
|
|
|
|
|
test "hasBlock":
|
|
|
|
(await store.putBlock(newBlock)).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
(await store.hasBlock(newBlock.cid)).tryGet()
|
|
|
|
await newBlock.cid in store
|
|
|
|
|
|
|
|
test "fail hasBlock":
|
|
|
|
check:
|
|
|
|
not (await store.hasBlock(newBlock.cid)).tryGet()
|
|
|
|
not (await newBlock.cid in store)
|
|
|
|
|
|
|
|
test "delBlock":
|
|
|
|
(await store.putBlock(newBlock1)).tryGet()
|
|
|
|
check (await store.hasBlock(newBlock1.cid)).tryGet()
|
|
|
|
|
|
|
|
(await store.delBlock(newBlock1.cid)).tryGet()
|
|
|
|
|
|
|
|
check not (await store.hasBlock(newBlock1.cid)).tryGet()
|
|
|
|
|
|
|
|
test "listBlocks Blocks":
|
|
|
|
let
|
|
|
|
blocks = @[newBlock1, newBlock2, newBlock3]
|
|
|
|
|
|
|
|
putHandles = await allFinished(
|
|
|
|
blocks.mapIt( store.putBlock( it ) ))
|
|
|
|
|
|
|
|
for handle in putHandles:
|
|
|
|
check not handle.failed
|
2023-03-10 07:02:54 +00:00
|
|
|
check handle.read.isOk
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cids = (await store.listBlocks(blockType = BlockType.Block)).tryGet()
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
for c in cids:
|
2023-08-01 23:47:57 +00:00
|
|
|
if cid =? await c:
|
2022-12-03 00:00:55 +00:00
|
|
|
check (await store.hasBlock(cid)).tryGet()
|
|
|
|
count.inc
|
|
|
|
|
|
|
|
check count == 3
|
|
|
|
|
|
|
|
test "listBlocks Manifest":
|
|
|
|
let
|
|
|
|
blocks = @[newBlock1, newBlock2, newBlock3]
|
2023-12-22 12:04:01 +00:00
|
|
|
manifestBlock = Block.new(manifest.encode().tryGet(), codec = ManifestCodec).tryGet()
|
2023-11-14 12:02:17 +00:00
|
|
|
treeBlock = Block.new(tree.encode()).tryGet()
|
2022-12-03 00:00:55 +00:00
|
|
|
putHandles = await allFinished(
|
2023-11-14 12:02:17 +00:00
|
|
|
(@[treeBlock, manifestBlock] & blocks).mapIt( store.putBlock( it ) ))
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
for handle in putHandles:
|
|
|
|
check not handle.failed
|
2023-03-10 07:02:54 +00:00
|
|
|
check handle.read.isOk
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cids = (await store.listBlocks(blockType = BlockType.Manifest)).tryGet()
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
for c in cids:
|
|
|
|
if cid =? (await c):
|
|
|
|
check manifestBlock.cid == cid
|
|
|
|
check (await store.hasBlock(cid)).tryGet()
|
|
|
|
count.inc
|
|
|
|
|
|
|
|
check count == 1
|
|
|
|
|
|
|
|
test "listBlocks Both":
|
|
|
|
let
|
|
|
|
blocks = @[newBlock1, newBlock2, newBlock3]
|
2023-12-22 12:04:01 +00:00
|
|
|
manifestBlock = Block.new(manifest.encode().tryGet(), codec = ManifestCodec).tryGet()
|
2023-11-14 12:02:17 +00:00
|
|
|
treeBlock = Block.new(tree.encode()).tryGet()
|
2022-12-03 00:00:55 +00:00
|
|
|
putHandles = await allFinished(
|
2023-11-14 12:02:17 +00:00
|
|
|
(@[treeBlock, manifestBlock] & blocks).mapIt( store.putBlock( it ) ))
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
for handle in putHandles:
|
|
|
|
check not handle.failed
|
2023-03-10 07:02:54 +00:00
|
|
|
check handle.read.isOk
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cids = (await store.listBlocks(blockType = BlockType.Both)).tryGet()
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
for c in cids:
|
|
|
|
if cid =? (await c):
|
|
|
|
check (await store.hasBlock(cid)).tryGet()
|
|
|
|
count.inc
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
check count == 5
|