test: use ephemeral ports in p2p_private_broadcast.py
What changed, and why it matters
This is a minor test-only change. It updates one Bitcoin Core functional test to let the operating system pick a free network port automatically, instead of the test guessing a port number. This prevents the test from failing when another test is already using the guessed port. It does not change the Bitcoin Core software that users run, and it has no security impact on the live Bitcoin network or user wallets.
No security action needed. This is a routine test improvement. Reviewers may verify the test still passes in parallel test runs.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies test/functional/p2p_private_broadcast.py to bind Python P2P listener nodes with port=0 so the OS assigns an ephemeral port. Previously the test computed a base port from tor_port(MAX_NODES)+1 and assigned sequential ports, which could collide with other concurrently running tests or TIME_WAIT sockets. The change removes the MAX_NODES import and the ports_base calculation, and adds a callback/on_listen_done mechanism to discover the assigned port. This is purely a test-framework robustness improvement.
Changed components
test/functional/p2p_private_broadcast.pyInspect captured patch +4 / −5
diff --git a/test/functional/p2p_private_broadcast.py b/test/functional/p2p_private_broadcast.py
index 4c3739d8..803444cc 100755
--- a/test/functional/p2p_private_broadcast.py
+++ b/test/functional/p2p_private_broadcast.py
@@ -36,7 +36,6 @@ from test_framework.test_framework import (
BitcoinTestFramework,
)
from test_framework.util import (
- MAX_NODES,
assert_equal,
assert_not_equal,
assert_raises_rpc_error,
@@ -181,9 +180,6 @@ class P2PPrivateBroadcast(BitcoinTestFramework):
self.socks5_server = Socks5Server(socks5_server_config)
self.socks5_server.start()
- # Tor ports are the highest among p2p/rpc/tor, so this should be the first available port.
- ports_base = tor_port(MAX_NODES) + 1
-
self.destinations = []
self.destinations_lock = threading.Lock()
@@ -215,9 +211,12 @@ class P2PPrivateBroadcast(BitcoinTestFramework):
actual_to_addr = addr
actual_to_port = port
+ # Use port=0 to let the OS assign an available port. This
+ # avoids "address already in use" errors when tests run
+ # concurrently or ports are still in TIME_WAIT state.
self.network_thread.listen(
addr="127.0.0.1",
- port=ports_base + i,
+ port=0,
p2p=listener,
callback=on_listen_done)
# Wait until the callback has been called.
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.