Skip to content

refactor!: remove dead NatTraversalEndpoint hole-punch chain#41

Merged
jacderida merged 3 commits into
rc-2026.4.1from
refactor/remove-dead-hole-punch-chain
Apr 6, 2026
Merged

refactor!: remove dead NatTraversalEndpoint hole-punch chain#41
jacderida merged 3 commits into
rc-2026.4.1from
refactor/remove-dead-hole-punch-chain

Conversation

@mickvandijke
Copy link
Copy Markdown
Collaborator

@mickvandijke mickvandijke commented Apr 6, 2026

Summary

  • Deletes the unused duplicate connect_with_fallback on NatTraversalEndpoint and its supporting "PATH_CHALLENGE hole-punch" chain (attempt_hole_punching, attempt_quic_hole_punching, get_candidate_pairs_for_addr, calculate_candidate_pair_priority, create_path_challenge_packet, store_successful_candidate_pair, get_successful_candidate_address) plus the orphaned successful_candidates DashMap field.
  • The deleted code was reachable only from the duplicate connect_with_fallback (also removed). The production hole-punch path lives in P2pEndpoint::connect_with_fallback_inner, which is what LinkTransport::dial_addr and the saorsa-transport example bin both call. Verified no in-tree or downstream consumers across this crate, saorsa-core, and ant-node.
  • Fixes a pre-existing nonminimal_bool clippy warning in the same file (!target_peer_id.is_some()target_peer_id.is_none()).

Why this is dead code, not just unused

The deleted hole-punch implementation could not have worked even if it had been wired in:

  1. attempt_quic_hole_punching bound a fresh std::net::UdpSocket to the local candidate address — always fails on a real node because Quinn already owns the port (UDP binds are exclusive).
  2. The "QUIC packet" it sent was a hand-rolled byte sequence (0x40 [0,0,0,1] 0x1a <8 random>) that is not a valid encrypted QUIC packet, so any receiving Quinn endpoint silently dropped it during connection-id lookup.
  3. The success branch then waited 100 ms blocking on recv_from for a "response" no compliant peer would ever send.

The #[allow(dead_code)] markers on every function in the chain disguised that nothing in the path could ever succeed, actively misleading anyone debugging hole-punch issues via grep. Comment blocks left in place point readers at the production path.

Breaking change

The ! indicates removal of a pub fn. No in-tree or downstream consumer depended on it, but the public API surface is technically narrower.

Test plan

  • cargo test --lib — 1459/1459 passed (3 ignored, unchanged)
  • cargo clippy --lib -- -D warnings — clean
  • rustfmt --edition 2024 --check — clean
  • Downstream saorsa-core test suite still green

🤖 Generated with Claude Code

Greptile Summary

Removes an unreachable NatTraversalEndpoint::connect_with_fallback duplicate and its seven supporting helper functions (plus the orphaned successful_candidates DashMap field) that were gated behind #[allow(dead_code)] and could not have worked — the inner UdpSocket bind races Quinn for the port, and the hand-rolled byte sequence is not a valid QUIC packet. The production hole-punch path in P2pEndpoint::connect_with_fallback_inner is untouched. All call-site verification confirms no in-tree consumer used the removed API.

Confidence Score: 5/5

Safe to merge — removes provably unreachable code with no in-tree callsites and adds tombstone comments.

All changed code is dead-code removal confirmed by grepping every call site. The single P2 finding (misleading log label touched by the clippy fix) does not affect correctness or runtime behaviour.

No files require special attention.

Important Files Changed

Filename Overview
src/nat_traversal_api.rs Removes unreachable hole-punch chain and orphaned successful_candidates field; adds tombstone comments; fixes a clippy nonminimal_bool warning.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[dial_addr / saorsa-transport bin] --> B[P2pEndpoint::connect_with_fallback]
    B --> C[P2pEndpoint::connect_with_fallback_inner]
    C --> D[PUNCH_ME_NOW coordinator flow]
    D --> E[Hole-punch attempt]
    E --> F[Connection established]
    G[NatTraversalEndpoint::connect_with_fallback REMOVED]
    G -.->|deleted chain| H[attempt_hole_punching
attempt_quic_hole_punching
get_candidate_pairs_for_addr
create_path_challenge_packet
store_successful_candidate_pair
ALL REMOVED]
    style G fill:#f99,stroke:#c33
    style H fill:#f99,stroke:#c33
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/nat_traversal_api.rs
Line: 5651-5660

Comment:
**Misleading `from_addr` log label**

The `from_addr={}` format slot receives `target_peer_id.is_none()` — a `bool` — so the log line always prints `from_addr=true` or `from_addr=false` rather than an actual address. This pre-existed the PR (`!target_peer_id.is_some()` had identical semantics), but since this line is touched by the clippy fix, it's a good opportunity to rename the label to match what is actually logged.

```suggestion
            "Sending PUNCH_ME_NOW coordination request for {} to coordinator {} (wire_id={}, from_peer_id={}, peer_id_missing={})",
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "refactor!: remove dead NatTraversalEndpo..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Deletes the unused duplicate connect_with_fallback on
NatTraversalEndpoint plus its supporting "PATH_CHALLENGE hole-punch"
chain (attempt_hole_punching, attempt_quic_hole_punching,
get_candidate_pairs_for_addr, calculate_candidate_pair_priority,
create_path_challenge_packet, store_successful_candidate_pair,
get_successful_candidate_address) and the orphaned
successful_candidates DashMap field.

The deleted code was reachable only from the duplicate
connect_with_fallback (also removed). The actual production hole-punch
path lives in P2pEndpoint::connect_with_fallback_inner, which is what
LinkTransport::dial_addr (link_transport_impl.rs) and the
saorsa-transport example bin both call. The duplicate had no in-tree
or downstream consumers — verified across this crate, saorsa-core,
and ant-node.

The deleted hole-punch implementation could not have worked even if
it had been wired in:

  1. attempt_quic_hole_punching bound a fresh std::net::UdpSocket to
     the local candidate address — always fails on a real node because
     Quinn already owns the port (UDP binds are exclusive).
  2. The "QUIC packet" it sent was a hand-rolled byte sequence
     (0x40 [0,0,0,1] 0x1a <8 random>) that is not a valid encrypted
     QUIC packet, so any receiving Quinn endpoint silently dropped it
     during connection-id lookup.
  3. The success branch then waited 100 ms blocking on recv_from for
     a "response" no compliant peer would ever send.

The #[allow(dead_code)] markers on every function in the chain
disguised that nothing in the path could ever succeed, actively
misleading anyone debugging hole-punch issues via grep. Comment blocks
left in place point readers at the production path.

Also fixes a pre-existing nonminimal_bool clippy warning in the same
file (`!target_peer_id.is_some()` -> `target_peer_id.is_none()`) so the
file now passes `cargo clippy --lib -- -D warnings`. The fix is a
single character but is called out here so it isn't hidden inside the
bulk deletion.

The `!` indicates removal of a `pub fn`. No in-tree or downstream
consumer depended on it, but the public API surface is technically
narrower.

Verified:
  cargo test --lib                  1459/1459 passed (3 ignored, unchanged)
  cargo clippy --lib -- -D warnings clean
  rustfmt --edition 2024 --check    clean
  Downstream saorsa-core test suite still green

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/nat_traversal_api.rs
Comment on lines 5651 to 5660
info!(
"Sending PUNCH_ME_NOW coordination request for {} to coordinator {} (wire_id={}, from_peer_id={}, from_addr={})",
target_addr, coordinator,
target_addr,
coordinator,
hex::encode(&target_wire_id[..8]),
target_peer_id.map(|p| hex::encode(&p[..8])).unwrap_or_else(|| "none".to_string()),
!target_peer_id.is_some(),
target_peer_id
.map(|p| hex::encode(&p[..8]))
.unwrap_or_else(|| "none".to_string()),
target_peer_id.is_none(),
);
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 Misleading from_addr log label

The from_addr={} format slot receives target_peer_id.is_none() — a bool — so the log line always prints from_addr=true or from_addr=false rather than an actual address. This pre-existed the PR (!target_peer_id.is_some() had identical semantics), but since this line is touched by the clippy fix, it's a good opportunity to rename the label to match what is actually logged.

Suggested change
info!(
"Sending PUNCH_ME_NOW coordination request for {} to coordinator {} (wire_id={}, from_peer_id={}, from_addr={})",
target_addr, coordinator,
target_addr,
coordinator,
hex::encode(&target_wire_id[..8]),
target_peer_id.map(|p| hex::encode(&p[..8])).unwrap_or_else(|| "none".to_string()),
!target_peer_id.is_some(),
target_peer_id
.map(|p| hex::encode(&p[..8]))
.unwrap_or_else(|| "none".to_string()),
target_peer_id.is_none(),
);
"Sending PUNCH_ME_NOW coordination request for {} to coordinator {} (wire_id={}, from_peer_id={}, peer_id_missing={})",
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/nat_traversal_api.rs
Line: 5651-5660

Comment:
**Misleading `from_addr` log label**

The `from_addr={}` format slot receives `target_peer_id.is_none()` — a `bool` — so the log line always prints `from_addr=true` or `from_addr=false` rather than an actual address. This pre-existed the PR (`!target_peer_id.is_some()` had identical semantics), but since this line is touched by the clippy fix, it's a good opportunity to rename the label to match what is actually logged.

```suggestion
            "Sending PUNCH_ME_NOW coordination request for {} to coordinator {} (wire_id={}, from_peer_id={}, peer_id_missing={})",
```

How can I resolve this? If you propose a fix, please make it concise.

mickvandijke and others added 2 commits April 6, 2026 15:59
Removes `pub struct CandidatePair` and `pub enum CandidatePairState` from
`nat_traversal_api.rs`. They were only ever used by the dead hole-punch
chain deleted in 5280123, and `connection/nat_traversal.rs` defines its
own unrelated `pub(super) CandidatePair` for the live coordinator path.
With the chain gone, both types are orphans in the public API surface.

Also tightens the two tombstone comments — drops the box-drawing
borders, fixes the misleading "implemented elsewhere in this file"
phrasing (the production flow spans `p2p_endpoint.rs` and the
PUNCH_ME_NOW helpers later in this file), and cross-links them.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Pre-existing formatting drift caught by `cargo fmt --all -- --check`
on this branch but unrelated to the hole-punch chain removal:

- `src/endpoint.rs:387` — collapse a `.as_ref().map(...)` chain that
  rustfmt now wants on one line.
- `src/p2p_endpoint.rs:1822` — split a multi-arg `info!` so each
  positional argument sits on its own line.

No semantic change.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jacderida jacderida merged commit 9ba3ecc into rc-2026.4.1 Apr 6, 2026
2 of 3 checks passed
mickvandijke added a commit that referenced this pull request Apr 6, 2026
PR #41 removed the dead `NatTraversalEndpoint::attempt_hole_punching`
chain (`get_candidate_pairs_for_addr`, `calculate_candidate_pair_priority`,
`attempt_quic_hole_punching`, `create_path_challenge_packet`,
`store_successful_candidate_pair`, `get_successful_candidate_address`)
as unreachable code gated behind `#[allow(dead_code)]` that could
not have worked in production.

PR #40 had added `CandidateSource::PortMapped` handling inside two of
those exact functions (`is_local_side` in `get_candidate_pairs_for_addr`
and a type-preference slot in `calculate_candidate_pair_priority`).
Those additions patched dead code and are safely dropped — the live
production pairing path in `P2pEndpoint::connect_with_fallback_inner`
drives the coordinator-mediated PUNCH_ME_NOW flow and consumes
priorities from `crate::connection::nat_traversal::calculate_candidate_priority`,
which works off `CandidateType`, not `CandidateSource`. The mapping
`CandidateSource::PortMapped -> CandidateType::ServerReflexive` in
`classify_candidate_type` at `src/connection/nat_traversal.rs:307`
(introduced by PR #40 and untouched by PR #41) is what carries the
PortMapped variant through the production pairing formula. No
further plumbing is required.

Extended PR #41's tombstone comment with a note explaining why
PortMapped needs no replacement pairing logic at this site, so the
next person grepping for `PortMapped` in `nat_traversal_api.rs`
lands on the explanation directly.

Validation:
- cargo fmt --all -- --check clean
- cargo clippy --features upnp --all-targets -- -D warnings clean
- cargo clippy --no-default-features --features platform-verifier,network-discovery --all-targets -- -D warnings clean
- cargo nextest run --features upnp --lib: 1470/1470 passed

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

2 participants