A BitTorrent client
Stage 10 of 12v2 · 51eeb1a9

Peer-aware scheduler

Assign pieces only to peers that have them and bound in-flight requests.

Peer-aware scheduler

Assign pieces only to peers that have them and bound in-flight requests. The implementation is cumulative and must preserve earlier operations.

Observable contract

The harness reads JSON and represents binary data as hexadecimal. Tracker and peer replies are supplied as fixtures to avoid external networking and randomness.

{
  "op": "schedule",
  "pieces": [0, 1, 2],
  "peers": [
    ["a", [0, 2]],
    ["b", [1, 2]]
  ],
  "maxPerPeer": 1
}

produces exactly:

{
  "assignments": [
    ["a", 0],
    ["b", 1]
  ],
  "pending": [2]
}

Emit compact JSON and a final newline. Invalid requests leave stdout empty, include error on stderr, and exit non-zero.

Implementation

Keep binary data as bytes: do not turn hashes, peer IDs, or pieces into text except where the protocol says so. Separate framing, peer state, scheduling, and integrity. Verify lengths and hashes before publishing a piece as complete.

Walk through the fixture by offsets and test fragmentation, duplicate messages, timeouts, and peers advertising inconsistent data. Tests do not use the Internet; connect the same logic to real sockets outside the harness.

Acceptance criteria

  • The representative case produces the exact output.
  • No unverified data reaches the final file.
  • Scheduling is deterministic for the same input.