@@ -78,6 +78,13 @@ def send_header_for_blocks(self, new_blocks):
7878 headers_message .headers = [CBlockHeader (b ) for b in new_blocks ]
7979 self .send_message (headers_message )
8080
81+ def request_headers_and_sync (self , locator , hashstop = 0 ):
82+ self .clear_block_announcement ()
83+ self .get_headers (locator , hashstop )
84+ assert (wait_until (self .received_block_announcement , timeout = 30 ))
85+ assert (self .received_block_announcement ())
86+ self .clear_block_announcement ()
87+
8188
8289class CompactBlocksTest (BitcoinTestFramework ):
8390 def __init__ (self ):
@@ -130,7 +137,7 @@ def make_utxos(self):
130137 # Test "sendcmpct":
131138 # - No compact block announcements or getdata(MSG_CMPCT_BLOCK) unless
132139 # sendcmpct is sent.
133- # - If sendcmpct is sent with version > 0 , the message is ignored.
140+ # - If sendcmpct is sent with version > 1 , the message is ignored.
134141 # - If sendcmpct is sent with boolean 0, then block announcements are not
135142 # made with compact blocks.
136143 # - If sendcmpct is then sent with boolean 1, then new block announcements
@@ -142,57 +149,66 @@ def test_sendcmpct(self):
142149 def received_sendcmpct ():
143150 return (self .test_node .last_sendcmpct is not None )
144151 got_message = wait_until (received_sendcmpct , timeout = 30 )
152+ assert (received_sendcmpct ())
145153 assert (got_message )
146154 assert_equal (self .test_node .last_sendcmpct .version , 1 )
147155
148156 tip = int (self .nodes [0 ].getbestblockhash (), 16 )
149157
150158 def check_announcement_of_new_block (node , peer , predicate ):
151- self . test_node .clear_block_announcement ()
159+ peer .clear_block_announcement ()
152160 node .generate (1 )
153- got_message = wait_until (peer .received_block_announcement , timeout = 30 )
161+ got_message = wait_until (lambda : peer .block_announced , timeout = 30 )
162+ assert (peer .block_announced )
154163 assert (got_message )
155164 with mininode_lock :
156- assert (predicate )
165+ assert (predicate ( peer ) )
157166
158167 # We shouldn't get any block announcements via cmpctblock yet.
159- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self . test_node .last_cmpctblock is None )
168+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is None )
160169
161170 # Try one more time, this time after requesting headers.
162- self .test_node .clear_block_announcement ()
163- self .test_node .get_headers (locator = [tip ], hashstop = 0 )
164- wait_until (self .test_node .received_block_announcement , timeout = 30 )
165- self .test_node .clear_block_announcement ()
171+ self .test_node .request_headers_and_sync (locator = [tip ])
172+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is None and p .last_inv is not None )
166173
167- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self .test_node .last_cmpctblock is None and self .test_node .last_inv is not None )
174+ # Test a few ways of using sendcmpct that should NOT
175+ # result in compact block announcements.
176+ # Before each test, sync the headers chain.
177+ self .test_node .request_headers_and_sync (locator = [tip ])
168178
169179 # Now try a SENDCMPCT message with too-high version
170180 sendcmpct = msg_sendcmpct ()
171181 sendcmpct .version = 2
172- self .test_node .send_message (sendcmpct )
182+ self .test_node .send_and_ping (sendcmpct )
183+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is None )
173184
174- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self .test_node .last_cmpctblock is None )
185+ # Headers sync before next test.
186+ self .test_node .request_headers_and_sync (locator = [tip ])
175187
176188 # Now try a SENDCMPCT message with valid version, but announce=False
177- self .test_node .send_message (msg_sendcmpct ())
178- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self .test_node .last_cmpctblock is None )
189+ self .test_node .send_and_ping (msg_sendcmpct ())
190+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is None )
191+
192+ # Headers sync before next test.
193+ self .test_node .request_headers_and_sync (locator = [tip ])
179194
180195 # Finally, try a SENDCMPCT message with announce=True
181196 sendcmpct .version = 1
182197 sendcmpct .announce = True
183- self .test_node .send_message (sendcmpct )
184- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self . test_node .last_cmpctblock is not None )
198+ self .test_node .send_and_ping (sendcmpct )
199+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is not None )
185200
186- # Try one more time
187- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self . test_node .last_cmpctblock is not None )
201+ # Try one more time (no headers sync should be needed!)
202+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is not None )
188203
189204 # Try one more time, after turning on sendheaders
190- self .test_node .send_message (msg_sendheaders ())
191- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self . test_node .last_cmpctblock is not None )
205+ self .test_node .send_and_ping (msg_sendheaders ())
206+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is not None )
192207
193208 # Now turn off announcements
194209 sendcmpct .announce = False
195- check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda : self .test_node .last_cmpctblock is None and self .test_node .last_headers is not None )
210+ self .test_node .send_and_ping (sendcmpct )
211+ check_announcement_of_new_block (self .nodes [0 ], self .test_node , lambda p : p .last_cmpctblock is None and p .last_headers is not None )
196212
197213 # This test actually causes bitcoind to (reasonably!) disconnect us, so do this last.
198214 def test_invalid_cmpctblock_message (self ):
0 commit comments