Skip to content

Commit f6a9166

Browse files
committed
Add functional test for block announcements during initial headers sync
1 parent 05f7f31 commit f6a9166

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2022 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 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 BitcoinTestFramework
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(BitcoinTestFramework):
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+
42+
# Wait for peer1 to receive a getheaders
43+
peer1.wait_for_getheaders()
44+
# An empty reply will clear the outstanding getheaders request,
45+
# allowing additional getheaders requests to be sent to this peer in
46+
# the future.
47+
peer1.send_message(msg_headers())
48+
49+
self.log.info("Connecting two more peers to node0")
50+
# Connect 2 more peers; they should not receive a getheaders yet
51+
peer2 = self.nodes[0].add_p2p_connection(P2PInterface())
52+
peer3 = self.nodes[0].add_p2p_connection(P2PInterface())
53+
54+
all_peers = [peer1, peer2, peer3]
55+
56+
self.log.info("Verify that peer2 and peer3 don't receive a getheaders after connecting")
57+
for p in all_peers:
58+
p.sync_with_ping()
59+
with p2p_lock:
60+
assert "getheaders" not in peer2.last_message
61+
assert "getheaders" not in peer3.last_message
62+
63+
with p2p_lock:
64+
peer1.last_message.pop("getheaders", None)
65+
66+
self.log.info("Have all peers announce a new block")
67+
self.announce_random_block(all_peers)
68+
69+
self.log.info("Check that peer1 receives a getheaders in response")
70+
peer1.wait_for_getheaders()
71+
peer1.send_message(msg_headers()) # Send empty response, see above
72+
with p2p_lock:
73+
peer1.last_message.pop("getheaders", None)
74+
75+
self.log.info("Check that exactly 1 of {peer2, peer3} received a getheaders in response")
76+
count = 0
77+
peer_receiving_getheaders = None
78+
for p in [peer2, peer3]:
79+
with p2p_lock:
80+
if "getheaders" in p.last_message:
81+
count += 1
82+
peer_receiving_getheaders = p
83+
p.last_message.pop("getheaders", None)
84+
p.send_message(msg_headers()) # Send empty response, see above
85+
86+
assert_equal(count, 1)
87+
88+
self.log.info("Announce another new block, from all peers")
89+
self.announce_random_block(all_peers)
90+
91+
self.log.info("Check that peer1 receives a getheaders in response")
92+
peer1.wait_for_getheaders()
93+
94+
self.log.info("Check that the remaining peer received a getheaders as well")
95+
expected_peer = peer2
96+
if peer2 == peer_receiving_getheaders:
97+
expected_peer = peer3
98+
99+
expected_peer.wait_for_getheaders()
100+
101+
self.log.info("Success!")
102+
103+
if __name__ == '__main__':
104+
HeadersSyncTest().main()
105+

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
'rpc_generate.py',
247247
'wallet_balance.py --legacy-wallet',
248248
'wallet_balance.py --descriptors',
249+
'p2p_initial_headers_sync.py',
249250
'feature_nulldummy.py',
250251
'mempool_accept.py',
251252
'mempool_expiry.py',

0 commit comments

Comments
 (0)