## 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 ./bitswap/protobuf/bitswap as pb import ./blocktype as bt import ./stores/blockstore import ./utils/asyncheapqueue import ./bitswap/network import ./bitswap/engine export network, blockstore, asyncheapqueue, engine logScope: topics = "dagger bitswap" const DefaultTaskQueueSize = 100 DefaultConcurrentTasks = 10 DefaultMaxRetries = 3 type Bitswap* = ref object of BlockStore engine*: BitswapEngine # bitswap decision engine taskQueue*: AsyncHeapQueue[BitswapPeerCtx] # peers we're currently processing tasks for bitswapTasks: seq[Future[void]] # future to control bitswap task bitswapRunning: bool # indicates if the bitswap 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 bitswapTaskRunner(b: Bitswap) {.async.} = ## process tasks in order of least amount of ## debt ratio ## while b.bitswapRunning: let peerCtx = await b.taskQueue.pop() asyncSpawn b.taskHandler(peerCtx) trace "Exiting bitswap task runner" proc start*(b: Bitswap) {.async.} = ## Start the bitswap task ## trace "bitswap start" if b.bitswapTasks.len > 0: warn "Starting bitswap twice" return b.bitswapRunning = true for i in 0..