2023-11-14 12:02:17 +00:00
|
|
|
import std/sequtils
|
|
|
|
|
2022-05-24 05:24:15 +00:00
|
|
|
import pkg/chronos
|
2022-03-18 19:50:53 +00:00
|
|
|
import pkg/libp2p
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/libp2p/varint
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/codex/blocktype
|
2022-05-24 05:24:15 +00:00
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/manifest
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/codex/merkletree
|
|
|
|
import pkg/codex/blockexchange
|
2022-05-24 05:24:15 +00:00
|
|
|
import pkg/codex/rng
|
2024-01-08 22:52:46 +00:00
|
|
|
import pkg/codex/utils
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
import ./helpers/nodeutils
|
|
|
|
import ./helpers/randomchunker
|
2023-11-14 12:02:17 +00:00
|
|
|
import ./helpers/mockchunker
|
2022-05-26 02:29:31 +00:00
|
|
|
import ./helpers/mockdiscovery
|
2023-09-04 14:42:09 +00:00
|
|
|
import ./helpers/always
|
2023-06-22 18:01:21 +00:00
|
|
|
import ../checktest
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
export randomchunker, nodeutils, mockdiscovery, mockchunker, always, checktest, manifest
|
2021-08-30 19:25:20 +00:00
|
|
|
|
2023-08-01 23:47:57 +00:00
|
|
|
export libp2p except setup, eventually
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
# NOTE: The meaning of equality for blocks
|
|
|
|
# is changed here, because blocks are now `ref`
|
|
|
|
# types. This is only in tests!!!
|
2023-11-14 12:02:17 +00:00
|
|
|
func `==`*(a, b: Block): bool =
|
2022-07-28 00:39:17 +00:00
|
|
|
(a.cid == b.cid) and (a.data == b.data)
|
2022-03-18 19:50:53 +00:00
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
proc calcEcBlocksCount*(blocksCount: int, ecK, ecM: int): int =
|
|
|
|
let
|
|
|
|
rounded = roundUp(blocksCount, ecK)
|
|
|
|
steps = divUp(blocksCount, ecK)
|
|
|
|
|
|
|
|
rounded + (steps * ecM)
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
proc lenPrefix*(msg: openArray[byte]): seq[byte] =
|
|
|
|
## Write `msg` with a varint-encoded length prefix
|
|
|
|
##
|
|
|
|
|
|
|
|
let vbytes = PB.toBytes(msg.len().uint64)
|
|
|
|
var buf = newSeqUninitialized[byte](msg.len() + vbytes.len)
|
|
|
|
buf[0..<vbytes.len] = vbytes.toOpenArray()
|
|
|
|
buf[vbytes.len..<buf.len] = msg
|
|
|
|
|
|
|
|
return buf
|
2022-05-24 05:24:15 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
proc makeManifestAndTree*(blocks: seq[Block]): ?!(Manifest, CodexTree) =
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
if blocks.len == 0:
|
|
|
|
return failure("Blocks list was empty")
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
let
|
2023-11-14 12:02:17 +00:00
|
|
|
datasetSize = blocks.mapIt(it.data.len).foldl(a + b)
|
|
|
|
blockSize = blocks.mapIt(it.data.len).foldl(max(a, b))
|
2023-12-21 06:41:43 +00:00
|
|
|
tree = ? CodexTree.init(blocks.mapIt(it.cid))
|
2023-11-14 12:02:17 +00:00
|
|
|
treeCid = ? tree.rootCid
|
|
|
|
manifest = Manifest.new(
|
|
|
|
treeCid = treeCid,
|
|
|
|
blockSize = NBytes(blockSize),
|
2023-12-21 06:41:43 +00:00
|
|
|
datasetSize = NBytes(datasetSize))
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
return success((manifest, tree))
|
|
|
|
|
|
|
|
proc makeWantList*(
|
|
|
|
cids: seq[Cid],
|
|
|
|
priority: int = 0,
|
|
|
|
cancel: bool = false,
|
|
|
|
wantType: WantType = WantType.WantHave,
|
|
|
|
full: bool = false,
|
|
|
|
sendDontHave: bool = false
|
|
|
|
): WantList =
|
|
|
|
WantList(
|
|
|
|
entries: cids.mapIt(
|
|
|
|
WantListEntry(
|
|
|
|
address: BlockAddress(leaf: false, cid: it),
|
|
|
|
priority: priority.int32,
|
|
|
|
cancel: cancel,
|
|
|
|
wantType: wantType,
|
|
|
|
sendDontHave: sendDontHave) ),
|
|
|
|
full: full)
|
|
|
|
|
|
|
|
proc storeDataGetManifest*(store: BlockStore, chunker: Chunker): Future[Manifest] {.async.} =
|
|
|
|
var cids = newSeq[Cid]()
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
|
|
|
let blk = Block.new(chunk).tryGet()
|
|
|
|
cids.add(blk.cid)
|
|
|
|
(await store.putBlock(blk)).tryGet()
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
let
|
|
|
|
tree = CodexTree.init(cids).tryGet()
|
2023-11-14 12:02:17 +00:00
|
|
|
treeCid = tree.rootCid.tryGet()
|
|
|
|
manifest = Manifest.new(
|
|
|
|
treeCid = treeCid,
|
|
|
|
blockSize = NBytes(chunker.chunkSize),
|
2023-12-21 06:41:43 +00:00
|
|
|
datasetSize = NBytes(chunker.offset))
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
for i in 0..<tree.leavesCount:
|
|
|
|
let proof = tree.getProof(i).tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
(await store.putCidAndProof(treeCid, i, cids[i], proof)).tryGet()
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
return manifest
|
|
|
|
|
2022-05-24 05:24:15 +00:00
|
|
|
proc corruptBlocks*(
|
2023-09-25 14:31:10 +00:00
|
|
|
store: BlockStore,
|
|
|
|
manifest: Manifest,
|
|
|
|
blks, bytes: int): Future[seq[int]] {.async.} =
|
2022-05-24 05:24:15 +00:00
|
|
|
var pos: seq[int]
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
doAssert blks < manifest.blocksCount
|
2022-11-02 17:40:28 +00:00
|
|
|
while pos.len < blks:
|
2023-11-14 12:02:17 +00:00
|
|
|
let i = Rng.instance.rand(manifest.blocksCount - 1)
|
2022-11-02 17:40:28 +00:00
|
|
|
if pos.find(i) >= 0:
|
2022-05-24 05:24:15 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
pos.add(i)
|
|
|
|
var
|
2023-11-14 12:02:17 +00:00
|
|
|
blk = (await store.getBlock(manifest.treeCid, i)).tryGet()
|
2022-05-24 05:24:15 +00:00
|
|
|
bytePos: seq[int]
|
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
doAssert bytes < blk.data.len
|
|
|
|
while bytePos.len <= bytes:
|
|
|
|
let ii = Rng.instance.rand(blk.data.len - 1)
|
|
|
|
if bytePos.find(ii) >= 0:
|
2022-05-24 05:24:15 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
bytePos.add(ii)
|
|
|
|
blk.data[ii] = byte 0
|
|
|
|
return pos
|