|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020 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 | +"""Test add_outbound_p2p_connection test framework functionality""" |
| 6 | + |
| 7 | +from test_framework.p2p import P2PInterface |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | +from test_framework.util import assert_equal |
| 10 | + |
| 11 | + |
| 12 | +def check_node_connections(*, node, num_in, num_out): |
| 13 | + info = node.getnetworkinfo() |
| 14 | + assert_equal(info["connections_in"], num_in) |
| 15 | + assert_equal(info["connections_out"], num_out) |
| 16 | + |
| 17 | + |
| 18 | +class P2PAddConnections(BitcoinTestFramework): |
| 19 | + def set_test_params(self): |
| 20 | + self.setup_clean_chain = False |
| 21 | + self.num_nodes = 2 |
| 22 | + |
| 23 | + def setup_network(self): |
| 24 | + self.setup_nodes() |
| 25 | + # Don't connect the nodes |
| 26 | + |
| 27 | + def run_test(self): |
| 28 | + self.log.info("Add 8 outbounds to node 0") |
| 29 | + for i in range(8): |
| 30 | + self.log.info(f"outbound: {i}") |
| 31 | + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i, connection_type="outbound-full-relay") |
| 32 | + |
| 33 | + self.log.info("Add 2 block-relay-only connections to node 0") |
| 34 | + for i in range(2): |
| 35 | + self.log.info(f"block-relay-only: {i}") |
| 36 | + # set p2p_idx based on the outbound connections already open to the |
| 37 | + # node, so add 8 to account for the previous full-relay connections |
| 38 | + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i + 8, connection_type="block-relay-only") |
| 39 | + |
| 40 | + self.log.info("Add 2 block-relay-only connections to node 1") |
| 41 | + for i in range(2): |
| 42 | + self.log.info(f"block-relay-only: {i}") |
| 43 | + self.nodes[1].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i, connection_type="block-relay-only") |
| 44 | + |
| 45 | + self.log.info("Add 5 inbound connections to node 1") |
| 46 | + for i in range(5): |
| 47 | + self.log.info(f"inbound: {i}") |
| 48 | + self.nodes[1].add_p2p_connection(P2PInterface()) |
| 49 | + |
| 50 | + self.log.info("Add 8 outbounds to node 1") |
| 51 | + for i in range(8): |
| 52 | + self.log.info(f"outbound: {i}") |
| 53 | + # bump p2p_idx to account for the 2 existing outbounds on node 1 |
| 54 | + self.nodes[1].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i + 2) |
| 55 | + |
| 56 | + self.log.info("Check the connections opened as expected") |
| 57 | + check_node_connections(node=self.nodes[0], num_in=0, num_out=10) |
| 58 | + check_node_connections(node=self.nodes[1], num_in=5, num_out=10) |
| 59 | + |
| 60 | + self.log.info("Disconnect p2p connections & try to re-open") |
| 61 | + self.nodes[0].disconnect_p2ps() |
| 62 | + check_node_connections(node=self.nodes[0], num_in=0, num_out=0) |
| 63 | + |
| 64 | + self.log.info("Add 8 outbounds to node 0") |
| 65 | + for i in range(8): |
| 66 | + self.log.info(f"outbound: {i}") |
| 67 | + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i) |
| 68 | + check_node_connections(node=self.nodes[0], num_in=0, num_out=8) |
| 69 | + |
| 70 | + self.log.info("Add 2 block-relay-only connections to node 0") |
| 71 | + for i in range(2): |
| 72 | + self.log.info(f"block-relay-only: {i}") |
| 73 | + # bump p2p_idx to account for the 8 existing outbounds on node 0 |
| 74 | + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i + 8, connection_type="block-relay-only") |
| 75 | + check_node_connections(node=self.nodes[0], num_in=0, num_out=10) |
| 76 | + |
| 77 | + self.log.info("Restart node 0 and try to reconnect to p2ps") |
| 78 | + self.restart_node(0) |
| 79 | + |
| 80 | + self.log.info("Add 4 outbounds to node 0") |
| 81 | + for i in range(4): |
| 82 | + self.log.info(f"outbound: {i}") |
| 83 | + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i) |
| 84 | + check_node_connections(node=self.nodes[0], num_in=0, num_out=4) |
| 85 | + |
| 86 | + self.log.info("Add 2 block-relay-only connections to node 0") |
| 87 | + for i in range(2): |
| 88 | + self.log.info(f"block-relay-only: {i}") |
| 89 | + # bump p2p_idx to account for the 4 existing outbounds on node 0 |
| 90 | + self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i + 4, connection_type="block-relay-only") |
| 91 | + check_node_connections(node=self.nodes[0], num_in=0, num_out=6) |
| 92 | + |
| 93 | + check_node_connections(node=self.nodes[1], num_in=5, num_out=10) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + P2PAddConnections().main() |
0 commit comments