|
| 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 random |
| 9 | + |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | +from test_framework.messages import ( |
| 12 | + NODE_NETWORK, |
| 13 | + NODE_WITNESS, |
| 14 | +) |
| 15 | +from test_framework.p2p import P2PInterface |
| 16 | + |
| 17 | + |
| 18 | +DESIRABLE_SERVICE_FLAGS = NODE_NETWORK | NODE_WITNESS |
| 19 | + |
| 20 | + |
| 21 | +class P2PHandshakeTest(BitcoinTestFramework): |
| 22 | + def set_test_params(self): |
| 23 | + self.num_nodes = 1 |
| 24 | + |
| 25 | + def run_test(self): |
| 26 | + node = self.nodes[0] |
| 27 | + self.log.info("Check that lacking desired service flags leads to disconnect") |
| 28 | + for i, conn_type in enumerate(["outbound-full-relay", "block-relay-only", "addr-fetch"]): |
| 29 | + services = random.choice([0, NODE_NETWORK, NODE_WITNESS]) |
| 30 | + assert (services & DESIRABLE_SERVICE_FLAGS) != DESIRABLE_SERVICE_FLAGS |
| 31 | + expected_debug_log = f'peer={i} does not offer the expected services ' \ |
| 32 | + f'({services:08x} offered, {DESIRABLE_SERVICE_FLAGS:08x} expected)' |
| 33 | + self.log.info(f' - services 0x{services:08x}, type "{conn_type}"') |
| 34 | + with node.assert_debug_log([expected_debug_log]): |
| 35 | + peer = node.add_outbound_p2p_connection(P2PInterface(), wait_for_verack=False, p2p_idx=i, |
| 36 | + connection_type=conn_type, services=services) |
| 37 | + peer.wait_for_disconnect() |
| 38 | + |
| 39 | + self.log.info("Check that feeler connections get disconnected immediately") |
| 40 | + with node.assert_debug_log(["feeler connection completed"]): |
| 41 | + peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=i+1, connection_type="feeler", services=0) |
| 42 | + peer.wait_for_disconnect() |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + P2PHandshakeTest().main() |
0 commit comments