2022-05-19 22:28:53 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## 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 std/sets
|
2022-07-28 00:39:17 +00:00
|
|
|
import std/options
|
|
|
|
import std/algorithm
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/chronicles
|
2023-08-01 23:47:57 +00:00
|
|
|
import pkg/libp2p/[cid, switch]
|
2023-07-20 07:56:28 +00:00
|
|
|
import pkg/metrics
|
2022-11-15 15:46:21 +00:00
|
|
|
import pkg/stint
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
import ../../stores/blockstore
|
|
|
|
import ../../blocktype as bt
|
|
|
|
import ../../utils
|
|
|
|
|
|
|
|
import ../protobuf/blockexc
|
|
|
|
import ../protobuf/presence
|
|
|
|
|
|
|
|
import ../network
|
|
|
|
import ../peers
|
|
|
|
|
|
|
|
import ./payments
|
|
|
|
import ./discovery
|
|
|
|
import ./pendingblocks
|
|
|
|
|
|
|
|
export peers, pendingblocks, payments, discovery
|
|
|
|
|
|
|
|
logScope:
|
2022-11-15 15:46:21 +00:00
|
|
|
topics = "codex blockexcengine"
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
declareCounter(codexBlockExchangeWantHaveListsSent, "codex blockexchange wantHave lists sent")
|
|
|
|
declareCounter(codexBlockExchangeWantHaveListsReceived, "codex blockexchange wantHave lists received")
|
|
|
|
declareCounter(codexBlockExchangeWantBlockListsSent, "codex blockexchange wantBlock lists sent")
|
|
|
|
declareCounter(codexBlockExchangeWantBlockListsReceived, "codex blockexchange wantBlock lists received")
|
|
|
|
declareCounter(codexBlockExchangeBlocksSent, "codex blockexchange blocks sent")
|
|
|
|
declareCounter(codexBlockExchangeBlocksReceived, "codex blockexchange blocks received")
|
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
const
|
|
|
|
DefaultMaxPeersPerRequest* = 10
|
|
|
|
DefaultTaskQueueSize = 100
|
|
|
|
DefaultConcurrentTasks = 10
|
2023-03-09 11:23:45 +00:00
|
|
|
# DefaultMaxRetries = 3
|
|
|
|
# DefaultConcurrentDiscRequests = 10
|
|
|
|
# DefaultConcurrentAdvertRequests = 10
|
|
|
|
# DefaultDiscoveryTimeout = 1.minutes
|
|
|
|
# DefaultMaxQueriedBlocksCache = 1000
|
|
|
|
# DefaultMinPeersPerBlock = 3
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TaskHandler* = proc(task: BlockExcPeerCtx): Future[void] {.gcsafe.}
|
|
|
|
TaskScheduler* = proc(task: BlockExcPeerCtx): bool {.gcsafe.}
|
|
|
|
|
|
|
|
BlockExcEngine* = ref object of RootObj
|
|
|
|
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
|
|
|
|
|
|
|
|
Pricing* = object
|
|
|
|
address*: EthAddress
|
|
|
|
price*: UInt256
|
|
|
|
|
|
|
|
proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool =
|
|
|
|
## Convenience method to check for entry prepense
|
|
|
|
##
|
|
|
|
|
|
|
|
a.anyIt( it.cid == b )
|
|
|
|
|
|
|
|
# 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.}
|
|
|
|
|
|
|
|
proc start*(b: BlockExcEngine) {.async.} =
|
|
|
|
## Start the blockexc task
|
|
|
|
##
|
|
|
|
|
|
|
|
await b.discovery.start()
|
|
|
|
|
|
|
|
trace "Blockexc starting with concurrent tasks", tasks = b.concurrentTasks
|
|
|
|
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
|
|
|
|
##
|
|
|
|
|
|
|
|
await b.discovery.stop()
|
|
|
|
|
|
|
|
trace "NetworkStore stop"
|
|
|
|
if not b.blockexcRunning:
|
|
|
|
warn "Stopping blockexc without starting it"
|
|
|
|
return
|
|
|
|
|
|
|
|
b.blockexcRunning = false
|
2023-03-10 07:02:54 +00:00
|
|
|
for task in b.blockexcTasks:
|
|
|
|
if not task.finished:
|
2022-05-19 22:28:53 +00:00
|
|
|
trace "Awaiting task to stop"
|
2023-03-10 07:02:54 +00:00
|
|
|
await task.cancelAndWait()
|
2022-05-19 22:28:53 +00:00
|
|
|
trace "Task stopped"
|
|
|
|
|
|
|
|
trace "NetworkStore stopped"
|
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
proc sendWantHave(b: BlockExcEngine, cid: Cid, selectedPeer: BlockExcPeerCtx, peers: seq[BlockExcPeerCtx]): Future[void] {.async.} =
|
|
|
|
trace "Sending wantHave request to peers", cid
|
|
|
|
for p in peers:
|
|
|
|
if p != selectedPeer:
|
|
|
|
if cid notin p.peerHave:
|
|
|
|
trace " wantHave > ", peer = p.id
|
|
|
|
await b.network.request.sendWantList(
|
|
|
|
p.id,
|
|
|
|
@[cid],
|
|
|
|
wantType = WantType.WantHave) # we only want to know if the peer has the block
|
|
|
|
|
|
|
|
proc sendWantBlock(b: BlockExcEngine, cid: Cid, blockPeer: BlockExcPeerCtx): Future[void] {.async.} =
|
|
|
|
trace "Sending wantBlock request to", peer = blockPeer.id, cid
|
|
|
|
await b.network.request.sendWantList(
|
|
|
|
blockPeer.id,
|
|
|
|
@[cid],
|
|
|
|
wantType = WantType.WantBlock) # we want this remote to send us a block
|
|
|
|
|
|
|
|
proc findCheapestPeerForBlock(b: BlockExcEngine, cheapestPeers: seq[BlockExcPeerCtx]): ?BlockExcPeerCtx =
|
|
|
|
if cheapestPeers.len <= 0:
|
|
|
|
trace "No cheapest peers, selecting first in list"
|
|
|
|
let
|
|
|
|
peers = toSeq(b.peers) # Get any peer
|
|
|
|
if peers.len <= 0:
|
|
|
|
return none(BlockExcPeerCtx)
|
|
|
|
return some(peers[0])
|
|
|
|
return some(cheapestPeers[0]) # get cheapest
|
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
proc requestBlock*(
|
|
|
|
b: BlockExcEngine,
|
|
|
|
cid: Cid,
|
2022-07-29 16:19:34 +00:00
|
|
|
timeout = DefaultBlockTimeout): Future[bt.Block] {.async.} =
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Begin block request", cid, peers = b.peers.len
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
if b.pendingBlocks.isInFlight(cid):
|
|
|
|
trace "Request handle already pending", cid
|
2022-07-29 16:19:34 +00:00
|
|
|
return await b.pendingBlocks.getWantHandle(cid, timeout)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
blk = b.pendingBlocks.getWantHandle(cid, timeout)
|
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Selecting peers who have", cid
|
2022-05-19 22:28:53 +00:00
|
|
|
var
|
|
|
|
peers = b.peers.selectCheapest(cid)
|
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
without blockPeer =? b.findCheapestPeerForBlock(peers):
|
|
|
|
trace "No peers to request blocks from. Queue discovery...", cid
|
2022-05-19 22:28:53 +00:00
|
|
|
b.discovery.queueFindBlocksReq(@[cid])
|
2022-07-29 16:19:34 +00:00
|
|
|
return await blk
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
proc blockHandleMonitor() {.async.} =
|
|
|
|
try:
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Monitoring block handle", cid
|
2022-11-15 15:46:21 +00:00
|
|
|
b.pendingBlocks.setInFlight(cid, true)
|
|
|
|
discard await blk
|
|
|
|
trace "Block handle success", cid
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Error block handle, disconnecting peer", cid, exc = exc.msg
|
|
|
|
|
|
|
|
# TODO: really, this is just a quick and dirty way of
|
|
|
|
# preventing hitting the same "bad" peer every time, however,
|
|
|
|
# we might as well discover this on or next iteration, so
|
|
|
|
# it doesn't mean that we're never talking to this peer again.
|
|
|
|
# TODO: we need a lot more work around peer selection and
|
|
|
|
# prioritization
|
|
|
|
|
|
|
|
# drop unresponsive peer
|
|
|
|
await b.network.switch.disconnect(blockPeer.id)
|
|
|
|
|
|
|
|
# monitor block handle
|
|
|
|
asyncSpawn blockHandleMonitor()
|
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
await b.sendWantBlock(cid, blockPeer)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
codexBlockExchangeWantBlockListsSent.inc()
|
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
if (peers.len - 1) == 0:
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "No peers to send want list to", cid
|
2022-05-19 22:28:53 +00:00
|
|
|
b.discovery.queueFindBlocksReq(@[cid])
|
2023-07-18 05:50:47 +00:00
|
|
|
return await blk
|
|
|
|
|
|
|
|
await b.sendWantHave(cid, blockPeer, toSeq(b.peers))
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
codexBlockExchangeWantHaveListsSent.inc()
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
return await blk
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
proc blockPresenceHandler*(
|
|
|
|
b: BlockExcEngine,
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2022-05-19 22:28:53 +00:00
|
|
|
blocks: seq[BlockPresence]) {.async.} =
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Received presence update for peer", peer, blocks = blocks.len
|
|
|
|
let
|
|
|
|
peerCtx = b.peers.get(peer)
|
|
|
|
wantList = toSeq(b.pendingBlocks.wantList)
|
|
|
|
|
|
|
|
if peerCtx.isNil:
|
2022-05-19 22:28:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
for blk in blocks:
|
|
|
|
if presence =? Presence.init(blk):
|
2022-11-02 17:40:28 +00:00
|
|
|
logScope:
|
|
|
|
cid = presence.cid
|
|
|
|
have = presence.have
|
|
|
|
price = presence.price
|
|
|
|
|
|
|
|
trace "Updating precense"
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtx.setPresence(presence)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
peerHave = peerCtx.peerHave
|
|
|
|
dontWantCids = peerHave.filterIt(
|
|
|
|
it notin wantList
|
2022-05-19 22:28:53 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
if dontWantCids.len > 0:
|
|
|
|
peerCtx.cleanPresence(dontWantCids)
|
|
|
|
|
|
|
|
let
|
|
|
|
wantCids = wantList.filterIt(
|
|
|
|
it in peerHave
|
|
|
|
)
|
|
|
|
|
|
|
|
if wantCids.len > 0:
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Peer has blocks in our wantList", peer, count = wantCids.len
|
2022-11-15 15:46:21 +00:00
|
|
|
discard await allFinished(
|
2023-07-18 05:50:47 +00:00
|
|
|
wantCids.mapIt(b.sendWantBlock(it, peerCtx)))
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
# if none of the connected peers report our wants in their have list,
|
|
|
|
# fire up discovery
|
|
|
|
b.discovery.queueFindBlocksReq(
|
|
|
|
toSeq(b.pendingBlocks.wantList)
|
|
|
|
.filter do(cid: Cid) -> bool:
|
|
|
|
not b.peers.anyIt( cid in it.peerHave ))
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
proc scheduleTasks(b: BlockExcEngine, blocks: seq[bt.Block]) {.async.} =
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Schedule a task for new blocks", items = blocks.len
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cids = blocks.mapIt( it.cid )
|
|
|
|
|
|
|
|
# schedule any new peers to provide blocks to
|
|
|
|
for p in b.peers:
|
|
|
|
for c in cids: # for each cid
|
2022-07-28 00:39:17 +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:
|
|
|
|
if await (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
|
|
|
|
|
|
|
|
proc resolveBlocks*(b: BlockExcEngine, blocks: seq[bt.Block]) {.async.} =
|
2022-05-19 22:28:53 +00:00
|
|
|
trace "Resolving blocks", blocks = blocks.len
|
|
|
|
|
|
|
|
b.pendingBlocks.resolve(blocks)
|
2022-07-28 00:39:17 +00:00
|
|
|
await b.scheduleTasks(blocks)
|
2022-05-19 22:28:53 +00:00
|
|
|
b.discovery.queueProvideBlocksReq(blocks.mapIt( it.cid ))
|
|
|
|
|
|
|
|
proc payForBlocks(engine: BlockExcEngine,
|
|
|
|
peer: BlockExcPeerCtx,
|
2022-07-29 16:19:34 +00:00
|
|
|
blocks: seq[bt.Block]) {.async.} =
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Paying for blocks", blocks = blocks.len
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
sendPayment = engine.network.request.sendPayment
|
|
|
|
price = peer.price(blocks.mapIt(it.cid))
|
|
|
|
|
|
|
|
if payment =? engine.wallet.pay(peer, price):
|
|
|
|
trace "Sending payment for blocks", price
|
2022-07-29 16:19:34 +00:00
|
|
|
await sendPayment(peer.id, payment)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
proc blocksHandler*(
|
|
|
|
b: BlockExcEngine,
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2022-05-19 22:28:53 +00:00
|
|
|
blocks: seq[bt.Block]) {.async.} =
|
|
|
|
trace "Got blocks from peer", peer, len = blocks.len
|
|
|
|
for blk in blocks:
|
2022-07-28 00:39:17 +00:00
|
|
|
if isErr (await b.localStore.putBlock(blk)):
|
2022-05-19 22:28:53 +00:00
|
|
|
trace "Unable to store block", cid = blk.cid
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
await b.resolveBlocks(blocks)
|
2023-07-20 07:56:28 +00:00
|
|
|
codexBlockExchangeBlocksReceived.inc(blocks.len.int64)
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
peerCtx = b.peers.get(peer)
|
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
if peerCtx != nil:
|
2022-07-29 16:19:34 +00:00
|
|
|
await b.payForBlocks(peerCtx, blocks)
|
2023-07-18 05:50:47 +00:00
|
|
|
## shouldn't we remove them from the want-list instead of this:
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtx.cleanPresence(blocks.mapIt( it.cid ))
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
proc wantListHandler*(
|
|
|
|
b: BlockExcEngine,
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2023-03-10 07:02:54 +00:00
|
|
|
wantList: Wantlist) {.async.} =
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Got wantList for peer", peer, items = wantList.entries.len
|
|
|
|
let
|
|
|
|
peerCtx = b.peers.get(peer)
|
2022-05-19 22:28:53 +00:00
|
|
|
if isNil(peerCtx):
|
|
|
|
return
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
var
|
2023-07-18 05:50:47 +00:00
|
|
|
presence: seq[BlockPresence]
|
2022-11-15 15:46:21 +00:00
|
|
|
|
|
|
|
for e in wantList.entries:
|
|
|
|
let
|
|
|
|
idx = peerCtx.peerWants.find(e)
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
peer = peerCtx.id
|
|
|
|
cid = e.cid
|
|
|
|
wantType = $e.wantType
|
|
|
|
|
|
|
|
if idx < 0: # updating entry
|
|
|
|
trace "Processing new want list entry", cid = e.cid
|
|
|
|
|
|
|
|
let
|
|
|
|
have = await e.cid in b.localStore
|
|
|
|
price = @(
|
|
|
|
b.pricing.get(Pricing(price: 0.u256))
|
|
|
|
.price.toBytesBE)
|
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
if e.wantType == WantType.WantHave:
|
|
|
|
codexBlockExchangeWantHaveListsReceived.inc()
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
if not have and e.sendDontHave:
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Adding dont have entry to presence response", cid = e.cid
|
|
|
|
presence.add(
|
2022-11-15 15:46:21 +00:00
|
|
|
BlockPresence(
|
|
|
|
cid: e.cid.data.buffer,
|
|
|
|
`type`: BlockPresenceType.DontHave,
|
|
|
|
price: price))
|
|
|
|
elif have and e.wantType == WantType.WantHave:
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Adding have entry to presence response", cid = e.cid
|
|
|
|
presence.add(
|
2022-11-15 15:46:21 +00:00
|
|
|
BlockPresence(
|
|
|
|
cid: e.cid.data.buffer,
|
|
|
|
`type`: BlockPresenceType.Have,
|
|
|
|
price: price))
|
|
|
|
elif e.wantType == WantType.WantBlock:
|
|
|
|
trace "Added entry to peer's want blocks list", cid = e.cid
|
|
|
|
peerCtx.peerWants.add(e)
|
2023-07-20 07:56:28 +00:00
|
|
|
codexBlockExchangeWantBlockListsReceived.inc()
|
2022-11-15 15:46:21 +00:00
|
|
|
else:
|
2022-05-19 22:28:53 +00:00
|
|
|
# peer doesn't want this block anymore
|
|
|
|
if e.cancel:
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Removing entry from peer want list"
|
2022-05-19 22:28:53 +00:00
|
|
|
peerCtx.peerWants.del(idx)
|
2022-11-15 15:46:21 +00:00
|
|
|
else:
|
|
|
|
trace "Updating entry in peer want list"
|
|
|
|
# peer might want to ask for the same cid with
|
|
|
|
# different want params
|
|
|
|
peerCtx.peerWants[idx] = e # update entry
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
if presence.len > 0:
|
|
|
|
trace "Sending presence to remote", items = presence.len
|
|
|
|
await b.network.request.sendPresence(peer, presence)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "Scheduling a task for this peer, to look over their want-list", peer
|
2022-05-19 22:28:53 +00:00
|
|
|
if not b.scheduleTask(peerCtx):
|
|
|
|
trace "Unable to schedule task for peer", peer
|
|
|
|
|
|
|
|
proc accountHandler*(
|
|
|
|
engine: BlockExcEngine,
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2022-05-19 22:28:53 +00:00
|
|
|
account: Account) {.async.} =
|
2023-07-18 05:50:47 +00:00
|
|
|
let
|
|
|
|
context = engine.peers.get(peer)
|
2022-05-19 22:28:53 +00:00
|
|
|
if context.isNil:
|
|
|
|
return
|
|
|
|
|
|
|
|
context.account = account.some
|
|
|
|
|
|
|
|
proc paymentHandler*(
|
|
|
|
engine: BlockExcEngine,
|
|
|
|
peer: PeerId,
|
|
|
|
payment: SignedState) {.async.} =
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Handling payments", peer
|
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
without context =? engine.peers.get(peer).option and
|
|
|
|
account =? context.account:
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "No context or account for peer", peer
|
2022-05-19 22:28:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if channel =? context.paymentChannel:
|
2023-07-18 05:50:47 +00:00
|
|
|
let
|
|
|
|
sender = account.address
|
2022-05-19 22:28:53 +00:00
|
|
|
discard engine.wallet.acceptPayment(channel, Asset, sender, payment)
|
|
|
|
else:
|
|
|
|
context.paymentChannel = engine.wallet.acceptChannel(payment).option
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
proc setupPeer*(b: BlockExcEngine, peer: PeerId) {.async.} =
|
2022-05-19 22:28:53 +00:00
|
|
|
## Perform initial setup, such as want
|
|
|
|
## list exchange
|
|
|
|
##
|
|
|
|
|
|
|
|
if peer notin b.peers:
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Setting up new peer", peer
|
2022-05-19 22:28:53 +00:00
|
|
|
b.peers.add(BlockExcPeerCtx(
|
|
|
|
id: peer
|
|
|
|
))
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Added peer", peers = b.peers.len
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
# broadcast our want list, the other peer will do the same
|
|
|
|
if b.pendingBlocks.len > 0:
|
2022-07-29 16:19:34 +00:00
|
|
|
await b.network.request.sendWantList(
|
|
|
|
peer, toSeq(b.pendingBlocks.wantList), full = true)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
if address =? b.pricing.?address:
|
2022-07-29 16:19:34 +00:00
|
|
|
await b.network.request.sendAccount(peer, Account(address: address))
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
proc dropPeer*(b: BlockExcEngine, peer: PeerId) =
|
2022-05-19 22:28:53 +00:00
|
|
|
## Cleanup disconnected peer
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Dropping peer", peer
|
|
|
|
|
|
|
|
# drop the peer from the peers table
|
|
|
|
b.peers.remove(peer)
|
|
|
|
|
|
|
|
proc taskHandler*(b: BlockExcEngine, task: BlockExcPeerCtx) {.gcsafe, async.} =
|
|
|
|
trace "Handling task for peer", peer = task.id
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
# Send to the peer blocks he wants to get,
|
2022-07-28 00:39:17 +00:00
|
|
|
# if they present in our local store
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
# TODO: There should be all sorts of accounting of
|
|
|
|
# bytes sent/received here
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
var
|
|
|
|
wantsBlocks = task.peerWants.filterIt(
|
|
|
|
it.wantType == WantType.WantBlock
|
|
|
|
)
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2023-07-18 05:50:47 +00:00
|
|
|
trace "wantsBlocks", peer = task.id, n = wantsBlocks.len
|
2022-05-19 22:28:53 +00:00
|
|
|
if wantsBlocks.len > 0:
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Got peer want blocks list", items = wantsBlocks.len
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
wantsBlocks.sort(SortOrder.Descending)
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
blockFuts = await allFinished(wantsBlocks.mapIt(
|
2022-05-19 22:28:53 +00:00
|
|
|
b.localStore.getBlock(it.cid)
|
2022-11-15 15:46:21 +00:00
|
|
|
))
|
2022-05-19 22:28:53 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
# Extract successfully received blocks
|
|
|
|
let
|
|
|
|
blocks = blockFuts
|
|
|
|
.filterIt(it.completed and it.read.isOk)
|
|
|
|
.mapIt(it.read.get)
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
if blocks.len > 0:
|
|
|
|
trace "Sending blocks to peer", peer = task.id, blocks = blocks.len
|
2022-07-29 16:19:34 +00:00
|
|
|
await b.network.request.sendBlocks(
|
2022-05-19 22:28:53 +00:00
|
|
|
task.id,
|
|
|
|
blocks)
|
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
codexBlockExchangeBlocksSent.inc(blocks.len.int64)
|
|
|
|
|
|
|
|
trace "About to remove entries from peerWants", blocks = blocks.len, items = task.peerWants.len
|
2022-07-28 00:39:17 +00:00
|
|
|
# Remove successfully sent blocks
|
|
|
|
task.peerWants.keepIf(
|
|
|
|
proc(e: Entry): bool =
|
|
|
|
not blocks.anyIt( it.cid == e.cid )
|
|
|
|
)
|
2022-11-15 15:46:21 +00:00
|
|
|
trace "Removed entries from peerWants", items = task.peerWants.len
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
proc blockexcTaskRunner(b: BlockExcEngine) {.async.} =
|
|
|
|
## process tasks
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Starting blockexc task runner"
|
|
|
|
while b.blockexcRunning:
|
|
|
|
let
|
|
|
|
peerCtx = await b.taskQueue.pop()
|
|
|
|
|
|
|
|
trace "Got new task from queue", peerId = peerCtx.id
|
|
|
|
await b.taskHandler(peerCtx)
|
|
|
|
|
|
|
|
trace "Exiting blockexc task runner"
|
|
|
|
|
|
|
|
proc new*(
|
2023-06-22 15:11:18 +00:00
|
|
|
T: type BlockExcEngine,
|
|
|
|
localStore: BlockStore,
|
|
|
|
wallet: WalletRef,
|
|
|
|
network: BlockExcNetwork,
|
|
|
|
discovery: DiscoveryEngine,
|
|
|
|
peerStore: PeerCtxStore,
|
|
|
|
pendingBlocks: PendingBlocksManager,
|
|
|
|
concurrentTasks = DefaultConcurrentTasks,
|
|
|
|
peersPerRequest = DefaultMaxPeersPerRequest
|
|
|
|
): BlockExcEngine =
|
|
|
|
## Create new block exchange engine instance
|
2023-07-20 07:56:28 +00:00
|
|
|
##
|
2022-05-19 22:28:53 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
engine = BlockExcEngine(
|
|
|
|
localStore: localStore,
|
|
|
|
peers: peerStore,
|
|
|
|
pendingBlocks: pendingBlocks,
|
|
|
|
peersPerRequest: peersPerRequest,
|
|
|
|
network: network,
|
|
|
|
wallet: wallet,
|
|
|
|
concurrentTasks: concurrentTasks,
|
|
|
|
taskQueue: newAsyncHeapQueue[BlockExcPeerCtx](DefaultTaskQueueSize),
|
|
|
|
discovery: discovery)
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
proc peerEventHandler(peerId: PeerId, event: PeerEvent) {.async.} =
|
2022-05-19 22:28:53 +00:00
|
|
|
if event.kind == PeerEventKind.Joined:
|
2022-07-29 16:19:34 +00:00
|
|
|
await engine.setupPeer(peerId)
|
2022-05-19 22:28:53 +00:00
|
|
|
else:
|
|
|
|
engine.dropPeer(peerId)
|
|
|
|
|
|
|
|
if not isNil(network.switch):
|
|
|
|
network.switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Joined)
|
|
|
|
network.switch.addPeerEventHandler(peerEventHandler, PeerEventKind.Left)
|
|
|
|
|
|
|
|
proc blockWantListHandler(
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2023-03-10 07:02:54 +00:00
|
|
|
wantList: Wantlist): Future[void] {.gcsafe.} =
|
2022-05-19 22:28:53 +00:00
|
|
|
engine.wantListHandler(peer, wantList)
|
|
|
|
|
|
|
|
proc blockPresenceHandler(
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2022-05-19 22:28:53 +00:00
|
|
|
presence: seq[BlockPresence]): Future[void] {.gcsafe.} =
|
|
|
|
engine.blockPresenceHandler(peer, presence)
|
|
|
|
|
|
|
|
proc blocksHandler(
|
2022-11-15 15:46:21 +00:00
|
|
|
peer: PeerId,
|
2022-05-19 22:28:53 +00:00
|
|
|
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,
|
|
|
|
onPayment: paymentHandler)
|
|
|
|
|
|
|
|
return engine
|