## 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 import ../blocktype as bt import ../utils/asyncheapqueue import ./blockstore import ../blockexchange/network import ../blockexchange/engine import ../blockexchange/peercontext import ../blockexchange/protobuf/blockexc as pb export blockstore, network, engine, asyncheapqueue logScope: topics = "dagger blockexc" const DefaultTaskQueueSize = 100 DefaultConcurrentTasks = 10 DefaultMaxRetries = 3 type 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 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 proc blockexcTaskRunner(b: BlockExc) {.async.} = ## process tasks ## while b.blockexcRunning: let peerCtx = await b.taskQueue.pop() asyncSpawn b.taskHandler(peerCtx) trace "Exiting blockexc task runner" proc start*(b: BlockExc) {.async.} = ## Start the blockexc task ## trace "blockexc start" if b.blockexcTasks.len > 0: warn "Starting blockexc twice" return b.blockexcRunning = true for i in 0..