diff --git a/codex/errors.nim b/codex/errors.nim index 9947d4b7..d98bfc04 100644 --- a/codex/errors.nim +++ b/codex/errors.nim @@ -38,6 +38,29 @@ func toFailure*[T](exp: Option[T]): Result[T, ref CatchableError] {.inline.} = else: T.failure("Option is None") +# allFuturesThrowing was moved to the tests in libp2p +proc allFuturesThrowing*[T](args: varargs[Future[T]]): Future[void] = + var futs: seq[Future[T]] + for fut in args: + futs &= fut + proc call() {.async.} = + var first: ref CatchableError = nil + futs = await allFinished(futs) + for fut in futs: + if fut.failed: + let err = fut.readError() + if err of Defect: + raise err + else: + if err of CancelledError: + raise err + if isNil(first): + first = err + if not isNil(first): + raise first + + return call() + proc allFutureResult*[T](fut: seq[Future[T]]): Future[?!void] {.async.} = try: await allFuturesThrowing(fut) diff --git a/codex/merkletree/codex/codex.nim b/codex/merkletree/codex/codex.nim index 974123f2..72b044f2 100644 --- a/codex/merkletree/codex/codex.nim +++ b/codex/merkletree/codex/codex.nim @@ -47,6 +47,14 @@ type CodexProof* = ref object of ByteProof mcodec*: MultiCodec +# CodeHashes is not exported from libp2p +# So we need to recreate it instead of +proc initMultiHashCodeTable(): Table[MultiCodec, MHash] {.compileTime.} = + for item in HashesList: + result[item.mcodec] = item + +const CodeHashes = initMultiHashCodeTable() + func mhash*(mcodec: MultiCodec): ?!MHash = let mhash = CodeHashes.getOrDefault(mcodec)