Skip to content

fix(torrent): make peers pay for the piece reads and hash folding their requests cause - #548

Merged
linroid merged 3 commits into
mainfrom
fix/torrent-serve-costs
Oct 14, 2026
Merged

linroid merged 3 commits into
mainfrom
fix/torrent-serve-costs

Conversation

@linroid

@linroid linroid commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Fixes two findings from the upload review. In both, a peer's small requests made us do far more disk and CPU work than the bytes we sent, with nothing bounding it while uploads are unlimited (the default).

Whole-piece reads for one block (v1 and v2)

To serve a block we read the whole piece back and hash it again, which is how we refuse to serve files changed on disk. v1 kept one piece per peer, so requests alternating between two pieces made us read and hash a whole piece (up to 16 MiB) for every 16 KiB we sent. Fast peers could do this through allowed-fast pieces even while we choked them. v2 had the same problem across many pieces.

  • PeerServeCredit gives each peer a share of serving work: two pieces plus 4 MiB, refilled over 30 s and by twice the payload the peer is sent. A peer that takes whole pieces earns back more than its reads cost. A peer asking for one block of each piece runs out after a few pieces.
  • v1 serves requests for the piece it already holds first, which on its own ends the alternating case. Reading any other piece is charged to the peer. Once a peer is out of credit, its requests for pieces we don't hold are rejected, and the peer is not choked or banned.
  • v2 charges the peer whose request starts a piece read, once per read. A piece that another peer's read already brought in costs nothing.

Hash requests (v2)

Hash requests above the piece layer but below the cached levels were folded from piece hashes every time. One 49-byte request could take about 131k SHA-256 operations, and the answers bypassed the upload limit.

  • Each request reserves at most MAX_PROOF_WORK from the peer's credit: 4096 folded hashes, plus one piece when it proves block hashes. TorrentV2HashServer.respond(maxWork) stops before folding past that and reports lastWork, and the unused part is refunded.
  • Every answer now counts against the upload limits (chargeAnswer), which pace later blocks and reads.
  • Piece-layer requests, the ones clients actually make, fold nothing and are unaffected.

I kept serving hashes to peers we choke, because peers need piece layers to verify what they download. The credit now bounds the work they can cause instead.

Tests

  • PeerServeCreditTest: charging, earning, refill, debt and refunds.
  • TorrentUploadTest: a real engine seeding 16 × 1 MiB pieces. A peer asking one block of each piece is served a few and then refused; a peer taking whole pieces is never refused.
  • PeerHashServingTest: a fold request past its limit is rejected, work is reported, and answers pay their bytes.

:library:torrent:jvmTest (all 202 suites) passes, and the torrent tests compile for the iOS simulator.

…ir requests cause

- PeerServeCredit: each peer's share of serving work (two pieces and 4 MiB, refilled over 30 s
  and by twice the payload it is sent), shared by v1 and v2
- v1 serves the cached piece's requests first and charges each new piece read; a peer out of
  credit has requests for other pieces rejected
- v2 charges the peer whose request starts a piece read
- BEP 52 hash requests reserve at most 4096 folded hashes of work, the hash server stops before
  folding past it, and every answer pays its bytes to the upload limits
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T02:37:27.344056Z 0a72431 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0a72431e95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 915 files  + 5   1 942 suites  +5   23m 37s ⏱️ - 1m 36s
12 906 tests +24  12 660 ✅ +23  245 💤 ±0  1 ❌ +1 
15 477 runs  +40  15 231 ✅ +39  245 💤 ±0  1 ❌ +1 

For more details on these failures, see this check.

Results for commit 0a72431. ± Comparison against base commit 9121cf5.

@linroid
linroid merged commit f6e1a38 into main Oct 14, 2026
11 checks passed
@linroid
linroid deleted the fix/torrent-serve-costs branch October 14, 2026 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant