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

Code before:
        peer_receiving_getheaders = None
        for p in [peer2, peer3]:
            with p2p_lock:
                if "getheaders" in p.last_message:
                    count += 1
                    peer_receiving_getheaders = p
                    p.send_without_ping(msg_headers()) # Send empty response, see above

        assert_equal(count, 1)

        self.log.info("Announce another new block, from all peers")
        self.announce_random_block(all_peers)

        self.log.info("Check that peer1 receives a getheaders in response")
        peer1.wait_for_getheaders(block_hash=best_block_hash)

        self.log.info("Check that the remaining peer received a getheaders as well")
        expected_peer = peer2
        if peer2 == peer_receiving_getheaders:
            expected_peer = peer3

        expected_peer.wait_for_getheaders(block_hash=best_block_hash)

        self.log.info("Success!")

if __name__ == '__main__':
    HeadersSyncTest(__file__).main()



Review comment (suggestion):
I think it would be good to drop the formatting changes to code you don't change for easier review. Some linters such as flake8 will suggest a 80 character limit, but that is not usually followed here - there will be longer lines in almost every functional test (it's fine to respect in the added code if you want).

Code after:
        peer1 = self.nodes[0].add_p2p_connection(P2PInterface())
        best_block_hash = int(self.nodes[0].getbestblockhash(), 16)

        # Wait for peer1 to receive a getheaders
        peer1.wait_for_getheaders(block_hash=best_block_hash)
        # An empty reply will clear the outstanding getheaders request,
        # allowing additional getheaders requests to be sent to this peer in
        # the future.
        peer1.send_without_ping(msg_headers())

        self.log.info("Connecting two more peers to node0")
        # Connect 2 more peers; they should not receive a getheaders yet
        peer2 = self.nodes[0].add_p2p_connection(P2PInterface())
        peer3 = self.nodes[0].add_p2p_connection(P2PInterface())

        all_peers = [peer1, peer2, peer3]

        self.log.info("Verify that peer2 and peer3 don't receive a getheaders after connecting")
        for p in all_peers:
            p.sync_with_ping()
        with p2p_lock:
            assert "getheaders" not in peer2.last_message
            assert "getheaders" not in peer3.last_message

        self.log.info("Have all peers announce a new block")
        self.announce_random_block(all_peers)

        self.log.info("Check that peer1 receives a getheaders in response")
        peer1.wait_for_getheaders(block_hash=best_block_hash)
        peer1.send_without_ping(msg_headers()) # Send empty response, see above

        self.log.info("Check that exactly 1 of {peer2, peer3} received a getheaders in response")
        peer_receiving_getheaders = self.assert_single_getheaders_recipient([peer2, peer3])
        peer_receiving_getheaders.send_without_ping(msg_headers()) # Send empty response, see above

        self.log.info("Announce another new block, from all peers")
        self.announce_random_block(all_peers)

        self.log.info("Check that peer1 receives a getheaders in response")
        peer1.wait_for_getheaders(block_hash=best_block_hash)

        self.log.info("Check that the remaining peer received a getheaders as well")
        expected_peer = peer2
        if peer2 == peer_receiving_getheaders:
            expected_peer = peer3

        expected_peer.wait_for_getheaders(block_hash=best_block_hash)

    def setup_timeout_test_peers(self):
        self.log.info("Add peer1 and check it receives an initial getheaders request")
        node = self.nodes[0]