2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2015-12-07 14:47:58 +01:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test node responses to invalid transactions.
|
|
|
|
|
2018-02-13 10:30:41 +01:00
|
|
|
In this test we connect to one node over p2p, and test tx requests."""
|
2015-12-07 14:47:58 +01:00
|
|
|
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.messages import (
|
2018-04-26 19:32:17 +02:00
|
|
|
COIN,
|
|
|
|
COutPoint,
|
|
|
|
CTransaction,
|
|
|
|
CTxIn,
|
|
|
|
CTxOut,
|
|
|
|
)
|
2018-06-29 18:04:25 +02:00
|
|
|
from test_framework.mininode import P2PDataStore
|
2018-02-13 10:30:41 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-04-26 19:32:17 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
wait_until,
|
|
|
|
)
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2020-04-19 13:04:31 +02:00
|
|
|
|
2018-02-13 10:30:41 +01:00
|
|
|
class InvalidTxRequestTest(BitcoinTestFramework):
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2015-12-07 14:47:58 +01:00
|
|
|
self.num_nodes = 1
|
2017-09-01 18:47:13 +02:00
|
|
|
self.setup_clean_chain = True
|
2018-04-26 19:32:17 +02:00
|
|
|
|
|
|
|
def bootstrap_p2p(self, *, num_connections=1):
|
|
|
|
"""Add a P2P connection to the node.
|
|
|
|
|
|
|
|
Helper to connect and wait for version handshake."""
|
|
|
|
for _ in range(num_connections):
|
|
|
|
self.nodes[0].add_p2p_connection(P2PDataStore())
|
|
|
|
|
|
|
|
def reconnect_p2p(self, **kwargs):
|
|
|
|
"""Tear down and bootstrap the P2P connection to the node.
|
|
|
|
|
|
|
|
The node gets disconnected several times in this test. This helper
|
|
|
|
method reconnects the p2p and restarts the network thread."""
|
|
|
|
self.nodes[0].disconnect_p2ps()
|
|
|
|
self.bootstrap_p2p(**kwargs)
|
2015-12-07 14:47:58 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-02-13 10:30:41 +01:00
|
|
|
node = self.nodes[0] # convenience reference to the node
|
|
|
|
|
2018-04-26 19:32:17 +02:00
|
|
|
self.bootstrap_p2p() # Add one p2p connection to the node
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2018-02-13 10:30:41 +01:00
|
|
|
best_block = self.nodes[0].getbestblockhash()
|
|
|
|
tip = int(best_block, 16)
|
|
|
|
best_block_time = self.nodes[0].getblock(best_block)['time']
|
|
|
|
block_time = best_block_time + 1
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2018-02-13 10:30:41 +01:00
|
|
|
self.log.info("Create a new block with an anyone-can-spend coinbase.")
|
2015-12-07 14:47:58 +01:00
|
|
|
height = 1
|
2018-02-13 10:30:41 +01:00
|
|
|
block = create_block(tip, create_coinbase(height), block_time)
|
2015-12-07 14:47:58 +01:00
|
|
|
block.solve()
|
|
|
|
# Save the coinbase for later
|
2018-02-13 10:30:41 +01:00
|
|
|
block1 = block
|
|
|
|
tip = block.sha256
|
2019-10-04 13:36:18 +02:00
|
|
|
|
|
|
|
# Create a second one to test orphan resolution via block receival
|
|
|
|
height += 1
|
|
|
|
block_time += 1
|
|
|
|
block = create_block(tip, create_coinbase(height), block_time)
|
|
|
|
block.solve()
|
|
|
|
# Save the coinbase for later
|
|
|
|
block2 = block
|
|
|
|
tip = block.sha256
|
|
|
|
node.p2p.send_blocks_and_test([block1, block2], node, success=True)
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2018-02-13 10:30:41 +01:00
|
|
|
self.log.info("Mature the block.")
|
|
|
|
self.nodes[0].generate(100)
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2016-04-10 16:54:28 +02:00
|
|
|
# b'\x64' is OP_NOTIF
|
2015-12-07 14:47:58 +01:00
|
|
|
# Transaction will be rejected with code 16 (REJECT_INVALID)
|
2018-04-26 19:32:17 +02:00
|
|
|
# and we get disconnected immediately
|
|
|
|
self.log.info('Test a transaction that is rejected')
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=b'\x64' * 35, amount=50 * COIN)
|
2018-06-01 14:02:16 +02:00
|
|
|
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)
|
2018-04-26 19:32:17 +02:00
|
|
|
|
|
|
|
# Make two p2p connections to provide the node with orphans
|
|
|
|
# * 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)
|
|
|
|
self.reconnect_p2p(num_connections=2)
|
|
|
|
|
|
|
|
self.log.info('Test orphan transaction handling ... ')
|
2019-10-04 13:36:18 +02:00
|
|
|
self.test_orphan_tx_handling(block1.vtx[0].sha256, False)
|
2020-04-19 13:04:31 +02:00
|
|
|
|
|
|
|
# restart node with sending BIP61 messages disabled, check that it disconnects without sending the reject message
|
|
|
|
self.log.info('Test a transaction that is rejected, with BIP61 disabled')
|
2020-06-13 06:50:03 +02:00
|
|
|
self.restart_node(0, ['-enablebip61=0', '-persistmempool=0'])
|
2020-04-19 13:04:31 +02:00
|
|
|
self.reconnect_p2p(num_connections=1)
|
2020-06-13 06:50:03 +02:00
|
|
|
with node.assert_debug_log(expected_msgs=[
|
|
|
|
"{} from peer=0 was not accepted: mandatory-script-verify-flag-failed (Invalid OP_IF construction) (code 16)".format(tx1.hash),
|
|
|
|
"disconnecting peer=0",
|
|
|
|
]):
|
|
|
|
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)
|
2020-04-19 13:04:31 +02:00
|
|
|
# send_txs_and_test will have waited for disconnect, so we can safely check that no reject has been received
|
|
|
|
assert_equal(node.p2p.reject_code_received, None)
|
|
|
|
|
|
|
|
self.log.info('Test orphan transaction handling, resolve via block')
|
|
|
|
self.restart_node(0, ['-persistmempool=0'])
|
2019-10-04 13:36:18 +02:00
|
|
|
self.reconnect_p2p(num_connections=2)
|
|
|
|
self.test_orphan_tx_handling(block2.vtx[0].sha256, True)
|
|
|
|
|
|
|
|
def test_orphan_tx_handling(self, base_tx, resolve_via_block):
|
|
|
|
node = self.nodes[0] # convenience reference to the node
|
|
|
|
|
2018-09-06 00:12:39 +02:00
|
|
|
# Create a root transaction that we withold until all dependent transactions
|
2018-04-26 19:32:17 +02:00
|
|
|
# are sent out and in the orphan cache
|
2018-05-12 16:33:07 +02:00
|
|
|
SCRIPT_PUB_KEY_OP_TRUE = b'\x51\x75' * 15 + b'\x51'
|
2018-04-26 19:32:17 +02:00
|
|
|
tx_withhold = CTransaction()
|
2019-10-04 13:36:18 +02:00
|
|
|
tx_withhold.vin.append(CTxIn(outpoint=COutPoint(base_tx, 0)))
|
2018-05-12 16:33:07 +02:00
|
|
|
tx_withhold.vout.append(CTxOut(nValue=50 * COIN - 12000, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
|
2018-04-26 19:32:17 +02:00
|
|
|
tx_withhold.calc_sha256()
|
|
|
|
|
|
|
|
# Our first orphan tx with some outputs to create further orphan txs
|
|
|
|
tx_orphan_1 = CTransaction()
|
|
|
|
tx_orphan_1.vin.append(CTxIn(outpoint=COutPoint(tx_withhold.sha256, 0)))
|
2018-05-12 16:33:07 +02:00
|
|
|
tx_orphan_1.vout = [CTxOut(nValue=10 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)] * 3
|
2018-04-26 19:32:17 +02:00
|
|
|
tx_orphan_1.calc_sha256()
|
|
|
|
|
|
|
|
# A valid transaction with low fee
|
|
|
|
tx_orphan_2_no_fee = CTransaction()
|
|
|
|
tx_orphan_2_no_fee.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 0)))
|
2018-05-12 16:33:07 +02:00
|
|
|
tx_orphan_2_no_fee.vout.append(CTxOut(nValue=10 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
|
2018-04-26 19:32:17 +02:00
|
|
|
|
|
|
|
# A valid transaction with sufficient fee
|
|
|
|
tx_orphan_2_valid = CTransaction()
|
|
|
|
tx_orphan_2_valid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 1)))
|
2018-05-12 16:33:07 +02:00
|
|
|
tx_orphan_2_valid.vout.append(CTxOut(nValue=10 * COIN - 12000, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
|
2018-04-26 19:32:17 +02:00
|
|
|
tx_orphan_2_valid.calc_sha256()
|
|
|
|
|
|
|
|
# An invalid transaction with negative fee
|
|
|
|
tx_orphan_2_invalid = CTransaction()
|
|
|
|
tx_orphan_2_invalid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2)))
|
2018-05-12 16:33:07 +02:00
|
|
|
tx_orphan_2_invalid.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
|
2018-04-26 19:32:17 +02:00
|
|
|
|
|
|
|
self.log.info('Send the orphans ... ')
|
|
|
|
# Send valid orphan txs from p2ps[0]
|
|
|
|
node.p2p.send_txs_and_test([tx_orphan_1, tx_orphan_2_no_fee, tx_orphan_2_valid], node, success=False)
|
|
|
|
# Send invalid tx from p2ps[1]
|
|
|
|
node.p2ps[1].send_txs_and_test([tx_orphan_2_invalid], node, success=False)
|
|
|
|
|
|
|
|
assert_equal(0, node.getmempoolinfo()['size']) # Mempool should be empty
|
|
|
|
assert_equal(2, len(node.getpeerinfo())) # p2ps[1] is still connected
|
|
|
|
|
|
|
|
self.log.info('Send the withhold tx ... ')
|
2019-10-04 13:36:18 +02:00
|
|
|
if resolve_via_block:
|
|
|
|
# Test orphan handling/resolution by publishing the withhold TX via a mined block
|
|
|
|
prev_block = node.getblockheader(node.getbestblockhash())
|
|
|
|
block = create_block(int(prev_block['hash'], 16), create_coinbase(prev_block['height'] + 1), prev_block["time"] + 1)
|
|
|
|
block.vtx.append(tx_withhold)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.solve()
|
|
|
|
node.p2p.send_blocks_and_test([block], node, success=True)
|
|
|
|
else:
|
|
|
|
# Test orphan handling/resolution by publishing the withhold TX via the mempool
|
|
|
|
node.p2p.send_txs_and_test([tx_withhold], node, success=True)
|
2018-04-26 19:32:17 +02:00
|
|
|
|
|
|
|
# Transactions that should end up in the mempool
|
|
|
|
expected_mempool = {
|
|
|
|
t.hash
|
|
|
|
for t in [
|
|
|
|
tx_withhold, # The transaction that is the root for all orphans
|
|
|
|
tx_orphan_1, # The orphan transaction that splits the coins
|
|
|
|
tx_orphan_2_valid, # The valid transaction (with sufficient fee)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
# Transactions that do not end up in the mempool
|
|
|
|
# tx_orphan_no_fee, because it has too low fee (p2ps[0] is not disconnected for relaying that tx)
|
|
|
|
# tx_orphan_invaid, because it has negative fee (p2ps[1] is disconnected for relaying that tx)
|
2019-10-04 13:36:18 +02:00
|
|
|
if resolve_via_block:
|
|
|
|
# This TX has appeared in a block instead of being broadcasted via the mempool
|
|
|
|
expected_mempool.remove(tx_withhold.hash)
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2018-04-26 19:32:17 +02:00
|
|
|
wait_until(lambda: 1 == len(node.getpeerinfo()), timeout=12) # p2ps[1] is no longer connected
|
|
|
|
assert_equal(expected_mempool, set(node.getrawmempool()))
|
2018-03-13 22:09:43 +01:00
|
|
|
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2020-06-13 06:50:03 +02:00
|
|
|
|
2015-12-07 14:47:58 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
InvalidTxRequestTest().main()
|