nim-dagger/tests/dagger/testerasure.nim

207 lines
4.9 KiB
Nim
Raw Normal View History

import std/sequtils
import pkg/asynctest
import pkg/chronos
import pkg/libp2p
import pkg/questionable
import pkg/questionable/results
import pkg/dagger/erasure
import pkg/dagger/manifest
import pkg/dagger/stores
import pkg/dagger/blocktype as bt
import pkg/dagger/rng
import ./helpers
suite "Erasure encode/decode":
2022-05-10 12:10:17 +00:00
const dataSetSize = BlockSize * 123 # weird geometry
var rng: Rng
var chunker: Chunker
var manifest: Manifest
var store: BlockStore
var erasure: Erasure
setup:
rng = Rng.instance()
chunker = RandomChunker.new(rng, size = dataSetSize, chunkSize = BlockSize)
manifest = !Manifest.new(blockSize = BlockSize)
store = CacheStore.new(cacheSize = (dataSetSize * 2), chunkSize = BlockSize)
erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider)
while (
let chunk = await chunker.getBytes();
chunk.len > 0):
let blk = bt.Block.new(chunk).tryGet()
manifest.add(blk.cid)
check (await store.putBlock(blk))
2022-05-10 12:10:17 +00:00
proc encode(buffers, parity: int): Future[Manifest] {.async.} =
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)
var
column = rng.rand(encoded.len div encoded.steps) # random column
dropped: seq[Cid]
for _ in 0..<encoded.M:
dropped.add(encoded[column])
check (await store.delBlock(encoded[column]))
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:
check d in store
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)
var
column = rng.rand(encoded.len div encoded.steps) # random column
dropped: seq[Cid]
for _ in 0..<encoded.M + 1:
dropped.add(encoded[column])
check (await store.delBlock(encoded[column]))
column.inc(encoded.steps)
var
decoded: Manifest
expect ResultFailure:
decoded = (await erasure.decode(encoded)).tryGet()
for d in dropped:
check d notin store
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)
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:
check (await store.delBlock(encoded[idx]))
2022-05-10 12:10:17 +00:00
discard (await erasure.decode(encoded)).tryGet()
for d in manifest:
check d in store
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)
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)
offset.inc
for idx in blocks:
check (await store.delBlock(encoded[idx]))
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)
for b in encoded.blocks[0..<encoded.steps * encoded.M]:
check (await store.delBlock(b))
2022-05-10 12:10:17 +00:00
discard (await erasure.decode(encoded)).tryGet()
for d in manifest:
check d in store
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)
for b in encoded.blocks[^(encoded.steps * encoded.M)..^1]:
check (await store.delBlock(b))
2022-05-10 12:10:17 +00:00
discard (await erasure.decode(encoded)).tryGet()
for d in manifest:
check d in store
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 12:10:17 +00:00
discard (await erasure.decode(encoded)).tryGet()