1 #!/usr/bin/env python3
2 # Copyright (c) 2022 The Limenka 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 initial headers download
6 7 Test that we only try to initially sync headers from one peer (until our chain
8 is close to caught up), and that each block announcement results in only one
9 additional peer receiving a getheaders message.
10 """
11 12 from test_framework.test_framework import LimenkaTestFramework
13 from test_framework.messages import (
14 CInv,
15 MSG_BLOCK,
16 msg_headers,
17 msg_inv,
18 )
19 from test_framework.p2p import (
20 p2p_lock,
21 P2PInterface,
22 )
23 from test_framework.util import (
24 assert_equal,
25 )
26 import random
27 28 class HeadersSyncTest(LimenkaTestFramework):
29 def set_test_params(self):
30 self.setup_clean_chain = True
31 self.num_nodes = 1
32 33 def announce_random_block(self, peers):
34 new_block_announcement = msg_inv(inv=[CInv(MSG_BLOCK, random.randrange(1<<256))])
35 for p in peers:
36 p.send_and_ping(new_block_announcement)
37 38 def run_test(self):
39 self.log.info("Adding a peer to node0")
40 peer1 = self.nodes[0].add_p2p_connection(P2PInterface())
41 best_block_hash = int(self.nodes[0].getbestblockhash(), 16)
42 43 # Wait for peer1 to receive a getheaders
44 peer1.wait_for_getheaders(block_hash=best_block_hash)
45 # An empty reply will clear the outstanding getheaders request,
46 # allowing additional getheaders requests to be sent to this peer in
47 # the future.
48 peer1.send_message(msg_headers())
49 50 self.log.info("Connecting two more peers to node0")
51 # Connect 2 more peers; they should not receive a getheaders yet
52 peer2 = self.nodes[0].add_p2p_connection(P2PInterface())
53 peer3 = self.nodes[0].add_p2p_connection(P2PInterface())
54 55 all_peers = [peer1, peer2, peer3]
56 57 self.log.info("Verify that peer2 and peer3 don't receive a getheaders after connecting")
58 for p in all_peers:
59 p.sync_with_ping()
60 with p2p_lock:
61 assert "getheaders" not in peer2.last_message
62 assert "getheaders" not in peer3.last_message
63 64 self.log.info("Have all peers announce a new block")
65 self.announce_random_block(all_peers)
66 67 self.log.info("Check that peer1 receives a getheaders in response")
68 peer1.wait_for_getheaders(block_hash=best_block_hash)
69 peer1.send_message(msg_headers()) # Send empty response, see above
70 71 self.log.info("Check that exactly 1 of {peer2, peer3} received a getheaders in response")
72 count = 0
73 peer_receiving_getheaders = None
74 for p in [peer2, peer3]:
75 with p2p_lock:
76 if "getheaders" in p.last_message:
77 count += 1
78 peer_receiving_getheaders = p
79 p.send_message(msg_headers()) # Send empty response, see above
80 81 assert_equal(count, 1)
82 83 self.log.info("Announce another new block, from all peers")
84 self.announce_random_block(all_peers)
85 86 self.log.info("Check that peer1 receives a getheaders in response")
87 peer1.wait_for_getheaders(block_hash=best_block_hash)
88 89 self.log.info("Check that the remaining peer received a getheaders as well")
90 expected_peer = peer2
91 if peer2 == peer_receiving_getheaders:
92 expected_peer = peer3
93 94 expected_peer.wait_for_getheaders(block_hash=best_block_hash)
95 96 self.log.info("Success!")
97 98 if __name__ == '__main__':
99 HeadersSyncTest(__file__).main()
100 101