test: make reusable starting a standalone P2P listener
What changed, and why it matters
This commit is a simple code cleanup in Bitcoin Core's test suite. It moves a small helper that starts a temporary peer-to-peer listener from one test file into a shared test framework file so other tests can reuse it. There is no change to the actual Bitcoin network code, no change to how nodes communicate, and no security fix or vulnerability.
No security action needed. This is a routine test-framework refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors p2p_private_broadcast.py by extracting the logic that starts a P2PConnection listener on an OS-assigned ephemeral port into a new shared function start_p2p_listener() in test_framework/p2p.py. The behavior is identical: it still binds to 127.0.0.1:0, waits for the callback, and returns the assigned address/port. Only test framework code is modified; no consensus, networking, or wallet code is touched.
Changed components
test/functional/p2p_private_broadcast.pytest/functional/test_framework/p2p.pyInspect captured patch +26 / −16
diff --git a/test/functional/p2p_private_broadcast.py b/test/functional/p2p_private_broadcast.py
index ce630fc1..c3efccd5 100755
--- a/test/functional/p2p_private_broadcast.py
+++ b/test/functional/p2p_private_broadcast.py
@@ -15,6 +15,7 @@ from test_framework.p2p import (
P2PInterface,
P2P_SERVICES,
P2P_VERSION,
+ start_p2p_listener,
)
from test_framework.messages import (
CAddress,
@@ -221,22 +222,7 @@ class P2PPrivateBroadcast(BitcoinTestFramework):
listener.peer_connect_helper(dstaddr="0.0.0.0", dstport=0, net=self.chain, timeout_factor=self.options.timeout_factor)
listener.peer_connect_send_version(services=P2P_SERVICES)
- def on_listen_done(addr, port):
- nonlocal actual_to_addr
- nonlocal actual_to_port
- 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=0,
- p2p=listener,
- callback=on_listen_done)
- # Wait until the callback has been called.
- self.wait_until(lambda: actual_to_port != 0)
+ actual_to_addr, actual_to_port = start_p2p_listener(self.network_thread, listener)
self.log.debug(f"Instructing the SOCKS5 proxy to redirect connection i={i} ({conn_type}) for "
f"{format_addr_port(requested_to_addr, requested_to_port)} to "
diff --git a/test/functional/test_framework/p2p.py b/test/functional/test_framework/p2p.py
index fae4eb94..4d812ce3 100755
--- a/test/functional/test_framework/p2p.py
+++ b/test/functional/test_framework/p2p.py
@@ -973,3 +973,27 @@ class P2PTxInvStore(P2PInterface):
self.wait_until(lambda: set(self.tx_invs_received.keys()) == set([int(tx, 16) for tx in txns]), timeout=timeout)
# Flush messages and wait for the getdatas to be processed
self.sync_with_ping()
+
+def start_p2p_listener(network_thread, listener):
+ listen_addr = ""
+ listen_port = 0
+
+ def on_listen_done(addr, port):
+ nonlocal listen_addr
+ nonlocal listen_port
+ listen_addr = addr
+ listen_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.
+ network_thread.listen(
+ addr="127.0.0.1",
+ port=0,
+ p2p=listener,
+ callback=on_listen_done)
+
+ # Wait until the callback has been called.
+ wait_until_helper_internal(lambda: listen_port != 0)
+
+ return listen_addr, listen_port
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.