Skip to content

feat: add transport protocol preference modes#7852

Closed
CharlieLZ wants to merge 1 commit into
tari-project:developmentfrom
CharlieLZ:bounty-7830-transport-types
Closed

feat: add transport protocol preference modes#7852
CharlieLZ wants to merge 1 commit into
tari-project:developmentfrom
CharlieLZ:bounty-7830-transport-types

Conversation

@CharlieLZ
Copy link
Copy Markdown

Fixes #7830.

Summary

  • Add tor_tcp and tcp_tor transport modes alongside tor and tcp.
  • Make tcp_tor the default transport mode.
  • Use the transport protocol list as the dial preference order when selecting peer addresses.
  • Pass the selected protocol order into the hidden service transport so Tor-only, Tor-preferred and TCP-preferred modes are reflected in peer selection.
  • Add unit and Cucumber coverage for the four public peer selection modes.

Notes

memory and socks5 are left in place for existing in-process test transport and proxy transport support. The four public peer selection modes requested by the bounty are covered by tor, tcp, tor_tcp, and tcp_tor.

Payout details can be provided on acceptance.

Validation

  • cargo +1.93.0 test -p tari_p2p transport_type -- --nocapture
  • cargo +1.93.0 test -p tari_comms select_dial_addresses -- --nocapture
  • cargo +1.93.0 check -p tari_p2p -p tari_comms
  • cargo +1.93.0 check -p tari_integration_tests --test cucumber
  • git diff --check

@github-actions
Copy link
Copy Markdown

⚠️ This PR contains unsigned commits. To get your PR merged, please sign those commits (git rebase --exec 'git commit -S --amend --no-edit -n' @{upstream}) and force push them to this branch (git push --force-with-lease).

If you're new to commit signing, there are different ways to set it up:

Sign commits with gpg

Follow the steps below to set up commit signing with gpg:

  1. Generate a GPG key
  2. Add the GPG key to your GitHub account
  3. Configure git to use your GPG key for commit signing
Sign commits with ssh-agent

Follow the steps below to set up commit signing with ssh-agent:

  1. Generate an SSH key and add it to ssh-agent
  2. Add the SSH key to your GitHub account
  3. Configure git to use your SSH key for commit signing
Sign commits with 1Password

You can also sign commits using 1Password, which lets you sign commits with biometrics without the signing key leaving the local 1Password process.

Learn how to use 1Password to sign your commits.

Watch the demo

@CharlieLZ CharlieLZ mentioned this pull request May 25, 2026
6 tasks
@CharlieLZ CharlieLZ force-pushed the bounty-7830-transport-types branch 2 times, most recently from 9c3fbd8 to 7af0215 Compare May 25, 2026 16:27
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces TorTcp and TcpTor transport types to allow configurable protocol preferences, setting TcpTor as the default. The dialer now prioritizes addresses based on these types. Feedback identifies a breaking change in the Tor transport behavior, an unintended change to Socks5 protocol preferences, and performance inefficiencies in the new address selection logic.

TransportType::Memory => vec![TransportProtocol::Memory],
TransportType::Tcp => vec![TransportProtocol::Ipv4, TransportProtocol::Ipv6],
TransportType::Tor => vec![
TransportType::Tor => vec![TransportProtocol::Onion],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Changing the Tor transport type to only support Onion protocols is a breaking change for existing node configurations. Previously, the Tor mode allowed communication with both Onion and TCP/IP addresses (via the Tor proxy). While TorTcp has been added to provide the previous behavior, users who have explicitly set type = "tor" in their configuration files will find their nodes restricted to Onion-only connections after this update. As this represents a fundamental change in transport logic, please ensure that tests are updated to validate the new behavior and that assertions testing obsolete logic are replaced, as per the repository guidelines.

References
  1. When an algorithm's logic is fundamentally changed (e.g., UTXO selection method), tests should be updated to validate the new behavior. Assertions testing obsolete logic (like selection order by age) should be removed and replaced with tests for the new algorithm's properties.

Comment on lines +130 to 134
TransportType::Socks5 => vec![
TransportProtocol::Ipv4,
TransportProtocol::Ipv6,
TransportProtocol::Onion,
],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The protocol preference for Socks5 has been changed from Tor-first (Onion, Ipv4, Ipv6) to TCP-first (Ipv4, Ipv6, Onion). While this aligns with the new project-wide default preference for TCP, the PR description states that socks5 was intended to be left in place. If this change in preference order for the SOCKS5 proxy was unintended, please revert it to maintain backward compatibility for users relying on the previous behavior. If the change was intended, ensure tests are updated to reflect the new selection order and obsolete assertions are removed.

Suggested change
TransportType::Socks5 => vec![
TransportProtocol::Ipv4,
TransportProtocol::Ipv6,
TransportProtocol::Onion,
],
TransportType::Socks5 => vec![
TransportProtocol::Onion,
TransportProtocol::Ipv4,
TransportProtocol::Ipv6,
],
References
  1. When an algorithm's logic is fundamentally changed (e.g., UTXO selection method), tests should be updated to validate the new behavior. Assertions testing obsolete logic (like selection order by age) should be removed and replaced with tests for the new algorithm's properties.

Comment on lines +83 to +102
fn select_dial_addresses(
peer_addresses: &[Multiaddr],
supported_transport_protocols: &[TransportProtocol],
excluded_dial_addresses: &[MultiaddrRange],
) -> Vec<Multiaddr> {
supported_transport_protocols
.iter()
.flat_map(|protocol| {
peer_addresses
.iter()
.filter(move |address| {
TransportProtocol::from(*address) == *protocol &&
!excluded_dial_addresses
.iter()
.any(|excluded| excluded.contains(address))
})
.cloned()
})
.collect()
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation of select_dial_addresses is inefficient as it performs the excluded_dial_addresses check repeatedly for every protocol in the preference list. Additionally, it calls TransportProtocol::from multiple times for each address. A more efficient approach would be to filter out excluded addresses once and then group or order the remaining addresses by protocol.

fn select_dial_addresses(
    peer_addresses: &[Multiaddr],
    supported_transport_protocols: &[TransportProtocol],
    excluded_dial_addresses: &[MultiaddrRange],
) -> Vec<Multiaddr> {
    let filtered_addresses: Vec<_> = peer_addresses
        .iter()
        .filter(|address| !excluded_dial_addresses.iter().any(|excluded| excluded.contains(address)))
        .collect();

    supported_transport_protocols
        .iter()
        .flat_map(|protocol| {
            filtered_addresses
                .iter()
                .filter(|address| TransportProtocol::from(***address) == *protocol)
                .map(|&addr| addr.clone())
        })
        .collect()
}

@CharlieLZ CharlieLZ force-pushed the bounty-7830-transport-types branch from 7af0215 to 304b6e7 Compare May 25, 2026 16:36
@CharlieLZ
Copy link
Copy Markdown
Author

Closing this earlier pass in favor of #7853. The newer PR keeps the same issue target and also applies the configured transport preference to peer candidate selection, not only dial address ordering.

@CharlieLZ CharlieLZ closed this May 25, 2026
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.

Connection types

1 participant