cleanup manifest
This commit is contained in:
parent
68a45d8f57
commit
663366a1a9
|
@ -50,20 +50,23 @@ template EmptyDigests: untyped =
|
||||||
emptyDigests
|
emptyDigests
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Manifest* = object
|
||||||
|
cid*: ?Cid
|
||||||
|
blocks*: seq[Cid]
|
||||||
|
|
||||||
BlocksManifest* = object
|
BlocksManifest* = object
|
||||||
blocks: seq[Cid]
|
manifest: Manifest
|
||||||
htree: ?Cid
|
|
||||||
version*: CidVersion
|
version*: CidVersion
|
||||||
hcodec*: MultiCodec
|
hcodec*: MultiCodec
|
||||||
codec*: MultiCodec
|
codec*: MultiCodec
|
||||||
|
|
||||||
proc len*(b: BlocksManifest): int = b.blocks.len
|
proc len*(b: BlocksManifest): int = b.manifest.blocks.len
|
||||||
|
|
||||||
iterator items*(b: BlocksManifest): Cid =
|
iterator items*(b: BlocksManifest): Cid =
|
||||||
for b in b.blocks:
|
for b in b.manifest.blocks:
|
||||||
yield b
|
yield b
|
||||||
|
|
||||||
proc hashBytes(mh: MultiHash): seq[byte] =
|
template hashBytes(mh: MultiHash): seq[byte] =
|
||||||
## get the hash bytes of a multihash object
|
## get the hash bytes of a multihash object
|
||||||
##
|
##
|
||||||
|
|
||||||
|
@ -73,13 +76,13 @@ proc cid*(b: var BlocksManifest): ?!Cid =
|
||||||
## Generate a root hash using the treehash algorithm
|
## Generate a root hash using the treehash algorithm
|
||||||
##
|
##
|
||||||
|
|
||||||
if htree =? b.htree:
|
if htree =? b.manifest.cid:
|
||||||
return htree.success
|
return htree.success
|
||||||
|
|
||||||
var
|
var
|
||||||
stack: seq[MultiHash]
|
stack: seq[MultiHash]
|
||||||
|
|
||||||
for cid in b.blocks:
|
for cid in b.manifest.blocks:
|
||||||
stack.add(? cid.mhash.mapFailure)
|
stack.add(? cid.mhash.mapFailure)
|
||||||
|
|
||||||
while stack.len > 1:
|
while stack.len > 1:
|
||||||
|
@ -97,23 +100,26 @@ proc cid*(b: var BlocksManifest): ?!Cid =
|
||||||
b.codec,
|
b.codec,
|
||||||
(? EmptyDigests[b.version][b.hcodec].catch))
|
(? EmptyDigests[b.version][b.hcodec].catch))
|
||||||
.mapFailure
|
.mapFailure
|
||||||
b.htree = cid.some
|
|
||||||
|
b.manifest.cid = cid.some
|
||||||
return cid.success
|
return cid.success
|
||||||
|
|
||||||
proc put*(b: var BlocksManifest, cid: Cid) =
|
proc put*(b: var BlocksManifest, cid: Cid) =
|
||||||
b.htree = Cid.none
|
b.manifest.cid = Cid.none
|
||||||
trace "Adding cid to manifest", cid
|
trace "Adding cid to manifest", cid
|
||||||
b.blocks.add(cid)
|
b.manifest.blocks.add(cid)
|
||||||
|
|
||||||
proc contains*(b: BlocksManifest, cid: Cid): bool =
|
proc contains*(b: BlocksManifest, cid: Cid): bool =
|
||||||
cid in b.blocks
|
cid in b.manifest.blocks
|
||||||
|
|
||||||
proc encode*(b: var BlocksManifest): ?!seq[byte] =
|
proc encode*(b: var BlocksManifest): ?!seq[byte] =
|
||||||
## Encode the manifest into a ``ManifestCodec``
|
## Encode the manifest into a ``ManifestCodec``
|
||||||
## multicodec container (Dag-pb) for now
|
## multicodec container (Dag-pb) for now
|
||||||
|
##
|
||||||
|
|
||||||
var pbNode = initProtoBuffer()
|
var pbNode = initProtoBuffer()
|
||||||
|
|
||||||
for c in b.blocks:
|
for c in b.manifest.blocks:
|
||||||
var pbLink = initProtoBuffer()
|
var pbLink = initProtoBuffer()
|
||||||
pbLink.write(1, c.data.buffer) # write Cid links
|
pbLink.write(1, c.data.buffer) # write Cid links
|
||||||
pbLink.finish()
|
pbLink.finish()
|
||||||
|
@ -125,15 +131,18 @@ proc encode*(b: var BlocksManifest): ?!seq[byte] =
|
||||||
|
|
||||||
return pbNode.buffer.success
|
return pbNode.buffer.success
|
||||||
|
|
||||||
proc decode*(_: type BlocksManifest, data: seq[byte]): ?!(Cid, seq[Cid]) =
|
proc decode*(_: type BlocksManifest, data: openArray[byte]): ?!Manifest =
|
||||||
## Decode a manifest from a byte seq
|
## Decode a manifest from a data blob
|
||||||
##
|
##
|
||||||
|
|
||||||
var
|
var
|
||||||
pbNode = initProtoBuffer(data)
|
pbNode = initProtoBuffer(data)
|
||||||
cidBuf: seq[byte]
|
cidBuf: seq[byte]
|
||||||
blocks: seq[Cid]
|
blocks: seq[Cid]
|
||||||
|
|
||||||
if pbNode.getField(1, cidBuf).isOk:
|
if pbNode.getField(1, cidBuf).isErr:
|
||||||
|
return failure("Unable to decode Cid from manifest!")
|
||||||
|
|
||||||
let cid = ? Cid.init(cidBuf).mapFailure
|
let cid = ? Cid.init(cidBuf).mapFailure
|
||||||
var linksBuf: seq[seq[byte]]
|
var linksBuf: seq[seq[byte]]
|
||||||
if pbNode.getRepeatedField(2, linksBuf).isOk:
|
if pbNode.getRepeatedField(2, linksBuf).isOk:
|
||||||
|
@ -144,11 +153,9 @@ proc decode*(_: type BlocksManifest, data: seq[byte]): ?!(Cid, seq[Cid]) =
|
||||||
pbLink = initProtoBuffer(pbLinkBuf)
|
pbLink = initProtoBuffer(pbLinkBuf)
|
||||||
|
|
||||||
if pbLink.getField(1, blockBuf).isOk:
|
if pbLink.getField(1, blockBuf).isOk:
|
||||||
let cidRes = Cid.init(blockBuf)
|
blocks.add(? Cid.init(blockBuf).mapFailure)
|
||||||
if cidRes.isOk:
|
|
||||||
blocks.add(cidRes.get())
|
|
||||||
|
|
||||||
return (cid, blocks).success
|
Manifest(cid: cid.some, blocks: blocks).success
|
||||||
|
|
||||||
proc init*(
|
proc init*(
|
||||||
T: type BlocksManifest,
|
T: type BlocksManifest,
|
||||||
|
@ -160,10 +167,10 @@ proc init*(
|
||||||
##
|
##
|
||||||
|
|
||||||
if hcodec notin EmptyDigests[version]:
|
if hcodec notin EmptyDigests[version]:
|
||||||
return failure("Unsuported manifest hash codec!")
|
return failure("Unsupported manifest hash codec!")
|
||||||
|
|
||||||
T(
|
T(
|
||||||
blocks: @blocks,
|
manifest: Manifest(blocks: @blocks),
|
||||||
version: version,
|
version: version,
|
||||||
codec: codec,
|
codec: codec,
|
||||||
hcodec: hcodec,
|
hcodec: hcodec,
|
||||||
|
@ -171,23 +178,23 @@ proc init*(
|
||||||
|
|
||||||
proc init*(
|
proc init*(
|
||||||
T: type BlocksManifest,
|
T: type BlocksManifest,
|
||||||
blk: Block): ?!T =
|
data: openArray[byte]): ?!T =
|
||||||
## Create manifest from a raw manifest block
|
## Create manifest from a raw data blob
|
||||||
## (in dag-pb for for now)
|
## (in dag-pb for for now)
|
||||||
##
|
##
|
||||||
|
|
||||||
let
|
let
|
||||||
(cid, blocks) = ? BlocksManifest.decode(blk.data)
|
manifest = ? BlocksManifest.decode(data)
|
||||||
|
cid = !manifest.cid
|
||||||
mhash = ? cid.mhash.mapFailure
|
mhash = ? cid.mhash.mapFailure
|
||||||
|
|
||||||
var
|
var blockManifest = ? BlocksManifest.init(
|
||||||
manifest = ? BlocksManifest.init(
|
manifest.blocks,
|
||||||
blocks,
|
|
||||||
cid.version,
|
cid.version,
|
||||||
mhash.mcodec,
|
mhash.mcodec,
|
||||||
cid.mcodec)
|
cid.mcodec)
|
||||||
|
|
||||||
if cid != (? manifest.cid):
|
if cid != ? blockManifest.cid:
|
||||||
return failure("Content hashes don't match!")
|
return failure("Decoded content hash doesn't match!")
|
||||||
|
|
||||||
return manifest.success
|
blockManifest.success
|
||||||
|
|
|
@ -99,11 +99,10 @@ proc retrieve*(
|
||||||
if mc == ManifestCodec:
|
if mc == ManifestCodec:
|
||||||
trace "Retrieving data set", cid, mc
|
trace "Retrieving data set", cid, mc
|
||||||
|
|
||||||
let res = BlocksManifest.init(blk)
|
without blockManifest =? BlocksManifest.init(blk.data):
|
||||||
if (res.isErr):
|
return failure("Unable to construct manifest!")
|
||||||
return failure(res.error.msg)
|
|
||||||
|
|
||||||
asyncSpawn node.streamBlocks(stream, res.get())
|
asyncSpawn node.streamBlocks(stream, blockManifest)
|
||||||
else:
|
else:
|
||||||
asyncSpawn (proc(): Future[void] {.async.} =
|
asyncSpawn (proc(): Future[void] {.async.} =
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -53,4 +53,4 @@ suite "Manifest":
|
||||||
e = manifest.encode().tryGet()
|
e = manifest.encode().tryGet()
|
||||||
(cid, decoded) = BlocksManifest.decode(e).tryGet()
|
(cid, decoded) = BlocksManifest.decode(e).tryGet()
|
||||||
|
|
||||||
check decoded == blocks
|
check manifest.blocks == blocks
|
||||||
|
|
Loading…
Reference in New Issue