pickPieces

fun pickPieces(peerBitfield: Bitfield, numBlocksWanted: Int, options: Int = OPTION_RAREST_FIRST, random: Random? = null, numPeers: Int = 0, requesterPeer: Any? = null): List<PieceBlock>

Pick up to numBlocksWanted interesting blocks for a peer whose available pieces are described by peerBitfield. This is a port of pick_pieces, including its option modes and the end-game busy-block tail.

"Interesting" means: the peer has the piece, we don't have it, and it isn't filtered (priority 0). Free blocks (BlockState.NONE) are returned first; only when those cannot satisfy the request does the end-game tail append one busy block (a block already requested from another peer). Callers detect it via numPeers(block) > 0 and apply their own end-game gates, exactly like request_a_block does upstream.

Option modes (combinable, same bit values as piece_picker):

  • OPTION_RAREST_FIRST: ascending effectivePriority (rarer + higher manual priority first; partial pieces a notch ahead). The default.

  • OPTION_REVERSE: walk the rarest-first order backwards (most common last-bucket pieces first). Used for snubbed peers, so they converge on the same common pieces instead of keeping rare pieces unavailable.

  • OPTION_SEQUENTIAL: top-priority (7) pieces first, then ascending piece index (OPTION_REVERSE: descending).

  • OPTION_PRIORITIZE_PARTIALS: already-downloading pieces are picked before anything else (sorted rarest-first when that flag is also set). Forced automatically when partial pieces sprawl: more than numPeers * 3/2 of them, or more than MAX_PARTIAL_BLOCKS blocks' worth (pick_pieces's anti-sprawl cap).

  • OPTION_ON_PAROLE: suppress the end-game tail (a parole peer must not double-request).

  • Neither rarest nor sequential: random-start walk over all pickable pieces (libtorrent's mode below initial_picker_threshold, usually combined with OPTION_PRIORITIZE_PARTIALS).

The returned blocks are not marked. The caller decides what to request and then calls markAsDownloading. Unlike upstream (which dedupes in request_a_block), the result never repeats a block.

Parameters

random

drives the in-bucket tie shuffle, the random-walk mode and the end-game piece/block choice; null = deterministic (ties by index, walk from piece 0, first busy candidate).

numPeers

number of connected peers, for the partial-sprawl cap.

requesterPeer

the picking peer's token: the end-game tail never returns a block whose last requester is this peer.