2022-03-14 16:06:36 +00:00
|
|
|
## Nim-Dagger
|
|
|
|
## 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-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-03-15 18:47:31 +00:00
|
|
|
import ../blocktype
|
2022-03-14 16:06:36 +00:00
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
type
|
2022-03-17 13:56:46 +00:00
|
|
|
Manifest* = ref object of RootObj
|
2022-04-05 00:46:13 +00:00
|
|
|
rootHash*: ?Cid # root (tree) hash of the contained data set
|
|
|
|
blockSize*: int # 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*: bool # Protected datasets have erasure coded info
|
|
|
|
of true:
|
|
|
|
K*: int # Number of blocks to encode
|
|
|
|
M*: 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
|
|
|
|
else:
|
|
|
|
discard
|
2022-03-14 16:06:36 +00:00
|
|
|
|
|
|
|
func len*(self: Manifest): int =
|
|
|
|
self.blocks.len
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
func size*(self: Manifest): int =
|
|
|
|
self.blocks.len * self.blockSize
|
|
|
|
|
2022-03-14 16:06:36 +00:00
|
|
|
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-03-17 13:56:46 +00:00
|
|
|
proc add*(self: Manifest, cid: Cid) =
|
2022-03-14 16:06:36 +00:00
|
|
|
self.rootHash = Cid.none
|
|
|
|
trace "Adding cid to manifest", cid
|
|
|
|
self.blocks.add(cid)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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:
|
|
|
|
let cid = ? Cid.init(
|
|
|
|
self.version,
|
|
|
|
self.codec,
|
|
|
|
(? EmptyDigests[self.version][self.hcodec].catch))
|
|
|
|
.mapFailure
|
|
|
|
|
|
|
|
self.rootHash = cid.some
|
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
success()
|
|
|
|
|
|
|
|
func rounded*(self: Manifest): int =
|
|
|
|
if (self.originalLen mod self.K) != 0:
|
|
|
|
return self.originalLen + (self.K - (self.originalLen mod self.K))
|
|
|
|
|
|
|
|
self.originalLen
|
|
|
|
|
|
|
|
func steps*(self: Manifest): int =
|
|
|
|
self.rounded div self.K # number of blocks per row
|
2022-03-14 16:06:36 +00:00
|
|
|
|
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-03-17 13:56:46 +00:00
|
|
|
proc new*(
|
2022-03-14 16:06:36 +00:00
|
|
|
T: type Manifest,
|
|
|
|
blocks: openArray[Cid] = [],
|
2022-04-05 00:46:13 +00:00
|
|
|
protected = false,
|
2022-03-14 16:06:36 +00:00
|
|
|
version = CIDv1,
|
|
|
|
hcodec = multiCodec("sha2-256"),
|
2022-03-15 18:47:31 +00:00
|
|
|
codec = multiCodec("raw"),
|
|
|
|
blockSize = BlockSize): ?!T =
|
2022-03-14 16:06:36 +00:00
|
|
|
## Create a manifest using array of `Cid`s
|
|
|
|
##
|
|
|
|
|
|
|
|
if hcodec notin EmptyDigests[version]:
|
|
|
|
return failure("Unsupported manifest hash codec!")
|
|
|
|
|
|
|
|
T(
|
|
|
|
blocks: @blocks,
|
|
|
|
version: version,
|
|
|
|
codec: codec,
|
|
|
|
hcodec: hcodec,
|
2022-04-05 00:46:13 +00:00
|
|
|
blockSize: blockSize,
|
|
|
|
protected: protected).success
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type Manifest,
|
|
|
|
manifest: Manifest,
|
|
|
|
K, M: int): ?!Manifest =
|
|
|
|
## Create an erasure protected dataset from an
|
|
|
|
## un-protected one
|
|
|
|
##
|
|
|
|
|
|
|
|
var
|
|
|
|
self = Manifest(
|
|
|
|
version: manifest.version,
|
|
|
|
codec: manifest.codec,
|
|
|
|
hcodec: manifest.hcodec,
|
|
|
|
blockSize: manifest.blockSize,
|
|
|
|
protected: true,
|
|
|
|
K: K, M: M,
|
|
|
|
originalCid: ? manifest.cid,
|
|
|
|
originalLen: manifest.len)
|
|
|
|
|
|
|
|
let
|
|
|
|
encodedLen = self.rounded + (self.steps * M)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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,
|
|
|
|
data: openArray[byte]): ?!T =
|
|
|
|
Manifest.decode(data)
|