Skip to content

Commit 1356a45

Browse files
committed
test: complete impl. of msg_merkleblock and wait_for_merkleblock
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.
1 parent 7591759 commit 1356a45

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
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(int(block_hash, 16))
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(int(block_hash, 16))
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/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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,13 @@ def test_function():
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() == blockhash
408407

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

0 commit comments

Comments
 (0)