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
|
|
|
|
|
|
|
|
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
|
2022-05-12 20:02:30 +00:00
|
|
|
try:
|
|
|
|
blk = await self.engine.requestBlock(cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
except CatchableError as exc:
|
2022-05-12 20:02:30 +00:00
|
|
|
trace "Exception requesting block", cid, exc = exc.msg
|
2022-01-10 15:32:56 +00:00
|
|
|
return failure(exc.msg)
|
|
|
|
|
|
|
|
trace "Retrieved block from local store", cid
|
|
|
|
return blk.success
|
|
|
|
|
|
|
|
method putBlock*(
|
|
|
|
self: NetworkStore,
|
|
|
|
blk: bt.Block): Future[bool] {.async.} =
|
2022-04-05 14:24:48 +00:00
|
|
|
## Store block locally and notify the
|
|
|
|
## network
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Puting block", cid = blk.cid
|
|
|
|
|
|
|
|
if not (await self.localStore.putBlock(blk)):
|
|
|
|
return false
|
|
|
|
|
2022-06-29 15:37:40 +00:00
|
|
|
await self.engine.resolveBlocks(@[blk])
|
2022-01-10 15:32:56 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
method delBlock*(
|
|
|
|
self: NetworkStore,
|
2022-06-28 16:10:05 +00:00
|
|
|
cid: Cid): Future[?!void] =
|
|
|
|
## Delete a block from the blockstore
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-06-28 16:10:05 +00:00
|
|
|
trace "Deleting block from networkstore", cid
|
|
|
|
return self.localStore.delBlock(cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
2022-06-29 15:37:40 +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-06-29 15:37:40 +00:00
|
|
|
trace "Checking NetworkStore 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
|