mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-09 18:00:13 +02:00
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.
This commit is contained in:
@ -14,10 +14,7 @@ from test_framework.messages import (
|
|||||||
msg_filteradd,
|
msg_filteradd,
|
||||||
msg_filterclear,
|
msg_filterclear,
|
||||||
)
|
)
|
||||||
from test_framework.mininode import (
|
from test_framework.mininode import P2PInterface
|
||||||
P2PInterface,
|
|
||||||
mininode_lock,
|
|
||||||
)
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
|
|
||||||
|
|
||||||
@ -69,18 +66,15 @@ class FilterTest(BitcoinTestFramework):
|
|||||||
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]
|
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]
|
||||||
|
|
||||||
self.log.info('Check that we receive merkleblock and tx if the filter matches a tx in a block')
|
self.log.info('Check that we receive merkleblock and tx if the filter matches a tx in a block')
|
||||||
filter_node.merkleblock_received = False
|
|
||||||
block_hash = self.nodes[0].generatetoaddress(1, filter_address)[0]
|
block_hash = self.nodes[0].generatetoaddress(1, filter_address)[0]
|
||||||
txid = self.nodes[0].getblock(block_hash)['tx'][0]
|
txid = self.nodes[0].getblock(block_hash)['tx'][0]
|
||||||
|
filter_node.wait_for_merkleblock(int(block_hash, 16))
|
||||||
filter_node.wait_for_tx(txid)
|
filter_node.wait_for_tx(txid)
|
||||||
assert filter_node.merkleblock_received
|
|
||||||
|
|
||||||
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
|
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
|
||||||
with mininode_lock:
|
|
||||||
filter_node.last_message.pop("merkleblock", None)
|
|
||||||
filter_node.tx_received = False
|
filter_node.tx_received = False
|
||||||
self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())
|
block_hash = self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())[0]
|
||||||
filter_node.wait_for_merkleblock()
|
filter_node.wait_for_merkleblock(int(block_hash, 16))
|
||||||
assert not filter_node.tx_received
|
assert not filter_node.tx_received
|
||||||
|
|
||||||
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')
|
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')
|
||||||
|
@ -1321,10 +1321,23 @@ class msg_headers:
|
|||||||
|
|
||||||
|
|
||||||
class msg_merkleblock:
|
class msg_merkleblock:
|
||||||
|
__slots__ = ("merkleblock",)
|
||||||
command = b"merkleblock"
|
command = b"merkleblock"
|
||||||
|
|
||||||
|
def __init__(self, merkleblock=None):
|
||||||
|
if merkleblock is None:
|
||||||
|
self.merkleblock = CMerkleBlock()
|
||||||
|
else:
|
||||||
|
self.merkleblock = merkleblock
|
||||||
|
|
||||||
def deserialize(self, f):
|
def deserialize(self, f):
|
||||||
pass # Placeholder for now
|
self.merkleblock.deserialize(f)
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
return self.merkleblock.serialize()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "msg_merkleblock(merkleblock=%s)" % (repr(self.merkleblock))
|
||||||
|
|
||||||
|
|
||||||
class msg_filterload:
|
class msg_filterload:
|
||||||
|
@ -397,14 +397,13 @@ class P2PInterface(P2PConnection):
|
|||||||
|
|
||||||
wait_until(test_function, timeout=timeout, lock=mininode_lock)
|
wait_until(test_function, timeout=timeout, lock=mininode_lock)
|
||||||
|
|
||||||
def wait_for_merkleblock(self, timeout=60):
|
def wait_for_merkleblock(self, blockhash, timeout=60):
|
||||||
def test_function():
|
def test_function():
|
||||||
assert self.is_connected
|
assert self.is_connected
|
||||||
last_filtered_block = self.last_message.get('merkleblock')
|
last_filtered_block = self.last_message.get('merkleblock')
|
||||||
if not last_filtered_block:
|
if not last_filtered_block:
|
||||||
return False
|
return False
|
||||||
# TODO change this method to take a hash value and only return true if the correct block has been received
|
return last_filtered_block.merkleblock.header.rehash() == blockhash
|
||||||
return True
|
|
||||||
|
|
||||||
wait_until(test_function, timeout=timeout, lock=mininode_lock)
|
wait_until(test_function, timeout=timeout, lock=mininode_lock)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user