2022-04-05 19:12:59 +00:00
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/erasure
|
|
|
|
import pkg/codex/manifest
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/rng
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
|
|
|
|
suite "Erasure encode/decode":
|
|
|
|
|
2022-09-05 06:54:28 +00:00
|
|
|
const
|
|
|
|
localBlockSize = 43*64 # check that code can work with non-default BlockSize
|
|
|
|
# (but it should be x*64 for Leopard)
|
|
|
|
dataSetSize = BlockSize * 123 # ... and weird geometry
|
2022-05-10 12:10:17 +00:00
|
|
|
|
|
|
|
var rng: Rng
|
|
|
|
var chunker: Chunker
|
|
|
|
var manifest: Manifest
|
|
|
|
var store: BlockStore
|
|
|
|
var erasure: Erasure
|
|
|
|
|
|
|
|
setup:
|
|
|
|
rng = Rng.instance()
|
2022-09-05 06:54:28 +00:00
|
|
|
chunker = RandomChunker.new(rng, size = dataSetSize, chunkSize = localBlockSize)
|
|
|
|
manifest = !Manifest.new(blockSize = localBlockSize)
|
|
|
|
store = CacheStore.new(cacheSize = dataSetSize*2, chunkSize = localBlockSize)
|
2022-05-10 12:10:17 +00:00
|
|
|
erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider)
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
manifest.add(blk.cid)
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(blk)).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
proc encode(buffers, parity: int): Future[Manifest] {.async.} =
|
2022-04-05 19:12:59 +00:00
|
|
|
let
|
|
|
|
encoded = (await erasure.encode(
|
|
|
|
manifest,
|
|
|
|
buffers,
|
|
|
|
parity)).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
encoded.len mod (buffers + parity) == 0
|
|
|
|
encoded.rounded == (manifest.len + (buffers - (manifest.len mod buffers)))
|
|
|
|
encoded.steps == encoded.rounded div buffers
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
return encoded
|
|
|
|
|
|
|
|
test "Should tolerate loosing M data blocks in a single random column":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 10
|
|
|
|
|
|
|
|
let encoded = await encode(buffers, parity)
|
|
|
|
|
2022-04-05 19:12:59 +00:00
|
|
|
var
|
|
|
|
column = rng.rand(encoded.len div encoded.steps) # random column
|
|
|
|
dropped: seq[Cid]
|
|
|
|
|
|
|
|
for _ in 0..<encoded.M:
|
|
|
|
dropped.add(encoded[column])
|
2022-06-28 16:10:05 +00:00
|
|
|
(await store.delBlock(encoded[column])).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
column.inc(encoded.steps)
|
|
|
|
|
|
|
|
var
|
|
|
|
decoded = (await erasure.decode(encoded)).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
decoded.cid.tryGet() == manifest.cid.tryGet()
|
|
|
|
decoded.cid.tryGet() == encoded.originalCid
|
|
|
|
decoded.len == encoded.originalLen
|
|
|
|
|
|
|
|
for d in dropped:
|
2022-07-28 00:39:17 +00:00
|
|
|
let present = await store.hasBlock(d)
|
|
|
|
check present.tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
test "Should not tolerate loosing more than M data blocks in a single random column":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 10
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
let encoded = await encode(buffers, parity)
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
column = rng.rand(encoded.len div encoded.steps) # random column
|
|
|
|
dropped: seq[Cid]
|
|
|
|
|
|
|
|
for _ in 0..<encoded.M + 1:
|
|
|
|
dropped.add(encoded[column])
|
2022-06-28 16:10:05 +00:00
|
|
|
(await store.delBlock(encoded[column])).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
column.inc(encoded.steps)
|
|
|
|
|
|
|
|
var
|
|
|
|
decoded: Manifest
|
|
|
|
|
|
|
|
expect ResultFailure:
|
|
|
|
decoded = (await erasure.decode(encoded)).tryGet()
|
|
|
|
|
|
|
|
for d in dropped:
|
2022-07-28 00:39:17 +00:00
|
|
|
let present = await store.hasBlock(d)
|
|
|
|
check not present.tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
test "Should tolerate loosing M data blocks in M random columns":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 10
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
let encoded = await encode(buffers, parity)
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
blocks: seq[int]
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
while offset < encoded.steps - 1:
|
|
|
|
let
|
|
|
|
blockIdx = toSeq(countup(offset, encoded.len - 1, encoded.steps))
|
|
|
|
|
|
|
|
for _ in 0..<encoded.M:
|
|
|
|
blocks.add(rng.sample(blockIdx, blocks))
|
|
|
|
offset.inc
|
|
|
|
|
|
|
|
for idx in blocks:
|
2022-06-28 16:10:05 +00:00
|
|
|
(await store.delBlock(encoded[idx])).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
discard (await erasure.decode(encoded)).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
for d in manifest:
|
2022-07-28 00:39:17 +00:00
|
|
|
let present = await store.hasBlock(d)
|
|
|
|
check present.tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
test "Should not tolerate loosing more than M data blocks in M random columns":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 10
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
let encoded = await encode(buffers, parity)
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
blocks: seq[int]
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
while offset < encoded.steps - 1:
|
|
|
|
let
|
|
|
|
blockIdx = toSeq(countup(offset, encoded.len - 1, encoded.steps))
|
|
|
|
|
|
|
|
for _ in 0..<encoded.M + 1: # NOTE: the +1
|
2022-04-07 23:08:43 +00:00
|
|
|
var idx: int
|
|
|
|
while true:
|
|
|
|
idx = rng.sample(blockIdx, blocks)
|
|
|
|
if not encoded[idx].isEmpty:
|
|
|
|
break
|
|
|
|
|
|
|
|
blocks.add(idx)
|
2022-04-05 19:12:59 +00:00
|
|
|
offset.inc
|
|
|
|
|
|
|
|
for idx in blocks:
|
2022-06-28 16:10:05 +00:00
|
|
|
(await store.delBlock(encoded[idx])).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
decoded: Manifest
|
|
|
|
|
|
|
|
expect ResultFailure:
|
|
|
|
decoded = (await erasure.decode(encoded)).tryGet()
|
|
|
|
|
|
|
|
test "Should tolerate loosing M (a.k.a row) contiguous data blocks":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 10
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
let encoded = await encode(buffers, parity)
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
for b in encoded.blocks[0..<encoded.steps * encoded.M]:
|
2022-06-28 16:10:05 +00:00
|
|
|
(await store.delBlock(b)).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
discard (await erasure.decode(encoded)).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
for d in manifest:
|
2022-07-28 00:39:17 +00:00
|
|
|
let present = await store.hasBlock(d)
|
|
|
|
check present.tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
test "Should tolerate loosing M (a.k.a row) contiguous parity blocks":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 10
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
let encoded = await encode(buffers, parity)
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
for b in encoded.blocks[^(encoded.steps * encoded.M)..^1]:
|
2022-06-28 16:10:05 +00:00
|
|
|
(await store.delBlock(b)).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
discard (await erasure.decode(encoded)).tryGet()
|
2022-04-05 19:12:59 +00:00
|
|
|
|
|
|
|
for d in manifest:
|
2022-07-28 00:39:17 +00:00
|
|
|
let present = await store.hasBlock(d)
|
|
|
|
check present.tryGet()
|
2022-05-10 11:50:22 +00:00
|
|
|
|
|
|
|
test "handles edge case of 0 parity blocks":
|
|
|
|
const
|
|
|
|
buffers = 20
|
|
|
|
parity = 0
|
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
let encoded = await encode(buffers, parity)
|
2022-05-10 11:50:22 +00:00
|
|
|
|
2022-05-10 12:10:17 +00:00
|
|
|
discard (await erasure.decode(encoded)).tryGet()
|