Merge bitcoin/bitcoin#29736: test: Extends wait_for_getheaders so a specific block hash can be checked

c4f857cc30 test: Extends wait_for_getheaders so a specific block hash can be checked (Sergi Delgado Segura)

Pull request description:

  Fixes https://github.com/bitcoin/bitcoin/issues/18614

  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.

ACKs for top commit:
  achow101:
    ACK c4f857cc30
  BrandonOdiwuor:
    crACK c4f857cc30
  cbergqvist:
    ACK c4f857cc30
  stratospher:
    tested ACK c4f857c. went through all getheaders messages sent in the tests and checked that it's the one we want.

Tree-SHA512: afc9a31673344dfaaefcf692ec2ab65958c3d4c005f5f3af525e9960f0622d8246d5311e59aba06cfd5c9e0ef9eb90a7fc8e210f030bfbe67b897c061efdeed1
This commit is contained in:
Ava Chow
2024-04-25 13:19:12 -04:00
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)