Merge bitcoin/bitcoin#36440: test: speed up functional tests by removing avoidable waits
What changed, and why it matters
This commit only changes Bitcoin Core's functional test framework to make tests run faster. It replaces a slow polling wait with an event-driven notification and enables a test-only peer-whitelist flag for transaction relay. There is no change to production node code, no user-facing behavior change, and no security issue.
No security action required. This is a test-framework performance improvement. Normal code-review approval is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies test/functional/test_framework/p2p.py to turn p2p_lock from a threading.Lock into a threading.Condition and calls notify_all() when a P2P message is delivered or the connection state changes. test/functional/test_framework/util.py’s wait_until_helper_internal now waits on the Condition instead of sleeping and polling. Several wallet functional tests set self.noban_tx_relay = True to skip ban-score-based transaction relay delays. All changes are confined to the functional test suite; no consensus, networking, or wallet runtime code is affected.
Changed components
test/functional/test_framework/p2p.pytest/functional/test_framework/util.pytest/functional/wallet_change_address.pytest/functional/wallet_conflicts.pytest/functional/wallet_exported_watchonly.pytest/functional/wallet_importprunedfunds.pytest/functional/wallet_orphanedreward.pytest/functional/wallet_signer.pyInspect captured patch +25 / −1
### test/functional/test_framework/p2p.py
@@ -245,6 +245,8 @@ def connection_made(self, transport):
if self.p2p_connected_to_node and not self.supports_v2_p2p:
self.send_version()
self.on_open()
+ with p2p_lock:
+ p2p_lock.notify_all()
def connection_lost(self, exc):
"""asyncio callback when a connection is closed."""
@@ -256,6 +258,8 @@ def connection_lost(self, exc):
self._transport = None
self.recvbuf = b""
self.on_close()
+ with p2p_lock:
+ p2p_lock.notify_all()
# v2 handshake method
def _on_data_v2_handshake(self):
@@ -528,6 +532,7 @@ def on_message(self, message):
except Exception:
print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0]))
raise
+ p2p_lock.notify_all()
# Callback methods. Can be overridden by subclasses in individual test
# cases to provide custom message handling behaviour.
@@ -735,7 +740,9 @@ def test_function():
# P2PConnection acquires this lock whenever delivering a message to a P2PInterface.
# This lock should be acquired in the thread running the test logic to synchronize
# access to any data shared with the P2PInterface or P2PConnection.
-p2p_lock = threading.Lock()
+# It is a Condition so that waiters can be woken up as soon as a message is
+# delivered or the connection state changes, instead of polling.
+p2p_lock = threading.Condition(threading.Lock())
class NetworkThread(threading.Thread):
### test/functional/test_framework/util.py
@@ -18,6 +18,7 @@
import random
import re
import shlex
+import threading
import time
import types
@@ -440,6 +441,10 @@ def wait_until_helper_internal(predicate, *, timeout=60, lock=None, timeout_fact
with lock:
if predicate():
return
+ if isinstance(lock, threading.Condition):
+ # Wake up early when the lock owner signals a state change
+ lock.wait(check_interval)
+ continue
else:
if predicate():
return
### test/functional/wallet_change_address.py
@@ -17,6 +17,8 @@ class WalletChangeAddressTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 3
+ # whitelist peers to speed up tx relay / mempool sync
+ self.noban_tx_relay = True
# discardfee is used to make change outputs less likely in the change_pos test
self.extra_args = [
[],
### test/functional/wallet_conflicts.py
@@ -17,6 +17,8 @@
class TxConflicts(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 3
+ # whitelist peers to speed up tx relay / mempool sync
+ self.noban_tx_relay = True
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
### test/functional/wallet_exported_watchonly.py
@@ -21,6 +21,8 @@ class WalletExportedWatchOnly(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
+ # whitelist peers to speed up tx relay / mempool sync
+ self.noban_tx_relay = True
self.extra_args = [[], [f"-keypool={KEYPOOL_SIZE}"]]
def setup_network(self):
### test/functional/wallet_importprunedfunds.py
@@ -26,6 +26,8 @@ class ImportPrunedFundsTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
+ # whitelist peers to speed up tx relay / mempool sync
+ self.noban_tx_relay = True
self.extra_args = [["-deprecatedrpc=removeprunedfunds"]] * 2
def skip_test_if_missing_module(self):
### test/functional/wallet_orphanedreward.py
@@ -11,6 +11,8 @@ class OrphanedBlockRewardTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
+ # whitelist peers to speed up tx relay / mempool sync
+ self.noban_tx_relay = True
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
### test/functional/wallet_signer.py
@@ -37,6 +37,8 @@ def mock_multi_signers_path(self):
def set_test_params(self):
self.num_nodes = 2
+ # whitelist peers to speed up tx relay / mempool sync
+ self.noban_tx_relay = True
self.extra_args = [
[],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.