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.
|
|
|
|
|
|
|
|
import std/options
|
2022-03-14 16:06:36 +00:00
|
|
|
import std/tables
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
|
|
|
|
# TODO: remove once exported by libp2p
|
|
|
|
import pkg/libp2p/routing_record
|
|
|
|
import pkg/libp2p/signed_envelope
|
|
|
|
|
|
|
|
import ./chunker
|
|
|
|
import ./blocktype as bt
|
2022-03-14 16:06:36 +00:00
|
|
|
import ./manifest
|
2022-01-10 15:32:56 +00:00
|
|
|
import ./stores/blockstore
|
|
|
|
import ./blockexchange
|
2022-03-30 02:43:35 +00:00
|
|
|
import ./streams
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "dagger node"
|
|
|
|
|
|
|
|
type
|
|
|
|
DaggerError = object of CatchableError
|
|
|
|
|
|
|
|
DaggerNodeRef* = ref object
|
|
|
|
switch*: Switch
|
|
|
|
networkId*: PeerID
|
|
|
|
blockStore*: BlockStore
|
|
|
|
engine*: BlockExcEngine
|
|
|
|
|
|
|
|
proc start*(node: DaggerNodeRef) {.async.} =
|
|
|
|
await node.switch.start()
|
|
|
|
await node.engine.start()
|
|
|
|
node.networkId = node.switch.peerInfo.peerId
|
|
|
|
notice "Started dagger node", id = $node.networkId, addrs = node.switch.peerInfo.addrs
|
|
|
|
|
|
|
|
proc stop*(node: DaggerNodeRef) {.async.} =
|
2022-03-30 02:43:35 +00:00
|
|
|
trace "Stopping node"
|
|
|
|
|
|
|
|
if not node.engine.isNil:
|
|
|
|
await node.engine.stop()
|
|
|
|
|
|
|
|
if not node.switch.isNil:
|
|
|
|
await node.switch.stop()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc findPeer*(
|
|
|
|
node: DaggerNodeRef,
|
|
|
|
peerId: PeerID): Future[?!PeerRecord] {.async.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc connect*(
|
|
|
|
node: DaggerNodeRef,
|
|
|
|
peerId: PeerID,
|
|
|
|
addrs: seq[MultiAddress]): Future[void] =
|
|
|
|
node.switch.connect(peerId, addrs)
|
|
|
|
|
|
|
|
proc retrieve*(
|
|
|
|
node: DaggerNodeRef,
|
2022-03-30 02:43:35 +00:00
|
|
|
cid: Cid): Future[?!LPStream] {.async.} =
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
trace "Received retrieval request", cid
|
|
|
|
without blk =? await node.blockStore.getBlock(cid):
|
|
|
|
return failure(
|
|
|
|
newException(DaggerError, "Couldn't retrieve block for Cid!"))
|
|
|
|
|
|
|
|
without mc =? blk.cid.contentType():
|
|
|
|
return failure(
|
|
|
|
newException(DaggerError, "Couldn't identify Cid!"))
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
# if we got a manifest, stream the blocks
|
2022-03-14 16:06:36 +00:00
|
|
|
if $mc in ManifestContainers:
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Retrieving data set", cid, mc
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
without manifest =? Manifest.decode(blk.data, ManifestContainers[$mc]):
|
2022-01-13 01:55:51 +00:00
|
|
|
return failure("Unable to construct manifest!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
return LPStream(StoreStream.new(node.blockStore, manifest)).success
|
|
|
|
|
|
|
|
let
|
|
|
|
stream = BufferStream.new()
|
|
|
|
|
|
|
|
proc streamOneBlock(): Future[void] {.async.} =
|
|
|
|
try:
|
|
|
|
await stream.pushData(blk.data)
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Unable to send block", cid
|
|
|
|
discard
|
|
|
|
finally:
|
|
|
|
await stream.pushEof()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
asyncSpawn streamOneBlock()
|
|
|
|
return LPStream(stream).success()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc store*(
|
|
|
|
node: DaggerNodeRef,
|
|
|
|
stream: LPStream): Future[?!Cid] {.async.} =
|
|
|
|
trace "Storing data"
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
without var blockManifest =? Manifest.new():
|
2022-01-10 15:32:56 +00:00
|
|
|
return failure("Unable to create Block Set")
|
|
|
|
|
|
|
|
let
|
2022-03-30 02:43:35 +00:00
|
|
|
chunker = LPStreamChunker.new(stream, chunkSize = BlockSize)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
|
|
|
trace "Got data from stream", len = chunk.len
|
2022-03-18 19:50:53 +00:00
|
|
|
without blk =? bt.Block.new(chunk):
|
2022-01-11 02:25:13 +00:00
|
|
|
return failure("Unable to init block from chunk!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-03-14 16:06:36 +00:00
|
|
|
blockManifest.add(blk.cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
if not (await node.blockStore.putBlock(blk)):
|
|
|
|
# trace "Unable to store block", cid = blk.cid
|
|
|
|
return failure("Unable to store block " & $blk.cid)
|
|
|
|
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
return failure(exc.msg)
|
|
|
|
finally:
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
# Generate manifest
|
|
|
|
without data =? blockManifest.encode():
|
|
|
|
return failure(
|
|
|
|
newException(DaggerError, "Could not generate dataset manifest!"))
|
|
|
|
|
|
|
|
# Store as a dag-pb block
|
2022-03-18 19:50:53 +00:00
|
|
|
without manifest =? bt.Block.new(data = data, codec = DagPBCodec):
|
2022-01-11 02:25:13 +00:00
|
|
|
trace "Unable to init block from manifest data!"
|
|
|
|
return failure("Unable to init block from manifest data!")
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if not (await node.blockStore.putBlock(manifest)):
|
|
|
|
trace "Unable to store manifest", cid = manifest.cid
|
|
|
|
return failure("Unable to store manifest " & $manifest.cid)
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
without cid =? blockManifest.cid, error:
|
|
|
|
trace "Unable to generate manifest Cid!", exc = error.msg
|
|
|
|
return failure(error.msg)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
trace "Stored data", manifestCid = manifest.cid,
|
2022-03-30 02:43:35 +00:00
|
|
|
contentCid = cid,
|
2022-01-13 01:55:51 +00:00
|
|
|
blocks = blockManifest.len
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
return manifest.cid.success
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type DaggerNodeRef,
|
|
|
|
switch: Switch,
|
|
|
|
store: BlockStore,
|
|
|
|
engine: BlockExcEngine): T =
|
|
|
|
T(
|
|
|
|
switch: switch,
|
|
|
|
blockStore: store,
|
|
|
|
engine: engine)
|