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.
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/sets
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/libp2p
|
|
|
|
|
|
|
|
import ../stores/blockstore
|
2021-08-30 19:25:20 +00:00
|
|
|
import ../blocktype as bt
|
2022-05-12 21:52:03 +00:00
|
|
|
import ../utils
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
import ./protobuf/blockexc
|
|
|
|
import ./protobuf/presence
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
import ./network
|
2022-05-19 02:29:15 +00:00
|
|
|
import ./peers
|
2021-04-14 14:19:45 +00:00
|
|
|
import ./engine/payments
|
2022-05-19 02:29:15 +00:00
|
|
|
import ./engine/discovery
|
|
|
|
import ./pendingblocks
|
2021-04-12 09:43:54 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
export peers, pendingblocks, payments, discovery
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
logScope:
|
2021-08-30 19:25:20 +00:00
|
|
|
topics = "dagger blockexc engine"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
DefaultMaxPeersPerRequest* = 10
|
2022-01-10 15:32:56 +00:00
|
|
|
DefaultTaskQueueSize = 100
|
|
|
|
DefaultConcurrentTasks = 10
|
|
|
|
DefaultMaxRetries = 3
|
2022-05-12 21:52:03 +00:00
|
|
|
DefaultConcurrentDiscRequests = 10
|
|
|
|
DefaultConcurrentAdvertRequests = 10
|
|
|
|
DefaultDiscoveryTimeout = 1.minutes
|
|
|
|
DefaultMaxQueriedBlocksCache = 1000
|
|
|
|
DefaultMinPeersPerBlock = 3
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
type
|
2021-08-30 19:25:20 +00:00
|
|
|
TaskHandler* = proc(task: BlockExcPeerCtx): Future[void] {.gcsafe.}
|
|
|
|
TaskScheduler* = proc(task: BlockExcPeerCtx): bool {.gcsafe.}
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
BlockExcEngine* = ref object of RootObj
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore*: BlockStore # Local block store for this instance
|
|
|
|
network*: BlockExcNetwork # Petwork interface
|
|
|
|
peers*: PeerCtxStore # Peers we're currently actively exchanging with
|
|
|
|
taskQueue*: AsyncHeapQueue[BlockExcPeerCtx] # Peers we're currently processing tasks for
|
|
|
|
concurrentTasks: int # Number of concurrent peers we're serving at any given time
|
|
|
|
blockexcTasks: seq[Future[void]] # Future to control blockexc task
|
|
|
|
blockexcRunning: bool # Indicates if the blockexc task is running
|
|
|
|
pendingBlocks*: PendingBlocksManager # Blocks we're awaiting to be resolved
|
|
|
|
peersPerRequest: int # Max number of peers to request from
|
|
|
|
wallet*: WalletRef # Nitro wallet for micropayments
|
|
|
|
pricing*: ?Pricing # Optional bandwidth pricing
|
|
|
|
discovery*: DiscoveryEngine
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
Pricing* = object
|
|
|
|
address*: EthAddress
|
|
|
|
price*: UInt256
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool =
|
|
|
|
## Convenience method to check for entry prepense
|
|
|
|
##
|
|
|
|
|
|
|
|
a.anyIt( it.cid == b )
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
# attach task scheduler to engine
|
|
|
|
proc scheduleTask(b: BlockExcEngine, task: BlockExcPeerCtx): bool {.gcsafe} =
|
|
|
|
b.taskQueue.pushOrUpdateNoWait(task).isOk()
|
|
|
|
|
|
|
|
proc blockexcTaskRunner(b: BlockExcEngine): Future[void] {.gcsafe.}
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc start*(b: BlockExcEngine) {.async.} =
|
|
|
|
## Start the blockexc task
|
|
|
|
##
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
await b.discovery.start()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
trace "Blockexc starting with concurrent tasks", tasks = b.concurrentTasks
|
2022-01-10 15:32:56 +00:00
|
|
|
if b.blockexcRunning:
|
|
|
|
warn "Starting blockexc twice"
|
|
|
|
return
|
|
|
|
|
|
|
|
b.blockexcRunning = true
|
|
|
|
for i in 0..<b.concurrentTasks:
|
|
|
|
b.blockexcTasks.add(blockexcTaskRunner(b))
|
|
|
|
|
|
|
|
proc stop*(b: BlockExcEngine) {.async.} =
|
|
|
|
## Stop the blockexc blockexc
|
|
|
|
##
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
await b.discovery.stop()
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "NetworkStore stop"
|
|
|
|
if not b.blockexcRunning:
|
|
|
|
warn "Stopping blockexc without starting it"
|
|
|
|
return
|
|
|
|
|
|
|
|
b.blockexcRunning = false
|
|
|
|
for t in b.blockexcTasks:
|
|
|
|
if not t.finished:
|
|
|
|
trace "Awaiting task to stop"
|
|
|
|
await t.cancelAndWait()
|
|
|
|
trace "Task stopped"
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "NetworkStore stopped"
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc requestBlock*(
|
2021-08-30 19:25:20 +00:00
|
|
|
b: BlockExcEngine,
|
2022-01-10 15:32:56 +00:00
|
|
|
cid: Cid,
|
2022-05-12 21:52:03 +00:00
|
|
|
timeout = DefaultBlockTimeout): Future[bt.Block] =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Request a block from remotes
|
|
|
|
##
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Requesting block", cid = $cid
|
2022-04-20 12:28:11 +00:00
|
|
|
|
|
|
|
if cid in b.pendingBlocks:
|
2022-05-12 21:52:03 +00:00
|
|
|
return b.pendingBlocks.getWantHandle(cid, timeout)
|
2022-04-20 12:28:11 +00:00
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
let
|
2022-05-12 21:52:03 +00:00
|
|
|
blk = b.pendingBlocks.getWantHandle(cid, timeout)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
var
|
|
|
|
peers = b.peers.selectCheapest(cid)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
if peers.len <= 0:
|
|
|
|
peers = toSeq(b.peers) # Get any peer
|
|
|
|
if peers.len <= 0:
|
|
|
|
trace "No peers to request blocks from", cid = $cid
|
|
|
|
b.discovery.queueFindBlocksReq(@[cid])
|
|
|
|
return blk
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
let
|
|
|
|
blockPeer = peers[0] # get cheapest
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
# request block
|
2022-01-10 15:32:56 +00:00
|
|
|
b.network.request.sendWantList(
|
2022-05-12 21:52:03 +00:00
|
|
|
blockPeer.id,
|
2022-01-10 15:32:56 +00:00
|
|
|
@[cid],
|
2021-02-26 00:23:22 +00:00
|
|
|
wantType = WantType.wantBlock) # we want this remote to send us a block
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
if (peers.len - 1) == 0:
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Not enough peers to send want list to", cid = $cid
|
2022-05-19 02:29:15 +00:00
|
|
|
b.discovery.queueFindBlocksReq(@[cid])
|
2022-05-12 21:52:03 +00:00
|
|
|
return blk # no peers to send wants to
|
|
|
|
|
|
|
|
# filter out the peer we've already requested from
|
|
|
|
let stop = min(peers.high, b.peersPerRequest)
|
|
|
|
trace "Sending want list requests to remaining peers", count = stop + 1
|
2022-05-19 02:29:15 +00:00
|
|
|
for p in peers[1..stop]:
|
2022-05-12 21:52:03 +00:00
|
|
|
if cid notin p.peerHave:
|
|
|
|
# just send wants
|
|
|
|
b.network.request.sendWantList(
|
|
|
|
p.id,
|
|
|
|
@[cid],
|
|
|
|
wantType = WantType.wantHave) # we only want to know if the peer has the block
|
|
|
|
|
|
|
|
return blk
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
proc blockPresenceHandler*(
|
2021-08-30 19:25:20 +00:00
|
|
|
b: BlockExcEngine,
|
2021-02-26 00:23:22 +00:00
|
|
|
peer: PeerID,
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[BlockPresence]) {.async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Handle block presence
|
|
|
|
##
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Received presence update for peer", peer
|
2022-05-19 02:29:15 +00:00
|
|
|
let peerCtx = b.peers.get(peer)
|
2022-05-12 21:52:03 +00:00
|
|
|
if isNil(peerCtx):
|
|
|
|
return
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-04-26 15:11:11 +00:00
|
|
|
for blk in blocks:
|
|
|
|
if presence =? Presence.init(blk):
|
2022-05-12 21:52:03 +00:00
|
|
|
peerCtx.updatePresence(presence)
|
|
|
|
|
|
|
|
var
|
|
|
|
cids = toSeq(b.pendingBlocks.wantList).filterIt(
|
|
|
|
it in peerCtx.peerHave
|
|
|
|
)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
trace "Received presence update for cids", peer, count = cids.len
|
2022-05-12 21:52:03 +00:00
|
|
|
if cids.len > 0:
|
|
|
|
b.network.request.sendWantList(
|
|
|
|
peer,
|
|
|
|
cids,
|
|
|
|
wantType = WantType.wantBlock) # we want this remote to send us a block
|
|
|
|
|
|
|
|
# if none of the connected peers report our wants in their have list,
|
|
|
|
# fire up discovery
|
2022-05-19 02:29:15 +00:00
|
|
|
b.discovery.queueFindBlocksReq(
|
|
|
|
toSeq(b.pendingBlocks.wantList)
|
|
|
|
.filter do(cid: Cid) -> bool:
|
|
|
|
not b.peers.anyIt( cid in it.peerHave ))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc scheduleTasks(b: BlockExcEngine, blocks: seq[bt.Block]) =
|
2021-02-26 00:23:22 +00:00
|
|
|
trace "Schedule a task for new blocks"
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
let
|
|
|
|
cids = blocks.mapIt( it.cid )
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
# schedule any new peers to provide blocks to
|
|
|
|
for p in b.peers:
|
|
|
|
for c in cids: # for each cid
|
2022-05-19 02:29:15 +00:00
|
|
|
# schedule a peer if it wants at least one
|
|
|
|
# cid and we have it in our local store
|
|
|
|
if c in p.peerWants and c in b.localStore:
|
|
|
|
if b.scheduleTask(p):
|
|
|
|
trace "Task scheduled for peer", peer = p.id
|
|
|
|
else:
|
|
|
|
trace "Unable to schedule task for peer", peer = p.id
|
|
|
|
|
|
|
|
break # do next peer
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc resolveBlocks*(b: BlockExcEngine, blocks: seq[bt.Block]) =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Resolve pending blocks from the pending blocks manager
|
|
|
|
## and schedule any new task to be ran
|
|
|
|
##
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Resolving blocks", blocks = blocks.len
|
2022-04-20 12:28:11 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
b.pendingBlocks.resolve(blocks)
|
|
|
|
b.scheduleTasks(blocks)
|
2022-05-19 02:29:15 +00:00
|
|
|
b.discovery.queueProvideBlocksReq(blocks.mapIt( it.cid ))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc payForBlocks(engine: BlockExcEngine,
|
|
|
|
peer: BlockExcPeerCtx,
|
2021-04-14 14:19:45 +00:00
|
|
|
blocks: seq[bt.Block]) =
|
2022-01-10 15:32:56 +00:00
|
|
|
let sendPayment = engine.network.request.sendPayment
|
2021-04-14 14:19:45 +00:00
|
|
|
if sendPayment.isNil:
|
|
|
|
return
|
|
|
|
|
2021-05-10 11:47:15 +00:00
|
|
|
let cids = blocks.mapIt(it.cid)
|
|
|
|
if payment =? engine.wallet.pay(peer, peer.price(cids)):
|
2021-04-14 14:19:45 +00:00
|
|
|
sendPayment(peer.id, payment)
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
proc blocksHandler*(
|
2021-08-30 19:25:20 +00:00
|
|
|
b: BlockExcEngine,
|
2021-02-26 00:23:22 +00:00
|
|
|
peer: PeerID,
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[bt.Block]) {.async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## handle incoming blocks
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Got blocks from peer", peer, len = blocks.len
|
2022-01-10 15:32:56 +00:00
|
|
|
for blk in blocks:
|
|
|
|
if not (await b.localStore.putBlock(blk)):
|
|
|
|
trace "Unable to store block", cid = blk.cid
|
|
|
|
continue
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
b.resolveBlocks(blocks)
|
2022-05-19 02:29:15 +00:00
|
|
|
let peerCtx = b.peers.get(peer)
|
2021-04-14 14:19:45 +00:00
|
|
|
if peerCtx != nil:
|
|
|
|
b.payForBlocks(peerCtx, blocks)
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
proc wantListHandler*(
|
2021-08-30 19:25:20 +00:00
|
|
|
b: BlockExcEngine,
|
2021-02-26 00:23:22 +00:00
|
|
|
peer: PeerID,
|
2022-01-10 15:32:56 +00:00
|
|
|
wantList: WantList) {.async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Handle incoming want lists
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Got want list for peer", peer
|
2022-05-19 02:29:15 +00:00
|
|
|
let peerCtx = b.peers.get(peer)
|
2021-02-26 00:23:22 +00:00
|
|
|
if isNil(peerCtx):
|
|
|
|
return
|
|
|
|
|
|
|
|
var dontHaves: seq[Cid]
|
|
|
|
let entries = wantList.entries
|
|
|
|
for e in entries:
|
|
|
|
let idx = peerCtx.peerWants.find(e)
|
|
|
|
if idx > -1:
|
|
|
|
# peer doesn't want this block anymore
|
|
|
|
if e.cancel:
|
|
|
|
peerCtx.peerWants.del(idx)
|
|
|
|
continue
|
|
|
|
|
|
|
|
peerCtx.peerWants[idx] = e # update entry
|
|
|
|
else:
|
|
|
|
peerCtx.peerWants.add(e)
|
|
|
|
|
|
|
|
trace "Added entry to peer's want list", peer = peerCtx.id, cid = $e.cid
|
|
|
|
|
|
|
|
# peer might want to ask for the same cid with
|
|
|
|
# different want params
|
2022-01-10 15:32:56 +00:00
|
|
|
if e.sendDontHave and e.cid notin b.localStore:
|
2021-02-26 00:23:22 +00:00
|
|
|
dontHaves.add(e.cid)
|
|
|
|
|
|
|
|
# send don't have's to remote
|
|
|
|
if dontHaves.len > 0:
|
2022-01-10 15:32:56 +00:00
|
|
|
b.network.request.sendPresence(
|
2021-02-26 00:23:22 +00:00
|
|
|
peer,
|
|
|
|
dontHaves.mapIt(
|
|
|
|
BlockPresence(
|
|
|
|
cid: it.data.buffer,
|
|
|
|
`type`: BlockPresenceType.presenceDontHave)))
|
|
|
|
|
|
|
|
if not b.scheduleTask(peerCtx):
|
|
|
|
trace "Unable to schedule task for peer", peer
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
proc accountHandler*(
|
|
|
|
engine: BlockExcEngine,
|
|
|
|
peer: PeerID,
|
|
|
|
account: Account) {.async.} =
|
2022-05-19 02:29:15 +00:00
|
|
|
let context = engine.peers.get(peer)
|
2021-04-08 12:27:49 +00:00
|
|
|
if context.isNil:
|
|
|
|
return
|
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
context.account = account.some
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
proc paymentHandler*(
|
|
|
|
engine: BlockExcEngine,
|
|
|
|
peer: PeerId,
|
|
|
|
payment: SignedState) {.async.} =
|
2022-05-19 02:29:15 +00:00
|
|
|
without context =? engine.peers.get(peer).option and
|
2021-05-10 14:21:47 +00:00
|
|
|
account =? context.account:
|
2021-04-19 14:47:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if channel =? context.paymentChannel:
|
2021-05-10 14:21:47 +00:00
|
|
|
let sender = account.address
|
2021-04-22 08:11:24 +00:00
|
|
|
discard engine.wallet.acceptPayment(channel, Asset, sender, payment)
|
2021-04-19 14:47:47 +00:00
|
|
|
else:
|
|
|
|
context.paymentChannel = engine.wallet.acceptChannel(payment).option
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc setupPeer*(b: BlockExcEngine, peer: PeerID) =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Perform initial setup, such as want
|
|
|
|
## list exchange
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Setting up new peer", peer
|
|
|
|
if peer notin b.peers:
|
2021-08-30 19:25:20 +00:00
|
|
|
b.peers.add(BlockExcPeerCtx(
|
2021-02-26 00:23:22 +00:00
|
|
|
id: peer
|
|
|
|
))
|
|
|
|
|
|
|
|
# broadcast our want list, the other peer will do the same
|
2022-05-12 21:52:03 +00:00
|
|
|
if b.pendingBlocks.len > 0:
|
|
|
|
b.network.request.sendWantList(peer, toSeq(b.pendingBlocks.wantList), full = true)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
if address =? b.pricing.?address:
|
2022-01-10 15:32:56 +00:00
|
|
|
b.network.request.sendAccount(peer, Account(address: address))
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc dropPeer*(b: BlockExcEngine, peer: PeerID) =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Cleanup disconnected peer
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Dropping peer", peer
|
|
|
|
|
|
|
|
# drop the peer from the peers table
|
2022-05-19 02:29:15 +00:00
|
|
|
b.peers.remove(peer)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc taskHandler*(b: BlockExcEngine, task: BlockExcPeerCtx) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
trace "Handling task for peer", peer = task.id
|
|
|
|
|
|
|
|
var wantsBlocks = newAsyncHeapQueue[Entry](queueType = QueueType.Max)
|
|
|
|
# get blocks and wants to send to the remote
|
|
|
|
for e in task.peerWants:
|
|
|
|
if e.wantType == WantType.wantBlock:
|
|
|
|
await wantsBlocks.push(e)
|
|
|
|
|
|
|
|
# TODO: There should be all sorts of accounting of
|
|
|
|
# bytes sent/received here
|
|
|
|
if wantsBlocks.len > 0:
|
2022-01-10 15:32:56 +00:00
|
|
|
let blockFuts = await allFinished(wantsBlocks.mapIt(
|
|
|
|
b.localStore.getBlock(it.cid)
|
2021-02-26 00:23:22 +00:00
|
|
|
))
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let blocks = blockFuts
|
|
|
|
.filterIt((not it.failed) and it.read.isOk)
|
|
|
|
.mapIt(!it.read)
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
if blocks.len > 0:
|
2022-05-12 21:52:03 +00:00
|
|
|
trace "Sending blocks to peer", peer = task.id, blocks = blocks.len
|
2022-01-10 15:32:56 +00:00
|
|
|
b.network.request.sendBlocks(
|
|
|
|
task.id,
|
|
|
|
blocks)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
# Remove successfully sent blocks
|
|
|
|
task.peerWants.keepIf(
|
|
|
|
proc(e: Entry): bool =
|
|
|
|
not blocks.anyIt( it.cid == e.cid )
|
|
|
|
)
|
|
|
|
|
|
|
|
var wants: seq[BlockPresence]
|
|
|
|
# do not remove wants from the queue unless
|
|
|
|
# we send the block or get a cancel
|
|
|
|
for e in task.peerWants:
|
|
|
|
if e.wantType == WantType.wantHave:
|
2021-05-10 10:47:45 +00:00
|
|
|
var presence = Presence(cid: e.cid)
|
|
|
|
presence.have = b.localStore.hasblock(presence.cid)
|
|
|
|
if presence.have and price =? b.pricing.?price:
|
|
|
|
presence.price = price
|
|
|
|
wants.add(BlockPresence.init(presence))
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
if wants.len > 0:
|
2022-01-10 15:32:56 +00:00
|
|
|
b.network.request.sendPresence(task.id, wants)
|
|
|
|
|
|
|
|
proc blockexcTaskRunner(b: BlockExcEngine) {.async.} =
|
|
|
|
## process tasks
|
|
|
|
##
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
trace "Starting blockexc task runner"
|
2022-01-10 15:32:56 +00:00
|
|
|
while b.blockexcRunning:
|
2022-05-19 02:29:15 +00:00
|
|
|
let
|
|
|
|
peerCtx = await b.taskQueue.pop()
|
|
|
|
|
|
|
|
trace "Got new task from queue", peerId = peerCtx.id
|
|
|
|
await b.taskHandler(peerCtx)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Exiting blockexc task runner"
|
|
|
|
|
|
|
|
proc new*(
|
2021-08-30 19:25:20 +00:00
|
|
|
T: type BlockExcEngine,
|
2021-02-26 00:23:22 +00:00
|
|
|
localStore: BlockStore,
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet: WalletRef,
|
2022-01-10 15:32:56 +00:00
|
|
|
network: BlockExcNetwork,
|
2022-05-19 02:29:15 +00:00
|
|
|
discovery: DiscoveryEngine,
|
|
|
|
peerStore: PeerCtxStore,
|
|
|
|
pendingBlocks: PendingBlocksManager,
|
2022-01-10 15:32:56 +00:00
|
|
|
concurrentTasks = DefaultConcurrentTasks,
|
2022-05-19 02:29:15 +00:00
|
|
|
peersPerRequest = DefaultMaxPeersPerRequest): T =
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
engine = BlockExcEngine(
|
|
|
|
localStore: localStore,
|
2022-05-19 02:29:15 +00:00
|
|
|
peers: peerStore,
|
|
|
|
pendingBlocks: pendingBlocks,
|
2022-05-12 21:52:03 +00:00
|
|
|
peersPerRequest: peersPerRequest,
|
|
|
|
network: network,
|
|
|
|
wallet: wallet,
|
|
|
|
concurrentTasks: concurrentTasks,
|
|
|
|
taskQueue: newAsyncHeapQueue[BlockExcPeerCtx](DefaultTaskQueueSize),
|
2022-05-19 02:29:15 +00:00
|
|
|
discovery: discovery)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc peerEventHandler(peerId: PeerID, event: PeerEvent) {.async.} =
|
|
|
|
if event.kind == PeerEventKind.Joined:
|
|
|
|
engine.setupPeer(peerId)
|
|
|
|
else:
|
|
|
|
engine.dropPeer(peerId)
|
|
|
|
|
|
|
|
if not isNil(network.switch):
|
|
|
|
network.switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Joined)
|
|
|
|
network.switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Left)
|
|
|
|
|
|
|
|
proc blockWantListHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
wantList: WantList): Future[void] {.gcsafe.} =
|
|
|
|
engine.wantListHandler(peer, wantList)
|
|
|
|
|
|
|
|
proc blockPresenceHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
presence: seq[BlockPresence]): Future[void] {.gcsafe.} =
|
|
|
|
engine.blockPresenceHandler(peer, presence)
|
|
|
|
|
|
|
|
proc blocksHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
blocks: seq[bt.Block]): Future[void] {.gcsafe.} =
|
|
|
|
engine.blocksHandler(peer, blocks)
|
|
|
|
|
|
|
|
proc accountHandler(peer: PeerId, account: Account): Future[void] {.gcsafe.} =
|
|
|
|
engine.accountHandler(peer, account)
|
|
|
|
|
|
|
|
proc paymentHandler(peer: PeerId, payment: SignedState): Future[void] {.gcsafe.} =
|
|
|
|
engine.paymentHandler(peer, payment)
|
|
|
|
|
|
|
|
network.handlers = BlockExcHandlers(
|
|
|
|
onWantList: blockWantListHandler,
|
|
|
|
onBlocks: blocksHandler,
|
|
|
|
onPresence: blockPresenceHandler,
|
|
|
|
onAccount: accountHandler,
|
2022-05-12 21:52:03 +00:00
|
|
|
onPayment: paymentHandler)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
return engine
|