From fb55e796499ccb615dabb95570a40bfaa6a2763a Mon Sep 17 00:00:00 2001 From: Tomasz Bekas Date: Fri, 3 Nov 2023 20:10:15 +0100 Subject: [PATCH] Test fixed, all working --- codex/erasure/erasure.nim | 19 +++++-------------- codex/merkletree/merkletree.nim | 26 ++++++++++++++------------ codex/stores/cachestore.nim | 4 ---- codex/utils/asynciter.nim | 13 +++++++------ tests/codex/testerasure.nim | 16 ++++++++++------ 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/codex/erasure/erasure.nim b/codex/erasure/erasure.nim index 8a1b6ece..987bff48 100644 --- a/codex/erasure/erasure.nim +++ b/codex/erasure/erasure.nim @@ -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.. 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) diff --git a/codex/merkletree/merkletree.nim b/codex/merkletree/merkletree.nim index c9612d59..d4a4fd62 100644 --- a/codex/merkletree/merkletree.nim +++ b/codex/merkletree/merkletree.nim @@ -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 ########################################################### diff --git a/codex/stores/cachestore.nim b/codex/stores/cachestore.nim index 6f1c1c35..8c3d74bd 100644 --- a/codex/stores/cachestore.nim +++ b/codex/stores/cachestore.nim @@ -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) diff --git a/codex/utils/asynciter.nim b/codex/utils/asynciter.nim index fda04d5e..e3ac7fb0 100644 --- a/codex/utils/asynciter.nim +++ b/codex/utils/asynciter.nim @@ -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) diff --git a/tests/codex/testerasure.nim b/tests/codex/testerasure.nim index bbc76514..bf8f8b96 100644 --- a/tests/codex/testerasure.nim +++ b/tests/codex/testerasure.nim @@ -58,20 +58,19 @@ asyncchecksuite "Erasure encode/decode": for _ in 0..