p2p_handshake.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2024 The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """ 6 Test P2P behaviour during the handshake phase (VERSION, VERACK messages). 7 """ 8 import itertools 9 import time 10 11 from test_framework.test_framework import BitcoinTestFramework 12 from test_framework.util import assert_not_equal 13 from test_framework.messages import ( 14 NODE_NETWORK, 15 NODE_NETWORK_LIMITED, 16 NODE_NONE, 17 NODE_P2P_V2, 18 NODE_WITNESS, 19 ) 20 from test_framework.p2p import P2PInterface 21 from test_framework.util import p2p_port 22 23 24 # Desirable service flags for outbound non-pruned and pruned peers. Note that 25 # the desirable service flags for pruned peers are dynamic and only apply if 26 # 1. the peer's service flag NODE_NETWORK_LIMITED is set *and* 27 # 2. the local chain is close to the tip (<24h) 28 DESIRABLE_SERVICE_FLAGS_FULL = NODE_NETWORK | NODE_WITNESS 29 DESIRABLE_SERVICE_FLAGS_PRUNED = NODE_NETWORK_LIMITED | NODE_WITNESS 30 31 32 class P2PHandshakeTest(BitcoinTestFramework): 33 def set_test_params(self): 34 self.num_nodes = 1 35 36 def add_outbound_connection(self, node, connection_type, services, wait_for_disconnect): 37 peer = node.add_outbound_p2p_connection( 38 P2PInterface(), p2p_idx=0, wait_for_disconnect=wait_for_disconnect, 39 connection_type=connection_type, services=services, 40 supports_v2_p2p=self.options.v2transport, advertise_v2_p2p=self.options.v2transport) 41 if not wait_for_disconnect: 42 # check that connection is alive past the version handshake and disconnect manually 43 peer.sync_with_ping() 44 peer.peer_disconnect() 45 peer.wait_for_disconnect() 46 self.wait_until(lambda: len(node.getpeerinfo()) == 0) 47 48 def test_desirable_service_flags(self, node, service_flag_tests, desirable_service_flags, expect_disconnect): 49 """Check that connecting to a peer either fails or succeeds depending on its offered 50 service flags in the VERSION message. The test is exercised for all relevant 51 outbound connection types where the desirable service flags check is done.""" 52 CONNECTION_TYPES = ["outbound-full-relay", "block-relay-only", "addr-fetch"] 53 for conn_type, services in itertools.product(CONNECTION_TYPES, service_flag_tests): 54 if self.options.v2transport: 55 services |= NODE_P2P_V2 56 expected_result = "disconnect" if expect_disconnect else "connect" 57 self.log.info(f' - services 0x{services:08x}, type "{conn_type}" [{expected_result}]') 58 if expect_disconnect: 59 assert_not_equal((services & desirable_service_flags), desirable_service_flags) 60 expected_debug_log = f'does not offer the expected services ' \ 61 f'({services:08x} offered, {desirable_service_flags:08x} expected)' 62 with node.assert_debug_log([expected_debug_log]): 63 self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=True) 64 else: 65 assert (services & desirable_service_flags) == desirable_service_flags 66 self.add_outbound_connection(node, conn_type, services, wait_for_disconnect=False) 67 68 def generate_at_mocktime(self, time): 69 self.nodes[0].setmocktime(time) 70 self.generate(self.nodes[0], 1) 71 self.nodes[0].setmocktime(0) 72 73 def run_test(self): 74 node = self.nodes[0] 75 self.log.info("Check that lacking desired service flags leads to disconnect (non-pruned peers)") 76 self.test_desirable_service_flags(node, [NODE_NONE, NODE_NETWORK, NODE_WITNESS], 77 DESIRABLE_SERVICE_FLAGS_FULL, expect_disconnect=True) 78 self.test_desirable_service_flags(node, [NODE_NETWORK | NODE_WITNESS], 79 DESIRABLE_SERVICE_FLAGS_FULL, expect_disconnect=False) 80 81 self.log.info("Check that limited peers are only desired if the local chain is close to the tip (<24h)") 82 self.generate_at_mocktime(int(time.time()) - 25 * 3600) # tip outside the 24h window, should fail 83 self.test_desirable_service_flags(node, [NODE_NETWORK_LIMITED | NODE_WITNESS], 84 DESIRABLE_SERVICE_FLAGS_FULL, expect_disconnect=True) 85 self.generate_at_mocktime(int(time.time()) - 23 * 3600) # tip inside the 24h window, should succeed 86 self.test_desirable_service_flags(node, [NODE_NETWORK_LIMITED | NODE_WITNESS], 87 DESIRABLE_SERVICE_FLAGS_PRUNED, expect_disconnect=False) 88 89 self.log.info("Check that feeler connections get disconnected immediately") 90 with node.assert_debug_log(["feeler connection completed"]): 91 self.add_outbound_connection(node, "feeler", NODE_NONE, wait_for_disconnect=True) 92 93 self.log.info("Check that connecting to ourself leads to immediate disconnect") 94 with node.assert_debug_log(["connected to self", "disconnecting"]): 95 node_listen_addr = f"127.0.0.1:{p2p_port(0)}" 96 node.addconnection(node_listen_addr, "outbound-full-relay", self.options.v2transport) 97 self.wait_until(lambda: len(node.getpeerinfo()) == 0) 98 99 100 if __name__ == '__main__': 101 P2PHandshakeTest(__file__).main()