A BitTorrent client
Stage 3 of 12v2 · 4089ec3a

Metadata and infohash

Extract announce, piece length, and hashes, and compute SHA-1 over the exact info bytes.

Metadata and infohash

Extract announce, piece length, and hashes, and compute SHA-1 over the exact info bytes. 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": "piece-layout",
  "length": 10,
  "pieceLength": 4,
  "piecesHex": "000000000000000000000000000000000000000011111111111111111111111111111111111111112222222222222222222222222222222222222222"
}

produces exactly:

{
  "pieceLengths": [4, 4, 2],
  "pieceHashes": [
    "0000000000000000000000000000000000000000",
    "1111111111111111111111111111111111111111",
    "2222222222222222222222222222222222222222"
  ]
}

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.