2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-03-14 16:06:36 +00:00
|
|
|
## Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
# This module implements serialization and deserialization of Manifest
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-03-14 16:06:36 +00:00
|
|
|
|
|
|
|
import std/tables
|
|
|
|
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
import ./manifest
|
2022-03-14 16:06:36 +00:00
|
|
|
import ../errors
|
2022-07-29 20:04:12 +00:00
|
|
|
import ../blocktype
|
2022-04-06 00:34:29 +00:00
|
|
|
import ./types
|
2022-03-15 18:47:31 +00:00
|
|
|
|
|
|
|
func encode*(_: DagPBCoder, manifest: Manifest): ?!seq[byte] =
|
2022-03-14 16:06:36 +00:00
|
|
|
## Encode the manifest into a ``ManifestCodec``
|
|
|
|
## multicodec container (Dag-pb) for now
|
|
|
|
##
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
? manifest.verify()
|
2022-03-14 16:06:36 +00:00
|
|
|
var pbNode = initProtoBuffer()
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
for c in manifest.blocks:
|
2022-03-14 16:06:36 +00:00
|
|
|
var pbLink = initProtoBuffer()
|
|
|
|
pbLink.write(1, c.data.buffer) # write Cid links
|
|
|
|
pbLink.finish()
|
|
|
|
pbNode.write(2, pbLink)
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
# NOTE: The `Data` field in the the `dag-pb`
|
|
|
|
# contains the following protobuf `Message`
|
|
|
|
#
|
|
|
|
# ```protobuf
|
2022-04-05 00:46:13 +00:00
|
|
|
# Message ErasureInfo {
|
|
|
|
# optional uint32 K = 1; # number of encoded blocks
|
|
|
|
# optional uint32 M = 2; # number of parity blocks
|
|
|
|
# optional bytes cid = 3; # cid of the original dataset
|
|
|
|
# optional uint32 original = 4; # number of original blocks
|
|
|
|
# }
|
2022-03-15 18:47:31 +00:00
|
|
|
# Message Header {
|
2022-04-05 00:46:13 +00:00
|
|
|
# optional bytes rootHash = 1; # the root (tree) hash
|
|
|
|
# optional uint32 blockSize = 2; # size of a single block
|
|
|
|
# optional uint32 blocksLen = 3; # total amount of blocks
|
|
|
|
# optional ErasureInfo erasure = 4; # erasure coding info
|
2022-08-24 12:15:59 +00:00
|
|
|
# optional uint64 originalBytes = 5;# exact file size
|
2022-03-15 18:47:31 +00:00
|
|
|
# }
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
|
|
|
|
let cid = !manifest.rootHash
|
|
|
|
var header = initProtoBuffer()
|
|
|
|
header.write(1, cid.data.buffer)
|
|
|
|
header.write(2, manifest.blockSize.uint32)
|
2022-03-30 02:43:35 +00:00
|
|
|
header.write(3, manifest.len.uint32)
|
2022-08-24 12:15:59 +00:00
|
|
|
header.write(5, manifest.originalBytes.uint64)
|
2022-04-05 00:46:13 +00:00
|
|
|
if manifest.protected:
|
|
|
|
var erasureInfo = initProtoBuffer()
|
|
|
|
erasureInfo.write(1, manifest.K.uint32)
|
|
|
|
erasureInfo.write(2, manifest.M.uint32)
|
|
|
|
erasureInfo.write(3, manifest.originalCid.data.buffer)
|
|
|
|
erasureInfo.write(4, manifest.originalLen.uint32)
|
|
|
|
erasureInfo.finish()
|
|
|
|
|
|
|
|
header.write(4, erasureInfo)
|
2022-03-15 18:47:31 +00:00
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
pbNode.write(1, header) # set the rootHash Cid as the data field
|
2022-03-14 16:06:36 +00:00
|
|
|
pbNode.finish()
|
|
|
|
|
|
|
|
return pbNode.buffer.success
|
|
|
|
|
|
|
|
func decode*(_: DagPBCoder, data: openArray[byte]): ?!Manifest =
|
|
|
|
## Decode a manifest from a data blob
|
|
|
|
##
|
|
|
|
|
|
|
|
var
|
|
|
|
pbNode = initProtoBuffer(data)
|
2022-03-15 18:47:31 +00:00
|
|
|
pbHeader: ProtoBuffer
|
2022-04-05 00:46:13 +00:00
|
|
|
pbErasureInfo: ProtoBuffer
|
2022-03-15 18:47:31 +00:00
|
|
|
rootHash: seq[byte]
|
2022-04-05 00:46:13 +00:00
|
|
|
originalCid: seq[byte]
|
2022-08-24 12:15:59 +00:00
|
|
|
originalBytes: uint64
|
2022-03-15 18:47:31 +00:00
|
|
|
blockSize: uint32
|
|
|
|
blocksLen: uint32
|
2022-04-05 00:46:13 +00:00
|
|
|
originalLen: uint32
|
|
|
|
K, M: uint32
|
2022-03-14 16:06:36 +00:00
|
|
|
blocks: seq[Cid]
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
# Decode `Header` message
|
|
|
|
if pbNode.getField(1, pbHeader).isErr:
|
|
|
|
return failure("Unable to decode `Header` from dag-pb manifest!")
|
|
|
|
|
|
|
|
# Decode `Header` contents
|
|
|
|
if pbHeader.getField(1, rootHash).isErr:
|
|
|
|
return failure("Unable to decode `rootHash` from manifest!")
|
|
|
|
|
|
|
|
if pbHeader.getField(2, blockSize).isErr:
|
|
|
|
return failure("Unable to decode `blockSize` from manifest!")
|
2022-03-14 16:06:36 +00:00
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
if pbHeader.getField(3, blocksLen).isErr:
|
|
|
|
return failure("Unable to decode `blocksLen` from manifest!")
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
if pbHeader.getField(5, originalBytes).isErr:
|
|
|
|
return failure("Unable to decode `originalBytes` from manifest!")
|
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
if pbHeader.getField(4, pbErasureInfo).isErr:
|
|
|
|
return failure("Unable to decode `erasureInfo` from manifest!")
|
|
|
|
|
|
|
|
if pbErasureInfo.buffer.len > 0:
|
|
|
|
if pbErasureInfo.getField(1, K).isErr:
|
|
|
|
return failure("Unable to decode `K` from manifest!")
|
|
|
|
|
|
|
|
if pbErasureInfo.getField(2, M).isErr:
|
|
|
|
return failure("Unable to decode `M` from manifest!")
|
|
|
|
|
|
|
|
if pbErasureInfo.getField(3, originalCid).isErr:
|
|
|
|
return failure("Unable to decode `originalCid` from manifest!")
|
|
|
|
|
|
|
|
if pbErasureInfo.getField(4, originalLen).isErr:
|
|
|
|
return failure("Unable to decode `originalLen` from manifest!")
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
let rootHashCid = ? Cid.init(rootHash).mapFailure
|
2022-03-14 16:06:36 +00:00
|
|
|
var linksBuf: seq[seq[byte]]
|
|
|
|
if pbNode.getRepeatedField(2, linksBuf).isOk:
|
|
|
|
for pbLinkBuf in linksBuf:
|
|
|
|
var
|
|
|
|
blocksBuf: seq[seq[byte]]
|
|
|
|
blockBuf: seq[byte]
|
|
|
|
pbLink = initProtoBuffer(pbLinkBuf)
|
|
|
|
|
|
|
|
if pbLink.getField(1, blockBuf).isOk:
|
|
|
|
blocks.add(? Cid.init(blockBuf).mapFailure)
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
if blocksLen.int != blocks.len:
|
|
|
|
return failure("Total blocks and length of blocks in header don't match!")
|
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
var
|
|
|
|
self = Manifest(
|
|
|
|
rootHash: rootHashCid.some,
|
2022-08-24 12:15:59 +00:00
|
|
|
originalBytes: originalBytes.int,
|
2022-04-05 00:46:13 +00:00
|
|
|
blockSize: blockSize.int,
|
|
|
|
blocks: blocks,
|
|
|
|
hcodec: (? rootHashCid.mhash.mapFailure).mcodec,
|
|
|
|
codec: rootHashCid.mcodec,
|
|
|
|
version: rootHashCid.cidver,
|
|
|
|
protected: pbErasureInfo.buffer.len > 0)
|
|
|
|
|
|
|
|
if self.protected:
|
|
|
|
self.K = K.int
|
|
|
|
self.M = M.int
|
|
|
|
self.originalCid = ? Cid.init(originalCid).mapFailure
|
|
|
|
self.originalLen = originalLen.int
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
? self.verify()
|
2022-04-05 00:46:13 +00:00
|
|
|
self.success
|
|
|
|
|
|
|
|
proc encode*(
|
|
|
|
self: Manifest,
|
|
|
|
encoder = ManifestContainers[$DagPBCodec]): ?!seq[byte] =
|
2022-03-15 18:47:31 +00:00
|
|
|
## Encode a manifest using `encoder`
|
|
|
|
##
|
|
|
|
|
|
|
|
if self.rootHash.isNone:
|
|
|
|
? self.makeRoot()
|
|
|
|
|
|
|
|
encoder.encode(self)
|
|
|
|
|
|
|
|
func decode*(
|
|
|
|
_: type Manifest,
|
|
|
|
data: openArray[byte],
|
|
|
|
decoder = ManifestContainers[$DagPBCodec]): ?!Manifest =
|
|
|
|
## Decode a manifest using `decoder`
|
|
|
|
##
|
|
|
|
|
|
|
|
decoder.decode(data)
|
2022-07-07 13:00:08 +00:00
|
|
|
|
2022-07-29 20:04:12 +00:00
|
|
|
func decode*(_: type Manifest, blk: Block): ?!Manifest =
|
2022-12-03 00:00:55 +00:00
|
|
|
## Decode a manifest using `decoder`
|
|
|
|
##
|
|
|
|
|
|
|
|
if not ? blk.cid.isManifest:
|
|
|
|
return failure "Cid not a manifest codec"
|
|
|
|
|
|
|
|
Manifest.decode(
|
|
|
|
blk.data,
|
|
|
|
? ManifestContainers[$(?blk.cid.contentType().mapFailure)].catch)
|