|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2014-2018 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 | +"""Test coinbase transactions return the correct categories. |
| 6 | +
|
| 7 | +Tests listtransactions, listsinceblock, and gettransaction. |
| 8 | +""" |
| 9 | + |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | +from test_framework.util import ( |
| 12 | + assert_array_result |
| 13 | +) |
| 14 | + |
| 15 | +class CoinbaseCategoryTest(BitcoinTestFramework): |
| 16 | + def set_test_params(self): |
| 17 | + self.num_nodes = 1 |
| 18 | + |
| 19 | + def skip_test_if_missing_module(self): |
| 20 | + self.skip_if_no_wallet() |
| 21 | + |
| 22 | + def assert_category(self, category, address, txid, skip): |
| 23 | + assert_array_result(self.nodes[0].listtransactions(skip=skip), |
| 24 | + {"address": address}, |
| 25 | + {"category": category}) |
| 26 | + assert_array_result(self.nodes[0].listsinceblock()["transactions"], |
| 27 | + {"address": address}, |
| 28 | + {"category": category}) |
| 29 | + assert_array_result(self.nodes[0].gettransaction(txid)["details"], |
| 30 | + {"address": address}, |
| 31 | + {"category": category}) |
| 32 | + |
| 33 | + def run_test(self): |
| 34 | + # Generate one block to an address |
| 35 | + address = self.nodes[0].getnewaddress() |
| 36 | + self.nodes[0].generatetoaddress(1, address) |
| 37 | + hash = self.nodes[0].getbestblockhash() |
| 38 | + txid = self.nodes[0].getblock(hash)["tx"][0] |
| 39 | + |
| 40 | + # Coinbase transaction is immature after 1 confirmation |
| 41 | + self.assert_category("immature", address, txid, 0) |
| 42 | + |
| 43 | + # Mine another 99 blocks on top |
| 44 | + self.nodes[0].generate(99) |
| 45 | + # Coinbase transaction is still immature after 100 confirmations |
| 46 | + self.assert_category("immature", address, txid, 99) |
| 47 | + |
| 48 | + # Mine one more block |
| 49 | + self.nodes[0].generate(1) |
| 50 | + # Coinbase transaction is now matured, so category is "generate" |
| 51 | + self.assert_category("generate", address, txid, 100) |
| 52 | + |
| 53 | + # Orphan block that paid to address |
| 54 | + self.nodes[0].invalidateblock(hash) |
| 55 | + # Coinbase transaction is now orphaned |
| 56 | + self.assert_category("orphan", address, txid, 100) |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + CoinbaseCategoryTest().main() |
0 commit comments