mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-01-26 20:59:44 +00:00
fbe161a073
* 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>
92 lines
2.1 KiB
Nim
92 lines
2.1 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/chronicles
|
|
import pkg/chronos
|
|
import pkg/libp2p
|
|
import pkg/libp2p/errors
|
|
|
|
import ../blocktype as bt
|
|
import ../utils/asyncheapqueue
|
|
|
|
import ./blockstore
|
|
import ../blockexchange/network
|
|
import ../blockexchange/engine
|
|
import ../blockexchange/peercontext
|
|
|
|
export blockstore, network, engine, asyncheapqueue
|
|
|
|
logScope:
|
|
topics = "dagger networkstore"
|
|
|
|
type
|
|
NetworkStore* = ref object of BlockStore
|
|
engine*: BlockExcEngine # blockexc decision engine
|
|
localStore*: BlockStore # local block store
|
|
|
|
method getBlock*(
|
|
self: NetworkStore,
|
|
cid: Cid): Future[?!bt.Block] {.async.} =
|
|
## Get a block from a remote peer
|
|
##
|
|
|
|
trace "Getting block", cid
|
|
without var blk =? (await self.localStore.getBlock(cid)):
|
|
trace "Couldn't get from local store", cid
|
|
blk = try:
|
|
await self.engine.requestBlock(cid)
|
|
except CatchableError as exc:
|
|
trace "Exception requestig block", cid, exc = exc.msg
|
|
return failure(exc.msg)
|
|
|
|
trace "Retrieved block from local store", cid
|
|
return blk.success
|
|
|
|
method putBlock*(
|
|
self: NetworkStore,
|
|
blk: bt.Block): Future[bool] {.async.} =
|
|
trace "Puting block", cid = blk.cid
|
|
|
|
if not (await self.localStore.putBlock(blk)):
|
|
return false
|
|
|
|
self.engine.resolveBlocks(@[blk])
|
|
return true
|
|
|
|
method delBlock*(
|
|
self: NetworkStore,
|
|
cid: Cid): Future[bool] =
|
|
## Delete a block/s from the block store
|
|
##
|
|
|
|
self.localStore.delBlock(cid)
|
|
|
|
{.pop.}
|
|
|
|
method hasBlock*(
|
|
self: NetworkStore,
|
|
cid: Cid): bool =
|
|
## Check if the block exists in the blockstore
|
|
##
|
|
|
|
self.localStore.hasBlock(cid)
|
|
|
|
proc new*(
|
|
T: type NetworkStore,
|
|
engine: BlockExcEngine,
|
|
localStore: BlockStore): T =
|
|
|
|
let b = NetworkStore(
|
|
localStore: localStore,
|
|
engine: engine)
|
|
|
|
return b
|