From d4025f4a5ff8ac8e0f152328ddf9be581a2b7b9c Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 7 Jul 2022 15:00:08 +0200 Subject: [PATCH] [manifest] Extract logic for decoding a manifest given a cid --- codex/manifest/coders.nim | 6 ++++++ codex/node.nim | 11 +---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/codex/manifest/coders.nim b/codex/manifest/coders.nim index f99277bd..d6506b13 100644 --- a/codex/manifest/coders.nim +++ b/codex/manifest/coders.nim @@ -173,3 +173,9 @@ func decode*( ## decoder.decode(data) + +func decode*(_: type Manifest, data: openArray[byte], cid: Cid): ?!Manifest = + without contentType =? cid.contentType() and + containerType =? ManifestContainers.?[$contentType]: + return failure "CID has invalid content type for manifest" + Manifest.decode(data, containerType) diff --git a/codex/node.nim b/codex/node.nim index 93f503b1..a6cf8cbd 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -69,16 +69,7 @@ proc retrieve*( return failure( newException(CodexError, "Couldn't retrieve block for Cid!")) - without mc =? blk.cid.contentType(): - return failure( - newException(CodexError, "Couldn't identify Cid!")) - - # if we got a manifest, stream the blocks - if $mc in ManifestContainers: - trace "Retrieving data set", cid, mc = $mc - - without manifest =? Manifest.decode(blk.data, ManifestContainers[$mc]): - return failure("Unable to construct manifest!") + if manifest =? Manifest.decode(blk.data, blk.cid): if manifest.protected: proc erasureJob(): Future[void] {.async.} =