mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-02-14 05:46:32 +00:00
* settup basic nim node * adding http utils * adding confutils * rough rest api proto * adding missing deps * turn tls emulation off * adding toml serialization * wip * adding missing deps * make sure to clean old state in teardown * adding file upload rest endpoint * renaming blockexchange to networkstore * updating nim-presto * updating libp2p * wip adding streaming upload * reworked chunking * bump to latest unstable * adding asyncfutures stream * make streamable * deleting unused files * reworking stores api * use new stores api * rework blockset and remove blockstream * don't return option from constructor * rework chunker * wip implement upload * fix tests * move unrelated logic to engine * don't print entire message * logging * basic encode/decode to/from dag-pb * add basic upload/download support * fix tests * renaming blockset to manifest * don't pass config to node * remove config and use new manifest * wip: make endpoints more reliable * wip: adding node tests * include correct manifest test * removing asyncfutures * proper chunking of files * simplify stream reading * test with encoding/decoding with many blocks * add block storing tests * adding retrieval test * add logging * tidy up chunker * tidy up manifest and node * use default chunk size * fix tests * fix tests * make sure Eof is set properly * wip * minor cleanup * add file utils * cleanup config * splitout DaggerServer and "main" * remove events since they are not used * add broadcast method to network peer * add and wire localstore * use localstore in the node * wip * logging * move file utils * use the constant * updating deps * fix memstore * use latest libp2p unstable * fix tests * rework block streaming * don't fail storing if the block already exists * add helper info endpoint * correct comment * rename localstore to fsstore * fix tests * remove unused tests * add test to retrieve one block * move some test files around * consolidate setup * Update dagger/blockexchange/engine.nim Co-authored-by: Tanguy <tanguy@status.im> * typo * better block path handling * don't inherit rootobj * remove useless template * Update tests/dagger/blockexc/testblockexc.nim Co-authored-by: markspanbroek <mark@spanbroek.net> * use isMainModule * use proper flag for starter/stoped * cleanup optional use * wrap in isMainModule * use `cancelAndAwait` * remove unused imports * wip * don't use optional * use functional error api * rework store tests and add fs tests * Block.new() to Block.init() * don't use optional for engine blocks * use result instead of optional for getBlock * remove unused imports * move stopping servers to `shutdown` * use result instead of optional * rework with results * fix tests * use waitFor in signal handlers * error helper * use `?` and mapFailure where possible * remove unnecesary `=?` * improve empty cid digest initialization Co-authored-by: Tanguy <tanguy@status.im> Co-authored-by: markspanbroek <mark@spanbroek.net>
179 lines
4.3 KiB
Nim
179 lines
4.3 KiB
Nim
## 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].}
|
|
|
|
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
|
|
emptyDigest {.threadvar.}: array[CidVersion, MultiHash]
|
|
|
|
type
|
|
BlocksManifest* = object
|
|
blocks: seq[Cid]
|
|
htree: ?Cid
|
|
version*: CidVersion
|
|
hcodec*: MultiCodec
|
|
codec*: MultiCodec
|
|
|
|
proc len*(b: BlocksManifest): int = b.blocks.len
|
|
|
|
iterator items*(b: BlocksManifest): Cid =
|
|
for b in b.blocks:
|
|
yield b
|
|
|
|
proc hashBytes(mh: MultiHash): seq[byte] =
|
|
mh.data.buffer[mh.dpos..(mh.dpos + mh.size - 1)]
|
|
|
|
proc cid*(b: var BlocksManifest): ?!Cid =
|
|
if htree =? b.htree:
|
|
return htree.success
|
|
|
|
var
|
|
stack: seq[MultiHash]
|
|
|
|
if stack.len == 1:
|
|
stack.add(emptyDigest[b.version])
|
|
|
|
for cid in b.blocks:
|
|
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:
|
|
let cid = ? Cid.init(b.version, b.codec, stack[0]).mapFailure
|
|
b.htree = cid.some
|
|
return cid.success
|
|
|
|
proc put*(b: var BlocksManifest, cid: Cid) =
|
|
b.htree = Cid.none
|
|
trace "Adding cid to manifest", cid
|
|
b.blocks.add(cid)
|
|
|
|
proc contains*(b: BlocksManifest, cid: Cid): bool =
|
|
cid in b.blocks
|
|
|
|
proc encode*(b: var BlocksManifest): ?!seq[byte] =
|
|
## Encode the manifest into a ``ManifestCodec``
|
|
## multicodec container (Dag-pb) for now
|
|
var pbNode = initProtoBuffer()
|
|
|
|
for c in b.blocks:
|
|
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
|
|
|
|
proc decode*(_: type BlocksManifest, data: seq[byte]): ?!(Cid, seq[Cid]) =
|
|
## Decode a manifest from a byte seq
|
|
##
|
|
var
|
|
pbNode = initProtoBuffer(data)
|
|
cidBuf: seq[byte]
|
|
blocks: seq[Cid]
|
|
|
|
if pbNode.getField(1, cidBuf).isOk:
|
|
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)
|
|
|
|
if pbLink.getField(1, blockBuf).isOk:
|
|
let cidRes = Cid.init(blockBuf)
|
|
if cidRes.isOk:
|
|
blocks.add(cidRes.get())
|
|
|
|
return (cid, blocks).success
|
|
|
|
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
|
|
##
|
|
|
|
# Only gets initialized once
|
|
once:
|
|
# TODO: The CIDs should be initialized at compile time,
|
|
# but the VM fails due to a `memmove` being invoked somewhere
|
|
|
|
for v in [CIDv0, CIDv1]:
|
|
let
|
|
cid = if v == CIDv1:
|
|
? Cid.init("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku").mapFailure
|
|
else:
|
|
? Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n").mapFailure
|
|
|
|
mhash = ? cid.mhash.mapFailure
|
|
digest = ? MultiHash.digest(
|
|
$hcodec,
|
|
mhash.hashBytes()).mapFailure
|
|
|
|
emptyDigest[v] = digest
|
|
|
|
T(
|
|
blocks: @blocks,
|
|
version: version,
|
|
codec: codec,
|
|
hcodec: hcodec,
|
|
).success
|
|
|
|
proc init*(
|
|
T: type BlocksManifest,
|
|
blk: Block): ?!T =
|
|
## Create manifest from a raw manifest block
|
|
## (in dag-pb for for now)
|
|
##
|
|
|
|
let
|
|
(cid, blocks) = ? BlocksManifest.decode(blk.data)
|
|
mhash = ? cid.mhash.mapFailure
|
|
|
|
var
|
|
manifest = ? BlocksManifest.init(
|
|
blocks,
|
|
cid.version,
|
|
mhash.mcodec,
|
|
cid.mcodec)
|
|
|
|
if cid != (? manifest.cid):
|
|
return failure("Content hashes don't match!")
|
|
|
|
return manifest.success
|