2022-05-25 00:42:49 +00:00
|
|
|
import std/os
|
2022-05-26 02:29:31 +00:00
|
|
|
import std/sequtils
|
2022-05-25 00:42:49 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/asynctest
|
|
|
|
|
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/streams
|
|
|
|
import pkg/codex/storageproofs as st
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
|
|
|
|
import ../helpers
|
|
|
|
|
|
|
|
const
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize = 31'nb * 64'nb
|
2022-05-25 00:42:49 +00:00
|
|
|
DataSetSize = BlockSize * 100
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Test PoR store":
|
2022-05-25 00:42:49 +00:00
|
|
|
let
|
2022-05-26 02:29:31 +00:00
|
|
|
blocks = toSeq([1, 5, 10, 14, 20, 12, 22]) # TODO: maybe make them random
|
2022-05-25 00:42:49 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
chunker: RandomChunker
|
|
|
|
manifest: Manifest
|
|
|
|
store: BlockStore
|
|
|
|
ssk: st.SecretKey
|
|
|
|
spk: st.PublicKey
|
|
|
|
repoDir: string
|
|
|
|
stpstore: st.StpStore
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream: StoreStream
|
2022-05-25 00:42:49 +00:00
|
|
|
por: PoR
|
2022-05-26 02:29:31 +00:00
|
|
|
porMsg: PorMessage
|
2022-05-25 00:42:49 +00:00
|
|
|
cid: Cid
|
2022-05-26 02:29:31 +00:00
|
|
|
tags: seq[Tag]
|
2022-05-25 00:42:49 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
setup:
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = DataSetSize.int, chunkSize = BlockSize)
|
2022-05-25 00:42:49 +00:00
|
|
|
store = CacheStore.new(cacheSize = DataSetSize, chunkSize = BlockSize)
|
|
|
|
manifest = Manifest.new(blockSize = BlockSize).tryGet()
|
|
|
|
(spk, ssk) = st.keyGen()
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
2022-05-25 00:42:49 +00:00
|
|
|
manifest.add(blk.cid)
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(blk)).tryGet()
|
2022-05-25 00:42:49 +00:00
|
|
|
|
|
|
|
cid = manifest.cid.tryGet()
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream = StoreStream.new(store, manifest)
|
2022-05-25 00:42:49 +00:00
|
|
|
por = await PoR.init(
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream,
|
2022-05-25 00:42:49 +00:00
|
|
|
ssk, spk,
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize.int)
|
2022-05-25 00:42:49 +00:00
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
porMsg = por.toMessage()
|
|
|
|
tags = blocks.mapIt(
|
|
|
|
Tag(idx: it, tag: porMsg.authenticators[it]) )
|
|
|
|
|
2022-07-22 23:38:49 +00:00
|
|
|
repoDir = getAppDir() / "stp"
|
2022-05-25 00:42:49 +00:00
|
|
|
createDir(repoDir)
|
|
|
|
stpstore = st.StpStore.init(repoDir)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
teardown:
|
|
|
|
await close(porStream)
|
2022-05-25 00:42:49 +00:00
|
|
|
removeDir(repoDir)
|
|
|
|
|
|
|
|
test "Should store Storage Proofs":
|
2022-05-26 02:29:31 +00:00
|
|
|
check (await stpstore.store(por.toMessage(), cid)).isOk
|
|
|
|
check fileExists(stpstore.stpPath(cid) / "por")
|
2022-05-25 00:42:49 +00:00
|
|
|
|
|
|
|
test "Should retrieve Storage Proofs":
|
2023-06-22 18:01:21 +00:00
|
|
|
discard await stpstore.store(por.toMessage(), cid)
|
2022-05-26 02:29:31 +00:00
|
|
|
check (await stpstore.retrieve(cid)).tryGet() == porMsg
|
|
|
|
|
|
|
|
test "Should store tags":
|
|
|
|
check (await stpstore.store(tags, cid)).isOk
|
|
|
|
for t in tags:
|
|
|
|
check fileExists(stpstore.stpPath(cid) / $t.idx )
|
|
|
|
|
|
|
|
test "Should retrieve tags":
|
2023-06-22 18:01:21 +00:00
|
|
|
discard await stpstore.store(tags, cid)
|
2022-05-26 02:29:31 +00:00
|
|
|
check (await stpstore.retrieve(cid, blocks)).tryGet() == tags
|