2022-01-10 15:32:56 +00:00
|
|
|
## Nim-Dagger
|
|
|
|
## Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-01-11 02:25:27 +00:00
|
|
|
import std/tables
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/libp2p/protobuf/minprotobuf
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
|
|
|
|
import ./blocktype
|
|
|
|
import ./errors
|
|
|
|
|
|
|
|
const
|
|
|
|
ManifestCodec* = multiCodec("dag-pb")
|
|
|
|
|
|
|
|
var
|
2022-01-11 02:25:27 +00:00
|
|
|
emptyDigests {.threadvar.}: array[CIDv0..CIDv1, Table[MultiCodec, MultiHash]]
|
|
|
|
once {.threadvar.}: bool
|
|
|
|
|
|
|
|
template EmptyDigests: untyped =
|
|
|
|
if not once:
|
|
|
|
emptyDigests = [
|
|
|
|
CIDv0: {
|
|
|
|
multiCodec("sha2-256"): Cid
|
|
|
|
.init("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku")
|
|
|
|
.get()
|
|
|
|
.mhash
|
|
|
|
.get()
|
|
|
|
}.toTable,
|
|
|
|
CIDv1: {
|
|
|
|
multiCodec("sha2-256"): Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n")
|
|
|
|
.get()
|
|
|
|
.mhash
|
|
|
|
.get()
|
|
|
|
}.toTable,
|
|
|
|
]
|
|
|
|
|
|
|
|
once = true
|
|
|
|
emptyDigests
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
type
|
2022-01-13 00:24:35 +00:00
|
|
|
Manifest* = object
|
|
|
|
cid*: ?Cid
|
|
|
|
blocks*: seq[Cid]
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
BlocksManifest* = object
|
2022-01-13 00:24:35 +00:00
|
|
|
manifest: Manifest
|
2022-01-10 15:32:56 +00:00
|
|
|
version*: CidVersion
|
|
|
|
hcodec*: MultiCodec
|
|
|
|
codec*: MultiCodec
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
proc len*(b: BlocksManifest): int = b.manifest.blocks.len
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
iterator items*(b: BlocksManifest): Cid =
|
2022-01-13 00:24:35 +00:00
|
|
|
for b in b.manifest.blocks:
|
2022-01-10 15:32:56 +00:00
|
|
|
yield b
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
template hashBytes(mh: MultiHash): seq[byte] =
|
2022-01-11 02:25:27 +00:00
|
|
|
## get the hash bytes of a multihash object
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
mh.data.buffer[mh.dpos..(mh.dpos + mh.size - 1)]
|
|
|
|
|
|
|
|
proc cid*(b: var BlocksManifest): ?!Cid =
|
2022-01-11 02:25:27 +00:00
|
|
|
## Generate a root hash using the treehash algorithm
|
|
|
|
##
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
if htree =? b.manifest.cid:
|
2022-01-10 15:32:56 +00:00
|
|
|
return htree.success
|
|
|
|
|
|
|
|
var
|
|
|
|
stack: seq[MultiHash]
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
for cid in b.manifest.blocks:
|
2022-01-10 15:32:56 +00:00
|
|
|
stack.add(? cid.mhash.mapFailure)
|
|
|
|
|
|
|
|
while stack.len > 1:
|
|
|
|
let
|
|
|
|
(b1, b2) = (stack.pop(), stack.pop())
|
|
|
|
mh = ? MultiHash.digest(
|
|
|
|
$b.hcodec,
|
|
|
|
(b1.hashBytes() & b2.hashBytes()))
|
|
|
|
.mapFailure
|
|
|
|
stack.add(mh)
|
|
|
|
|
|
|
|
if stack.len == 1:
|
2022-01-11 03:04:00 +00:00
|
|
|
let cid = ? Cid.init(
|
|
|
|
b.version,
|
|
|
|
b.codec,
|
|
|
|
(? EmptyDigests[b.version][b.hcodec].catch))
|
|
|
|
.mapFailure
|
2022-01-13 00:24:35 +00:00
|
|
|
|
|
|
|
b.manifest.cid = cid.some
|
2022-01-10 15:32:56 +00:00
|
|
|
return cid.success
|
|
|
|
|
|
|
|
proc put*(b: var BlocksManifest, cid: Cid) =
|
2022-01-13 00:24:35 +00:00
|
|
|
b.manifest.cid = Cid.none
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Adding cid to manifest", cid
|
2022-01-13 00:24:35 +00:00
|
|
|
b.manifest.blocks.add(cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc contains*(b: BlocksManifest, cid: Cid): bool =
|
2022-01-13 00:24:35 +00:00
|
|
|
cid in b.manifest.blocks
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc encode*(b: var BlocksManifest): ?!seq[byte] =
|
|
|
|
## Encode the manifest into a ``ManifestCodec``
|
|
|
|
## multicodec container (Dag-pb) for now
|
2022-01-13 00:24:35 +00:00
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
var pbNode = initProtoBuffer()
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
for c in b.manifest.blocks:
|
2022-01-10 15:32:56 +00:00
|
|
|
var pbLink = initProtoBuffer()
|
|
|
|
pbLink.write(1, c.data.buffer) # write Cid links
|
|
|
|
pbLink.finish()
|
|
|
|
pbNode.write(2, pbLink)
|
|
|
|
|
|
|
|
let cid = ? b.cid
|
|
|
|
pbNode.write(1, cid.data.buffer) # set the treeHash Cid as the data field
|
|
|
|
pbNode.finish()
|
|
|
|
|
|
|
|
return pbNode.buffer.success
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
proc decode*(_: type BlocksManifest, data: openArray[byte]): ?!Manifest =
|
|
|
|
## Decode a manifest from a data blob
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
2022-01-13 00:24:35 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
var
|
|
|
|
pbNode = initProtoBuffer(data)
|
|
|
|
cidBuf: seq[byte]
|
|
|
|
blocks: seq[Cid]
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
if pbNode.getField(1, cidBuf).isErr:
|
|
|
|
return failure("Unable to decode Cid from manifest!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
let cid = ? Cid.init(cidBuf).mapFailure
|
|
|
|
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)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
if pbLink.getField(1, blockBuf).isOk:
|
|
|
|
blocks.add(? Cid.init(blockBuf).mapFailure)
|
|
|
|
|
|
|
|
Manifest(cid: cid.some, blocks: blocks).success
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type BlocksManifest,
|
|
|
|
blocks: openArray[Cid] = [],
|
|
|
|
version = CIDv1,
|
|
|
|
hcodec = multiCodec("sha2-256"),
|
|
|
|
codec = multiCodec("raw")): ?!T =
|
|
|
|
## Create a manifest using array of `Cid`s
|
|
|
|
##
|
|
|
|
|
2022-01-11 02:25:27 +00:00
|
|
|
if hcodec notin EmptyDigests[version]:
|
2022-01-13 00:24:35 +00:00
|
|
|
return failure("Unsupported manifest hash codec!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
T(
|
2022-01-13 00:24:35 +00:00
|
|
|
manifest: Manifest(blocks: @blocks),
|
2022-01-10 15:32:56 +00:00
|
|
|
version: version,
|
|
|
|
codec: codec,
|
|
|
|
hcodec: hcodec,
|
|
|
|
).success
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type BlocksManifest,
|
2022-01-13 00:24:35 +00:00
|
|
|
data: openArray[byte]): ?!T =
|
|
|
|
## Create manifest from a raw data blob
|
2022-01-10 15:32:56 +00:00
|
|
|
## (in dag-pb for for now)
|
|
|
|
##
|
|
|
|
|
|
|
|
let
|
2022-01-13 00:24:35 +00:00
|
|
|
manifest = ? BlocksManifest.decode(data)
|
|
|
|
cid = !manifest.cid
|
2022-01-10 15:32:56 +00:00
|
|
|
mhash = ? cid.mhash.mapFailure
|
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
var blockManifest = ? BlocksManifest.init(
|
|
|
|
manifest.blocks,
|
|
|
|
cid.version,
|
|
|
|
mhash.mcodec,
|
|
|
|
cid.mcodec)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
if cid != ? blockManifest.cid:
|
|
|
|
return failure("Decoded content hash doesn't match!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-01-13 00:24:35 +00:00
|
|
|
blockManifest.success
|