|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2014 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +# |
| 7 | +# Test resurrection of mined transactions when |
| 8 | +# the blockchain is re-organized. |
| 9 | +# |
| 10 | + |
| 11 | +from test_framework import BitcoinTestFramework |
| 12 | +from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException |
| 13 | +from util import * |
| 14 | +import os |
| 15 | +import shutil |
| 16 | + |
| 17 | +# Create one-input, one-output, no-fee transaction: |
| 18 | +class MempoolCoinbaseTest(BitcoinTestFramework): |
| 19 | + |
| 20 | + def setup_network(self): |
| 21 | + # Just need one node for this test |
| 22 | + args = ["-checkmempool", "-debug=mempool"] |
| 23 | + self.nodes = [] |
| 24 | + self.nodes.append(start_node(0, self.options.tmpdir, args)) |
| 25 | + self.is_network_split = False |
| 26 | + |
| 27 | + def create_tx(self, from_txid, to_address, amount): |
| 28 | + inputs = [{ "txid" : from_txid, "vout" : 0}] |
| 29 | + outputs = { to_address : amount } |
| 30 | + rawtx = self.nodes[0].createrawtransaction(inputs, outputs) |
| 31 | + signresult = self.nodes[0].signrawtransaction(rawtx) |
| 32 | + assert_equal(signresult["complete"], True) |
| 33 | + return signresult["hex"] |
| 34 | + |
| 35 | + def run_test(self): |
| 36 | + node0_address = self.nodes[0].getnewaddress() |
| 37 | + |
| 38 | + # Spend block 1/2/3's coinbase transactions |
| 39 | + # Mine a block. |
| 40 | + # Create three more transactions, spending the spends |
| 41 | + # Mine another block. |
| 42 | + # ... make sure all the transactions are confirmed |
| 43 | + # Invalidate both blocks |
| 44 | + # ... make sure all the transactions are put back in the mempool |
| 45 | + # Mine a new block |
| 46 | + # ... make sure all the transactions are confirmed again. |
| 47 | + |
| 48 | + b = [ self.nodes[0].getblockhash(n) for n in range(1, 4) ] |
| 49 | + coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ] |
| 50 | + spends1_raw = [ self.create_tx(txid, node0_address, 50) for txid in coinbase_txids ] |
| 51 | + spends1_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw ] |
| 52 | + |
| 53 | + blocks = [] |
| 54 | + blocks.extend(self.nodes[0].setgenerate(True, 1)) |
| 55 | + |
| 56 | + spends2_raw = [ self.create_tx(txid, node0_address, 49.99) for txid in spends1_id ] |
| 57 | + spends2_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw ] |
| 58 | + |
| 59 | + blocks.extend(self.nodes[0].setgenerate(True, 1)) |
| 60 | + |
| 61 | + # mempool should be empty, all txns confirmed |
| 62 | + assert_equal(set(self.nodes[0].getrawmempool()), set()) |
| 63 | + for txid in spends1_id+spends2_id: |
| 64 | + tx = self.nodes[0].gettransaction(txid) |
| 65 | + assert(tx["confirmations"] > 0) |
| 66 | + |
| 67 | + # Use invalidateblock to re-org back; all transactions should |
| 68 | + # end up unconfirmed and back in the mempool |
| 69 | + for node in self.nodes: |
| 70 | + node.invalidateblock(blocks[0]) |
| 71 | + |
| 72 | + # mempool should be empty, all txns confirmed |
| 73 | + assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id)) |
| 74 | + for txid in spends1_id+spends2_id: |
| 75 | + tx = self.nodes[0].gettransaction(txid) |
| 76 | + assert(tx["confirmations"] == 0) |
| 77 | + |
| 78 | + # Generate another block, they should all get mined |
| 79 | + self.nodes[0].setgenerate(True, 1) |
| 80 | + # mempool should be empty, all txns confirmed |
| 81 | + assert_equal(set(self.nodes[0].getrawmempool()), set()) |
| 82 | + for txid in spends1_id+spends2_id: |
| 83 | + tx = self.nodes[0].gettransaction(txid) |
| 84 | + assert(tx["confirmations"] > 0) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + MempoolCoinbaseTest().main() |
0 commit comments