Repository navigation
fix(torrent): make peers pay for the piece reads and hash folding their requests cause - #548
Conversation
…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
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
There was a problem hiding this comment.
💡 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".
Test Results 1 915 files + 5 1 942 suites +5 23m 37s ⏱️ - 1m 36s For more details on these failures, see this check. Results for commit 0a72431. ± Comparison against base commit 9121cf5. |
… or clear unused serve credit
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.
PeerServeCreditgives 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.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.
MAX_PROOF_WORKfrom the peer's credit: 4096 folded hashes, plus one piece when it proves block hashes.TorrentV2HashServer.respond(maxWork)stops before folding past that and reportslastWork, and the unused part is refunded.chargeAnswer), which pace later blocks and reads.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.