Skip to content

Commit 59d6249

Browse files
author
MarcoFalke
committed
Merge #18593: test: complete impl. of msg_merkleblock and wait_for_merkleblock
8543828 refactor: test: improve wait_for{header,merkleblock} interface (Sebastian Falbesoner) 1356a45 test: complete impl. of msg_merkleblock and wait_for_merkleblock (Sebastian Falbesoner) Pull request description: Implements the missing initialization/serialization methods for `msg_merkleblock`, based on the already present class `CMerkleBlock`. Also changes the method `wait_for_merkleblock()` to be more precise by waiting for a merkleblock with a specified blockhash instead of an arbitrary one. In the BIP37 test `p2p_filter.py`, this new method is used to make the test of receiving merkleblock and tx if a filter is set to be more precise, by checking if they also arrive in the right order. In the course of this PR, also the interface for the methods `wait_for_merkleblock()` and `wait_for_header()` are improved to take a hex string instead of an integer, which is more typesafe and less of a burden to the caller. ACKs for top commit: MarcoFalke: ACK 8543828 Tree-SHA512: adaf0ac728ef0b9929cb417a7a7b4c1346c400b2d365bf6914515c67b6cfe8f4a7ecc62fb514afdce9792f0bed833416f6bca6b9620f3d5dcdc66e4d5b0b7ea3
2 parents 76143bf + 8543828 commit 59d6249

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

test/functional/p2p_filter.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
msg_filteradd,
1515
msg_filterclear,
1616
)
17-
from test_framework.mininode import (
18-
P2PInterface,
19-
mininode_lock,
20-
)
17+
from test_framework.mininode import P2PInterface
2118
from test_framework.test_framework import BitcoinTestFramework
2219

2320

@@ -69,18 +66,15 @@ def run_test(self):
6966
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]
7067

7168
self.log.info('Check that we receive merkleblock and tx if the filter matches a tx in a block')
72-
filter_node.merkleblock_received = False
7369
block_hash = self.nodes[0].generatetoaddress(1, filter_address)[0]
7470
txid = self.nodes[0].getblock(block_hash)['tx'][0]
71+
filter_node.wait_for_merkleblock(block_hash)
7572
filter_node.wait_for_tx(txid)
76-
assert filter_node.merkleblock_received
7773

7874
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
79-
with mininode_lock:
80-
filter_node.last_message.pop("merkleblock", None)
8175
filter_node.tx_received = False
82-
self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())
83-
filter_node.wait_for_merkleblock()
76+
block_hash = self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())[0]
77+
filter_node.wait_for_merkleblock(block_hash)
8478
assert not filter_node.tx_received
8579

8680
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')

test/functional/p2p_invalid_locator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def run_test(self):
3434
msg.locator.vHave = [int(node.getblockhash(i - 1), 16) for i in range(block_count, block_count - (MAX_LOCATOR_SZ), -1)]
3535
node.p2p.send_message(msg)
3636
if type(msg) == msg_getheaders:
37-
node.p2p.wait_for_header(int(node.getbestblockhash(), 16))
37+
node.p2p.wait_for_header(node.getbestblockhash())
3838
else:
3939
node.p2p.wait_for_block(int(node.getbestblockhash(), 16))
4040

test/functional/test_framework/messages.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,10 +1321,23 @@ def __repr__(self):
13211321

13221322

13231323
class msg_merkleblock:
1324+
__slots__ = ("merkleblock",)
13241325
command = b"merkleblock"
13251326

1327+
def __init__(self, merkleblock=None):
1328+
if merkleblock is None:
1329+
self.merkleblock = CMerkleBlock()
1330+
else:
1331+
self.merkleblock = merkleblock
1332+
13261333
def deserialize(self, f):
1327-
pass # Placeholder for now
1334+
self.merkleblock.deserialize(f)
1335+
1336+
def serialize(self):
1337+
return self.merkleblock.serialize()
1338+
1339+
def __repr__(self):
1340+
return "msg_merkleblock(merkleblock=%s)" % (repr(self.merkleblock))
13281341

13291342

13301343
class msg_filterload:

test/functional/test_framework/mininode.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,17 @@ def test_function():
393393
last_headers = self.last_message.get('headers')
394394
if not last_headers:
395395
return False
396-
return last_headers.headers[0].rehash() == blockhash
396+
return last_headers.headers[0].rehash() == int(blockhash, 16)
397397

398398
wait_until(test_function, timeout=timeout, lock=mininode_lock)
399399

400-
def wait_for_merkleblock(self, timeout=60):
400+
def wait_for_merkleblock(self, blockhash, timeout=60):
401401
def test_function():
402402
assert self.is_connected
403403
last_filtered_block = self.last_message.get('merkleblock')
404404
if not last_filtered_block:
405405
return False
406-
# TODO change this method to take a hash value and only return true if the correct block has been received
407-
return True
406+
return last_filtered_block.merkleblock.header.rehash() == int(blockhash, 16)
408407

409408
wait_until(test_function, timeout=timeout, lock=mininode_lock)
410409

0 commit comments

Comments
 (0)