Skip to content

Negotiate full-identity v2 and hybrid peer routes - #191

Closed
linroid wants to merge 1 commit into
torrent-v2-08-frame-boundsfrom
torrent-v2-08-handshake
Closed

linroid wants to merge 1 commit into
torrent-v2-08-frame-boundsfrom
torrent-v2-08-handshake

Conversation

@linroid

@linroid linroid commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Adds direct v1/v2 and explicitly offered hybrid-upgrade handshakes while retaining the caller's full torrent identity separately from the 20-byte wire tag. Negotiation rejects unexpected upgrades/downgrades, ambiguous local aliases, invalid framing, self-connections and mismatched expected peer IDs. Scratch admission precedes I/O; timeout, cancellation and validation failures close the connection.

Stacked on #190; advances step 08 of #162. A loopback test negotiates v2 using a validated document, then issues a hash request through the admitted transport and verifies the reply. Wire tags remain routing hints, not full remote-content authentication. Multi-torrent listener routing, the complete payload actor, magnet integration and independent-client v2 interoperability remain pending. Extension/DHT advertisement defaults off.

Validation: five focused JVM handshake/connection tests pass with zero failures/errors/skips. All 399 iOS simulator torrent tests execute and pass with the simulator opt-in flag. The existing test-clock usage now declares its coroutine opt-in explicitly.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 12, 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-09-12T17:09:54.328105Z c6f4ddf 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.

@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: c6f4ddf992

ℹ️ 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".

require(matchesV1 != matchesV2) { "Unknown or ambiguous incoming swarm tag" }
val mode = if (matchesV2 || allowUpgrade && remote.upgrade && v2 != null) Mode.V2 else Mode.V1
val result = result(remote, mode, peerId, null)
connection.write(encode(mode, peerId, upgrade = false))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Advertise acceptance in the hybrid-upgrade response

When allowUpgrade accepts a v1 hybrid offer, mode becomes V2, but the response unconditionally clears BEP 52's v2 reserved bit. The local initiator still succeeds because it infers acceptance solely from the changed tag, so the loopback test masks this; a standards-compliant peer that requires both sides to advertise v2 support will instead reject or continue as v1. Set the upgrade bit on responses that select Mode.V2 through this negotiation, and validate it on the initiating side.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Keeping the current negotiation behavior. BEP 52’s Upgrade Path assigns this bit to the initiating peer’s SHA-1 handshake; the responder accepts by returning the v2 infohash. It does not require a second acceptance bit: https://github.com/bittorrent/bittorrent.org/blob/master/beps/bep_0052.rst#L514-L517.

This also matches the independent libtorrent implementation: its encoder sets 0x10 only while the connection is still v1 (https://github.com/arvidn/libtorrent/blob/RC_2_0/src/bt_peer_connection.cpp#L770-L774), and its initiator recognizes the returned v2 hash without checking that bit (https://github.com/arvidn/libtorrent/blob/RC_2_0/src/bt_peer_connection.cpp#L3472-L3480). Requiring the flag in the response would reject this valid negotiation. The existing directAndOfferedUpgradeRoutesKeepTheFullIdentity test explicitly covers an offered upgrade accepted with the v2 tag and the response flag clear. Independent-client full v2 interoperability remains a roadmap gate; the local loopback alone is not claimed to prove it.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

  632 files  +10    632 suites  +10   4m 6s ⏱️ -13s
2 639 tests +15  2 639 ✅ +15  0 💤 ±0  0 ❌ ±0 
3 477 runs  +25  3 477 ✅ +25  0 💤 ±0  0 ❌ ±0 

Results for commit c6f4ddf. ± Comparison against base commit 3b1f9cf.

@linroid

linroid commented Sep 20, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #235 (V2 handshake, hash exchange and bounded transport) in the consolidated eight-PR stack for #162.

The replacement preserves this PR's code and subsequent fixes; its description links the original head and review discussions. The only additional consolidation fix is the CI-discovered listener cancellation regression, documented on the replacement. All eight replacements have passed their required CI checks.

Closing without merging. This branch and its discussions remain available for reference. Review index: #162 (comment)

@linroid linroid closed this Sep 20, 2026
@linroid
linroid deleted the torrent-v2-08-handshake branch September 20, 2026 11:13
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