2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-01-10 15:32:56 +00:00
|
|
|
## 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.
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
|
|
|
|
import ../blocktype as bt
|
|
|
|
import ../utils/asyncheapqueue
|
|
|
|
|
|
|
|
import ./blockstore
|
2022-05-19 02:29:15 +00:00
|
|
|
import ../blockexchange
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
export blockstore, blockexchange, asyncheapqueue
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
logScope:
|
2022-05-19 19:56:03 +00:00
|
|
|
topics = "codex networkstore"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
NetworkStore* = ref object of BlockStore
|
|
|
|
engine*: BlockExcEngine # blockexc decision engine
|
|
|
|
localStore*: BlockStore # local block store
|
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
method getBlock*(self: NetworkStore, cid: Cid): Future[?!bt.Block] {.async.} =
|
2022-01-10 15:32:56 +00:00
|
|
|
## Get a block from a remote peer
|
|
|
|
##
|
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
trace "Getting block from local store or network", cid
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
without blk =? await self.localStore.getBlock(cid), error:
|
|
|
|
if not (error of BlockNotFoundError): return failure error
|
|
|
|
trace "Block not in local store", cid
|
2022-07-28 00:39:17 +00:00
|
|
|
# TODO: What if block isn't available in the engine too?
|
2022-08-19 00:56:36 +00:00
|
|
|
# TODO: add retrieved block to the local store
|
|
|
|
return (await self.engine.requestBlock(cid)).catch
|
|
|
|
|
|
|
|
return success blk
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
method putBlock*(
|
|
|
|
self: NetworkStore,
|
|
|
|
blk: bt.Block,
|
|
|
|
ttl = Duration.none): Future[?!void] {.async.} =
|
2022-07-28 00:39:17 +00:00
|
|
|
## Store block locally and notify the network
|
2022-04-05 14:24:48 +00:00
|
|
|
##
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Puting block into network store", cid = blk.cid
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
let res = await self.localStore.putBlock(blk, ttl)
|
2022-07-28 00:39:17 +00:00
|
|
|
if res.isErr:
|
|
|
|
return res
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
await self.engine.resolveBlocks(@[blk])
|
|
|
|
return success()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method delBlock*(self: NetworkStore, cid: Cid): Future[?!void] =
|
2022-06-28 16:10:05 +00:00
|
|
|
## Delete a block from the blockstore
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Deleting block from network store", cid
|
2022-06-28 16:10:05 +00:00
|
|
|
return self.localStore.delBlock(cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method hasBlock*(self: NetworkStore, cid: Cid): Future[?!bool] {.async.} =
|
2022-01-10 15:32:56 +00:00
|
|
|
## Check if the block exists in the blockstore
|
|
|
|
##
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Checking network store for block existence", cid
|
|
|
|
return await self.localStore.hasBlock(cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-22 23:38:49 +00:00
|
|
|
method close*(self: NetworkStore): Future[void] {.async.} =
|
|
|
|
## Close the underlying local blockstore
|
|
|
|
##
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
if not self.localStore.isNil:
|
|
|
|
await self.localStore.close
|
2022-07-22 23:38:49 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc new*(
|
|
|
|
T: type NetworkStore,
|
|
|
|
engine: BlockExcEngine,
|
|
|
|
localStore: BlockStore): T =
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
let
|
|
|
|
self = NetworkStore(
|
|
|
|
localStore: localStore,
|
|
|
|
engine: engine)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
return self
|