From 405ac819af1eb0f6cf6d1805cb668f4e8ab4a6f3 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 26 Jan 2024 03:28:33 +0100 Subject: [PATCH 1/3] test: p2p: support disconnect waiting for `add_outbound_p2p_connection` Adds a new boolean parameter `wait_for_disconnect` to the `add_outbound_p2p_connection` method. If set, the node under test is checked to disconnect immediately after receiving the version message (same logic as for feeler connections). --- test/functional/test_framework/test_node.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index 3baa78fd79..851888468e 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -726,7 +726,7 @@ class TestNode(): return p2p_conn - def add_outbound_p2p_connection(self, p2p_conn, *, wait_for_verack=True, p2p_idx, connection_type="outbound-full-relay", supports_v2_p2p=None, advertise_v2_p2p=None, **kwargs): + def add_outbound_p2p_connection(self, p2p_conn, *, wait_for_verack=True, wait_for_disconnect=False, p2p_idx, connection_type="outbound-full-relay", supports_v2_p2p=None, advertise_v2_p2p=None, **kwargs): """Add an outbound p2p connection from node. Must be an "outbound-full-relay", "block-relay-only", "addr-fetch" or "feeler" connection. @@ -773,7 +773,7 @@ class TestNode(): if reconnect: p2p_conn.wait_for_reconnect() - if connection_type == "feeler": + if connection_type == "feeler" or wait_for_disconnect: # feeler connections are closed as soon as the node receives a `version` message p2p_conn.wait_until(lambda: p2p_conn.message_count["version"] == 1, check_connected=False) p2p_conn.wait_until(lambda: not p2p_conn.is_connected, check_connected=False) From c4a67d396d0aa99f658cafe381e39622859eb0be Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 19 Jan 2024 03:08:29 +0100 Subject: [PATCH 2/3] test: p2p: check disconnect due to lack of desirable service flags --- test/functional/p2p_handshake.py | 69 ++++++++++++++++++++++ test/functional/test_framework/messages.py | 1 + test/functional/test_runner.py | 2 + 3 files changed, 72 insertions(+) create mode 100755 test/functional/p2p_handshake.py diff --git a/test/functional/p2p_handshake.py b/test/functional/p2p_handshake.py new file mode 100755 index 0000000000..b8544f9016 --- /dev/null +++ b/test/functional/p2p_handshake.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# Copyright (c) 2024 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +""" +Test P2P behaviour during the handshake phase (VERSION, VERACK messages). +""" +import itertools + +from test_framework.test_framework import BitcoinTestFramework +from test_framework.messages import ( + NODE_NETWORK, + NODE_NONE, + NODE_P2P_V2, + NODE_WITNESS, +) +from test_framework.p2p import P2PInterface + + +# usual desirable service flags for outbound non-pruned peers +DESIRABLE_SERVICE_FLAGS = NODE_NETWORK | NODE_WITNESS + + +class P2PHandshakeTest(BitcoinTestFramework): + def set_test_params(self): + self.num_nodes = 1 + + def add_outbound_connection(self, node, connection_type, services, wait_for_disconnect): + peer = node.add_outbound_p2p_connection( + P2PInterface(), p2p_idx=0, wait_for_disconnect=wait_for_disconnect, + connection_type=connection_type, services=services, + supports_v2_p2p=self.options.v2transport, advertise_v2_p2p=self.options.v2transport) + if not wait_for_disconnect: + # check that connection is alive past the version handshake and disconnect manually + peer.sync_with_ping() + peer.peer_disconnect() + peer.wait_for_disconnect() + + def test_desirable_service_flags(self, node, service_flag_tests, expect_disconnect): + """Check that connecting to a peer either fails or succeeds depending on its offered + service flags in the VERSION message. The test is exercised for all relevant + outbound connection types where the desirable service flags check is done.""" + CONNECTION_TYPES = ["outbound-full-relay", "block-relay-only", "addr-fetch"] + for conn_type, services in itertools.product(CONNECTION_TYPES, service_flag_tests): + if self.options.v2transport: + services |= NODE_P2P_V2 + expected_result = "disconnect" if expect_disconnect else "connect" + self.log.info(f' - services 0x{services:08x}, type "{conn_type}" [{expected_result}]') + if expect_disconnect: + expected_debug_log = f'does not offer the expected services ' \ + f'({services:08x} offered, {DESIRABLE_SERVICE_FLAGS:08x} expected)' + with node.assert_debug_log([expected_debug_log]): + self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=True) + else: + self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=False) + + def run_test(self): + node = self.nodes[0] + self.log.info("Check that lacking desired service flags leads to disconnect (non-pruned peers)") + self.test_desirable_service_flags(node, [NODE_NONE, NODE_NETWORK, NODE_WITNESS], expect_disconnect=True) + self.test_desirable_service_flags(node, [NODE_NETWORK | NODE_WITNESS], expect_disconnect=False) + + self.log.info("Check that feeler connections get disconnected immediately") + with node.assert_debug_log([f"feeler connection completed"]): + self.add_outbound_connection(node, "feeler", NODE_NONE, wait_for_disconnect=True) + + +if __name__ == '__main__': + P2PHandshakeTest().main() diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 1780678de1..4e496a9275 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -46,6 +46,7 @@ MAX_PROTOCOL_MESSAGE_LENGTH = 4000000 # Maximum length of incoming protocol mes MAX_HEADERS_RESULTS = 2000 # Number of headers sent in one getheaders result MAX_INV_SIZE = 50000 # Maximum number of entries in an 'inv' protocol message +NODE_NONE = 0 NODE_NETWORK = (1 << 0) NODE_BLOOM = (1 << 2) NODE_WITNESS = (1 << 3) diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 4d66ea97c8..9cdd54090b 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -395,6 +395,8 @@ BASE_SCRIPTS = [ 'rpc_getdescriptorinfo.py', 'rpc_mempool_info.py', 'rpc_help.py', + 'p2p_handshake.py', + 'p2p_handshake.py --v2transport', 'feature_dirsymlinks.py', 'feature_help.py', 'feature_shutdown.py', From 2f23987849758537f76df7374d85a7e87b578b61 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Wed, 28 Feb 2024 03:02:21 +0100 Subject: [PATCH 3/3] test: p2p: check limited peers desirability (depending on best block depth) This adds coverage for the logic introduced in PR #28170 ("p2p: adaptive connections services flags"). --- test/functional/p2p_handshake.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/test/functional/p2p_handshake.py b/test/functional/p2p_handshake.py index b8544f9016..3fbb940cbd 100755 --- a/test/functional/p2p_handshake.py +++ b/test/functional/p2p_handshake.py @@ -6,10 +6,12 @@ Test P2P behaviour during the handshake phase (VERSION, VERACK messages). """ import itertools +import time from test_framework.test_framework import BitcoinTestFramework from test_framework.messages import ( NODE_NETWORK, + NODE_NETWORK_LIMITED, NODE_NONE, NODE_P2P_V2, NODE_WITNESS, @@ -17,8 +19,12 @@ from test_framework.messages import ( from test_framework.p2p import P2PInterface -# usual desirable service flags for outbound non-pruned peers -DESIRABLE_SERVICE_FLAGS = NODE_NETWORK | NODE_WITNESS +# Desirable service flags for outbound non-pruned and pruned peers. Note that +# the desirable service flags for pruned peers are dynamic and only apply if +# 1. the peer's service flag NODE_NETWORK_LIMITED is set *and* +# 2. the local chain is close to the tip (<24h) +DESIRABLE_SERVICE_FLAGS_FULL = NODE_NETWORK | NODE_WITNESS +DESIRABLE_SERVICE_FLAGS_PRUNED = NODE_NETWORK_LIMITED | NODE_WITNESS class P2PHandshakeTest(BitcoinTestFramework): @@ -36,7 +42,7 @@ class P2PHandshakeTest(BitcoinTestFramework): peer.peer_disconnect() peer.wait_for_disconnect() - def test_desirable_service_flags(self, node, service_flag_tests, expect_disconnect): + def test_desirable_service_flags(self, node, service_flag_tests, desirable_service_flags, expect_disconnect): """Check that connecting to a peer either fails or succeeds depending on its offered service flags in the VERSION message. The test is exercised for all relevant outbound connection types where the desirable service flags check is done.""" @@ -47,18 +53,30 @@ class P2PHandshakeTest(BitcoinTestFramework): expected_result = "disconnect" if expect_disconnect else "connect" self.log.info(f' - services 0x{services:08x}, type "{conn_type}" [{expected_result}]') if expect_disconnect: + assert (services & desirable_service_flags) != desirable_service_flags expected_debug_log = f'does not offer the expected services ' \ - f'({services:08x} offered, {DESIRABLE_SERVICE_FLAGS:08x} expected)' + f'({services:08x} offered, {desirable_service_flags:08x} expected)' with node.assert_debug_log([expected_debug_log]): self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=True) else: + assert (services & desirable_service_flags) == desirable_service_flags self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=False) def run_test(self): node = self.nodes[0] self.log.info("Check that lacking desired service flags leads to disconnect (non-pruned peers)") - self.test_desirable_service_flags(node, [NODE_NONE, NODE_NETWORK, NODE_WITNESS], expect_disconnect=True) - self.test_desirable_service_flags(node, [NODE_NETWORK | NODE_WITNESS], expect_disconnect=False) + self.test_desirable_service_flags(node, [NODE_NONE, NODE_NETWORK, NODE_WITNESS], + DESIRABLE_SERVICE_FLAGS_FULL, expect_disconnect=True) + self.test_desirable_service_flags(node, [NODE_NETWORK | NODE_WITNESS], + DESIRABLE_SERVICE_FLAGS_FULL, expect_disconnect=False) + + self.log.info("Check that limited peers are only desired if the local chain is close to the tip (<24h)") + node.setmocktime(int(time.time()) + 25 * 3600) # tip outside the 24h window, should fail + self.test_desirable_service_flags(node, [NODE_NETWORK_LIMITED | NODE_WITNESS], + DESIRABLE_SERVICE_FLAGS_FULL, expect_disconnect=True) + node.setmocktime(int(time.time()) + 23 * 3600) # tip inside the 24h window, should succeed + self.test_desirable_service_flags(node, [NODE_NETWORK_LIMITED | NODE_WITNESS], + DESIRABLE_SERVICE_FLAGS_PRUNED, expect_disconnect=False) self.log.info("Check that feeler connections get disconnected immediately") with node.assert_debug_log([f"feeler connection completed"]):