implement randomize ports #35585

pull victorandre957 wants to merge 1 commits into bitcoin:master from victorandre957:feat/implement-randomize-ports changing 6 files +330 −11
  1. victorandre957 commented at 7:03 PM on June 22, 2026: none

    Adds a new opt-in -randomizep2pport mode that selects a non-default clearnet P2P listening port on first startup, persists it to the dynamic settings file, and reuses it on later restarts.

    The feature is independent from fixed -port usage. -randomizep2pport and -port are mutually exclusive: users can either let Bitcoin Core manage a randomized persisted P2P port, or explicitly choose one with -port, but not both.

    Behavior

    • Adds -randomizep2pport=1.
    • Selects a listening port from 49152-65534 when no randomized port has been persisted yet.
    • Persists the selected port in settings.json as a hidden network-specific setting.
    • Reuses the persisted randomized port on later restarts.
    • Rejects reserved Bitcoin P2P ports, test-chain default P2P ports, bad ports, and ports outside the randomized range for randomized local listening or advertised ports.
    • Rejects using -randomizep2pport together with -port.
    • If the first generated candidate is unavailable during initial selection, tries another candidate instead of failing immediately.
    • If the persisted randomized port is unavailable on restart, fails startup instead of silently selecting a new port.
    • Keeps remote peer ports unrestricted, so connections to existing peers on 8333 and other advertised ports continue to work.
    • Keeps Params().GetDefaultPort(), CConnman::GetDefaultPort(), DNS seed behavior, addrman behavior, and chain params unchanged.

    Notes

    This is best-effort on the public P2P network. Bitcoin Core can listen on and advertise the randomized port, but remote peers still need to learn that port through address relay, RPC, manual configuration, or external discovery before they can connect.

    This does not change outbound compatibility with the existing network: nodes can still connect to legacy peers using 8333.

    Onion/I2P behavior is unchanged.

    Tests

    • cmake --build build --target bitcoind -j2
    • cmake --build build --target test_bitcoin -j2
    • build/bin/test_bitcoin --run_test=net_tests
    • test/functional/feature_randomize_p2p_port.py --configfile build/test/config.ini
  2. implement randomize ports d5a92ed2a3
  3. DrahtBot commented at 7:03 PM on June 22, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35585.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. victorandre957 closed this on Jun 22, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-07-27 01:50 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me