Skip to content

Commit c4d1e24

Browse files
committed
Merge #20047: test: use wait_for_{block,header} helpers in p2p_fingerprint.py
6b56c1f test: remove last_{block,header}_equals() in p2p_fingerprint.py (Sebastian Falbesoner) 136d96b test: use wait_for_{block,header} helpers in p2p_fingerprint.py (Sebastian Falbesoner) Pull request description: This small PR takes use of the message receiving helper functions `wait_for_block()` and `wait_for_header()` (from module `test_framework.p2p`) in the test `p2p_fingerprint.py`. It also simplifies the checks for very old stale blocks/headers requests by getting rid of the functions `last_block_equals()` and `last_header_equals()` and rather only testing that not any blocks/headers message is received at all. Unneeded sending of requests are also removed and calls to time.sleep(...) substituted by ping syncs. ACKs for top commit: guggero: ACK 6b56c1f Tree-SHA512: 9114db70f3804adad4ab658236762d4fa73fef91158c5756dd1af2d24196ea740451b0028667e0c4047f1f89fe1355031921d3dfb973acc1370052a4bc12c2ab
2 parents 7aa9456 + 6b56c1f commit c4d1e24

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

test/functional/p2p_fingerprint.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
msg_block,
1919
msg_getdata,
2020
msg_getheaders,
21+
p2p_lock,
2122
)
2223
from test_framework.test_framework import BitcoinTestFramework
2324
from test_framework.util import (
@@ -57,18 +58,6 @@ def send_header_request(self, block_hash, node):
5758
msg.hashstop = block_hash
5859
node.send_message(msg)
5960

60-
# Check whether last block received from node has a given hash
61-
def last_block_equals(self, expected_hash, node):
62-
block_msg = node.last_message.get("block")
63-
return block_msg and block_msg.block.rehash() == expected_hash
64-
65-
# Check whether last block header received from node has a given hash
66-
def last_header_equals(self, expected_hash, node):
67-
headers_msg = node.last_message.get("headers")
68-
return (headers_msg and
69-
headers_msg.headers and
70-
headers_msg.headers[0].rehash() == expected_hash)
71-
7261
# Checks that stale blocks timestamped more than a month ago are not served
7362
# by the node while recent stale blocks and old active chain blocks are.
7463
# This does not currently test that stale blocks timestamped within the
@@ -101,34 +90,31 @@ def run_test(self):
10190

10291
# Check that getdata request for stale block succeeds
10392
self.send_block_request(stale_hash, node0)
104-
test_function = lambda: self.last_block_equals(stale_hash, node0)
105-
self.wait_until(test_function, timeout=3)
93+
node0.wait_for_block(stale_hash, timeout=3)
10694

10795
# Check that getheader request for stale block header succeeds
10896
self.send_header_request(stale_hash, node0)
109-
test_function = lambda: self.last_header_equals(stale_hash, node0)
110-
self.wait_until(test_function, timeout=3)
97+
node0.wait_for_header(hex(stale_hash), timeout=3)
11198

11299
# Longest chain is extended so stale is much older than chain tip
113100
self.nodes[0].setmocktime(0)
114-
tip = self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)[0]
101+
self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
115102
assert_equal(self.nodes[0].getblockcount(), 14)
116-
117-
# Send getdata & getheaders to refresh last received getheader message
118-
block_hash = int(tip, 16)
119-
self.send_block_request(block_hash, node0)
120-
self.send_header_request(block_hash, node0)
121103
node0.sync_with_ping()
122104

123105
# Request for very old stale block should now fail
106+
with p2p_lock:
107+
node0.last_message.pop("block", None)
124108
self.send_block_request(stale_hash, node0)
125-
time.sleep(3)
126-
assert not self.last_block_equals(stale_hash, node0)
109+
node0.sync_with_ping()
110+
assert "block" not in node0.last_message
127111

128112
# Request for very old stale block header should now fail
113+
with p2p_lock:
114+
node0.last_message.pop("headers", None)
129115
self.send_header_request(stale_hash, node0)
130-
time.sleep(3)
131-
assert not self.last_header_equals(stale_hash, node0)
116+
node0.sync_with_ping()
117+
assert "headers" not in node0.last_message
132118

133119
# Verify we can fetch very old blocks and headers on the active chain
134120
block_hash = int(block_hashes[2], 16)
@@ -137,12 +123,10 @@ def run_test(self):
137123
node0.sync_with_ping()
138124

139125
self.send_block_request(block_hash, node0)
140-
test_function = lambda: self.last_block_equals(block_hash, node0)
141-
self.wait_until(test_function, timeout=3)
126+
node0.wait_for_block(block_hash, timeout=3)
142127

143128
self.send_header_request(block_hash, node0)
144-
test_function = lambda: self.last_header_equals(block_hash, node0)
145-
self.wait_until(test_function, timeout=3)
129+
node0.wait_for_header(hex(block_hash), timeout=3)
146130

147131
if __name__ == '__main__':
148132
P2PFingerprintTest().main()

0 commit comments

Comments
 (0)