Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_invalid_tx.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2017 The Bitcoin Core developers | # Copyright (c) 2015-2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test node responses to invalid transactions. | """Test node responses to invalid transactions. | ||||
In this test we connect to one node over p2p, and test tx requests. | In this test we connect to one node over p2p, and test tx requests. | ||||
""" | """ | ||||
from test_framework.blocktools import ( | from test_framework.blocktools import ( | ||||
create_block, | create_block, | ||||
create_coinbase, | create_coinbase, | ||||
create_tx_with_script, | |||||
) | ) | ||||
from test_framework.txtools import pad_tx | from test_framework.txtools import pad_tx | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
COIN, | COIN, | ||||
COutPoint, | COutPoint, | ||||
CTransaction, | CTransaction, | ||||
CTxIn, | CTxIn, | ||||
CTxOut, | CTxOut, | ||||
) | ) | ||||
from test_framework.mininode import P2PDataStore | from test_framework.mininode import P2PDataStore | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
from data import invalid_txs | |||||
class InvalidTxRequestTest(BitcoinTestFramework): | class InvalidTxRequestTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [ | self.extra_args = [ | ||||
["-acceptnonstdtxn=1", ] | ["-acceptnonstdtxn=1", ] | ||||
Show All 33 Lines | def run_test(self): | ||||
block1 = block | block1 = block | ||||
tip = block.sha256 | tip = block.sha256 | ||||
node.p2p.send_blocks_and_test([block], node, success=True) | node.p2p.send_blocks_and_test([block], node, success=True) | ||||
self.log.info("Mature the block.") | self.log.info("Mature the block.") | ||||
self.nodes[0].generatetoaddress( | self.nodes[0].generatetoaddress( | ||||
100, self.nodes[0].get_deterministic_priv_key().address) | 100, self.nodes[0].get_deterministic_priv_key().address) | ||||
# b'\x64' is OP_NOTIF | # Iterate through a list of known invalid transaction types, ensuring each is | ||||
# Transaction will be rejected with code 16 (REJECT_INVALID) | # rejected. Some are consensus invalid and some just violate policy. | ||||
# and we get disconnected immediately | for BadTxTemplate in invalid_txs.iter_all_templates(): | ||||
self.log.info('Test a transaction that is rejected') | self.log.info( | ||||
tx1 = create_tx_with_script( | "Testing invalid transaction: %s", | ||||
block1.vtx[0], 0, script_sig=b'\x64' * 35, amount=50 * COIN - 12000) | BadTxTemplate.__name__) | ||||
template = BadTxTemplate(spend_block=block1) | |||||
tx = template.get_tx() | |||||
node.p2p.send_txs_and_test( | node.p2p.send_txs_and_test( | ||||
[tx1], node, success=False, expect_disconnect=True) | [tx], node, success=False, | ||||
expect_disconnect=template.expect_disconnect, | |||||
reject_reason=template.reject_reason, | |||||
) | |||||
if template.expect_disconnect: | |||||
self.log.info("Reconnecting to peer") | |||||
self.reconnect_p2p() | |||||
# Make two p2p connections to provide the node with orphans | # Make two p2p connections to provide the node with orphans | ||||
# * p2ps[0] will send valid orphan txs (one with low fee) | # * p2ps[0] will send valid orphan txs (one with low fee) | ||||
# * p2ps[1] will send an invalid orphan tx (and is later disconnected for that) | # * p2ps[1] will send an invalid orphan tx (and is later disconnected for that) | ||||
self.reconnect_p2p(num_connections=2) | self.reconnect_p2p(num_connections=2) | ||||
self.log.info('Test orphan transaction handling ... ') | self.log.info('Test orphan transaction handling ... ') | ||||
# Create a root transaction that we withold until all dependend transactions | # Create a root transaction that we withold until all dependend transactions | ||||
▲ Show 20 Lines • Show All 81 Lines • Show Last 20 Lines |