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."""
|
|
|
|
import time
|
2015-12-07 14:47:58 +01:00
|
|
|
|
2018-02-13 10:30:41 +01:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase, create_transaction
|
|
|
|
from test_framework.mininode import COIN
|
|
|
|
from test_framework.mininode import network_thread_start, P2PDataStore
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2015-12-07 14:47:58 +01: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-02-13 10:30:41 +01:00
|
|
|
self.extra_args = [["-whitelist=127.0.0.1"]]
|
2015-12-07 14:47:58 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-02-13 10:30:41 +01:00
|
|
|
# Add p2p connection to node0
|
|
|
|
node = self.nodes[0] # convenience reference to the node
|
|
|
|
node.add_p2p_connection(P2PDataStore())
|
|
|
|
|
2017-12-12 12:52:33 +01:00
|
|
|
network_thread_start()
|
2018-02-13 10:30:41 +01:00
|
|
|
node.p2p.wait_for_verack()
|
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)
|
|
|
|
block_time += 1
|
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
|
2015-12-07 14:47:58 +01:00
|
|
|
height += 1
|
2018-02-13 10:30:41 +01:00
|
|
|
node.p2p.send_blocks_and_test([block], 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-02-13 10:30:41 +01:00
|
|
|
tx1 = create_transaction(block1.vtx[0], 0, b'\x64', 50 * COIN)
|
|
|
|
node.p2p.send_txs_and_test([tx1], node, success=False, reject_code=16, reject_reason=b'mandatory-script-verify-flag-failed (Invalid OP_IF construction)')
|
2015-12-07 14:47:58 +01:00
|
|
|
|
|
|
|
# TODO: test further transactions...
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
InvalidTxRequestTest().main()
|