Merge bitcoin/bitcoin#22490: test: Disable automatic connections per default in the functional tests

8ca51af1ec 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 8ca51af1ec

Tree-SHA512: bcfb2de610e6c35a97a2bd7ad6267e968b1ac7529638d99276993cd5bc93ce9919d54e22d6dc84e1b02ecd626ab6554e201693552ea065c29794eece38c43f7d
pull/826/head
MarcoFalke 3 years ago
commit 78f040a6b3
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

@ -23,6 +23,7 @@ def check_node_connections(*, node, num_in, num_out):
class AnchorsTest(BitcoinTestFramework): class AnchorsTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.num_nodes = 1 self.num_nodes = 1
self.disable_autoconnect = False
def setup_network(self): def setup_network(self):
self.setup_nodes() self.setup_nodes()

@ -17,6 +17,7 @@ class ConfArgsTest(BitcoinTestFramework):
self.num_nodes = 1 self.num_nodes = 1
self.supports_cli = False self.supports_cli = False
self.wallet_names = [] self.wallet_names = []
self.disable_autoconnect = False
def test_config_file_parser(self): def test_config_file_parser(self):
self.stop_node(0) self.stop_node(0)

@ -112,6 +112,9 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
# By default the wallet is not required. Set to true by skip_if_no_wallet(). # By default the wallet is not required. Set to true by skip_if_no_wallet().
# When False, we ignore wallet_names regardless of what it is. # When False, we ignore wallet_names regardless of what it is.
self.requires_wallet = False self.requires_wallet = False
# Disable ThreadOpenConnections by default, so that adding entries to
# addrman will not result in automatic connections to them.
self.disable_autoconnect = True
self.set_test_params() self.set_test_params()
assert self.wallet_names is None or len(self.wallet_names) <= self.num_nodes assert self.wallet_names is None or len(self.wallet_names) <= self.num_nodes
if self.options.timeout_factor == 0 : if self.options.timeout_factor == 0 :
@ -711,7 +714,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
if not os.path.isdir(cache_node_dir): if not os.path.isdir(cache_node_dir):
self.log.debug("Creating cache directory {}".format(cache_node_dir)) self.log.debug("Creating cache directory {}".format(cache_node_dir))
initialize_datadir(self.options.cachedir, CACHE_NODE_ID, self.chain) initialize_datadir(self.options.cachedir, CACHE_NODE_ID, self.chain, self.disable_autoconnect)
self.nodes.append( self.nodes.append(
TestNode( TestNode(
CACHE_NODE_ID, CACHE_NODE_ID,
@ -769,7 +772,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
self.log.debug("Copy cache directory {} to node {}".format(cache_node_dir, i)) self.log.debug("Copy cache directory {} to node {}".format(cache_node_dir, i))
to_dir = get_datadir_path(self.options.tmpdir, i) to_dir = get_datadir_path(self.options.tmpdir, i)
shutil.copytree(cache_node_dir, to_dir) shutil.copytree(cache_node_dir, to_dir)
initialize_datadir(self.options.tmpdir, i, self.chain) # Overwrite port/rpcport in bitcoin.conf initialize_datadir(self.options.tmpdir, i, self.chain, self.disable_autoconnect) # Overwrite port/rpcport in bitcoin.conf
def _initialize_chain_clean(self): def _initialize_chain_clean(self):
"""Initialize empty blockchain for use by the test. """Initialize empty blockchain for use by the test.
@ -777,7 +780,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
Create an empty blockchain and num_nodes wallets. Create an empty blockchain and num_nodes wallets.
Useful if a test case wants complete control over initialization.""" Useful if a test case wants complete control over initialization."""
for i in range(self.num_nodes): for i in range(self.num_nodes):
initialize_datadir(self.options.tmpdir, i, self.chain) initialize_datadir(self.options.tmpdir, i, self.chain, self.disable_autoconnect)
def skip_if_no_py3_zmq(self): def skip_if_no_py3_zmq(self):
"""Attempt to import the zmq package and skip the test if the import fails.""" """Attempt to import the zmq package and skip the test if the import fails."""

@ -337,17 +337,17 @@ def rpc_url(datadir, i, chain, rpchost):
################ ################
def initialize_datadir(dirname, n, chain): def initialize_datadir(dirname, n, chain, disable_autoconnect=True):
datadir = get_datadir_path(dirname, n) datadir = get_datadir_path(dirname, n)
if not os.path.isdir(datadir): if not os.path.isdir(datadir):
os.makedirs(datadir) os.makedirs(datadir)
write_config(os.path.join(datadir, "bitcoin.conf"), n=n, chain=chain) write_config(os.path.join(datadir, "bitcoin.conf"), n=n, chain=chain, disable_autoconnect=disable_autoconnect)
os.makedirs(os.path.join(datadir, 'stderr'), exist_ok=True) os.makedirs(os.path.join(datadir, 'stderr'), exist_ok=True)
os.makedirs(os.path.join(datadir, 'stdout'), exist_ok=True) os.makedirs(os.path.join(datadir, 'stdout'), exist_ok=True)
return datadir return datadir
def write_config(config_path, *, n, chain, extra_config=""): def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect=True):
# Translate chain subdirectory name to config name # Translate chain subdirectory name to config name
if chain == 'testnet3': if chain == 'testnet3':
chain_name_conf_arg = 'testnet' chain_name_conf_arg = 'testnet'
@ -375,6 +375,8 @@ def write_config(config_path, *, n, chain, extra_config=""):
f.write("shrinkdebugfile=0\n") f.write("shrinkdebugfile=0\n")
# To improve SQLite wallet performance so that the tests don't timeout, use -unsafesqlitesync # To improve SQLite wallet performance so that the tests don't timeout, use -unsafesqlitesync
f.write("unsafesqlitesync=1\n") f.write("unsafesqlitesync=1\n")
if disable_autoconnect:
f.write("connect=0\n")
f.write(extra_config) f.write(extra_config)

Loading…
Cancel
Save