2021-02-26 00:23:22 +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/sequtils
|
|
|
|
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/libp2p/errors
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
import ../blocktype as bt
|
|
|
|
import ../utils/asyncheapqueue
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
import ./blockstore
|
|
|
|
import ../blockexchange/network
|
|
|
|
import ../blockexchange/engine
|
|
|
|
import ../blockexchange/peercontext
|
|
|
|
import ../blockexchange/protobuf/blockexc as pb
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
export blockstore, network, engine, asyncheapqueue
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
logScope:
|
2021-08-30 19:25:20 +00:00
|
|
|
topics = "dagger blockexc"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
DefaultTaskQueueSize = 100
|
|
|
|
DefaultConcurrentTasks = 10
|
|
|
|
DefaultMaxRetries = 3
|
|
|
|
|
|
|
|
type
|
2021-08-30 19:25:20 +00:00
|
|
|
BlockExc* = ref object of BlockStore
|
|
|
|
engine*: BlockExcEngine # blockexc decision engine
|
|
|
|
taskQueue*: AsyncHeapQueue[BlockExcPeerCtx] # peers we're currently processing tasks for
|
|
|
|
blockexcTasks: seq[Future[void]] # future to control blockexc task
|
|
|
|
blockexcRunning: bool # indicates if the blockexc task is running
|
2021-02-26 00:23:22 +00:00
|
|
|
concurrentTasks: int # number of concurrent peers we're serving at any given time
|
|
|
|
maxRetries: int # max number of tries for a failed block
|
|
|
|
taskHandler: TaskHandler # handler provided by the engine called by the runner
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc blockexcTaskRunner(b: BlockExc) {.async.} =
|
2021-05-10 12:06:34 +00:00
|
|
|
## process tasks
|
2021-02-26 00:23:22 +00:00
|
|
|
##
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
while b.blockexcRunning:
|
2021-02-26 00:23:22 +00:00
|
|
|
let peerCtx = await b.taskQueue.pop()
|
|
|
|
asyncSpawn b.taskHandler(peerCtx)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
trace "Exiting blockexc task runner"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc start*(b: BlockExc) {.async.} =
|
|
|
|
## Start the blockexc task
|
2021-02-26 00:23:22 +00:00
|
|
|
##
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
trace "blockexc start"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
if b.blockexcTasks.len > 0:
|
|
|
|
warn "Starting blockexc twice"
|
2021-02-26 00:23:22 +00:00
|
|
|
return
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
b.blockexcRunning = true
|
2021-02-26 00:23:22 +00:00
|
|
|
for i in 0..<b.concurrentTasks:
|
2021-08-30 19:25:20 +00:00
|
|
|
b.blockexcTasks.add(b.blockexcTaskRunner)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc stop*(b: BlockExc) {.async.} =
|
|
|
|
## Stop the blockexc blockexc
|
2021-02-26 00:23:22 +00:00
|
|
|
##
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
trace "BlockExc stop"
|
|
|
|
if b.blockexcTasks.len <= 0:
|
|
|
|
warn "Stopping blockexc without starting it"
|
2021-02-26 00:23:22 +00:00
|
|
|
return
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
b.blockexcRunning = false
|
|
|
|
for t in b.blockexcTasks:
|
2021-02-26 00:23:22 +00:00
|
|
|
if not t.finished:
|
|
|
|
trace "Awaiting task to stop"
|
|
|
|
t.cancel()
|
|
|
|
trace "Task stopped"
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
trace "BlockExc stopped"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
method getBlocks*(b: BlockExc, cid: seq[Cid]): Future[seq[bt.Block]] {.async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Get a block from a remote peer
|
|
|
|
##
|
|
|
|
|
|
|
|
let blocks = await allFinished(b.engine.requestBlocks(cid))
|
|
|
|
return blocks.filterIt(
|
|
|
|
not it.failed
|
|
|
|
).mapIt(
|
|
|
|
it.read
|
|
|
|
)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
method putBlocks*(b: BlockExc, blocks: seq[bt.Block]) =
|
2021-02-26 00:23:22 +00:00
|
|
|
b.engine.resolveBlocks(blocks)
|
|
|
|
|
|
|
|
procCall BlockStore(b).putBlocks(blocks)
|
|
|
|
|
|
|
|
proc new*(
|
2021-08-30 19:25:20 +00:00
|
|
|
T: type BlockExc,
|
2021-02-26 00:23:22 +00:00
|
|
|
localStore: BlockStore,
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet: WalletRef,
|
2021-08-30 19:25:20 +00:00
|
|
|
network: BlockExcNetwork,
|
2021-02-26 00:23:22 +00:00
|
|
|
concurrentTasks = DefaultConcurrentTasks,
|
|
|
|
maxRetries = DefaultMaxRetries,
|
|
|
|
peersPerRequest = DefaultMaxPeersPerRequest): T =
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
let engine = BlockExcEngine.new(
|
2021-02-26 00:23:22 +00:00
|
|
|
localStore = localStore,
|
2021-04-08 13:05:04 +00:00
|
|
|
wallet = wallet,
|
2021-02-26 00:23:22 +00:00
|
|
|
peersPerRequest = peersPerRequest,
|
|
|
|
request = network.request,
|
|
|
|
)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
let b = BlockExc(
|
2021-02-26 00:23:22 +00:00
|
|
|
engine: engine,
|
2021-08-30 19:25:20 +00:00
|
|
|
taskQueue: newAsyncHeapQueue[BlockExcPeerCtx](DefaultTaskQueueSize),
|
2021-02-26 00:23:22 +00:00
|
|
|
concurrentTasks: concurrentTasks,
|
|
|
|
maxRetries: maxRetries,
|
|
|
|
)
|
|
|
|
|
|
|
|
# attach engine's task handler
|
2021-08-30 19:25:20 +00:00
|
|
|
b.taskHandler = proc(task: BlockExcPeerCtx): Future[void] {.gcsafe.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
engine.taskHandler(task)
|
|
|
|
|
|
|
|
# attach task scheduler to engine
|
2021-08-30 19:25:20 +00:00
|
|
|
engine.scheduleTask = proc(task: BlockExcPeerCtx): bool {.gcsafe} =
|
2021-02-26 00:23:22 +00:00
|
|
|
b.taskQueue.pushOrUpdateNoWait(task).isOk()
|
|
|
|
|
2021-10-29 14:33:43 +00:00
|
|
|
proc peerEventHandler(peerId: PeerID, event: PeerEvent) {.async.} =
|
2021-08-30 19:25:20 +00:00
|
|
|
# TODO: temporary until libp2p moves back to PeerID
|
2021-02-26 00:23:22 +00:00
|
|
|
if event.kind == PeerEventKind.Joined:
|
|
|
|
b.engine.setupPeer(peerId)
|
|
|
|
else:
|
|
|
|
b.engine.dropPeer(peerId)
|
|
|
|
|
|
|
|
network.switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Joined)
|
|
|
|
network.switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Left)
|
|
|
|
|
|
|
|
proc blockWantListHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
wantList: WantList) {.gcsafe.} =
|
|
|
|
engine.wantListHandler(peer, wantList)
|
|
|
|
|
|
|
|
proc blockPresenceHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
presence: seq[BlockPresence]) {.gcsafe.} =
|
|
|
|
engine.blockPresenceHandler(peer, presence)
|
|
|
|
|
|
|
|
proc blocksHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
blocks: seq[bt.Block]) {.gcsafe.} =
|
|
|
|
engine.blocksHandler(peer, blocks)
|
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
proc accountHandler(peer: PeerId, account: Account) =
|
|
|
|
engine.accountHandler(peer, account)
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2021-04-19 14:47:47 +00:00
|
|
|
proc paymentHandler(peer: PeerId, payment: SignedState) =
|
|
|
|
engine.paymentHandler(peer, payment)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
network.handlers = BlockExcHandlers(
|
2021-02-26 00:23:22 +00:00
|
|
|
onWantList: blockWantListHandler,
|
|
|
|
onBlocks: blocksHandler,
|
|
|
|
onPresence: blockPresenceHandler,
|
2021-05-10 14:21:47 +00:00
|
|
|
onAccount: accountHandler,
|
2021-04-19 14:47:47 +00:00
|
|
|
onPayment: paymentHandler
|
2021-02-26 00:23:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return b
|