Skip to content

Commit 8ca51af

Browse files
committed
test: Disable automatic connections by default
This prevents the node from trying to connect to random IPs on the internet while running the functional tests. Exceptions are added when required for the test to pass.
1 parent 54e3174 commit 8ca51af

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
@@ -338,17 +338,17 @@ def rpc_url(datadir, i, chain, rpchost):
338338
################
339339

340340

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

350350

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

381383

0 commit comments

Comments
 (0)