@@ -12,6 +12,7 @@ import std/sets
1212import std/ options
1313import std/ algorithm
1414import std/ sugar
15+ import std/ random
1516
1617import pkg/ chronos
1718import pkg/ libp2p/ [cid, switch, multihash, multicodec]
@@ -199,7 +200,6 @@ proc refreshBlockKnowledge(self: BlockExcEngine) {.async: (raises: [CancelledErr
199200
200201 # In dynamic swarms, staleness will dominate latency.
201202 if peer.lastRefresh < self.pendingBlocks.lastInclusion or peer.isKnowledgeStale:
202- trace " Refreshing block knowledge for peer" , peer = peer.id
203203 peer.refreshRequested ()
204204 # TODO : optimize this by keeping track of what was sent and sending deltas.
205205 # This should allow us to run much more frequent refreshes, and be way more
@@ -269,8 +269,9 @@ proc downloadInternal(
269269
270270 # We now wait for a bit and then retry. If the handle gets completed in the
271271 # meantime (cause the presence handler might have requested the block and
272- # received it in the meantime), we are done.
273- await handle or sleepAsync (self.pendingBlocks.retryInterval)
272+ # received it in the meantime), we are done. Retry delays are randomized
273+ # so we don't get all block loops spinning at the same time.
274+ await handle or sleepAsync (secs (rand (self.pendingBlocks.retryInterval.secs)))
274275 if handle.finished:
275276 break
276277 # If we still don't have the block, we'll go for another cycle.
@@ -484,6 +485,9 @@ proc cancelBlocks(
484485 # If so, schedules a cancellation.
485486 scheduledCancellations[peerCtx.id] = intersection
486487
488+ if scheduledCancellations.len == 0 :
489+ return
490+
487491 let (succeededFuts, failedFuts) = await allFinishedFailed [PeerId ](
488492 toSeq (scheduledCancellations.pairs).map (dispatchCancellations)
489493 )
0 commit comments