Skip to content

Commit 78f040a

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22490: test: Disable automatic connections per default in the functional tests
8ca51af test: Disable automatic connections by default (Martin Zumsande) Pull request description: A node normally doesn't make automatic connections to peers in the functional tests because neither DNS seeds nor hardcoded peers are available on regtest. However, when random entries are inserted into addrman as part of a functional test (e.g. while testing addr relay), `ThreadOpenConnections` will periodically try to connect to them, resulting in log entries such as: `[opencon] [net.cpp:400] [ConnectNode] trying connection 18.166.1.1:8333 lastseen=0.0hrs` I don't think it's desirable that functional tests try to connect to random computers on the internet, aside from the possibility that at some point in time someone out there might actually answer in a way to ruin a test. This PR fixes this problem by disabling `ThreadOpenConnections` by adding `-connect=0` to the default args, and adding exceptions only when needed for the test to pass. ACKs for top commit: tryphe: Concept ACK, light code review ACK 8ca51af Tree-SHA512: bcfb2de610e6c35a97a2bd7ad6267e968b1ac7529638d99276993cd5bc93ce9919d54e22d6dc84e1b02ecd626ab6554e201693552ea065c29794eece38c43f7d
2 parents b6c3fce + 8ca51af commit 78f040a

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

test/functional/feature_anchors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def check_node_connections(*, node, num_in, num_out):
2323
class AnchorsTest(BitcoinTestFramework):
2424
def set_test_params(self):
2525
self.num_nodes = 1
26+
self.disable_autoconnect = False
2627

2728
def setup_network(self):
2829
self.setup_nodes()

test/functional/feature_config_args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def set_test_params(self):
1717
self.num_nodes = 1
1818
self.supports_cli = False
1919
self.wallet_names = []
20+
self.disable_autoconnect = False
2021

2122
def test_config_file_parser(self):
2223
self.stop_node(0)

test/functional/test_framework/test_framework.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ def __init__(self):
112112
# By default the wallet is not required. Set to true by skip_if_no_wallet().
113113
# When False, we ignore wallet_names regardless of what it is.
114114
self.requires_wallet = False
115+
# Disable ThreadOpenConnections by default, so that adding entries to
116+
# addrman will not result in automatic connections to them.
117+
self.disable_autoconnect = True
115118
self.set_test_params()
116119
assert self.wallet_names is None or len(self.wallet_names) <= self.num_nodes
117120
if self.options.timeout_factor == 0 :
@@ -711,7 +714,7 @@ def _initialize_chain(self):
711714
if not os.path.isdir(cache_node_dir):
712715
self.log.debug("Creating cache directory {}".format(cache_node_dir))
713716

714-
initialize_datadir(self.options.cachedir, CACHE_NODE_ID, self.chain)
717+
initialize_datadir(self.options.cachedir, CACHE_NODE_ID, self.chain, self.disable_autoconnect)
715718
self.nodes.append(
716719
TestNode(
717720
CACHE_NODE_ID,
@@ -769,15 +772,15 @@ def cache_path(*paths):
769772
self.log.debug("Copy cache directory {} to node {}".format(cache_node_dir, i))
770773
to_dir = get_datadir_path(self.options.tmpdir, i)
771774
shutil.copytree(cache_node_dir, to_dir)
772-
initialize_datadir(self.options.tmpdir, i, self.chain) # Overwrite port/rpcport in bitcoin.conf
775+
initialize_datadir(self.options.tmpdir, i, self.chain, self.disable_autoconnect) # Overwrite port/rpcport in bitcoin.conf
773776

774777
def _initialize_chain_clean(self):
775778
"""Initialize empty blockchain for use by the test.
776779
777780
Create an empty blockchain and num_nodes wallets.
778781
Useful if a test case wants complete control over initialization."""
779782
for i in range(self.num_nodes):
780-
initialize_datadir(self.options.tmpdir, i, self.chain)
783+
initialize_datadir(self.options.tmpdir, i, self.chain, self.disable_autoconnect)
781784

782785
def skip_if_no_py3_zmq(self):
783786
"""Attempt to import the zmq package and skip the test if the import fails."""

test/functional/test_framework/util.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,17 @@ def rpc_url(datadir, i, chain, rpchost):
337337
################
338338

339339

340-
def initialize_datadir(dirname, n, chain):
340+
def initialize_datadir(dirname, n, chain, disable_autoconnect=True):
341341
datadir = get_datadir_path(dirname, n)
342342
if not os.path.isdir(datadir):
343343
os.makedirs(datadir)
344-
write_config(os.path.join(datadir, "bitcoin.conf"), n=n, chain=chain)
344+
write_config(os.path.join(datadir, "bitcoin.conf"), n=n, chain=chain, disable_autoconnect=disable_autoconnect)
345345
os.makedirs(os.path.join(datadir, 'stderr'), exist_ok=True)
346346
os.makedirs(os.path.join(datadir, 'stdout'), exist_ok=True)
347347
return datadir
348348

349349

350-
def write_config(config_path, *, n, chain, extra_config=""):
350+
def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect=True):
351351
# Translate chain subdirectory name to config name
352352
if chain == 'testnet3':
353353
chain_name_conf_arg = 'testnet'
@@ -375,6 +375,8 @@ def write_config(config_path, *, n, chain, extra_config=""):
375375
f.write("shrinkdebugfile=0\n")
376376
# To improve SQLite wallet performance so that the tests don't timeout, use -unsafesqlitesync
377377
f.write("unsafesqlitesync=1\n")
378+
if disable_autoconnect:
379+
f.write("connect=0\n")
378380
f.write(extra_config)
379381

380382

0 commit comments

Comments
 (0)