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 defines all operations on Manifest
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-03-14 16:06:36 +00:00
|
|
|
|
|
|
|
import pkg/libp2p/protobuf/minprotobuf
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/chronicles
|
|
|
|
|
|
|
|
import ../errors
|
2022-08-24 12:15:59 +00:00
|
|
|
import ../utils
|
2023-11-09 08:47:09 +00:00
|
|
|
import ../utils/json
|
2023-07-06 23:23:27 +00:00
|
|
|
import ../units
|
2022-03-15 18:47:31 +00:00
|
|
|
import ../blocktype
|
2022-04-06 00:34:29 +00:00
|
|
|
import ./types
|
2022-03-14 16:06:36 +00:00
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
export types
|
|
|
|
|
2023-07-19 14:06:59 +00:00
|
|
|
type
|
|
|
|
Manifest* = ref object of RootObj
|
2023-11-09 08:47:09 +00:00
|
|
|
rootHash {.serialize.}: ?Cid # Root (tree) hash of the contained data set
|
|
|
|
originalBytes* {.serialize.}: NBytes # Exact size of the original (uploaded) file
|
|
|
|
blockSize {.serialize.}: NBytes # Size of each contained block (might not be needed if blocks are len-prefixed)
|
|
|
|
blocks: seq[Cid] # Block Cid
|
|
|
|
version: CidVersion # Cid version
|
|
|
|
hcodec: MultiCodec # Multihash codec
|
|
|
|
codec: MultiCodec # Data set codec
|
|
|
|
case protected {.serialize.}: bool # Protected datasets have erasure coded info
|
2023-07-19 14:06:59 +00:00
|
|
|
of true:
|
2023-09-25 14:31:10 +00:00
|
|
|
ecK: int # Number of blocks to encode
|
|
|
|
ecM: int # Number of resulting parity blocks
|
|
|
|
originalCid: Cid # The original Cid of the dataset being erasure coded
|
|
|
|
originalLen: int # The length of the original manifest
|
2023-07-19 14:06:59 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Accessors
|
|
|
|
############################################################
|
|
|
|
|
|
|
|
proc blockSize*(self: Manifest): NBytes =
|
|
|
|
self.blockSize
|
|
|
|
|
|
|
|
proc blocks*(self: Manifest): seq[Cid] =
|
|
|
|
self.blocks
|
|
|
|
|
|
|
|
proc version*(self: Manifest): CidVersion =
|
|
|
|
self.version
|
|
|
|
|
|
|
|
proc hcodec*(self: Manifest): MultiCodec =
|
|
|
|
self.hcodec
|
|
|
|
|
|
|
|
proc codec*(self: Manifest): MultiCodec =
|
|
|
|
self.codec
|
|
|
|
|
|
|
|
proc protected*(self: Manifest): bool =
|
|
|
|
self.protected
|
|
|
|
|
|
|
|
proc ecK*(self: Manifest): int =
|
|
|
|
self.ecK
|
|
|
|
|
|
|
|
proc ecM*(self: Manifest): int =
|
|
|
|
self.ecM
|
|
|
|
|
|
|
|
proc originalCid*(self: Manifest): Cid =
|
|
|
|
self.originalCid
|
|
|
|
|
|
|
|
proc originalLen*(self: Manifest): int =
|
|
|
|
self.originalLen
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
############################################################
|
|
|
|
# Operations on block list
|
|
|
|
############################################################
|
|
|
|
|
2022-03-14 16:06:36 +00:00
|
|
|
func len*(self: Manifest): int =
|
|
|
|
self.blocks.len
|
|
|
|
|
|
|
|
func `[]`*(self: Manifest, i: Natural): Cid =
|
|
|
|
self.blocks[i]
|
|
|
|
|
|
|
|
func `[]=`*(self: var Manifest, i: Natural, item: Cid) =
|
2022-03-15 18:47:31 +00:00
|
|
|
self.rootHash = Cid.none
|
2022-03-14 16:06:36 +00:00
|
|
|
self.blocks[i] = item
|
|
|
|
|
|
|
|
func `[]`*(self: Manifest, i: BackwardsIndex): Cid =
|
|
|
|
self.blocks[self.len - i.int]
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
func `[]=`*(self: Manifest, i: BackwardsIndex, item: Cid) =
|
2022-03-15 18:47:31 +00:00
|
|
|
self.rootHash = Cid.none
|
2022-03-14 16:06:36 +00:00
|
|
|
self.blocks[self.len - i.int] = item
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
func isManifest*(cid: Cid): ?!bool =
|
2023-08-01 23:47:57 +00:00
|
|
|
let res = ?cid.contentType().mapFailure(CodexError)
|
|
|
|
($(res) in ManifestContainers).success
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
func isManifest*(mc: MultiCodec): ?!bool =
|
|
|
|
($mc in ManifestContainers).success
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
proc add*(self: Manifest, cid: Cid) =
|
2022-08-24 12:15:59 +00:00
|
|
|
assert not self.protected # we expect that protected manifests are created with properly-sized self.blocks
|
2022-03-14 16:06:36 +00:00
|
|
|
self.rootHash = Cid.none
|
|
|
|
trace "Adding cid to manifest", cid
|
|
|
|
self.blocks.add(cid)
|
2023-07-06 23:23:27 +00:00
|
|
|
self.originalBytes = self.blocks.len.NBytes * self.blockSize
|
2022-03-14 16:06:36 +00:00
|
|
|
|
|
|
|
iterator items*(self: Manifest): Cid =
|
|
|
|
for b in self.blocks:
|
|
|
|
yield b
|
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
iterator pairs*(self: Manifest): tuple[key: int, val: Cid] =
|
|
|
|
for pair in self.blocks.pairs():
|
|
|
|
yield pair
|
|
|
|
|
2022-03-14 16:06:36 +00:00
|
|
|
func contains*(self: Manifest, cid: Cid): bool =
|
|
|
|
cid in self.blocks
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Various sizes and verification
|
|
|
|
############################################################
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
func bytes*(self: Manifest, pad = true): NBytes =
|
2022-08-24 12:15:59 +00:00
|
|
|
## Compute how many bytes corresponding StoreStream(Manifest, pad) will return
|
|
|
|
if pad or self.protected:
|
2023-07-06 23:23:27 +00:00
|
|
|
self.len.NBytes * self.blockSize
|
2022-08-24 12:15:59 +00:00
|
|
|
else:
|
|
|
|
self.originalBytes
|
|
|
|
|
|
|
|
func rounded*(self: Manifest): int =
|
|
|
|
## Number of data blocks in *protected* manifest including padding at the end
|
2023-03-10 07:02:54 +00:00
|
|
|
roundUp(self.originalLen, self.ecK)
|
2022-08-24 12:15:59 +00:00
|
|
|
|
|
|
|
func steps*(self: Manifest): int =
|
|
|
|
## Number of EC groups in *protected* manifest
|
2023-03-10 07:02:54 +00:00
|
|
|
divUp(self.originalLen, self.ecK)
|
2022-08-24 12:15:59 +00:00
|
|
|
|
|
|
|
func verify*(self: Manifest): ?!void =
|
|
|
|
## Check manifest correctness
|
|
|
|
##
|
|
|
|
let originalLen = (if self.protected: self.originalLen else: self.len)
|
|
|
|
|
|
|
|
if divUp(self.originalBytes, self.blockSize) != originalLen:
|
|
|
|
return failure newException(CodexError, "Broken manifest: wrong originalBytes")
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
if self.protected and (self.len != self.steps * (self.ecK + self.ecM)):
|
2022-08-24 12:15:59 +00:00
|
|
|
return failure newException(CodexError, "Broken manifest: wrong originalLen")
|
|
|
|
|
|
|
|
return success()
|
|
|
|
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Cid computation
|
|
|
|
############################################################
|
|
|
|
|
2022-03-14 16:06:36 +00:00
|
|
|
template hashBytes(mh: MultiHash): seq[byte] =
|
|
|
|
## get the hash bytes of a multihash object
|
|
|
|
##
|
|
|
|
|
|
|
|
mh.data.buffer[mh.dpos..(mh.dpos + mh.size - 1)]
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
proc makeRoot*(self: Manifest): ?!void =
|
2022-03-15 18:47:31 +00:00
|
|
|
## Create a tree hash root of the contained
|
|
|
|
## block hashes
|
|
|
|
##
|
|
|
|
|
2022-03-14 16:06:36 +00:00
|
|
|
var
|
|
|
|
stack: seq[MultiHash]
|
|
|
|
|
|
|
|
for cid in self:
|
|
|
|
stack.add(? cid.mhash.mapFailure)
|
|
|
|
|
|
|
|
while stack.len > 1:
|
|
|
|
let
|
|
|
|
(b1, b2) = (stack.pop(), stack.pop())
|
|
|
|
mh = ? MultiHash.digest(
|
|
|
|
$self.hcodec,
|
|
|
|
(b1.hashBytes() & b2.hashBytes()))
|
|
|
|
.mapFailure
|
|
|
|
stack.add(mh)
|
|
|
|
|
|
|
|
if stack.len == 1:
|
2023-08-01 23:47:57 +00:00
|
|
|
let digest = ? EmptyDigests[self.version][self.hcodec].catch
|
|
|
|
let cid = ? Cid.init(self.version, self.codec, digest).mapFailure
|
2022-03-14 16:06:36 +00:00
|
|
|
|
|
|
|
self.rootHash = cid.some
|
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
success()
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
proc cid*(self: Manifest): ?!Cid =
|
2022-03-14 16:06:36 +00:00
|
|
|
## Generate a root hash using the treehash algorithm
|
|
|
|
##
|
|
|
|
|
|
|
|
if self.rootHash.isNone:
|
|
|
|
? self.makeRoot()
|
|
|
|
|
|
|
|
(!self.rootHash).success
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Constructors
|
|
|
|
############################################################
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
proc new*(
|
2023-06-22 15:11:18 +00:00
|
|
|
T: type Manifest,
|
|
|
|
blocks: openArray[Cid] = [],
|
|
|
|
protected = false,
|
|
|
|
version = CIDv1,
|
|
|
|
hcodec = multiCodec("sha2-256"),
|
|
|
|
codec = multiCodec("raw"),
|
2023-07-06 23:23:27 +00:00
|
|
|
blockSize = DefaultBlockSize
|
2023-06-22 15:11:18 +00:00
|
|
|
): ?!Manifest =
|
|
|
|
## Create a manifest using an array of `Cid`s
|
2022-03-14 16:06:36 +00:00
|
|
|
##
|
|
|
|
|
2023-09-25 14:31:10 +00:00
|
|
|
if hcodec notin EmptyDigests[version]:
|
|
|
|
return failure("Unsupported manifest hash codec!")
|
2022-03-14 16:06:36 +00:00
|
|
|
|
|
|
|
T(
|
|
|
|
blocks: @blocks,
|
|
|
|
version: version,
|
|
|
|
codec: codec,
|
|
|
|
hcodec: hcodec,
|
2022-04-05 00:46:13 +00:00
|
|
|
blockSize: blockSize,
|
2023-07-06 23:23:27 +00:00
|
|
|
originalBytes: blocks.len.NBytes * blockSize,
|
2022-04-05 00:46:13 +00:00
|
|
|
protected: protected).success
|
|
|
|
|
|
|
|
proc new*(
|
2023-06-22 15:11:18 +00:00
|
|
|
T: type Manifest,
|
|
|
|
manifest: Manifest,
|
|
|
|
ecK, ecM: int
|
|
|
|
): ?!Manifest =
|
2022-04-05 00:46:13 +00:00
|
|
|
## Create an erasure protected dataset from an
|
|
|
|
## un-protected one
|
|
|
|
##
|
|
|
|
|
|
|
|
var
|
|
|
|
self = Manifest(
|
|
|
|
version: manifest.version,
|
|
|
|
codec: manifest.codec,
|
|
|
|
hcodec: manifest.hcodec,
|
2022-08-24 12:15:59 +00:00
|
|
|
originalBytes: manifest.originalBytes,
|
2022-04-05 00:46:13 +00:00
|
|
|
blockSize: manifest.blockSize,
|
|
|
|
protected: true,
|
2023-03-10 07:02:54 +00:00
|
|
|
ecK: ecK, ecM: ecM,
|
2022-04-05 00:46:13 +00:00
|
|
|
originalCid: ? manifest.cid,
|
|
|
|
originalLen: manifest.len)
|
|
|
|
|
|
|
|
let
|
2023-03-10 07:02:54 +00:00
|
|
|
encodedLen = self.rounded + (self.steps * ecM)
|
2022-04-05 00:46:13 +00:00
|
|
|
|
|
|
|
self.blocks = newSeq[Cid](encodedLen)
|
|
|
|
|
|
|
|
# copy original manifest blocks
|
|
|
|
for i in 0..<self.rounded:
|
|
|
|
if i < manifest.len:
|
|
|
|
self.blocks[i] = manifest[i]
|
|
|
|
else:
|
|
|
|
self.blocks[i] = EmptyCid[manifest.version]
|
|
|
|
.catch
|
|
|
|
.get()[manifest.hcodec]
|
|
|
|
.catch
|
|
|
|
.get()
|
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
? self.verify()
|
2022-04-05 00:46:13 +00:00
|
|
|
self.success
|
2022-03-15 18:47:31 +00:00
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
proc new*(
|
2022-03-15 18:47:31 +00:00
|
|
|
T: type Manifest,
|
2022-04-06 00:34:29 +00:00
|
|
|
data: openArray[byte],
|
2023-06-22 15:11:18 +00:00
|
|
|
decoder = ManifestContainers[$DagPBCodec]
|
|
|
|
): ?!Manifest =
|
|
|
|
## Create a manifest instance from given data
|
2023-07-19 14:06:59 +00:00
|
|
|
##
|
2022-04-06 00:34:29 +00:00
|
|
|
Manifest.decode(data, decoder)
|
2023-07-19 14:06:59 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type Manifest,
|
|
|
|
rootHash: Cid,
|
|
|
|
originalBytes: NBytes,
|
|
|
|
blockSize: NBytes,
|
|
|
|
blocks: seq[Cid],
|
|
|
|
version: CidVersion,
|
|
|
|
hcodec: MultiCodec,
|
|
|
|
codec: MultiCodec,
|
|
|
|
ecK: int,
|
|
|
|
ecM: int,
|
|
|
|
originalCid: Cid,
|
|
|
|
originalLen: int
|
|
|
|
): Manifest =
|
|
|
|
Manifest(
|
|
|
|
rootHash: rootHash.some,
|
|
|
|
originalBytes: originalBytes,
|
|
|
|
blockSize: blockSize,
|
|
|
|
blocks: blocks,
|
|
|
|
version: version,
|
|
|
|
hcodec: hcodec,
|
|
|
|
codec: codec,
|
|
|
|
protected: true,
|
|
|
|
ecK: ecK,
|
|
|
|
ecM: ecM,
|
|
|
|
originalCid: originalCid,
|
|
|
|
originalLen: originalLen
|
|
|
|
)
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type Manifest,
|
|
|
|
rootHash: Cid,
|
|
|
|
originalBytes: NBytes,
|
|
|
|
blockSize: NBytes,
|
|
|
|
blocks: seq[Cid],
|
|
|
|
version: CidVersion,
|
|
|
|
hcodec: MultiCodec,
|
|
|
|
codec: MultiCodec
|
|
|
|
): Manifest =
|
|
|
|
Manifest(
|
|
|
|
rootHash: rootHash.some,
|
|
|
|
originalBytes: originalBytes,
|
|
|
|
blockSize: blockSize,
|
|
|
|
blocks: blocks,
|
|
|
|
version: version,
|
|
|
|
hcodec: hcodec,
|
|
|
|
codec: codec,
|
|
|
|
protected: false,
|
|
|
|
)
|