@@ -78,6 +78,13 @@ def send_header_for_blocks(self, new_blocks):
78
78
headers_message .headers = [CBlockHeader (b ) for b in new_blocks ]
79
79
self .send_message (headers_message )
80
80
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
+
81
88
82
89
class CompactBlocksTest (BitcoinTestFramework ):
83
90
def __init__ (self ):
@@ -130,7 +137,7 @@ def make_utxos(self):
130
137
# Test "sendcmpct":
131
138
# - No compact block announcements or getdata(MSG_CMPCT_BLOCK) unless
132
139
# 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.
134
141
# - If sendcmpct is sent with boolean 0, then block announcements are not
135
142
# made with compact blocks.
136
143
# - If sendcmpct is then sent with boolean 1, then new block announcements
@@ -142,57 +149,66 @@ def test_sendcmpct(self):
142
149
def received_sendcmpct ():
143
150
return (self .test_node .last_sendcmpct is not None )
144
151
got_message = wait_until (received_sendcmpct , timeout = 30 )
152
+ assert (received_sendcmpct ())
145
153
assert (got_message )
146
154
assert_equal (self .test_node .last_sendcmpct .version , 1 )
147
155
148
156
tip = int (self .nodes [0 ].getbestblockhash (), 16 )
149
157
150
158
def check_announcement_of_new_block (node , peer , predicate ):
151
- self . test_node .clear_block_announcement ()
159
+ peer .clear_block_announcement ()
152
160
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 )
154
163
assert (got_message )
155
164
with mininode_lock :
156
- assert (predicate )
165
+ assert (predicate ( peer ) )
157
166
158
167
# 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 )
160
169
161
170
# 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 )
166
173
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 ])
168
178
169
179
# Now try a SENDCMPCT message with too-high version
170
180
sendcmpct = msg_sendcmpct ()
171
181
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 )
173
184
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 ])
175
187
176
188
# 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 ])
179
194
180
195
# Finally, try a SENDCMPCT message with announce=True
181
196
sendcmpct .version = 1
182
197
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 )
185
200
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 )
188
203
189
204
# 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 )
192
207
193
208
# Now turn off announcements
194
209
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 )
196
212
197
213
# This test actually causes bitcoind to (reasonably!) disconnect us, so do this last.
198
214
def test_invalid_cmpctblock_message (self ):
0 commit comments