Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/mininode.py
Show First 20 Lines • Show All 591 Lines • ▼ Show 20 Lines | def send_blocks_and_test(self, blocks, node, *, success=True, request_block=True, | ||||
- if success is False: assert that the node's tip doesn't advance | - if success is False: assert that the node's tip doesn't advance | ||||
- if reject_reason is set: assert that the correct reject message is logged""" | - if reject_reason is set: assert that the correct reject message is logged""" | ||||
with mininode_lock: | with mininode_lock: | ||||
for block in blocks: | for block in blocks: | ||||
self.block_store[block.sha256] = block | self.block_store[block.sha256] = block | ||||
self.last_block_hash = block.sha256 | self.last_block_hash = block.sha256 | ||||
reject_reason = [reject_reason] if reject_reason else [] | def test(): | ||||
with node.assert_debug_log(expected_msgs=reject_reason): | |||||
self.send_message(msg_headers([CBlockHeader(blocks[-1])])) | self.send_message(msg_headers([CBlockHeader(blocks[-1])])) | ||||
if request_block: | if request_block: | ||||
wait_until( | wait_until( | ||||
lambda: blocks[-1].sha256 in self.getdata_requests, timeout=timeout, lock=mininode_lock) | lambda: blocks[-1].sha256 in self.getdata_requests, timeout=timeout, lock=mininode_lock) | ||||
if expect_disconnect: | if expect_disconnect: | ||||
self.wait_for_disconnect(timeout=timeout) | self.wait_for_disconnect(timeout=timeout) | ||||
else: | else: | ||||
self.sync_with_ping(timeout=timeout) | self.sync_with_ping(timeout=timeout) | ||||
if success: | if success: | ||||
wait_until(lambda: node.getbestblockhash() | wait_until(lambda: node.getbestblockhash() == | ||||
== blocks[-1].hash, timeout=timeout) | blocks[-1].hash, timeout=timeout) | ||||
else: | else: | ||||
assert node.getbestblockhash() != blocks[-1].hash | assert node.getbestblockhash() != blocks[-1].hash | ||||
if reject_reason: | |||||
with node.assert_debug_log(expected_msgs=[reject_reason]): | |||||
test() | |||||
else: | |||||
test() | |||||
def send_txs_and_test(self, txs, node, *, success=True, | def send_txs_and_test(self, txs, node, *, success=True, | ||||
expect_disconnect=False, reject_reason=None): | expect_disconnect=False, reject_reason=None): | ||||
"""Send txs to test node and test whether they're accepted to the mempool. | """Send txs to test node and test whether they're accepted to the mempool. | ||||
- add all txs to our tx_store | - add all txs to our tx_store | ||||
- send tx messages for all txs | - send tx messages for all txs | ||||
- if success is True/False: assert that the txs are/are not accepted to the mempool | - if success is True/False: assert that the txs are/are not accepted to the mempool | ||||
- if expect_disconnect is True: Skip the sync with ping | - if expect_disconnect is True: Skip the sync with ping | ||||
- if reject_reason is set: assert that the correct reject message is logged.""" | - if reject_reason is set: assert that the correct reject message is logged.""" | ||||
with mininode_lock: | with mininode_lock: | ||||
for tx in txs: | for tx in txs: | ||||
self.tx_store[tx.sha256] = tx | self.tx_store[tx.sha256] = tx | ||||
reject_reason = [reject_reason] if reject_reason else [] | def test(): | ||||
with node.assert_debug_log(expected_msgs=reject_reason): | |||||
for tx in txs: | for tx in txs: | ||||
self.send_message(msg_tx(tx)) | self.send_message(msg_tx(tx)) | ||||
if expect_disconnect: | if expect_disconnect: | ||||
self.wait_for_disconnect() | self.wait_for_disconnect() | ||||
else: | else: | ||||
self.sync_with_ping() | self.sync_with_ping() | ||||
raw_mempool = node.getrawmempool() | raw_mempool = node.getrawmempool() | ||||
if success: | if success: | ||||
# Check that all txs are now in the mempool | # Check that all txs are now in the mempool | ||||
for tx in txs: | for tx in txs: | ||||
assert tx.hash in raw_mempool, "{} not found in mempool".format( | assert tx.hash in raw_mempool, "{} not found in mempool".format( | ||||
tx.hash) | tx.hash) | ||||
else: | else: | ||||
# Check that none of the txs are now in the mempool | # Check that none of the txs are now in the mempool | ||||
for tx in txs: | for tx in txs: | ||||
assert tx.hash not in raw_mempool, "{} tx found in mempool".format( | assert tx.hash not in raw_mempool, "{} tx found in mempool".format( | ||||
tx.hash) | tx.hash) | ||||
if reject_reason: | |||||
with node.assert_debug_log(expected_msgs=[reject_reason]): | |||||
test() | |||||
else: | |||||
test() |