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