Test fixed, all working

This commit is contained in:
Tomasz Bekas 2023-11-03 20:10:15 +01:00
parent 2ad9b77899
commit fb55e79649
No known key found for this signature in database
GPG Key ID: 4854E04C98824959
5 changed files with 36 additions and 42 deletions

View File

@ -412,19 +412,6 @@ proc decode*(
finally:
decoder.release()
without encodedTree =? MerkleTree.init(cids[]), err:
return failure(err)
without encodedTreeCid =? encodedTree.rootCid, err:
return failure(err)
if encodedTreeCid != encoded.treeCid:
return failure("Encoded tree root differs from the tree root computed out of recovered data")
let idxIter = Iter.fromItems(recoveredIndices)
if err =? (await self.store.putSomeProofs(encodedTree, idxIter)).errorOption:
return failure(err)
without tree =? MerkleTree.init(cids[0..<encoded.originalBlocksCount]), err:
return failure(err)
@ -434,7 +421,11 @@ proc decode*(
if treeCid != encoded.originalTreeCid:
return failure("Original tree root differs from the tree root computed out of recovered data")
if err =? (await self.store.putSomeProofs(tree, idxIter.filter((i: int) => i < tree.leavesCount))).errorOption:
let idxIter = Iter
.fromItems(recoveredIndices)
.filter((i: int) => i < tree.leavesCount)
if err =? (await self.store.putSomeProofs(tree, idxIter)).errorOption:
return failure(err)
let decoded = Manifest.new(encoded)

View File

@ -10,10 +10,12 @@
import std/math
import std/bitops
import std/sequtils
import std/options
import std/sugar
import std/algorithm
import pkg/chronicles
import pkg/questionable
import pkg/questionable/results
import pkg/nimcrypto/sha2
import pkg/libp2p/[cid, multicodec, multihash, vbuffer]
@ -289,18 +291,6 @@ proc init*(
else:
failure("Expected nodesBuffer len to be " & $(totalNodes * digestSize) & " but was " & $nodesBuffer.len)
proc init*(
T: type MerkleTree,
cids: openArray[Cid]
): ?!MerkleTree =
let leaves = collect:
for cid in cids:
without mhash =? cid.mhash.mapFailure, errx:
return failure(errx)
mhash
MerkleTree.init(leaves)
proc init*(
T: type MerkleTree,
leaves: openArray[MultiHash]
@ -316,6 +306,18 @@ proc init*(
builder.build()
proc init*(
T: type MerkleTree,
cids: openArray[Cid]
): ?!MerkleTree =
let leaves = collect:
for idx, cid in cids:
without mhash =? cid.mhash.mapFailure, errx:
return failure(errx)
mhash
MerkleTree.init(leaves)
###########################################################
# MerkleProof
###########################################################

View File

@ -21,7 +21,6 @@ import pkg/questionable
import pkg/questionable/results
import ./blockstore
import ./treereader
import ../units
import ../chunker
import ../errors
@ -274,9 +273,6 @@ proc new*(
currentSize: currentSize,
size: cacheSize)
# proc getBlockFromStore(cid: Cid): Future[?!Block] = store.getBlock(cid)
# store.treeReader = TreeReader.new(getBlockFromStore)
for blk in blocks:
discard store.putBlockSync(blk)

View File

@ -95,22 +95,23 @@ proc map*[T, U](iter: Iter[T], fn: Function[T, U]): Iter[U] =
)
proc filter*[T](iter: Iter[T], predicate: Function[T, bool]): Iter[T] =
var nextItem: T
var nextT: Option[T]
proc tryFetch(): void =
nextT = T.none
while not iter.finished:
let item = iter.next()
if predicate(item):
nextItem = item
let t = iter.next()
if predicate(t):
nextT = some(t)
break
proc genNext(): T =
let t = nextItem
let t = nextT.unsafeGet
tryFetch()
return t
proc isFinished(): bool =
iter.finished
nextT.isNone
tryFetch()
Iter.new(genNext, isFinished)

View File

@ -58,20 +58,19 @@ asyncchecksuite "Erasure encode/decode":
for _ in 0..<encoded.ecM:
dropped.add(column)
(await store.delBlock(encoded.treeCid, column)).tryGet()
(await store.delBlock(manifest.treeCid, column)).tryGet()
column.inc(encoded.steps - 1)
echo $dropped
var
decoded = (await erasure.decode(encoded)).tryGet()
check:
decoded.cid.tryGet() == manifest.cid.tryGet()
decoded.cid.tryGet() == encoded.originalCid
decoded.treeCid == manifest.treeCid
decoded.treeCid == encoded.originalCid
decoded.blocksCount == encoded.originalBlocksCount
for d in dropped:
let present = await store.hasBlock(encoded.treeCid, d)
let present = await store.hasBlock(manifest.treeCid, d)
check present.tryGet()
test "Should not tolerate losing more than M data blocks in a single random column":
@ -88,6 +87,7 @@ asyncchecksuite "Erasure encode/decode":
for _ in 0..<encoded.ecM + 1:
dropped.add(column)
(await store.delBlock(encoded.treeCid, column)).tryGet()
(await store.delBlock(manifest.treeCid, column)).tryGet()
column.inc(encoded.steps)
var
@ -97,7 +97,7 @@ asyncchecksuite "Erasure encode/decode":
decoded = (await erasure.decode(encoded)).tryGet()
for d in dropped:
let present = await store.hasBlock(encoded.treeCid, d)
let present = await store.hasBlock(manifest.treeCid, d)
check not present.tryGet()
test "Should tolerate losing M data blocks in M random columns":
@ -121,6 +121,7 @@ asyncchecksuite "Erasure encode/decode":
for idx in blocks:
(await store.delBlock(encoded.treeCid, idx)).tryGet()
(await store.delBlock(manifest.treeCid, idx)).tryGet()
discard
discard (await erasure.decode(encoded)).tryGet()
@ -157,6 +158,7 @@ asyncchecksuite "Erasure encode/decode":
for idx in blocks:
(await store.delBlock(encoded.treeCid, idx)).tryGet()
(await store.delBlock(manifest.treeCid, idx)).tryGet()
discard
var
@ -174,6 +176,7 @@ asyncchecksuite "Erasure encode/decode":
for b in 0..<encoded.steps * encoded.ecM:
(await store.delBlock(encoded.treeCid, b)).tryGet()
(await store.delBlock(manifest.treeCid, b)).tryGet()
discard (await erasure.decode(encoded)).tryGet()
@ -190,6 +193,7 @@ asyncchecksuite "Erasure encode/decode":
for b in (encoded.blocksCount - encoded.steps * encoded.ecM)..<encoded.blocksCount:
(await store.delBlock(encoded.treeCid, b)).tryGet()
(await store.delBlock(manifest.treeCid, b)).tryGet()
discard (await erasure.decode(encoded)).tryGet()