Skip to content

Commit 25fd6e2

Browse files
committed
[tests] refactor check_last_announcement() in sendheaders.py
All calls of check_last_announcement() asserted that the return value was True. Just assert inside the function instead. This gives better debug information if the assert fails. Also only check the contents of the most recent inv and header if check_last_announcement() is called with the relevant argument.
1 parent f39d4bb commit 25fd6e2

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

test/functional/sendheaders.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,27 @@ def check_last_announcement(self, headers=None, inv=None):
171171
"""Test whether the last announcement received had the right header or the right inv.
172172
173173
inv and headers should be lists of block hashes."""
174-
expect_headers = headers if headers is not None else []
175-
expect_inv = inv if inv is not None else []
174+
176175
test_function = lambda: self.block_announced
177176
wait_until(test_function, timeout=60, lock=mininode_lock)
177+
178178
with mininode_lock:
179179
self.block_announced = False
180180

181-
success = True
182181
compare_inv = []
183182
if "inv" in self.last_message:
184183
compare_inv = [x.hash for x in self.last_message["inv"].inv]
185-
if compare_inv != expect_inv:
186-
success = False
184+
if inv is not None:
185+
assert_equal(compare_inv, inv)
187186

188-
hash_headers = []
187+
compare_headers = []
189188
if "headers" in self.last_message:
190-
# treat headers as a list of block hashes
191-
hash_headers = [x.sha256 for x in self.last_message["headers"].headers]
192-
if hash_headers != expect_headers:
193-
success = False
189+
compare_headers = [x.sha256 for x in self.last_message["headers"].headers]
190+
if headers is not None:
191+
assert_equal(compare_headers, headers)
194192

195193
self.last_message.pop("inv", None)
196194
self.last_message.pop("headers", None)
197-
return success
198195

199196
class SendHeadersTest(BitcoinTestFramework):
200197
def set_test_params(self):
@@ -256,7 +253,7 @@ def test_null_locators(self, test_node):
256253
self.log.info("Verify getheaders with null locator and valid hashstop returns headers.")
257254
test_node.clear_last_announcement()
258255
test_node.send_get_headers(locator=[], hashstop=tip_hash)
259-
assert_equal(test_node.check_last_announcement(headers=[tip_hash]), True)
256+
test_node.check_last_announcement(headers=[tip_hash])
260257

261258
self.log.info("Verify getheaders with null locator and invalid hashstop does not return headers.")
262259
block = create_block(int(tip["hash"], 16), create_coinbase(tip["height"] + 1), tip["mediantime"] + 1)
@@ -277,8 +274,8 @@ def test_nonnull_locators(self, test_node, inv_node):
277274
for i in range(4):
278275
old_tip = tip
279276
tip = self.mine_blocks(1)
280-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
281-
assert_equal(test_node.check_last_announcement(inv=[tip]), True)
277+
inv_node.check_last_announcement(inv=[tip], headers=[])
278+
test_node.check_last_announcement(inv=[tip], headers=[])
282279
# Try a few different responses; none should affect next announcement
283280
if i == 0:
284281
# first request the block
@@ -316,8 +313,8 @@ def test_nonnull_locators(self, test_node, inv_node):
316313

317314
# Now that we've synced headers, headers announcements should work
318315
tip = self.mine_blocks(1)
319-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
320-
assert_equal(test_node.check_last_announcement(headers=[tip]), True)
316+
inv_node.check_last_announcement(inv=[tip], headers=[])
317+
test_node.check_last_announcement(headers=[tip])
321318

322319
height = self.nodes[0].getblockcount() + 1
323320
block_time += 10 # Advance far enough ahead
@@ -361,8 +358,8 @@ def test_nonnull_locators(self, test_node, inv_node):
361358
assert "inv" not in inv_node.last_message
362359
assert "headers" not in inv_node.last_message
363360
tip = self.mine_blocks(1)
364-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
365-
assert_equal(test_node.check_last_announcement(headers=[tip]), True)
361+
inv_node.check_last_announcement(inv=[tip], headers=[])
362+
test_node.check_last_announcement(headers=[tip])
366363
height += 1
367364
block_time += 1
368365

@@ -376,16 +373,16 @@ def test_nonnull_locators(self, test_node, inv_node):
376373
# First try mining a reorg that can propagate with header announcement
377374
new_block_hashes = self.mine_reorg(length=7)
378375
tip = new_block_hashes[-1]
379-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
380-
assert_equal(test_node.check_last_announcement(headers=new_block_hashes), True)
376+
inv_node.check_last_announcement(inv=[tip], headers=[])
377+
test_node.check_last_announcement(headers=new_block_hashes)
381378

382379
block_time += 8
383380

384381
# Mine a too-large reorg, which should be announced with a single inv
385382
new_block_hashes = self.mine_reorg(length=8)
386383
tip = new_block_hashes[-1]
387-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
388-
assert_equal(test_node.check_last_announcement(inv=[tip]), True)
384+
inv_node.check_last_announcement(inv=[tip], headers=[])
385+
test_node.check_last_announcement(inv=[tip], headers=[])
389386

390387
block_time += 9
391388

@@ -394,15 +391,15 @@ def test_nonnull_locators(self, test_node, inv_node):
394391

395392
# Use getblocks/getdata
396393
test_node.send_getblocks(locator=[fork_point])
397-
assert_equal(test_node.check_last_announcement(inv=new_block_hashes), True)
394+
test_node.check_last_announcement(inv=new_block_hashes, headers=[])
398395
test_node.send_get_data(new_block_hashes)
399396
test_node.wait_for_block(new_block_hashes[-1])
400397

401398
for i in range(3):
402399
# Mine another block, still should get only an inv
403400
tip = self.mine_blocks(1)
404-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
405-
assert_equal(test_node.check_last_announcement(inv=[tip]), True)
401+
inv_node.check_last_announcement(inv=[tip], headers=[])
402+
test_node.check_last_announcement(inv=[tip], headers=[])
406403
if i == 0:
407404
# Just get the data -- shouldn't cause headers announcements to resume
408405
test_node.send_get_data([tip])
@@ -427,8 +424,8 @@ def test_nonnull_locators(self, test_node, inv_node):
427424
test_node.sync_with_ping()
428425
# New blocks should now be announced with header
429426
tip = self.mine_blocks(1)
430-
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
431-
assert_equal(test_node.check_last_announcement(headers=[tip]), True)
427+
inv_node.check_last_announcement(inv=[tip], headers=[])
428+
test_node.check_last_announcement(headers=[tip])
432429

433430
self.log.info("Part 3: success!")
434431

0 commit comments

Comments
 (0)