Repository: bitcoin/bitcoin
File: test/functional/p2p_sendheaders.py (Python)

Code before:
        blocks = []
        # Now we test that if we repeatedly don't send connecting headers, we
        # don't go into an infinite loop trying to get them to connect.
        MAX_NUM_UNCONNECTING_HEADERS_MSGS = 10
        for _ in range(MAX_NUM_UNCONNECTING_HEADERS_MSGS + 1):
            blocks.append(create_block(tip, create_coinbase(height), block_time))
            blocks[-1].solve()
            tip = blocks[-1].sha256
            block_time += 1
            height += 1

        for i in range(1, MAX_NUM_UNCONNECTING_HEADERS_MSGS):
            # Send a header that doesn't connect, check that we get a getheaders.
            with p2p_lock:
                test_node.last_message.pop("getheaders", None)
            test_node.send_header_for_blocks([blocks[i]])
            test_node.wait_for_getheaders()

        # Next header will connect, should re-set our count:
        test_node.send_header_for_blocks([blocks[0]])

        # Remove the first two entries (blocks[1] would connect):
        blocks = blocks[2:]

        # Now try to see how many unconnecting headers we can send
        # before we get disconnected.  Should be 5*MAX_NUM_UNCONNECTING_HEADERS_MSGS
        for i in range(MAX_NUM_UNCONNECTING_HEADERS_MSGS - 1):
            # Send a header that doesn't connect, check that we get a getheaders.
            with p2p_lock:
                test_node.last_message.pop("getheaders", None)
            test_node.send_header_for_blocks([blocks[i % len(blocks)]])
            test_node.wait_for_getheaders()

        # Eventually this stops working.
        test_node.send_header_for_blocks([blocks[-1]])

        # Should get disconnected
        test_node.wait_for_disconnect()

        self.log.info("Part 5: success!")

        # Finally, check that the inv node never received a getdata request,
        # throughout the test
        assert "getdata" not in inv_node.last_message

if __name__ == '__main__':
    SendHeadersTest().main()


Review comment (suggestion):
```suggestion
        # Now try to see how many unconnecting headers we can send
        # before we get disconnected.  Should be MAX_NUM_UNCONNECTING_HEADERS_MSGS
```

Code after:
        # Now we test that if we repeatedly don't send connecting headers, we
        # don't go into an infinite loop trying to get them to connect.
        for _ in range(NUM_HEADERS + 1):
            blocks.append(create_block(tip, create_coinbase(height), block_time))
            blocks[-1].solve()
            tip = blocks[-1].sha256
            block_time += 1
            height += 1

        for i in range(1, NUM_HEADERS):
            with p2p_lock:
                test_node.last_message.pop("getheaders", None)
            # Send an empty header as a failed response to the received getheaders
            # (from the previous iteration). Otherwise, the new headers will be
            # treated as a response instead of as an announcement.
            test_node.send_header_for_blocks([])
            # Send the actual unconnecting header, which should trigger a new getheaders.
            test_node.send_header_for_blocks([blocks[i]])
            test_node.wait_for_getheaders(block_hash=expected_hash)

        # Finally, check that the inv node never received a getdata request,
        # throughout the test
        assert "getdata" not in inv_node.last_message

if __name__ == '__main__':
    SendHeadersTest().main()