test: Extends wait_for_getheaders so a specific block hash can be checked

Previously, `wait_for_getheaders` would check whether a node had received **any**
getheaders message. This implied that, if a test needed to check for a specific block
hash within a headers message, it had to make sure that it was checking the desired message.
This normally involved having to manually clear `last_message`. This method, apart from being
too verbose, was error prone, given an undesired `getheaders` would make tests pass.

This adds the ability to check for a specific block_hash within the last `getheaders` message.
This commit is contained in:
Sergi Delgado Segura
2024-03-26 16:03:41 +01:00
parent 71c51c161d
commit c4f857cc30
6 changed files with 28 additions and 31 deletions

View File

@@ -644,15 +644,17 @@ class P2PInterface(P2PConnection):
self.wait_until(test_function, timeout=timeout)
def wait_for_getheaders(self, *, timeout=60):
"""Waits for a getheaders message.
def wait_for_getheaders(self, block_hash=None, *, timeout=60):
"""Waits for a getheaders message containing a specific block hash.
Receiving any getheaders message will satisfy the predicate. the last_message["getheaders"]
value must be explicitly cleared before calling this method, or this will return
immediately with success. TODO: change this method to take a hash value and only
return true if the correct block header has been requested."""
If no block hash is provided, checks whether any getheaders message has been received by the node."""
def test_function():
return self.last_message.get("getheaders")
last_getheaders = self.last_message.pop("getheaders", None)
if block_hash is None:
return last_getheaders
if last_getheaders is None:
return False
return block_hash == last_getheaders.locator.vHave[0]
self.wait_until(test_function, timeout=timeout)