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-07-28 00:39:17 +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-07-28 00:39:17 +00:00
|
|
|
trace "Getting block from network store", cid
|
|
|
|
|
|
|
|
let blk = await self.localStore.getBlock(cid)
|
|
|
|
if blk.isErr:
|
|
|
|
return blk
|
|
|
|
if blk.get.isSome:
|
|
|
|
trace "Retrieved block from local store", cid
|
|
|
|
return blk
|
|
|
|
|
|
|
|
trace "Block not found in local store", cid
|
|
|
|
try:
|
|
|
|
# TODO: What if block isn't available in the engine too?
|
|
|
|
let blk = await self.engine.requestBlock(cid)
|
|
|
|
# TODO: add block to the local store
|
|
|
|
return blk.some.success
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception requesting block", cid, exc = exc.msg
|
|
|
|
return failure(exc)
|
|
|
|
|
|
|
|
method putBlock*(self: NetworkStore, blk: bt.Block): Future[?!void] {.async.} =
|
|
|
|
## 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-07-28 00:39:17 +00:00
|
|
|
let res = await self.localStore.putBlock(blk)
|
|
|
|
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
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type NetworkStore,
|
|
|
|
engine: BlockExcEngine,
|
|
|
|
localStore: BlockStore): T =
|
|
|
|
|
|
|
|
let b = NetworkStore(
|
|
|
|
localStore: localStore,
|
|
|
|
engine: engine)
|
|
|
|
|
|
|
|
return b
|