|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2021 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 addr-fetch connections |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | + |
| 11 | +from test_framework.messages import msg_addr, CAddress, NODE_NETWORK, NODE_WITNESS |
| 12 | +from test_framework.p2p import P2PInterface, p2p_lock |
| 13 | +from test_framework.test_framework import BitcoinTestFramework |
| 14 | +from test_framework.util import assert_equal |
| 15 | + |
| 16 | +ADDR = CAddress() |
| 17 | +ADDR.time = int(time.time()) |
| 18 | +ADDR.nServices = NODE_NETWORK | NODE_WITNESS |
| 19 | +ADDR.ip = "192.0.0.8" |
| 20 | +ADDR.port = 18444 |
| 21 | + |
| 22 | + |
| 23 | +class P2PAddrFetch(BitcoinTestFramework): |
| 24 | + |
| 25 | + def set_test_params(self): |
| 26 | + self.setup_clean_chain = True |
| 27 | + self.num_nodes = 1 |
| 28 | + |
| 29 | + def run_test(self): |
| 30 | + node = self.nodes[0] |
| 31 | + self.log.info("Connect to an addr-fetch peer") |
| 32 | + peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="addr-fetch") |
| 33 | + info = node.getpeerinfo() |
| 34 | + assert_equal(len(info), 1) |
| 35 | + assert_equal(info[0]['connection_type'], 'addr-fetch') |
| 36 | + |
| 37 | + self.log.info("Check that we send getaddr but don't try to sync headers with the addr-fetch peer") |
| 38 | + peer.sync_send_with_ping() |
| 39 | + with p2p_lock: |
| 40 | + assert peer.message_count['getaddr'] == 1 |
| 41 | + assert peer.message_count['getheaders'] == 0 |
| 42 | + |
| 43 | + self.log.info("Check that answering the getaddr with a single address does not lead to disconnect") |
| 44 | + # This prevents disconnecting on self-announcements |
| 45 | + msg = msg_addr() |
| 46 | + msg.addrs = [ADDR] |
| 47 | + peer.send_and_ping(msg) |
| 48 | + assert_equal(len(node.getpeerinfo()), 1) |
| 49 | + |
| 50 | + self.log.info("Check that answering with larger addr messages leads to disconnect") |
| 51 | + msg.addrs = [ADDR] * 2 |
| 52 | + peer.send_message(msg) |
| 53 | + peer.wait_for_disconnect(timeout=5) |
| 54 | + |
| 55 | + self.log.info("Check timeout for addr-fetch peer that does not send addrs") |
| 56 | + peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, connection_type="addr-fetch") |
| 57 | + node.setmocktime(int(time.time()) + 301) # Timeout: 5 minutes |
| 58 | + peer.wait_for_disconnect(timeout=5) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + P2PAddrFetch().main() |
0 commit comments