|
20 | 20 | class MempoolSpendCoinbaseTest(BitcoinTestFramework):
|
21 | 21 | def set_test_params(self):
|
22 | 22 | self.num_nodes = 1
|
23 |
| - self.setup_clean_chain = True |
24 | 23 |
|
25 | 24 | def run_test(self):
|
26 | 25 | wallet = MiniWallet(self.nodes[0])
|
27 | 26 |
|
28 |
| - wallet.generate(200) |
29 |
| - chain_height = self.nodes[0].getblockcount() |
30 |
| - assert_equal(chain_height, 200) |
| 27 | + # Invalidate two blocks, so that miniwallet has access to a coin that will mature in the next block |
| 28 | + chain_height = 198 |
| 29 | + self.nodes[0].invalidateblock(self.nodes[0].getblockhash(chain_height + 1)) |
| 30 | + assert_equal(chain_height, self.nodes[0].getblockcount()) |
31 | 31 |
|
32 | 32 | # Coinbase at height chain_height-100+1 ok in mempool, should
|
33 | 33 | # get mined. Coinbase at height chain_height-100+2 is
|
34 | 34 | # too immature to spend.
|
35 |
| - b = [self.nodes[0].getblockhash(n) for n in range(101, 103)] |
36 |
| - coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b] |
37 |
| - utxo_101 = wallet.get_utxo(txid=coinbase_txids[0]) |
38 |
| - utxo_102 = wallet.get_utxo(txid=coinbase_txids[1]) |
| 35 | + wallet.scan_blocks(start=chain_height - 100 + 1, num=1) |
| 36 | + utxo_mature = wallet.get_utxo() |
| 37 | + wallet.scan_blocks(start=chain_height - 100 + 2, num=1) |
| 38 | + utxo_immature = wallet.get_utxo() |
39 | 39 |
|
40 |
| - spend_101_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_101)["txid"] |
| 40 | + spend_mature_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_mature)["txid"] |
41 | 41 |
|
42 |
| - # coinbase at height 102 should be too immature to spend |
43 |
| - immature_tx = wallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_102, mempool_valid=False) |
| 42 | + # other coinbase should be too immature to spend |
| 43 | + immature_tx = wallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_immature, mempool_valid=False) |
44 | 44 | assert_raises_rpc_error(-26,
|
45 | 45 | "bad-txns-premature-spend-of-coinbase",
|
46 | 46 | lambda: self.nodes[0].sendrawtransaction(immature_tx['hex']))
|
47 | 47 |
|
48 |
| - # mempool should have just spend_101: |
49 |
| - assert_equal(self.nodes[0].getrawmempool(), [spend_101_id]) |
| 48 | + # mempool should have just the mature one |
| 49 | + assert_equal(self.nodes[0].getrawmempool(), [spend_mature_id]) |
50 | 50 |
|
51 |
| - # mine a block, spend_101 should get confirmed |
| 51 | + # mine a block, mature one should get confirmed |
52 | 52 | self.nodes[0].generate(1)
|
53 | 53 | assert_equal(set(self.nodes[0].getrawmempool()), set())
|
54 | 54 |
|
55 |
| - # ... and now height 102 can be spent: |
56 |
| - spend_102_id = self.nodes[0].sendrawtransaction(immature_tx['hex']) |
57 |
| - assert_equal(self.nodes[0].getrawmempool(), [spend_102_id]) |
| 55 | + # ... and now previously immature can be spent: |
| 56 | + spend_new_id = self.nodes[0].sendrawtransaction(immature_tx['hex']) |
| 57 | + assert_equal(self.nodes[0].getrawmempool(), [spend_new_id]) |
58 | 58 |
|
59 | 59 |
|
60 | 60 | if __name__ == '__main__':
|
|
0 commit comments