|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test resurrection of mined transactions when the blockchain is re-organized."""
|
6 | 6 |
|
7 |
| -from test_framework.blocktools import create_raw_transaction |
8 | 7 | from test_framework.test_framework import BitcoinTestFramework
|
9 | 8 | from test_framework.util import assert_equal
|
| 9 | +from test_framework.wallet import MiniWallet |
10 | 10 |
|
11 | 11 |
|
12 | 12 | class MempoolCoinbaseTest(BitcoinTestFramework):
|
13 | 13 | def set_test_params(self):
|
14 | 14 | self.num_nodes = 1
|
15 |
| - |
16 |
| - def skip_test_if_missing_module(self): |
17 |
| - self.skip_if_no_wallet() |
| 15 | + self.setup_clean_chain = True |
18 | 16 |
|
19 | 17 | def run_test(self):
|
20 |
| - node0_address = self.nodes[0].getnewaddress() |
| 18 | + node = self.nodes[0] |
| 19 | + wallet = MiniWallet(node) |
| 20 | + |
| 21 | + # Add enough mature utxos to the wallet so that all txs spend confirmed coins |
| 22 | + wallet.generate(3) |
| 23 | + node.generate(100) |
| 24 | + |
21 | 25 | # Spend block 1/2/3's coinbase transactions
|
22 |
| - # Mine a block. |
| 26 | + # Mine a block |
23 | 27 | # Create three more transactions, spending the spends
|
24 |
| - # Mine another block. |
| 28 | + # Mine another block |
25 | 29 | # ... make sure all the transactions are confirmed
|
26 | 30 | # Invalidate both blocks
|
27 | 31 | # ... make sure all the transactions are put back in the mempool
|
28 | 32 | # Mine a new block
|
29 |
| - # ... make sure all the transactions are confirmed again. |
30 |
| - |
31 |
| - b = [self.nodes[0].getblockhash(n) for n in range(1, 4)] |
32 |
| - coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b] |
33 |
| - spends1_raw = [create_raw_transaction(self.nodes[0], txid, node0_address, amount=49.99) for txid in coinbase_txids] |
34 |
| - spends1_id = [self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw] |
35 |
| - |
| 33 | + # ... make sure all the transactions are confirmed again |
36 | 34 | blocks = []
|
37 |
| - blocks.extend(self.nodes[0].generate(1)) |
38 |
| - |
39 |
| - spends2_raw = [create_raw_transaction(self.nodes[0], txid, node0_address, amount=49.98) for txid in spends1_id] |
40 |
| - spends2_id = [self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw] |
| 35 | + spends1_ids = [wallet.send_self_transfer(from_node=node)['txid'] for _ in range(3)] |
| 36 | + blocks.extend(node.generate(1)) |
| 37 | + spends2_ids = [wallet.send_self_transfer(from_node=node)['txid'] for _ in range(3)] |
| 38 | + blocks.extend(node.generate(1)) |
41 | 39 |
|
42 |
| - blocks.extend(self.nodes[0].generate(1)) |
| 40 | + spends_ids = set(spends1_ids + spends2_ids) |
43 | 41 |
|
44 | 42 | # mempool should be empty, all txns confirmed
|
45 |
| - assert_equal(set(self.nodes[0].getrawmempool()), set()) |
46 |
| - for txid in spends1_id+spends2_id: |
47 |
| - tx = self.nodes[0].gettransaction(txid) |
48 |
| - assert tx["confirmations"] > 0 |
| 43 | + assert_equal(set(node.getrawmempool()), set()) |
| 44 | + confirmed_txns = set(node.getblock(blocks[0])['tx'] + node.getblock(blocks[1])['tx']) |
| 45 | + # Checks that all spend txns are contained in the mined blocks |
| 46 | + assert(spends_ids < confirmed_txns) |
49 | 47 |
|
50 | 48 | # Use invalidateblock to re-org back
|
51 |
| - for node in self.nodes: |
52 |
| - node.invalidateblock(blocks[0]) |
| 49 | + node.invalidateblock(blocks[0]) |
53 | 50 |
|
54 | 51 | # All txns should be back in mempool with 0 confirmations
|
55 |
| - assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id)) |
56 |
| - for txid in spends1_id+spends2_id: |
57 |
| - tx = self.nodes[0].gettransaction(txid) |
58 |
| - assert tx["confirmations"] == 0 |
| 52 | + assert_equal(set(node.getrawmempool()), spends_ids) |
59 | 53 |
|
60 | 54 | # Generate another block, they should all get mined
|
61 |
| - self.nodes[0].generate(1) |
| 55 | + node.generate(1) |
62 | 56 | # mempool should be empty, all txns confirmed
|
63 |
| - assert_equal(set(self.nodes[0].getrawmempool()), set()) |
64 |
| - for txid in spends1_id+spends2_id: |
65 |
| - tx = self.nodes[0].gettransaction(txid) |
66 |
| - assert tx["confirmations"] > 0 |
| 57 | + assert_equal(set(node.getrawmempool()), set()) |
| 58 | + confirmed_txns = set(node.getblock(blocks[0])['tx'] + node.getblock(blocks[1])['tx']) |
| 59 | + assert(spends_ids < confirmed_txns) |
67 | 60 |
|
68 | 61 |
|
69 | 62 | if __name__ == '__main__':
|
|
0 commit comments