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
|
2022-05-24 05:24:15 +00:00
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/manifest
|
|
|
|
import pkg/codex/rng
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
import ./helpers/nodeutils
|
|
|
|
import ./helpers/randomchunker
|
2022-05-26 02:29:31 +00:00
|
|
|
import ./helpers/mockdiscovery
|
2022-07-18 08:46:24 +00:00
|
|
|
import ./helpers/eventually
|
2023-06-22 18:01:21 +00:00
|
|
|
import ../checktest
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
export randomchunker, nodeutils, mockdiscovery, eventually, checktest
|
2021-08-30 19:25:20 +00:00
|
|
|
|
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!!!
|
2022-05-24 05:24:15 +00:00
|
|
|
func `==`*(a, b: bt.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
|
|
|
|
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
|
|
|
|
|
|
|
proc corruptBlocks*(
|
2023-06-22 15:11:18 +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]
|
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
doAssert blks < manifest.len
|
|
|
|
while pos.len < blks:
|
|
|
|
let i = Rng.instance.rand(manifest.len - 1)
|
|
|
|
if pos.find(i) >= 0:
|
2022-05-24 05:24:15 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
pos.add(i)
|
|
|
|
var
|
2022-08-19 00:56:36 +00:00
|
|
|
blk = (await store.getBlock(manifest[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
|